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
/**
* 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>
* @date 13 Jan 2016
* @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 {
private static $_singleton;
protected $_resourcesPath = null;
/**
* CRM_Civiconfig_Config constructor.
* @var self $_singleton
*/
function __construct() {
// 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));
}
}
private static $_singleton;
/**
* Singleton method
*
* @return CRM_Civiconfig_Config
* @access public
* @static
* Return class instance.
* @return self
*/
public static function singleton() {
if (!self::$_singleton) {
self::$_singleton = new CRM_Civiconfig_Config();
self::$_singleton = new \CRM_Civiconfig_Config();
}
return self::$_singleton;
}
/**
* Returns all entity types that can be configured with this extension.
*
* The order of this array determines the order of configuration of the
* entity types.
*
*
* @return array array of entity type names.
*/
private function getConfigurableEntityTypes() {
public function getSupportedEntityTypes() {
// TODO: make this list configurable.
return array(
'CivicrmSetting',
'ContactType',
'MembershipType',
'RelationshipType',
'OptionGroup',
'Group',
'Tag',
'FinancialAccount',
'FinancialType',
'EventType',
'ActivityType',
'Tag',
'LocationType',
'CaseType',
'CustomGroup',
// customGroep 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
// with custom groups.
// 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 with custom groups.
);
}
}
\ No newline at end of file
......@@ -7,10 +7,11 @@
* @license AGPL-3.0
*/
abstract class CRM_Civiconfig_Entity {
/**
* Method to create or update any entity
*
* @param array $params
* @param array $params Parameters
* @return mixed
* @throws Exception when error from API RelationshipType Create
*/
......@@ -18,15 +19,14 @@ abstract class CRM_Civiconfig_Entity {
/**
* Creates/updates all objects at once.
*
* @param $paramsProvider ParamsProvider to provide the 'params' for the
* objects to be created.
* This function now simply gets an array of items instead of having to fetch it here.
*
* @param array $paramsArray
*/
public function createAll(CRM_Civiconfig_ParamsProvider $paramsProvider) {
$paramsArray = $paramsProvider->getParamsArray();
public function createAll($paramsArray) {
foreach ($paramsArray as $params) {
$this->create($params);
}
}
}
......@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_ActivityType extends CRM_Civiconfig_Entity_OptionVal
*/
protected function validateCreateParams($params) {
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;
try {
$this->_apiParams['option_group_id'] = $this->getOptionGroupId();
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Unable to find option group for activity_type in CRM_Civiconfig_ActivityType, contact your system administrator');
} catch (\CiviCRM_API3_Exception $ex) {
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 {
*/
protected function validateCreateParams($params) {
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;
}
......@@ -42,9 +42,9 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
try {
civicrm_api3('ContactType', 'Create', $this->_apiParams);
$this->updateNavigationMenuUrl();
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update contact type with name '.$this->_apiParams['name']
.', error from API ContactType Create: '.$ex->getMessage());
} catch (\CiviCRM_API3_Exception $ex) {
throw new \CRM_Civiconfig_EntityException('Could not create or update contact type with name '.$this->_apiParams['name']
.'. Error from API ContactType.Create: '.$ex->getMessage().'.');
}
}
......@@ -91,7 +91,7 @@ class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
public function getWithName($contactTypeName) {
try {
return civicrm_api3('ContactType', 'Getsingle', array('name' => $contactTypeName));
} catch (CiviCRM_API3_Exception $ex) {
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
......
......@@ -26,8 +26,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name']) || !isset($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
mandatory parameters and can not be empty in class CRM_Civiconfig_CustomField');
throw new \CRM_Civiconfig_EntityException("Missing mandatory parameters 'name' and/or 'custom_group_id' in class " . get_class() . ".");
}
$this->_apiParams = $params;
if (isset($this->_apiParams['option_group'])) {
......@@ -64,9 +63,9 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
if (isset($params['option_group'])) {
$this->fixOptionGroups($customField['values'], $params['option_group']);
}
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('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());
} catch (\CiviCRM_API3_Exception $ex) {
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() . '.');
}
}
......@@ -80,7 +79,7 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
public function getWithNameCustomGroupId($name, $customGroupId) {
try {
return civicrm_api3('CustomField', 'Getsingle', array('name' => $name, 'custom_group_id' => $customGroupId));
} catch (CiviCRM_API3_Exception $ex) {
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
......@@ -125,12 +124,16 @@ class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
try {
$existingCustomFields = civicrm_api3('CustomField', 'Get', array('custom_group_id' => $customGroupId));
foreach ($existingCustomFields['values'] as $existingId => $existingField) {
// if existing field not in config custom data, delete custom field
if (!isset($configCustomGroupData['fields'][$existingField['name']])) {
// If existing field not in config custom data, delete custom field
// 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));
}
}
} catch (CiviCRM_API3_Exception $ex) {
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
return TRUE;
......
......@@ -28,8 +28,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name']) || !isset($params['extends']) ||
empty($params['extends'])) {
throw new Exception('When trying to create a Custom Group name and extends are mandatory parameters
and can not be empty in class CRM_Civiconfig_CustomGroup');
throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' and/or 'extends' in class " . get_class() . ".");
}
$this->buildApiParams($params);
}
......@@ -54,10 +53,10 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
}
try {
$customGroup = civicrm_api3('CustomGroup', 'Create', $this->_apiParams);
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update custom group with name ' . $this->_apiParams['name']
. ' to extend ' . $this->_apiParams['extends'] . ', error from API CustomGroup Create: ' .
$ex->getMessage() . ", parameters : " . implode(";", $this->_apiParams));
} catch (\CiviCRM_API3_Exception $ex) {
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: ' .
$ex->getMessage() . ", parameters : " . implode(";", $this->_apiParams) . '.');
}
$created = $customGroup['values'][$customGroup['id']];
......@@ -81,7 +80,7 @@ class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
public function getWithName($name) {
try {
return civicrm_api3('CustomGroup', 'Getsingle', array('name' => $name));
} catch (CiviCRM_API3_Exception $ex) {
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
......@@ -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 (is_array($this->_apiParams['extends_entity_column_value'])) {
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());
if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['value'];
......@@ -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 (is_array($this->_apiParams['extends_entity_column_value'])) {
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());
if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['value'];
......
......@@ -15,13 +15,13 @@ class CRM_Civiconfig_Entity_EventType extends CRM_Civiconfig_Entity_OptionValue
*/
protected function validateCreateParams($params) {
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;
try {
$this->_apiParams['option_group_id'] = $this->getOptionGroupId();
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Unable to find option group for event_type in CRM_Civiconfig_EventType, contact your system administrator');
} catch (\CiviCRM_API3_Exception $ex) {
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',
array('name' => $name));
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
}
\ No newline at end of file
<?php
/**
* Class CRM_Civiconfig_Entity_FinancialType.
*
* @author Kevin Levie (CiviCooP) <kevin.levie@civicoop.org>
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_FinancialType extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
/**
* CRM_Civiconfig_Entity_FinancialType 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 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 FinancialType.Create: '.$ex->getMessage() . '.');
}
}
/**
* Function to get the financial type by name
*
* @param string $name
* @return array|bool
* @access public
* @static
*/
public function getWithName($name) {
try {
return civicrm_api3('FinancialType', 'Getsingle',
array('name' => $name));
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
*/
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new Exception('Missing mandatory param name in class CRM_Civiconfig_Group');
throw new \CRM_Civiconfig_EntityException("Missing mandatory parameter 'name' in class " . get_class() . ".");
}
$this->_apiParams = $params;
}
......@@ -32,7 +32,8 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
/**
* Method to create or update group
*
* @param $params
* @param array $params
* @return void
* @throws Exception when error in API Group Create or when missing mandatory param name
* @access public
*/
......@@ -57,9 +58,9 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
try {
$group = civicrm_api3('Group', 'Create', $this->_apiParams);
$this->fixName($group);
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update group type with name'
.$this->_apiParams['name'].', error from API Group Create: ' . $ex->getMessage());
} catch (\CiviCRM_API3_Exception $ex) {
throw new \CRM_Civiconfig_EntityException('Could not create or update group type with name'
.$this->_apiParams['name'].'. Error from API Group.Create: ' . $ex->getMessage() . '.');
}
}
......@@ -73,7 +74,7 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
public function getWithName($groupName) {
try {
return civicrm_api3('Group', 'Getsingle', array('name' => $groupName));
} catch (CiviCRM_API3_Exception $ex) {
} catch (\CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
......