Commit 7ed19e00 authored by Johan Vervloet's avatar Johan Vervloet
Browse files

Merge branch 'master' into 1-smart_groups

Conflicts:
	CRM/Civiconfig/Entity/Group.php
parents a69c4d77 351d2c7d
......@@ -5,6 +5,9 @@
* @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 {
......@@ -20,16 +23,14 @@ class CRM_Civiconfig_Config {
// the construction below will not work.
$settings = civicrm_api3('Setting', 'Getsingle', array());
$this->resourcesPath = $settings['extensionsDir'].'/org.iida.civiconfig/resources/';
$this->setContactTypes();
$this->setMembershipTypes();
$this->setRelationshipTypes();
$this->setOptionGroups();
$this->setGroups();
$this->setEventTypes();
$this->setActivityTypes();
$this->setTags();
// customData as last one because it might need one of the previous ones (option group, relationship types)
$this->setCustomData();
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));
}
}
/**
......@@ -45,186 +46,31 @@ class CRM_Civiconfig_Config {
}
return self::$_singleton;
}
/**
* Method to create or get relationship types
*
* @throws Exception when resource file could not be loaded
*/
protected function setRelationshipTypes() {
$jsonFile = $this->resourcesPath.'relationship_types.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load relationship types configuration file for extension,
contact your system administrator!');
}
$relationshipTypesJson = file_get_contents($jsonFile);
$relationshipTypes = json_decode($relationshipTypesJson, true);
foreach ($relationshipTypes as $relationName => $params) {
$relationshipType = new CRM_Civiconfig_RelationshipType();
$relationshipType->create($params);
}
}
/**
* Method to create or get membership types
*
* @throws Exception when resource file could not be loaded
*/
protected function setMembershipTypes() {
$jsonFile = $this->resourcesPath.'membership_types.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load membership types configuration file for extension,
contact your system administrator!');
}
$membershipTypesJson = file_get_contents($jsonFile);
$membershipTypes = json_decode($membershipTypesJson, true);
foreach ($membershipTypes as $membershipName => $params) {
$membershipType = new CRM_Civiconfig_MembershipType();
$membershipType->create($params);
}
}
/**
* Method to create option groups
*
* @throws Exception when resource file not found
* @access protected
*/
protected function setOptionGroups() {
$jsonFile = $this->resourcesPath.'option_groups.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load option_groups configuration file for extension,
contact your system administrator!');
}
$optionGroupsJson = file_get_contents($jsonFile);
$optionGroups = json_decode($optionGroupsJson, true);
foreach ($optionGroups as $name => $optionGroupParams) {
$optionGroup = new CRM_Civiconfig_OptionGroup();
$optionGroup->create($optionGroupParams);
}
}
/**
* Method to create contact types
*
* @throws Exception when resource file not found
* @access protected
*/
protected function setContactTypes() {
$jsonFile = $this->resourcesPath.'contact_sub_types.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load contact_sub_types configuration file for extension,
contact your system administrator!');
}
$contactTypesJson = file_get_contents($jsonFile);
$contactTypes = json_decode($contactTypesJson, true);
foreach ($contactTypes as $name => $params) {
$contactType = new CRM_Civiconfig_ContactType();
$contactType->create($params);
}
}
/**
* Method to create event types
*
* @throws Exception when resource file not found
* @access protected
* 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.
*/
protected function setEventTypes() {
$jsonFile = $this->resourcesPath.'event_types.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load event_types configuration file for extension,
contact your system administrator!');
}
$eventTypesJson = file_get_contents($jsonFile);
$eventTypes = json_decode($eventTypesJson, true);
foreach ($eventTypes as $name => $params) {
$eventType = new CRM_Civiconfig_EventType();
$eventType->create($params);
}
}
/**
* Method to create activity types
*
* @throws Exception when resource file not found
* @access protected
*/
protected function setActivityTypes() {
$jsonFile = $this->resourcesPath.'activity_types.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load activity_types configuration file for extension,
contact your system administrator!');
}
$activityTypesJson = file_get_contents($jsonFile);
$activityTypes = json_decode($activityTypesJson, true);
foreach ($activityTypes as $name => $params) {
$activityType = new CRM_Civiconfig_ActivityType();
$activityType->create($params);
}
}
/**
* Method to create or get groups
*
* @throws Exception when resource file could not be loaded
*/
protected function setGroups() {
$jsonFile = $this->resourcesPath . 'groups.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load groups configuration file for extension,
contact your system administrator!');
}
$groupJson = file_get_contents($jsonFile);
$groups = json_decode($groupJson, true);
foreach ($groups as $params) {
$group = new CRM_Civiconfig_Group();
$group->create($params);
}
}
/**
* Method to create or get tags
*
* @throws Exception when resource file could not be loaded
*/
protected function setTags() {
$jsonFile = $this->resourcesPath . 'tags.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load tags configuration file for extension,
contact your system administrator!');
}
$tagsJson = file_get_contents($jsonFile);
$tags = json_decode($tagsJson, true);
foreach ($tags as $params) {
$tag = new CRM_Civiconfig_Tag();
$tag->create($params);
}
}
/**
* Method to set the custom data groups and fields
*
* @throws Exception when config json could not be loaded
* @access protected
*/
protected function setCustomData() {
$jsonFile = $this->resourcesPath.'custom_data.json';
if (!file_exists($jsonFile)) {
throw new Exception('Could not load custom data configuration file for extension, contact your system administrator!');
}
$customDataJson = file_get_contents($jsonFile);
$customData = json_decode($customDataJson, true);
foreach ($customData as $customGroupName => $customGroupData) {
$customGroup = new CRM_Civiconfig_CustomGroup();
$created = $customGroup->create($customGroupData);
foreach ($customGroupData['fields'] as $customFieldName => $customFieldData) {
$customFieldData['custom_group_id'] = $created['id'];
$customField = new CRM_Civiconfig_CustomField();
$customField->create($customFieldData);
}
// remove custom fields that are still on install but no longer in config
CRM_Civiconfig_CustomField::removeUnwantedCustomFields($created['id'], $customGroupData);
}
private function getConfigurableEntityTypes() {
// TODO: make this list configurable.
return array(
'ContactType',
'MembershipType',
'RelationshipType',
'OptionGroup',
'Group',
'EventType',
'ActivityType',
'Tag',
'LocationType',
'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.
);
}
}
\ No newline at end of file
<?php
/**
* Abstract base class for entity configuration
*
* @author Johan Vervloet (Chirojeugd-Vlaanderen vzw) <helpdesk@chiro.be>
* @date 6 Jun 2016
* @license AGPL-3.0
*/
abstract class CRM_Civiconfig_Entity {
/**
* Method to create or update any entity
*
* @param array $params
* @return mixed
* @throws Exception when error from API RelationshipType Create
*/
public abstract function create(array $params);
/**
* Creates/updates all objects at once.
*
* @param $paramsProvider ParamsProvider to provide the 'params' for the
* objects to be created.
*/
public function createAll(CRM_Civiconfig_ParamsProvider $paramsProvider) {
$paramsArray = $paramsProvider->getParamsArray();
foreach ($paramsArray as $params) {
$this->create($params);
}
}
}
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_ActivityType extends CRM_Civiconfig_OptionValue {
class CRM_Civiconfig_Entity_ActivityType extends CRM_Civiconfig_Entity_OptionValue {
/**
* Overridden parent method to validate params for create
*
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_ContactType {
class CRM_Civiconfig_Entity_ContactType extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -30,7 +30,7 @@ class CRM_Civiconfig_ContactType {
* @return mixed
* @throws Exception when error from API ContactType Create
*/
public function create($params) {
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_CustomField {
class CRM_Civiconfig_Entity_CustomField extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -32,7 +32,7 @@ class CRM_Civiconfig_CustomField {
$this->_apiParams = $params;
if (isset($this->_apiParams['option_group'])) {
$this->_apiParams['option_type'] = 0;
$optionGroup = new CRM_Civiconfig_OptionGroup();
$optionGroup = new CRM_Civiconfig_Entity_OptionGroup();
$found = $optionGroup->getWithName($this->_apiParams['option_group']);
if (!empty($found)) {
$this->_apiParams['option_group_id'] = $found['id'];
......@@ -50,7 +50,7 @@ class CRM_Civiconfig_CustomField {
* @param array $params
* @throws Exception when error from API CustomField Create
*/
public function create($params) {
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithNameCustomGroupId($this->_apiParams['name'], $this->_apiParams['custom_group_id']);
if (isset($existing['id'])) {
......@@ -94,7 +94,7 @@ class CRM_Civiconfig_CustomField {
* @throws CiviCRM_API3_Exception
*/
protected function fixOptionGroups($customField, $optionGroupName) {
$optionGroup = new CRM_Civiconfig_OptionGroup();
$optionGroup = new CRM_Civiconfig_Entity_OptionGroup();
$found = $optionGroup->getWithName($optionGroupName);
// only if found is not equal to created custom field value
if ($found['id'] != $customField[key($customField)]['option_group_id']) {
......
<?php
/**
* Class for CustomGroup configuration
*
* This class creates the custom fields as well.
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_CustomGroup {
class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -33,13 +35,15 @@ class CRM_Civiconfig_CustomGroup {
}
/**
* Method to create custom group
* Method to create custom group with custom fields.
*
* @param array $params
* @return array
* @throws Exception when error from API CustomGroup Create
*/
public function create($params) {
public function create(array $params) {
$fieldParamsArray = $params['fields'];
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
......@@ -55,7 +59,17 @@ class CRM_Civiconfig_CustomGroup {
. ' to extend ' . $this->_apiParams['extends'] . ', error from API CustomGroup Create: ' .
$ex->getMessage() . ", parameters : " . implode(";", $this->_apiParams));
}
return $customGroup['values'][$customGroup['id']];
$created = $customGroup['values'][$customGroup['id']];
foreach ($fieldParamsArray as $customFieldData) {
$customFieldData['custom_group_id'] = $created['id'];
$customField = new CRM_Civiconfig_Entity_CustomField();
$customField->create($customFieldData);
}
// remove custom fields that are still on install but no longer in config
CRM_Civiconfig_Entity_CustomField::removeUnwantedCustomFields($created['id'], $params);
return $created;
}
/**
......@@ -78,6 +92,7 @@ class CRM_Civiconfig_CustomGroup {
* @param array $params
*/
protected function buildApiParams($params) {
// This can probably be refactored as well.
$this->_apiParams = array();
foreach ($params as $name => $value) {
if ($name != 'fields') {
......@@ -97,7 +112,7 @@ class CRM_Civiconfig_CustomGroup {
unset ($activityType);
}
} else {
$activityType = new CRM_Civiconfig_ActivityType();
$activityType = new CRM_Civiconfig_Entity_ActivityType();
$found = $activityType->getWithNameAndOptionGroupId($this->_apiParams['extends_entity_column_value'], $activityType->getOptionGroupId());
if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'] = $found['value'];
......@@ -109,7 +124,7 @@ class CRM_Civiconfig_CustomGroup {
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) {
$membershipType = new CRM_Civiconfig_MembershipType();
$membershipType = new CRM_Civiconfig_Entity_MembershipType();
$found = $membershipType->getWithName($extendsValue);
if (isset($found['id'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['id'];
......@@ -117,7 +132,7 @@ class CRM_Civiconfig_CustomGroup {
unset ($membershipType);
}
} else {
$membershipType = new CRM_Civiconfig_MembershipType();
$membershipType = new CRM_Civiconfig_Entity_MembershipType();
$found = $membershipType->getWithName($this->_apiParams['extends_entity_column_value']);
if (isset($found['id'])) {
$this->_apiParams['extends_entity_column_value'] = $found['id'];
......@@ -129,7 +144,7 @@ class CRM_Civiconfig_CustomGroup {
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) {
$relationshipType = new CRM_Civiconfig_RelationshipType();
$relationshipType = new CRM_Civiconfig_Entity_RelationshipType();
$found = $relationshipType->getWithNameAb($extendsValue);
if (isset($found['id'])) {
$this->_apiParams['extends_entity_column_value'][] = $found['id'];
......@@ -137,7 +152,7 @@ class CRM_Civiconfig_CustomGroup {
unset ($relationshipType);
}
} else {
$relationshipType = new CRM_Civiconfig_RelationshipType();
$relationshipType = new CRM_Civiconfig_Entity_RelationshipType();
$found = $relationshipType->getWithNameAb($this->_apiParams['extends_entity_column_value']);
if (isset($found['id'])) {
$this->_apiParams['extends_entity_column_value'] = $found['id'];
......@@ -157,7 +172,7 @@ class CRM_Civiconfig_CustomGroup {
unset ($eventType);
}
} else {
$eventType = new CRM_Civiconfig_EventType();
$eventType = new CRM_Civiconfig_Entity_EventType();
$found = $eventType->getWithNameAndOptionGroupId($this->_apiParams['extends_entity_column_value'], $eventType->getOptionGroupId());
if (isset($found['value'])) {
$this->_apiParams['extends_entity_column_value'] = $found['value'];
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_EventType extends CRM_Civiconfig_OptionValue {
class CRM_Civiconfig_Entity_EventType extends CRM_Civiconfig_Entity_OptionValue {
/**
* Overridden parent method to validate params for create
*
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_Group {
class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -36,7 +36,7 @@ class CRM_Civiconfig_Group {
* @throws Exception when error in API Group Create or when missing mandatory param name
* @access public
*/
public function create($params) {
public function create(array $params) {
if (!empty($params['form_values'])) {
// Hack for smart groups.
$formValues = $params['form_values'];
......
<?php
/**
* Class for LocationType configuration
*
* @author Johan Vervloet (Chirojeugd-Vlaanderen vzw) <helpdesk@chiro.be>
* @date 6 Jun 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_LocationType extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
/**
* CRM_Civiconfig_RelationshipType 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 Exception('When trying to create a Location Type, name is mandatory
parameter and can not be empty 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 location type
*
* @param array $params
* @return mixed
* @throws Exception when error from API LocationType Create
*/
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('LocationType', 'Create', $this->_apiParams);
} catch (CiviCRM_API3_Exception $ex) {
throw new Exception('Could not create or update location type with name '.$this->_apiParams['name']
.', error from API LocationType Create: '.$ex->getMessage());
}
}
/**
* Function to get the location type with a name
*
* @param string $name
* @return array|bool
* @access public
* @static
*/
public function getWithName($name) {
try {
return civicrm_api3('LocationType', 'Getsingle',
array('name' => $name));
} catch (CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_MembershipType {
class CRM_Civiconfig_Entity_MembershipType extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -185,7 +185,7 @@ class CRM_Civiconfig_MembershipType {
* @return mixed
* @throws Exception when error from API MembershipType Create
*/
public function create($params) {
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_OptionGroup {
class CRM_Civiconfig_Entity_OptionGroup extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -36,7 +36,7 @@ class CRM_Civiconfig_OptionGroup {
* @return array
* @throws Exception when error in API Option Group Create
*/
public function create($params) {
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
......@@ -67,7 +67,7 @@ class CRM_Civiconfig_OptionGroup {
protected function processOptionValues($optionGroupId, $optionValueParams) {
foreach ($optionValueParams as $optionValueName => $params) {
$params['option_group_id'] = $optionGroupId;
$optionValue = new CRM_Civiconfig_OptionValue();
$optionValue = new CRM_Civiconfig_Entity_OptionValue();
$optionValue->create($params);
}
}
......
......@@ -6,7 +6,7 @@
* @date 3 Feb 2016
* @license AGPL-3.0
*/
class CRM_Civiconfig_OptionValue {
class CRM_Civiconfig_Entity_OptionValue extends CRM_Civiconfig_Entity {
protected $_apiParams = array();
......@@ -39,7 +39,7 @@ class CRM_Civiconfig_OptionValue {
* @return array
* @throws Exception when error in API Option Value Create
*/
public function create($params) {
public function create(array $params) {
$this->validateCreateParams($params);
$existing = $this->getWithNameAndOptionGroupId($this->_apiParams['name'], $this->_apiParams['option_group_id']);
if (isset($existing['id'])) {