Merge pull request #15958 from civicrm/5.20
[civicrm-core.git] / CRM / Extension / Manager / Search.php
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 $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
28 self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name'
29 );
30 }
31
32 /**
33 * @param CRM_Extension_Info $info
34 *
35 * @return bool
36 * @throws CRM_Core_Exception
37 */
38 public function onPreInstall(CRM_Extension_Info $info) {
39 $customSearchesByName = $this->getCustomSearchesByName();
40 if (array_key_exists($info->key, $customSearchesByName)) {
41 throw new CRM_Core_Exception(ts('This custom search is already registered.'));
42 }
43
44 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
45 ['option_group_id' => $this->groupId]
46 );
47
48 $params = [
49 'option_group_id' => $this->groupId,
50 'weight' => $weight,
51 'description' => $info->label . ' (' . $info->key . ')',
52 'name' => $info->key,
53 'value' => max($customSearchesByName) + 1,
54 'label' => $info->key,
55 'is_active' => 1,
56 ];
57
58 $ids = [];
59 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
60
61 return $optionValue ? TRUE : FALSE;
62 }
63
64 /**
65 * @param CRM_Extension_Info $info
66 *
67 * @return bool
68 * @throws Exception
69 */
70 public function onPreUninstall(CRM_Extension_Info $info) {
71 $customSearchesByName = $this->getCustomSearchesByName();
72 if (!array_key_exists($info->key, $customSearchesByName)) {
73 CRM_Core_Error::fatal('This custom search is not registered.');
74 }
75
76 $cs = $this->getCustomSearchesById();
77 $id = $cs[$customSearchesByName[$info->key]];
78 $optionValue = CRM_Core_BAO_OptionValue::del($id);
79
80 return TRUE;
81 }
82
83 /**
84 * @param CRM_Extension_Info $info
85 */
86 public function onPreDisable(CRM_Extension_Info $info) {
87 $customSearchesByName = $this->getCustomSearchesByName();
88 $cs = $this->getCustomSearchesById();
89 $id = $cs[$customSearchesByName[$info->key]];
90 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
91 }
92
93 /**
94 * @param CRM_Extension_Info $info
95 */
96 public function onPreEnable(CRM_Extension_Info $info) {
97 $customSearchesByName = $this->getCustomSearchesByName();
98 $cs = $this->getCustomSearchesById();
99 $id = $cs[$customSearchesByName[$info->key]];
100 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
101 }
102
103 /**
104 * @return array
105 */
106 protected function getCustomSearchesByName() {
107 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
108 }
109
110 /**
111 * @return array
112 */
113 protected function getCustomSearchesById() {
114 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
115 }
116
117 }