diff --git a/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader.php b/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader.php index bdf96f8284f..f4a27f17aae 100755 --- a/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader.php +++ b/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader.php @@ -28,6 +28,7 @@ class CRM_HRCore_Upgrader extends CRM_HRCore_Upgrader_Base { use CRM_HRCore_Upgrader_Steps_1018; use CRM_HRCore_Upgrader_Steps_1020; use CRM_HRCore_Upgrader_Steps_1021; + use CRM_HRCore_Upgrader_Steps_1022; /** * @var array diff --git a/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader/Steps/1022.php b/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader/Steps/1022.php new file mode 100644 index 00000000000..3f885aa8bcf --- /dev/null +++ b/uk.co.compucorp.civicrm.hrcore/CRM/HRCore/Upgrader/Steps/1022.php @@ -0,0 +1,82 @@ +up1022_getContactEditOptionValuesToRemove(); + $activeOptionValues = $this->up1022_getActiveContactEditOptionValues(); + + $activeOptionValues = array_diff($activeOptionValues, $valuesToRemove); + civicrm_api3('Setting', 'create', [ + 'contact_edit_options' => $activeOptionValues, + ]); + + return TRUE; + } + + /** + * Retrieves active contact edit option setting value + * + * @return array + */ + private function up1022_getActiveContactEditOptionValues() { + $result = civicrm_api3('Setting', 'get', [ + 'return' => ['contact_edit_options'], + ]); + + return $result['values'][$result['id']]['contact_edit_options']; + } + + /** + * Retrieves value for contact edit options to be removed + * + * @return array + */ + private function up1022_getContactEditOptionValuesToRemove() { + $values = []; + $names = $this->up1022_getUnusedContactEditOptionNames(); + + $params = [ + 'return' => ['value', 'name'], + 'option_group_id' => 'contact_edit_options', + 'is_active' => 1, + 'name' => ['IN' => $names] + ]; + + $results = civicrm_api3('OptionValue', 'get', $params); + foreach ($results['values'] as $result) { + array_push($values, $result['value']); + } + + return $values; + } + + /** + * Get names of contact edit options not in use + * + * @return array + */ + private function up1022_getUnusedContactEditOptionNames() { + $names = []; + + foreach (['Contact', 'IM', 'Website'] as $name) { + $params = ['return' => ['id']]; + if ($name === 'Contact') { + $params['suffix_id'] = ['IS NOT NULL' => 1]; + } + + $result = civicrm_api3($name, 'get', $params); + if ($result['count'] == 0) { + array_push($names, $name === 'Contact' ? 'Suffix' : $name); + } + } + + return $names; + } + +}