Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * This class stores logic for managing CiviCRM extensions. | |
30 | * | |
31 | * @package CRM | |
8c9251b3 | 32 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
33 | */ |
34 | class CRM_Extension_Manager_Search extends CRM_Extension_Manager_Base { | |
35 | ||
7da04cde | 36 | const CUSTOM_SEARCH_GROUP_NAME = 'custom_search'; |
6a488035 | 37 | |
8246bca4 | 38 | /** |
39 | * CRM_Extension_Manager_Search constructor. | |
40 | */ | |
6a488035 TO |
41 | public function __construct() { |
42 | parent::__construct(TRUE); | |
43 | $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
44 | self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name' | |
45 | ); | |
46 | } | |
47 | ||
e0ef6999 EM |
48 | /** |
49 | * @param CRM_Extension_Info $info | |
50 | * | |
51 | * @return bool | |
52 | * @throws Exception | |
53 | */ | |
6a488035 TO |
54 | public function onPreInstall(CRM_Extension_Info $info) { |
55 | $customSearchesByName = $this->getCustomSearchesByName(); | |
56 | if (array_key_exists($info->key, $customSearchesByName)) { | |
57 | CRM_Core_Error::fatal('This custom search is already registered.'); | |
58 | } | |
59 | ||
60 | $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
61 | array('option_group_id' => $this->groupId) | |
62 | ); | |
63 | ||
64 | $params = array( | |
65 | 'option_group_id' => $this->groupId, | |
66 | 'weight' => $weight, | |
67 | 'description' => $info->label . ' (' . $info->key . ')', | |
68 | 'name' => $info->key, | |
69 | 'value' => max($customSearchesByName) + 1, | |
70 | 'label' => $info->key, | |
71 | 'is_active' => 1, | |
72 | ); | |
73 | ||
74 | $ids = array(); | |
75 | $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids); | |
76 | ||
77 | return $optionValue ? TRUE : FALSE; | |
78 | } | |
79 | ||
e0ef6999 EM |
80 | /** |
81 | * @param CRM_Extension_Info $info | |
82 | * | |
83 | * @return bool | |
84 | * @throws Exception | |
85 | */ | |
6a488035 TO |
86 | public function onPreUninstall(CRM_Extension_Info $info) { |
87 | $customSearchesByName = $this->getCustomSearchesByName(); | |
88 | if (!array_key_exists($info->key, $customSearchesByName)) { | |
89 | CRM_Core_Error::fatal('This custom search is not registered.'); | |
90 | } | |
91 | ||
353ffa53 TO |
92 | $cs = $this->getCustomSearchesById(); |
93 | $id = $cs[$customSearchesByName[$info->key]]; | |
6a488035 TO |
94 | $optionValue = CRM_Core_BAO_OptionValue::del($id); |
95 | ||
96 | return TRUE; | |
97 | } | |
98 | ||
e0ef6999 EM |
99 | /** |
100 | * @param CRM_Extension_Info $info | |
101 | */ | |
6a488035 TO |
102 | public function onPreDisable(CRM_Extension_Info $info) { |
103 | $customSearchesByName = $this->getCustomSearchesByName(); | |
353ffa53 TO |
104 | $cs = $this->getCustomSearchesById(); |
105 | $id = $cs[$customSearchesByName[$info->key]]; | |
6a488035 TO |
106 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0); |
107 | } | |
108 | ||
e0ef6999 EM |
109 | /** |
110 | * @param CRM_Extension_Info $info | |
111 | */ | |
6a488035 TO |
112 | public function onPreEnable(CRM_Extension_Info $info) { |
113 | $customSearchesByName = $this->getCustomSearchesByName(); | |
353ffa53 TO |
114 | $cs = $this->getCustomSearchesById(); |
115 | $id = $cs[$customSearchesByName[$info->key]]; | |
6a488035 TO |
116 | $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1); |
117 | } | |
118 | ||
e0ef6999 EM |
119 | /** |
120 | * @return array | |
121 | */ | |
6a488035 TO |
122 | protected function getCustomSearchesByName() { |
123 | return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE); | |
124 | } | |
125 | ||
e0ef6999 EM |
126 | /** |
127 | * @return array | |
128 | */ | |
6a488035 TO |
129 | protected function getCustomSearchesById() { |
130 | return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE); | |
131 | } | |
96025800 | 132 | |
6a488035 | 133 | } |