Commit fb93e0dd authored by ErikHommel's avatar ErikHommel 🏘
Browse files

initial load - locally tested and ok for issue 4594

parents
<?php
use CRM_Aivlbasicsignup_ExtensionUtil as E;
/**
* Config class for AIVL Basic Singup
*
* @author Erik Hommel <erik.hommel@civicoop.org>
* @date 17 Jul 2019
* @license AGPL-3.0
*/
class CRM_Aivlbasicsignup_BasicSignupConfig {
// property for singleton pattern
private static $_singleton = NULL;
// properties for option group ids
private $_basicSignupSourcesOptionGroupId = NULL;
// properties for custom groups
private $_basicSignupCustomGroup = [];
// properties for relationship type ids
private $_employeeRelationshipTypeId = NULL;
// properties for activity types
private $_webformResponseActivityTypeId = NULL;
private $_toCheckActivityTypeId = NULL;
// other properties
private $_aivlContactId = NULL;
/**
* CRM_Aivlbasicsignup_BasicSignupConfig constructor.
*/
public function __construct() {
$this->setOptionGroups();
$this->setActivityTypes();
$this->setRelationshipTypes();
$this->setCustomData();
$this->setAivlContactId();
}
/**
* Getter for to check activity type id
*
* @return null
*/
public function getToCheckActivityTypeId() {
return $this->_toCheckActivityTypeId;
}
/**
* Getter for response via webform activity type id
*
* @return null
*/
public function getWebformResponseActivityTypeId() {
return $this->_webformResponseActivityTypeId;
}
/**
* Getter for AIVL Contact Id (contact organization Amnesty International Vlaanderen)
* @return null
*/
public function getAivlContactId() {
return $this->_aivlContactId;
}
/**
* Getter for sign up sources option group id
* @return null
*/
public function getBasicSignupSourcesOptionGroupId() {
return $this->_basicSignupSourcesOptionGroupId;
}
/**
* Getter for employer/employee relationship type id
*
* @return array|null
*/
public function getEmployeeRelationshipTypId() {
return $this->_employeeRelationshipTypeId;
}
/**
* Getter for basic sign up custom group
*
* @param null $key
* @return array|mixed
*/
public function getBasicSignupCustomGroup($key = NULL) {
if ($key && isset($this->_basicSignUpCustomGroup[$key])) {
return $this->_basicSignupCustomGroup[$key];
}
else {
return $this->_basicSignupCustomGroup;
}
}
/**
* Getter for basic signup data custom field
*
* @param $customFieldName
* @param $key
* @return mixed
*/
public function getBasicSignupCustomField($customFieldName, $key = NULL) {
foreach ($this->_basicSignupCustomGroup['custom_fields'] as $customField) {
if ($customField['name'] == $customFieldName) {
if ($key && isset($customField[$key])) {
return $customField[$key];
}
else {
return $customField;
}
}
}
return FALSE;
}
/**
* Method to set the relevant option groups
*/
private function setOptionGroups() {
$optionGroupNames = [
'aivl_basic_signup_sources',
];
try {
$foundOptionGroups = civicrm_api3('OptionGroup', 'get', [
'return' => ["id", "name"],
'name' => ['IN' => $optionGroupNames],
'options' => ['limit' => 0],
])['values'];
foreach ($foundOptionGroups as $foundOptionGroup) {
$property = $this->getPropertyFromName($foundOptionGroup['name']) . 'OptionGroupId';
$this->$property = $foundOptionGroup['id'];
}
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Could not find a unique option group in ') . __METHOD__);
}
}
/**
* Method to set the relevant activity types
*/
private function setActivityTypes() {
try {
$result = civicrm_api3('OptionValue', 'get', [
'sequential' => 1,
'return' => ["value", "name"],
'option_group_id' => "activity_type",
'name' => ['IN' => ["To check", "Response via webform"]],
]);
foreach ($result['values'] as $activityType) {
switch ($activityType['name']) {
case 'Response via webform':
$this->_webformResponseActivityTypeId = $activityType['value'];
break;
case 'To check':
$this->_toCheckActivityTypeId = $activityType['value'];
break;
}
}
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Could not find activity types response via webform or to check in ') . __METHOD__);
}
}
/**
* Method to find and set the Amnesty International Vlaanderen VZW contact id
*/
private function setAivlContactId() {
try {
$this->_aivlContactId = (int) civicrm_api3('Contact', 'getvalue', [
'return' => "id",
'contact_type' => "Organization",
'legal_name' => ['IN' => ["Amnesty International Vlaanderen VZW", "Amnesty International vzw"]],
]);
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Could not find an organization with legal name Amnesty International Vlaanderen VZW in ') . __METHOD__);
}
}
/**
* Method to set the relevant relationship types
*/
private function setRelationshipTypes() {
try {
$result = civicrm_api3('RelationshipType', 'get', [
'sequential' => 1,
'return' => ["id", "name_a_b", "name_b_a"],
'name_a_b' => ['IN' => ["Employee of"]],
'name_b_a' => ['IN' => ["Employer of"]],
'options' => ['limit' => 0],
]);
foreach ($result['values'] as $relType) {
switch ($relType['name_a_b']) {
case 'Employee of':
$this->_employeeRelationshipTypeId = $relType['id'];
break;
}
}
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Could not find relationship types employee/employer in ') . __METHOD__);
}
}
/**
* Method to set the property from the custom group name
* @param $customGroupName
* @return bool|string|null
*/
private function getPropertyFromName($customGroupName) {
if (empty($customGroupName)) {
return FALSE;
}
$property = NULL;
$parts = explode('_', $customGroupName);
$count = count($parts);
if ($parts[0] == 'aivl') {
$start = 1;
}
else {
$start = 0;
}
for ($x = $start; $x < $count; $x++) {
if (!$property) {
$property = '_' . strtolower($parts[$x]);
}
else {
$property .= ucfirst(strtolower($parts[$x]));
}
}
return $property;
}
/**
* Method to set the custom data
*/
private function setCustomData() {
$relevantCustomGroups = [
'aivl_basic_signup',
];
try {
$customGroups = civicrm_api3('CustomGroup', 'get', [
'options' => ['limit' => 0],
'name' => ['IN' => ["aivl_basic_signup"]],
]);
foreach ($customGroups['values'] as $customGroupId => $customGroup) {
if (in_array($customGroup['name'], $relevantCustomGroups)) {
$property = $this->getPropertyFromName($customGroup['name']) . 'CustomGroup';
// add custom fields
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => $customGroup['id'],
'options' => ['limit' => 0],
]);
$customGroup['custom_fields'] = $customFields['values'];
$this->$property = $customGroup;
}
}
}
catch (CiviCRM_API3_Exception $ex) {
}
}
/**
* Method to return the singleton object or instantiate
*
* @return CRM_Aivlbasicsignup_BasicSignupConfig|null
* @throws
*/
public static function &singleton() {
if (self::$_singleton === NULL) {
self::$_singleton = new CRM_Aivlbasicsignup_BasicSignupConfig();
}
return self::$_singleton;
}
}
<?php
use CRM_Aivlbasicsignup_ExtensionUtil as E;
/**
* Class for ActivityType configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 27 Jun 2019
* @license AGPL-3.0
*/
class CRM_Aivlbasicsignup_ConfigItems_ActivityType extends CRM_Aivlbasicsignup_ConfigItems_OptionValue {
/**
* Overridden parent method to validate params for create
*
* @param $params
* @throws Exception when missing mandatory params
*/
protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
Civi::log()->error(E::ts('Missing mandatory param name in ') . __METHOD__);
}
$this->_apiParams = $params;
try {
$this->_apiParams['option_group_id'] = $this->getOptionGroupId();
}
catch (CiviCRM_API3_Exception $ex) {
throw new Exception(E::ts('Unable to find option group for activity_type in ') . __METHOD__
. E::ts(', contact your system administrator'));
}
}
/**
* Method to get option group id for activity type
*
* @return array
* @throws CiviCRM_API3_Exception
*/
public function getOptionGroupId() {
return civicrm_api3('OptionGroup', 'Getvalue', [
'name' => 'activity_type',
'return' => 'id',
]);
}
/**
* Method to disable activity type
*
* @param $activityTypeName
*/
public function disableActivityType($activityTypeName) {
if (!empty($activityTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get activity type with name
$activityTypeId = civicrm_api3('OptionValue', 'getvalue', [
'option_group_id' => 'activity_type',
'name' => $activityTypeName,
'return' => 'id',
]);
$sqlActivityType = "UPDATE civicrm_option_value SET is_active = %1 WHERE id = %2";
CRM_Core_DAO::executeQuery($sqlActivityType, array(
1 => array(0, 'Integer'),
2 => array($activityTypeId, 'Integer'))
);
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Failed to disable activity type ') . $activityTypeName);
}
}
}
/**
* Method to enable activity type
*
* @param $activityTypeName
*/
public function enableActivityType($activityTypeName) {
if (!empty($activityTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get activity type with name
$activityTypeId = civicrm_api3('OptionValue', 'getvalue', [
'option_group_id' => 'activity_type',
'name' => $activityTypeName,
'return' => 'id',
]);
$sqlActivityType = "UPDATE civicrm_option_value SET is_active = %1 WHERE id = %2";
CRM_Core_DAO::executeQuery($sqlActivityType, [
1 => [1, 'Integer'],
2 => [$activityTypeId, 'Integer']
]);
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Failed to enable activity type ') . $activityTypeName);
}
}
}
/**
* Method to uninstall activity type
*
* @param $activityTypeName
*/
public function uninstallActivityType($activityTypeName) {
if (!empty($activityTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get activity type with name
$activityTypeId = civicrm_api3('OptionValue', 'getvalue', [
'option_group_id' => 'activity_type',
'name' => $activityTypeName,
'return' => 'id',
]);
civicrm_api3('OptionValue', 'delete', ['id' => $activityTypeId]);
}
catch (CiviCRM_API3_Exception $ex) {
Civi::log()->error(E::ts('Failed to delete activity type ') . $activityTypeName);
}
}
}
}
\ No newline at end of file
<?php
use CRM_Aivlbasicsignup_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 27 Jun 2019
* @license AGPL-3.0
*/
class CRM_Aivlbasicsignup_ConfigItems_ConfigItems {
private static $_singleton;
private $_resourcesPath = NULL;
/**
* CRM_Aivlfirstyeardonorjourney_ConfigItems_ConfigItems constructor.
*
* @throws Exception
*/
public function __construct() {
// get the directory of the extension based on the name.
$this->_resourcesPath = CRM_Aivlbasicsignup_ConfigItems_ConfigItems::getResourcesPath();
$this->setOptionGroups();
// customData as last one because it might need one of the previous ones (option group, relationship types, activity types)
$this->setCustomData();
}
/**
* Method to get the resources path depending on CiviCRM version
*
* @return string
* @throws Exception
*/
private static function getResourcesPath() {
$container = CRM_Extension_System::singleton()->getFullContainer();
$resourcesPath = $container->getPath('aivlbasicsignup').'/CRM/Aivlbasicsignup/ConfigItems/resources/';
return $resourcesPath;
}
/**
* Singleton method
*
* @return CRM_Aivlbasicsignup_ConfigItems_ConfigItems
*/
public static function singleton() {
if (!self::$_singleton) {
self::$_singleton = new CRM_Aivlbasicsignup_ConfigItems_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)) {
Civi::log()->error(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_Aivlbasicsignup_ConfigItems_OptionGroup();
$optionGroup->create($optionGroupParams);
}
}
/**
* Method to set the custom data groups and fields
*
* @throws Exception when config json could not be loaded
* @access protected
*/
private 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_Aivlbasicsignup_ConfigItems_CustomGroup();
$created = $customGroup->create($customGroupData);
foreach ($customGroupData['fields'] as $customFieldName => $customFieldData) {
$customFieldData['custom_group_id'] = $created['id'];
$customField = new CRM_Aivlbasicsignup_ConfigItems_CustomField();
$customField->create($customFieldData);
}
// remove custom fields that are still on install but no longer in config
CRM_Aivlbasicsignup_ConfigItems_CustomField::removeUnwantedCustomFields($created['id'], $customGroupData);
}
}
}
else {
Civi::log()->error(E::ts('Could not find any files in folder ') . $customDataPath
. E::ts(' in ') . __METHOD__);
}
}
/**
* Method to disable configuration items
*/
public static function disable() {
self::disableCustomData();
self::disableOptionGroups();
}
/**
* Method to enable configuration items
*/
public static function enable() {
self::enableCustomData();
self::enableOptionGroups();
}
/**
* Method to uninstall configuration items
*/
public static function uninstall() {
self::uninstallCustomData();
self::uninstallOptionGroups();
}
/**
* Method to uninstall custom data
*/
private static function uninstallCustomData() {
// read all json files from custom_groups dir
$resourcesPath = CRM_Aivlbasicsignup_ConfigItems_ConfigItems::getResourcesPath();
$customDataPath = $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_Aivlbasicsignup_ConfigItems_CustomGroup();
$customGroup->uninstall($customGroupName);
}
}
}
}
/**
* Method to enable custom data
*/
private static function enableCustomData() {
// read all json files from custom_groups dir
$resourcesPath = CRM_Aivlbasicsignup_ConfigItems_ConfigItems::getResourcesPath();
$customDataPath = $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_Aivlbasicsignup_ConfigItems_CustomGroup();
$customGroup->enable($customGroupName);
}
}
}
}
/**
* Method to disable custom data
*/
private static function disableCustomData() {
$resourcesPath = CRM_Aivlbasicsignup_ConfigItems_ConfigItems::getResourcesPath();
$customDataPath = $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_Aivlbasicsignup_ConfigItems_CustomGroup();
$customGroup->disable($customGroupName);
}
}
}
}
/**