| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * This class stores logic for managing CiviCRM extensions. |
| 14 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | */ |
| 18 | class CRM_Extension_Manager_Search extends CRM_Extension_Manager_Base { |
| 19 | |
| 20 | const CUSTOM_SEARCH_GROUP_NAME = 'custom_search'; |
| 21 | |
| 22 | /** |
| 23 | * CRM_Extension_Manager_Search constructor. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | parent::__construct(TRUE); |
| 27 | } |
| 28 | |
| 29 | public function getGroupId() { |
| 30 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
| 31 | self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name' |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param CRM_Extension_Info $info |
| 37 | * |
| 38 | * @return bool |
| 39 | * @throws CRM_Core_Exception |
| 40 | */ |
| 41 | public function onPreInstall(CRM_Extension_Info $info) { |
| 42 | $customSearchesByName = $this->getCustomSearchesByName(); |
| 43 | if (array_key_exists($info->key, $customSearchesByName)) { |
| 44 | throw new CRM_Core_Exception(ts('This custom search is already registered.')); |
| 45 | } |
| 46 | |
| 47 | $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', |
| 48 | ['option_group_id' => $this->getGroupId()] |
| 49 | ); |
| 50 | |
| 51 | $params = [ |
| 52 | 'option_group_id' => $this->getGroupId(), |
| 53 | 'weight' => $weight, |
| 54 | 'description' => $info->label . ' (' . $info->key . ')', |
| 55 | 'name' => $info->key, |
| 56 | 'value' => max($customSearchesByName) + 1, |
| 57 | 'label' => $info->key, |
| 58 | 'is_active' => 1, |
| 59 | ]; |
| 60 | |
| 61 | $optionValue = CRM_Core_BAO_OptionValue::add($params); |
| 62 | |
| 63 | return $optionValue ? TRUE : FALSE; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param CRM_Extension_Info $info |
| 68 | * |
| 69 | * @return bool |
| 70 | * @throws Exception |
| 71 | */ |
| 72 | public function onPreUninstall(CRM_Extension_Info $info) { |
| 73 | $customSearchesByName = $this->getCustomSearchesByName(); |
| 74 | if (!array_key_exists($info->key, $customSearchesByName)) { |
| 75 | throw new CRM_Core_Exception('This custom search is not registered.'); |
| 76 | } |
| 77 | |
| 78 | $cs = $this->getCustomSearchesById(); |
| 79 | $id = $cs[$customSearchesByName[$info->key]]; |
| 80 | CRM_Core_BAO_OptionValue::del($id); |
| 81 | |
| 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param CRM_Extension_Info $info |
| 87 | */ |
| 88 | public function onPreDisable(CRM_Extension_Info $info) { |
| 89 | $customSearchesByName = $this->getCustomSearchesByName(); |
| 90 | $cs = $this->getCustomSearchesById(); |
| 91 | $id = $cs[$customSearchesByName[$info->key]]; |
| 92 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param CRM_Extension_Info $info |
| 97 | */ |
| 98 | public function onPreEnable(CRM_Extension_Info $info) { |
| 99 | $customSearchesByName = $this->getCustomSearchesByName(); |
| 100 | $cs = $this->getCustomSearchesById(); |
| 101 | $id = $cs[$customSearchesByName[$info->key]]; |
| 102 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return array |
| 107 | */ |
| 108 | protected function getCustomSearchesByName() { |
| 109 | return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return array |
| 114 | */ |
| 115 | protected function getCustomSearchesById() { |
| 116 | return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE); |
| 117 | } |
| 118 | |
| 119 | } |