Commit 194161c7 authored by KevinLevie's avatar KevinLevie Committed by GitHub
Browse files

Merge pull request #7 from johanv/inactive_option_values

Merged #7, includes PR #6.
parents 53ea9e99 b182df8a
<?php <?php
/** /**
* Class following Singleton pattern for specific extension configuration * Class following Singleton pattern for specific extension configuration
* The actual configuration loader has moved to CRM_Civiconfig_Loader!
* *
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org> * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 13 Jan 2016 * @date 13 Jan 2016
* @license AGPL-3.0 * @license AGPL-3.0
*
* OOP-fixes Copyright Johan Vervloet (Chirojeugd-Vlaanderen vzw) 2016,
* licensed under the terms of AGPL-3.0.
*/ */
class CRM_Civiconfig_Config { class CRM_Civiconfig_Config {
private static $_singleton;
protected $_resourcesPath = null;
/** /**
* CRM_Civiconfig_Config constructor. * @var self $_singleton
*/ */
function __construct() { private static $_singleton;
// TODO: If the extensions dir is '[civicrm.files]/ext/' (which is the default)
// the construction below will not work.
$settings = civicrm_api3('Setting', 'Getsingle', array());
$this->resourcesPath = $settings['extensionsDir'].'/org.iida.civiconfig/resources/';
foreach ($this->getConfigurableEntityTypes() as $entityType) {
$configClass = "CRM_Civiconfig_Entity_$entityType";
// TODO: Check whether class exists.
$entityTypeConfig = new $configClass();
// Create all entities using the json files in the resources directory.
$entityTypeConfig->createAll(new CRM_Civiconfig_ParamsProvider_ResourcesDir($entityType));
}
}
/** /**
* Singleton method * Return class instance.
* * @return self
* @return CRM_Civiconfig_Config
* @access public
* @static
*/ */
public static function singleton() { public static function singleton() {
if (!self::$_singleton) { if (!self::$_singleton) {
self::$_singleton = new CRM_Civiconfig_Config(); self::$_singleton = new \CRM_Civiconfig_Config();
} }
return self::$_singleton; return self::$_singleton;
} }
/** /**
* Returns all entity types that can be configured with this extension. * Returns all entity types that can be configured with this extension.
*
* The order of this array determines the order of configuration of the * The order of this array determines the order of configuration of the
* entity types. * entity types.
* *
* @return array array of entity type names. * @return array array of entity type names.
*/ */
private function getConfigurableEntityTypes() { public function getSupportedEntityTypes() {
// TODO: make this list configurable. // TODO: make this list configurable.
return array( return array(
'CivicrmSetting',
'ContactType', 'ContactType',
'MembershipType', 'MembershipType',
'RelationshipType', 'RelationshipType',
'OptionGroup', 'OptionGroup',
'Group', 'Group',
'Tag',
'FinancialAccount',
'FinancialType',
'EventType', 'EventType',
'ActivityType', 'ActivityType',
'Tag',
'LocationType', 'LocationType',
'CaseType',
'CustomGroup', 'CustomGroup',
// customGroep as last one because it might need one of the previous ones (option group, relationship types) // CustomGroup as last one because it might need one of the previous ones (option group, relationship types)
// DO NOT INCLUDE CustomField, because custom fields are updated together // DO NOT INCLUDE CustomField, because custom fields are updated together with custom groups.
// with custom groups.
); );
} }
} }
\ No newline at end of file
...@@ -7,10 +7,11 @@ ...@@ -7,10 +7,11 @@
* @license AGPL-3.0 * @license AGPL-3.0
*/ */
abstract class CRM_Civiconfig_Entity { abstract class CRM_Civiconfig_Entity {
/** /**
* Method to create or update any entity * Method to create or update any entity
* *
* @param array $params * @param array $params Parameters
* @return mixed * @return mixed
* @throws Exception when error from API RelationshipType Create * @throws Exception when error from API RelationshipType Create
*/ */
...@@ -18,15 +19,14 @@ abstract class CRM_Civiconfig_Entity { ...@@ -18,15 +19,14 @@ abstract class CRM_Civiconfig_Entity {
/** /**
* Creates/updates all objects at once. * Creates/updates all objects at once.
* * This function now simply gets an array of items instead of having to fetch it here.
* @param $paramsProvider ParamsProvider to provide the 'params' for the *
* objects to be created. * @param array $paramsArray
*/ */
public function createAll(CRM_Civiconfig_ParamsProvider $paramsProvider) { public function createAll($paramsArray) {
$paramsArray = $paramsProvider->getParamsArray();
foreach ($paramsArray as $params) { foreach ($paramsArray as $params) {
$this->create($params); $this->create($params);
} }
} }
} }
...@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_ActivityType extends CRM_Civiconfig_Entity_OptionVal ...@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_ActivityType extends CRM_Civiconfig_Entity_OptionVal
*/ */
protected function validateCreateParams($params) { protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) { if (!isset($params['name']) || empty($params['name'])) {
throw new Exception('Missing mandatory param name in class CRM_Civiconfig_ActivityType'); throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
} }
$this->_apiParams = $params; $this->_apiParams = $params;
try { try {
$this->_apiParams['option_group_id'] = $this->getOptionGroupId(); $this->_apiParams['option_group_id'] = $this->getOptionGroupId();
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
throw new Exception('Unable to find option group for activity_type in CRM_Civiconfig_ActivityType, contact your system administrator'); throw new \CRM_Civiconfig_EntityException("Unable to find option group for activity_type in " . get_class() . ", contact your system administrator.");
} }
} }
......
<?php
/**
* Class CRM_Civiconfig_Entity_CaseType.
*
* @author Kevin Levie (CiviCooP) <kevin.levie@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_CaseType extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
/**
* CRM_Civiconfig_Entity_CaseType constructor.
*/
public function __construct() {
$this->_apiParams = array();
}
/**
* Method to validate params passed to create
*
* @param $params
* @throws Exception when required param not found
*/
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
}
// The line below is in a strange place in the code. But I'll keep it
// there, because it is there as well for every other entity type.
$this->_apiParams = $params;
}
/**
* Method to create or update a case type
*
* @param array $params
* @return mixed
* @throws Exception if an API error occurs
*/
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
try {
civicrm_api3('FinancialType', 'Create', $this->_apiParams);
} catch (\CiviCRM_API3_Exception $ex) {
throw new \CRM_Civiconfig_EntityException('Could not create or update financial type with name '.$this->_apiParams['name'] . '. Error from API CaseType.Create: '.$ex->getMessage() . '.');
}
}
/**
* Function to get the case type by name
*
* @param string $name
* @return array|bool
* @access public
* @static
*/
public function getWithName($name) {
try {
return civicrm_api3('CaseType', 'Getsingle',
array('name' => $name));
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
}
\ No newline at end of file
<?php
/**
* Class CRM_Civiconfig_Entity_CivicrmSetting.
* This class adds support for updating CiviCRM system settings that are stored
* in the civicrm_setting table using the Setting.Create API.
*
* @author Kevin Levie (CiviCooP) <kevin.levie@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_CivicrmSetting extends CRM_Civiconfig_Entity {
protected $_apiParams = [];
/**
* CRM_Civiconfig_Entity_CivicrmSetting constructor.
*/
public function __construct() {
$this->_apiParams = [];
}
/**
* Creates/updates all objects at once.
* Overloading createAll here because we can add all settings in one API call.
*
* @param array $paramsArray
* @return bool Success
* @throws Exception if an API error occurs
*/
public function createAll($paramsArray) {
if (!is_array($paramsArray) || count($paramsArray) == 0) {
return FALSE;
}
$this->_apiParams = $paramsArray;
try {
civicrm_api3('Setting', 'create', $this->_apiParams);
} catch (\CiviCRM_API3_Exception $ex) {
throw new \CRM_Civiconfig_EntityException('Could not create or update CiviCRM settings. Error from API Setting.Create: ' . $ex->getMessage() . '.');
}
return TRUE;
}
/**
* Method to create or update a CiviCRM setting (redirects to createAll above).
*
* @param array $params Parameters
* @return bool Success
*/
public function create(array $params) {
return $this->createAll([$params]);
}
}
\ No newline at end of file
...@@ -18,7 +18,7 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity { ...@@ -18,7 +18,7 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
*/ */
protected function validateCreateParams($params) { protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) { if (!isset($params['name']) || empty($params['name'])) {
throw new Exception('Missing mandatory param name in class CRM_Civiconfig_ContactType'); throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
} }
$this->_apiParams = $params; $this->_apiParams = $params;
} }
...@@ -42,9 +42,9 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity { ...@@ -42,9 +42,9 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
try { try {
civicrm_api3('ContactType', 'Create', $this->_apiParams); civicrm_api3('ContactType', 'Create', $this->_apiParams);
$this->updateNavigationMenuUrl(); $this->updateNavigationMenuUrl();
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update contact type with name '.$this->_apiParams['name'] throw new \CRM_Civiconfig_EntityException('Could not create or update contact type with name '.$this->_apiParams['name']
.', error from API ContactType Create: '.$ex->getMessage()); .'. Error from API ContactType.Create: '.$ex->getMessage().'.');
} }
} }
...@@ -91,7 +91,7 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity { ...@@ -91,7 +91,7 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
public function getWithName($contactTypeName) { public function getWithName($contactTypeName) {
try { try {
return civicrm_api3('ContactType', 'Getsingle', array('name' => $contactTypeName)); return civicrm_api3('ContactType', 'Getsingle', array('name' => $contactTypeName));
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
return FALSE; return FALSE;
} }
} }
......
...@@ -26,8 +26,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity { ...@@ -26,8 +26,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
private function validateCreateParams($params) { private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name']) || !isset($params['custom_group_id']) if (!isset($params['name']) || empty($params['name']) || !isset($params['custom_group_id'])
|| empty($params['custom_group_id'])) { || empty($params['custom_group_id'])) {
throw new Exception('When trying to create a Custom Field name and custom_group_id are throw new \CRM_Civiconfig_EntityException("Missing mandatory parameters 'name' and/or 'custom_group_id' in class " . get_class() . ".");
mandatory parameters and can not be empty in class CRM_Civiconfig_CustomField');
} }
$this->_apiParams = $params; $this->_apiParams = $params;
if (isset($this->_apiParams['option_group'])) { if (isset($this->_apiParams['option_group'])) {
...@@ -64,9 +63,9 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity { ...@@ -64,9 +63,9 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
if (isset($params['option_group'])) { if (isset($params['option_group'])) {
$this->fixOptionGroups($customField['values'], $params['option_group']); $this->fixOptionGroups($customField['values'], $params['option_group']);
} }
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
throw new Exception('could not create or update custom field with name '.$this->_apiParams['name'] throw new \CRM_Civiconfig_EntityException('Could not create or update custom field with name '.$this->_apiParams['name']
.' in custom group '.$this->_apiParams['custom_group_id'].' error from API CustomField Create: '.$ex->getMessage()); .' in custom group '.$this->_apiParams['custom_group_id'].'. Error from API CustomField.Create: '.$ex->getMessage() . '.');
} }
} }
...@@ -80,7 +79,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity { ...@@ -80,7 +79,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
public function getWithNameCustomGroupId($name, $customGroupId) { public function getWithNameCustomGroupId($name, $customGroupId) {
try { try {
return civicrm_api3('CustomField', 'Getsingle', array('name' => $name, 'custom_group_id' => $customGroupId)); return civicrm_api3('CustomField', 'Getsingle', array('name' => $name, 'custom_group_id' => $customGroupId));
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
return FALSE; return FALSE;
} }
} }
...@@ -125,12 +124,16 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity { ...@@ -125,12 +124,16 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
try { try {
$existingCustomFields = civicrm_api3('CustomField', 'Get', array('custom_group_id' => $customGroupId)); $existingCustomFields = civicrm_api3('CustomField', 'Get', array('custom_group_id' => $customGroupId));
foreach ($existingCustomFields['values'] as $existingId => $existingField) { foreach ($existingCustomFields['values'] as $existingId => $existingField) {
// if existing field not in config custom data, delete custom field // If existing field not in config custom data, delete custom field
if (!isset($configCustomGroupData['fields'][$existingField['name']])) { // Fix KL: check field['name'] if the custom groups array doesn't use names as keys
if (
!isset($configCustomGroupData['fields'][$existingField['name']]) &&
!in_array($existingField['name'], array_column($configCustomGroupData['fields'], 'name'))
) {
civicrm_api3('CustomField', 'Delete', array('id' => $existingId)); civicrm_api3('CustomField', 'Delete', array('id' => $existingId));
} }
} }
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
......
...@@ -28,8 +28,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity { ...@@ -28,8 +28,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
private function validateCreateParams($params) { private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name']) || !isset($params['extends']) || if (!isset($params['name']) || empty($params['name']) || !isset($params['extends']) ||
empty($params['extends'])) { empty($params['extends'])) {
throw new Exception('When trying to create a Custom Group name and extends are mandatory parameters throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' and/or 'extends' in class " . get_class() . ".");
and can not be empty in class CRM_Civiconfig_CustomGroup');
} }
$this->buildApiParams($params); $this->buildApiParams($params);
} }
...@@ -54,10 +53,10 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity { ...@@ -54,10 +53,10 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
} }
try { try {
$customGroup = civicrm_api3('CustomGroup', 'Create', $this->_apiParams); $customGroup = civicrm_api3('CustomGroup', 'Create', $this->_apiParams);
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update custom group with name ' . $this->_apiParams['name'] throw new \CRM_Civiconfig_EntityException('Could not create or update custom group with name ' . $this->_apiParams['name']
. ' to extend ' . $this->_apiParams['extends'] . ', error from API CustomGroup Create: ' . . ' to extend ' . $this->_apiParams['extends'] . '. Error from API CustomGroup.Create: ' .
$ex->getMessage() . ", parameters : " . implode(";", $this->_apiParams)); $ex->getMessage() . ", parameters : " . implode(";", $this->_apiParams) . '.');
} }
$created = $customGroup['values'][$customGroup['id']]; $created = $customGroup['values'][$customGroup['id']];
...@@ -81,7 +80,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity { ...@@ -81,7 +80,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
public function getWithName($name) { public function getWithName($name) {
try { try {
return civicrm_api3('CustomGroup', 'Getsingle', array('name' => $name)); return civicrm_api3('CustomGroup', 'Getsingle', array('name' => $name));
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
return FALSE; return FALSE;
} }
} }
...@@ -104,7 +103,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity { ...@@ -104,7 +103,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
if (isset($this->_apiParams['extends_entity_column_value']) && !empty($this->_apiParams['extends_entity_column_value'])) { if (isset($this->_apiParams['extends_entity_column_value']) && !empty($this->_apiParams['extends_entity_column_value'])) {
if (is_array($this->_apiParams['extends_entity_column_value'])) { if (is_array($this->_apiParams['extends_entity_column_value'])) {
foreach ($this->_apiParams['extends_entity_column_value'] as $extendsValue) { foreach ($this->_apiParams['extends_entity_column_value'] as $extendsValue) {
$activityType = new CRM_Civiconfig_ActivityType(); $activityType = new CRM_Civiconfig_Entity_ActivityType();
$found = $activityType->getWithNameAndOptionGroupId($extendsValue, $activityType->getOptionGroupId()); $found = $activityType->getWithNameAndOptionGroupId($extendsValue, $activityType->getOptionGroupId());
if (isset($found['value'])) { if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['value']; $this->_apiParams['extends_entity_column_value'][] = $found['value'];
...@@ -164,7 +163,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity { ...@@ -164,7 +163,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
if (isset($this->_apiParams['extends_entity_column_value']) && !empty($this->_apiParams['extends_entity_column_value'])) { if (isset($this->_apiParams['extends_entity_column_value']) && !empty($this->_apiParams['extends_entity_column_value'])) {
if (is_array($this->_apiParams['extends_entity_column_value'])) { if (is_array($this->_apiParams['extends_entity_column_value'])) {
foreach ($this->_apiParams['extends_entity_column_value'] as $extendsValue) { foreach ($this->_apiParams['extends_entity_column_value'] as $extendsValue) {
$eventType = new CRM_Civiconfig_EventType(); $eventType = new CRM_Civiconfig_Entity_EventType();
$found = $eventType->getWithNameAndOptionGroupId($extendsValue, $eventType->getOptionGroupId()); $found = $eventType->getWithNameAndOptionGroupId($extendsValue, $eventType->getOptionGroupId());
if (isset($found['value'])) { if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['value']; $this->_apiParams['extends_entity_column_value'][] = $found['value'];
......
...@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_EventType extends CRM_Civiconfig_Entity_OptionValue ...@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_EventType extends CRM_Civiconfig_Entity_OptionValue
*/ */
protected function validateCreateParams($params) { protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) { if (!isset($params['name']) || empty($params['name'])) {
throw new Exception('Missing mandatory param name in class CRM_Civiconfig_EventType'); throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
} }
$this->_apiParams = $params; $this->_apiParams = $params;
try { try {
$this->_apiParams['option_group_id'] = $this->getOptionGroupId(); $this->_apiParams['option_group_id'] = $this->getOptionGroupId();
} catch (CiviCRM_API3_Exception $ex) { } catch (\CiviCRM_API3_Exception $ex) {
throw new Exception('Unable to find option group for event_type in CRM_Civiconfig_EventType, contact your system administrator'); throw new \CRM_Civiconfig_EntityException("Unable to find option group for event_type in " . get_class() . ", contact your system administrator.");
} }
} }
......
<?php
/**
* Class CRM_Civiconfig_Entity_FinancialAccount.
*
* @author Kevin Levie (CiviCooP) <kevin.levie@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_FinancialAccount extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
/**
* CRM_Civiconfig_Entity_FinancialAccount constructor.
*/
public function __construct() {
$this->_apiParams = array();
}
/**
* Method to validate params passed to create
*
* @param $params
* @throws Exception when required param not found
*/
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
}
// The line below is in a strange place in the code. But I'll keep it
// there, because it is there as well for every other entity type.
$this->_apiParams = $params;
}
/**
* Method to create or update a financial account
*
* @param array $params
* @return mixed
* @throws Exception if an API error occurs
*/
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
try {
civicrm_api3('FinancialAccount', 'Create', $this->_apiParams);
} catch (\CiviCRM_API3_Exception $ex) {
throw new \CRM_Civiconfig_EntityException('Could not create or update financial account with name '.$this->_apiParams['name'] . '. Error from API FinancialAccount.Create: '.$ex->getMessage() . '.');
}
}
/**
* Function to get the financial account by name
*
* @param string $name
* @return array|bool
* @access public
* @static
*/
public function getWithName($name) {
try {
return civicrm_api3('FinancialAccount', 'Getsingle',