Merge pull request #16967 from wmortada/wp#46-2
[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 $optionValue = CRM_Core_BAO_OptionValue::add($params);
59
60 return $optionValue ? TRUE : FALSE;
61 }
62
63 /**
64 * @param CRM_Extension_Info $info
65 *
66 * @return bool
67 * @throws Exception
68 */
69 public function onPreUninstall(CRM_Extension_Info $info) {
70 $customSearchesByName = $this->getCustomSearchesByName();
71 if (!array_key_exists($info->key, $customSearchesByName)) {
72 CRM_Core_Error::fatal('This custom search is not registered.');
73 }
74
75 $cs = $this->getCustomSearchesById();
76 $id = $cs[$customSearchesByName[$info->key]];
77 $optionValue = CRM_Core_BAO_OptionValue::del($id);
78
79 return TRUE;
80 }
81
82 /**
83 * @param CRM_Extension_Info $info
84 */
85 public function onPreDisable(CRM_Extension_Info $info) {
86 $customSearchesByName = $this->getCustomSearchesByName();
87 $cs = $this->getCustomSearchesById();
88 $id = $cs[$customSearchesByName[$info->key]];
89 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
90 }
91
92 /**
93 * @param CRM_Extension_Info $info
94 */
95 public function onPreEnable(CRM_Extension_Info $info) {
96 $customSearchesByName = $this->getCustomSearchesByName();
97 $cs = $this->getCustomSearchesById();
98 $id = $cs[$customSearchesByName[$info->key]];
99 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
100 }
101
102 /**
103 * @return array
104 */
105 protected function getCustomSearchesByName() {
106 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
107 }
108
109 /**
110 * @return array
111 */
112 protected function getCustomSearchesById() {
113 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
114 }
115
116 }