Commit a431aacb authored by ErikHommel's avatar ErikHommel
Browse files

initial load

parents
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class following Singleton pattern o create or update configuration items from
* JSON files in resources folder
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_ConfigItems {
private static $_singleton;
protected $_resourcesPath;
protected $_customDataDir;
/**
* CRM_Acqconfigitem_ConfigItems constructor.
*
* @throws API_Exception
*/
public function __construct() {
// Get the directory of the extension based on the name.
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcesPath = $container->getPath('acqconfigitems') . '/CRM/Acqconfigitems/resources/';
if (!is_dir($resourcesPath) || !file_exists($resourcesPath)) {
throw new API_Exception(E::ts('Could not find the folder ') . $resourcesPath
. E::ts(' which is required for extension acqconfigitems in ') . __METHOD__
. E::ts('.It does not exist or is not a folder, contact your system administrator'), 9927);
}
$this->_resourcesPath = $resourcesPath;
$this->setContactTypes();
//$this->setRelationshipTypes();
$this->setOptionGroups();
//$this->setTags();
// customData as last one because it might need one of the previous ones (option group, relationship types, activity types)
$this->setCustomData();
}
/**
* Singleton method
*
* @return CRM_Acqconfigitems_ConfigItems
* @access public
* @static
*/
public static function singleton() {
if (!self::$_singleton) {
self::$_singleton = new CRM_Acqconfigitems_ConfigItems();
}
return self::$_singleton;
}
/**
* Method to create option groups
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setOptionGroups() {
$jsonFile = $this->_resourcesPath . 'option_groups.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load option_groups configuration file for extension,
contact your system administrator!'), 9928);
}
$optionGroupsJson = file_get_contents($jsonFile);
$optionGroups = json_decode($optionGroupsJson, TRUE);
foreach ($optionGroups as $name => $optionGroupParams) {
$optionGroup = new CRM_Acqconfigitems_OptionGroup();
$optionGroup->create($optionGroupParams);
}
}
/**
* Method to create contact types
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setContactTypes() {
$jsonFile = $this->_resourcesPath . 'contact_types.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load contact_types configuration file for extension,
contact your system administrator!'), 9929);
}
$contactTypesJson = file_get_contents($jsonFile);
$contactTypes = json_decode($contactTypesJson, TRUE);
foreach ($contactTypes as $name => $contactTypeParams) {
$contactType = new CRM_Acqconfigitems_ContactType();
$contactType->create($contactTypeParams);
}
}
/**
* Method to create tags
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setTags() {
$jsonFile = $this->_resourcesPath . 'tags.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load tags configuration file for extension,
contact your system administrator!'), 9930);
}
$tagsJson = file_get_contents($jsonFile);
$tags = json_decode($tagsJson, TRUE);
foreach ($tags as $name => $tagParams) {
$tag = new CRM_Acqconfigitems_Tag();
$tag->create($tagParams);
}
}
/**
* Method to create relationship types
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setRelationshipTypes() {
$jsonFile = $this->_resourcesPath . 'relationship_types.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load relationship_types configuration file for extension,
contact your system administrator!'), 9931);
}
$relationshipTypesJson = file_get_contents($jsonFile);
$relationshipTypes = json_decode($relationshipTypesJson, TRUE);
$relationshipType = new CRM_Acqconfigitems_RelationshipType();
foreach ($relationshipTypes as $name => $relationshipTypeParams) {
$relationshipType->create($relationshipTypeParams);
}
// disable core relationship types that are not required
$relationshipType->disableRelationshipType("Child of");
$relationshipType->disableRelationshipType("Spouse of");
$relationshipType->disableRelationshipType("Sibling of");
$relationshipType->disableRelationshipType("Volunteer for");
$relationshipType->disableRelationshipType("Head of Household for");
$relationshipType->disableRelationshipType("Household Member of");
$relationshipType->disableRelationshipType("Homeless Services Coordinator is");
$relationshipType->disableRelationshipType("Health Services Coordinator is");
$relationshipType->disableRelationshipType("Senior Services Coordinator is");
$relationshipType->disableRelationshipType("Benefits Specialist is");
}
/**
* Method to create activity types
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setActivityTypes() {
$jsonFile = $this->_resourcesPath . 'activity_types.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load activity_types configuration file for extension,
activity your system administrator!'), 9932);
}
$activityTypesJson = file_get_contents($jsonFile);
$activityTypes = json_decode($activityTypesJson, TRUE);
foreach ($activityTypes as $name => $activityTypeParams) {
$activityType = new CRM_Acqconfigitems_ActivityType();
$activityType->create($activityTypeParams);
}
}
/**
* Method to create campaign types
*
* @throws API_Exception when resource file not found
* @access protected
*/
protected function setCampaignTypes() {
$jsonFile = $this->_resourcesPath . 'campaign_types.json';
if (!file_exists($jsonFile)) {
throw new API_Exception(E::ts('Could not load campaign_types configuration file for extension,
activity your system administrator!'), 9932);
}
$campaignTypesJson = file_get_contents($jsonFile);
$campaignTypes = json_decode($campaignTypesJson, TRUE);
foreach ($campaignTypes as $name => $campaignTypeParams) {
$campaignType = new CRM_Acqconfigitems_CampaignType();
$campaignType->create($campaignTypeParams);
}
}
/**
* Method to set the custom data groups and fields
*
* @access protected
*/
protected function setCustomData() {
// read all json files from custom_groups dir
$customDataPath = $this->_resourcesPath . 'custom_groups';
if (file_exists($customDataPath) && is_dir($customDataPath)) {
// get all json files from dir
$jsonFiles = glob($customDataPath . DIRECTORY_SEPARATOR . "*.json");
foreach ($jsonFiles as $customDataFile) {
$customDataJson = file_get_contents($customDataFile);
$customData = json_decode($customDataJson, TRUE);
foreach ($customData as $customGroupName => $customGroupData) {
$customGroup = new CRM_Acqconfigitems_CustomGroup();
$created = $customGroup->create($customGroupData);
foreach ($customGroupData['fields'] as $customFieldName => $customFieldData) {
$customFieldData['custom_group_id'] = $created['id'];
$customField = new CRM_Acqconfigitems_CustomField();
try {
$customField->create($customFieldData);
}
catch (Exception $exception) {
}
}
// remove custom fields that are still on install but no longer in config
CRM_Acqconfigitems_CustomField::removeUnwantedCustomFields($created['id'], $customGroupData);
}
}
}
}
/**
* Method to disable configuration items
*/
public static function disable() {
self::disableCustomData();
self::disableOptionGroups();
self::disableContactTypes();
self::disableTags();
}
/**
* Method to enable configuration items
*/
public static function enable() {
self::enableCustomData();
self::enableOptionGroups();
self::enableContactTypes();
self::enableTags();
}
/**
* Method to uninstall configuration items
*/
public static function uninstall() {
self::uninstallCustomData();
self::uninstallOptionGroups();
self::uninstallContactTypes();
self::uninstallTags();
}
/**
* Method to uninstall custom data
*/
private static function uninstallCustomData() {
// read all json files from custom_groups dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$customDataPath = $container->getPath('nihrconfigitems') . '/CRM/Acqconfigitems/resources/custom_groups';
if (file_exists($customDataPath) && is_dir($customDataPath)) {
// get all json files from dir
$jsonFiles = glob($customDataPath . DIRECTORY_SEPARATOR . "*.json");
foreach ($jsonFiles as $customDataFile) {
$customDataJson = file_get_contents($customDataFile);
$customData = json_decode($customDataJson, TRUE);
foreach ($customData as $customGroupName => $customGroupData) {
$customGroup = new CRM_Acqconfigitems_CustomGroup();
$customGroup->uninstall($customGroupName);
}
}
}
}
/**
* Method to enable custom data
*/
private static function enableCustomData() {
// read all json files from custom_groups dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$customDataPath = $container->getPath('nihrconfigitems') . '/CRM/Acqconfigitems/resources/custom_groups';
if (file_exists($customDataPath) && is_dir($customDataPath)) {
// get all json files from dir
$jsonFiles = glob($customDataPath . DIRECTORY_SEPARATOR . "*.json");
foreach ($jsonFiles as $customDataFile) {
$customDataJson = file_get_contents($customDataFile);
$customData = json_decode($customDataJson, TRUE);
foreach ($customData as $customGroupName => $customGroupData) {
$customGroup = new CRM_Acqconfigitems_CustomGroup();
$customGroup->enable($customGroupName);
}
}
}
}
/**
* Method to disable custom data
*/
private static function disableCustomData() {
// read all json files from custom_groups dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$customDataPath = $container->getPath('nihrconfigitems') . '/CRM/Acqconfigitems/resources/custom_groups';
if (file_exists($customDataPath) && is_dir($customDataPath)) {
// get all json files from dir
$jsonFiles = glob($customDataPath . DIRECTORY_SEPARATOR . "*.json");
foreach ($jsonFiles as $customDataFile) {
$customDataJson = file_get_contents($customDataFile);
$customData = json_decode($customDataJson, TRUE);
foreach ($customData as $customGroupName => $customGroupData) {
$customGroup = new CRM_Acqconfigitems_CustomGroup();
$customGroup->disable($customGroupName);
}
}
}
}
/**
* Method to disable option groups
*/
private static function disableOptionGroups() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('nihrconfigitems') . '/CRM/Acqconfigitems/resources/';
$jsonFile = $resourcePath . 'option_groups.json';
if (file_exists($jsonFile)) {
$optionGroupsJson = file_get_contents($jsonFile);
$optionGroups = json_decode($optionGroupsJson, TRUE);
foreach ($optionGroups as $name => $optionGroupParams) {
$optionGroup = new CRM_Acqconfigitems_OptionGroup();
$optionGroup->disable($name);
}
}
}
/**
* Method to disable contact types
*/
private static function disableContactTypes() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('nihrconfigitems') . '/CRM/Acqconfigitems/resources//';
$jsonFile = $resourcePath . 'contact_types.json';
if (file_exists($jsonFile)) {
$contactTypesJson = file_get_contents($jsonFile);
$contactTypes = json_decode($contactTypesJson, TRUE);
foreach ($contactTypes as $name => $contactTypeParams) {
$contactType = new CRM_Acqconfigitems_ContactType();
$contactType->disableContactType($name);
}
}
}
/**
* Method to enable contact types
*/
private static function enableContactTypes() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'contact_types.json';
if (file_exists($jsonFile)) {
$contactTypesJson = file_get_contents($jsonFile);
$contactTypes = json_decode($contactTypesJson, TRUE);
foreach ($contactTypes as $name => $contactTypeParams) {
$contactType = new CRM_Basis_ConfigItems_ContactType();
$contactType->enableContactType($name);
}
}
}
/**
* Method to enable tags
*/
private static function enableTags() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'tags.json';
if (file_exists($jsonFile)) {
$tagsJson = file_get_contents($jsonFile);
$tags = json_decode($tagsJson, TRUE);
foreach ($tags as $name => $tagParams) {
$tag = new CRM_Basis_ConfigItems_Tag();
$tag->enableTag($name);
}
}
}
/**
* Method to disable tags
*/
private static function disableTags() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'tags.json';
if (file_exists($jsonFile)) {
$tagsJson = file_get_contents($jsonFile);
$tags = json_decode($tagsJson, TRUE);
foreach ($tags as $name => $tagParams) {
$tag = new CRM_Basis_ConfigItems_Tag();
$tag->disableTag($name);
}
}
}
/**
* Method to uninstall tags
*/
private static function uninstallTags() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'tags.json';
if (file_exists($jsonFile)) {
$tagsJson = file_get_contents($jsonFile);
$tags = json_decode($tagsJson, TRUE);
foreach ($tags as $name => $tagParams) {
$tag = new CRM_Basis_ConfigItems_Tag();
$tag->uninstallTag($name);
}
}
}
/**
* Method to uninstall contact types
*/
private static function uninstallContactTypes() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'contact_types.json';
if (file_exists($jsonFile)) {
$contactTypesJson = file_get_contents($jsonFile);
$contactTypes = json_decode($contactTypesJson, TRUE);
foreach ($contactTypes as $name => $contactTypeParams) {
$contactType = new CRM_Basis_ConfigItems_ContactType();
$contactType->uninstallContactType($name);
}
}
}
/**
* Method to enable option groups
*/
private static function enableOptionGroups() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'option_groups.json';
if (file_exists($jsonFile)) {
$optionGroupsJson = file_get_contents($jsonFile);
$optionGroups = json_decode($optionGroupsJson, TRUE);
foreach ($optionGroups as $name => $optionGroupParams) {
$optionGroup = new CRM_Basis_ConfigItems_OptionGroup();
$optionGroup->enable($name);
}
}
}
/**
* Method to uninstall option groups
*/
private static function uninstallOptionGroups() {
// read all json files from dir
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcePath = $container->getPath('be.mediwe.basis') . '/CRM/Basis/ConfigItems/resources/';
$jsonFile = $resourcePath . 'option_groups.json';
if (file_exists($jsonFile)) {
$optionGroupsJson = file_get_contents($jsonFile);
$optionGroups = json_decode($optionGroupsJson, TRUE);
foreach ($optionGroups as $name => $optionGroupParams) {
$optionGroup = new CRM_Basis_ConfigItems_OptionGroup();
$optionGroup->uninstall($name);
}
}
}
/**
* Method to get the Case Type definition JSON and turn it into valid definition array
* @param $caseType
* @return array
*/
private function getCaseDefinition($caseType) {
$result = array();
$jsonFile = $this->_resourcesPath . $caseType . '.json';
if (file_exists($jsonFile)) {
$definitionJson = file_get_contents($jsonFile);
$definition = json_decode($definitionJson, TRUE);
foreach ($definition as $definitionKey => $definitionValues) {
switch ($definitionKey) {
case "activityTypes":
foreach ($definitionValues as $typeKey => $typeValues) {
$result['activityTypes'][] = $typeValues;
}
break;
case "activitySets":
foreach ($definitionValues as $setKey => $setValues) {
$temp = $setValues;
unset($temp['activityTypes']);
if (isset($setValues['activityTypes'])) {
foreach ($setValues['activityTypes'] as $setTypeKey => $setTypeValues) {
$temp['activityTypes'][] = $setTypeValues;
}
}
if (!empty($temp)) {
$result['activitySets'][] = $temp;
}
}
break;
case "caseRoles":
foreach ($definitionValues as $roleKey => $roleValues) {
$result['caseRoles'][] = $roleValues;
}
break;
}
}
}
return $result;
}
}
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class for ContactType configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_ContactType {
protected $_apiParams = [];
/**
* Method to validate params for create
*
* @param $params
* @throws API_Exception when missing mandatory params
*/
protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new Exception(E::ts('Missing mandatory param name in ') . __METHOD__, 9921);
}
$this->_apiParams = $params;
}
/**
* Method to create contact type
*
* @param array $params
* @return mixed
* @throws API_Exception when error from API ContactType Create
*/
public function create($params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
if (!isset($this->_apiParams['label']) || empty($this->_apiParams['label'])) {
$this->_apiParams['label'] = CRM_Acqconfigitems_Utils::buildLabelFromName($this->_apiParams['name']);
}
// if parent, retrieve parent_id
if (isset($this->_apiParams['parent'])) {
$this->_apiParams['parent_id'] = $this->getContactTypeIdWithName($this->_apiParams['parent']);
unset($this->_apiParams['parent']);
}
$this->_apiParams['is_active'] = 1;
try {
civicrm_api3('ContactType', 'create', $this->_apiParams);
$this->updateNavigationMenuUrl();
}
catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception(E::ts('Could not create or update membership type with name ') . $this->_apiParams['name']
. E::ts(' in ') . __METHOD__ . E::ts(', error from API ContactType Create: ') . $ex->getMessage(), 9922);
}
}
/**
* Method to check if there is a navigation menu option for the contact type
* and if so, update name and url
*
* @access private
*/
private function updateNavigationMenuUrl() {
$query = "SELECT * FROM civicrm_navigation WHERE label = %1";
$label = "New " . $this->_apiParams['label'];
$dao = CRM_Core_DAO::executeQuery($query, [1 => [$label, 'String']]);
$validParent = ["New Organization", "New Individual", "New Household"];
$contactTypeName = $this->getContactTypeNameWithId($this->_apiParams['parent_id']);
$newUrl = CRM_Utils_System::url('civicrm/contact/add', 'ct=' . $contactTypeName . '&cst=' . $this->_apiParams['name'] . '&reset=1', TRUE);
$newName = "New " . $this->_apiParams['name'];
while ($dao->fetch()) {
// parent should be either New Organization, New Individual or New Household