Merge pull request #8042 from jitendrapurohit/CRM-18250
[civicrm-core.git] / CRM / Extension / Manager / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This class stores logic for managing CiviCRM extensions.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2016
33 */
34 class CRM_Extension_Manager_Search extends CRM_Extension_Manager_Base {
35
36 const CUSTOM_SEARCH_GROUP_NAME = 'custom_search';
37
38 public function __construct() {
39 parent::__construct(TRUE);
40 $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
41 self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name'
42 );
43 }
44
45 /**
46 * @param CRM_Extension_Info $info
47 *
48 * @return bool
49 * @throws Exception
50 */
51 public function onPreInstall(CRM_Extension_Info $info) {
52 $customSearchesByName = $this->getCustomSearchesByName();
53 if (array_key_exists($info->key, $customSearchesByName)) {
54 CRM_Core_Error::fatal('This custom search is already registered.');
55 }
56
57 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
58 array('option_group_id' => $this->groupId)
59 );
60
61 $params = array(
62 'option_group_id' => $this->groupId,
63 'weight' => $weight,
64 'description' => $info->label . ' (' . $info->key . ')',
65 'name' => $info->key,
66 'value' => max($customSearchesByName) + 1,
67 'label' => $info->key,
68 'is_active' => 1,
69 );
70
71 $ids = array();
72 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
73
74 return $optionValue ? TRUE : FALSE;
75 }
76
77 /**
78 * @param CRM_Extension_Info $info
79 *
80 * @return bool
81 * @throws Exception
82 */
83 public function onPreUninstall(CRM_Extension_Info $info) {
84 $customSearchesByName = $this->getCustomSearchesByName();
85 if (!array_key_exists($info->key, $customSearchesByName)) {
86 CRM_Core_Error::fatal('This custom search is not registered.');
87 }
88
89 $cs = $this->getCustomSearchesById();
90 $id = $cs[$customSearchesByName[$info->key]];
91 $optionValue = CRM_Core_BAO_OptionValue::del($id);
92
93 return TRUE;
94 }
95
96 /**
97 * @param CRM_Extension_Info $info
98 */
99 public function onPreDisable(CRM_Extension_Info $info) {
100 $customSearchesByName = $this->getCustomSearchesByName();
101 $cs = $this->getCustomSearchesById();
102 $id = $cs[$customSearchesByName[$info->key]];
103 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
104 }
105
106 /**
107 * @param CRM_Extension_Info $info
108 */
109 public function onPreEnable(CRM_Extension_Info $info) {
110 $customSearchesByName = $this->getCustomSearchesByName();
111 $cs = $this->getCustomSearchesById();
112 $id = $cs[$customSearchesByName[$info->key]];
113 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
114 }
115
116 /**
117 * @return array
118 */
119 protected function getCustomSearchesByName() {
120 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
121 }
122
123 /**
124 * @return array
125 */
126 protected function getCustomSearchesById() {
127 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
128 }
129
130 }