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