3b8fdefaf075fdfbd79e22742cf1acabe19cd377
[civicrm-core.git] / CRM / Extension / Manager / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 public function __construct() {
44 parent::__construct(TRUE);
45 $this->groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
46 self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name'
47 );
48 }
49
50 public function onPreInstall(CRM_Extension_Info $info) {
51 $customSearchesByName = $this->getCustomSearchesByName();
52 if (array_key_exists($info->key, $customSearchesByName)) {
53 CRM_Core_Error::fatal('This custom search is already registered.');
54 }
55
56 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
57 array('option_group_id' => $this->groupId)
58 );
59
60 $params = array(
61 'option_group_id' => $this->groupId,
62 'weight' => $weight,
63 'description' => $info->label . ' (' . $info->key . ')',
64 'name' => $info->key,
65 'value' => max($customSearchesByName) + 1,
66 'label' => $info->key,
67 'is_active' => 1,
68 );
69
70 $ids = array();
71 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
72
73 return $optionValue ? TRUE : FALSE;
74 }
75
76 public function onPreUninstall(CRM_Extension_Info $info) {
77 $customSearchesByName = $this->getCustomSearchesByName();
78 if (!array_key_exists($info->key, $customSearchesByName)) {
79 CRM_Core_Error::fatal('This custom search is not registered.');
80 }
81
82 $cs = $this->getCustomSearchesById();
83 $id = $cs[$customSearchesByName[$info->key]];
84 $optionValue = CRM_Core_BAO_OptionValue::del($id);
85
86 return TRUE;
87 }
88
89 public function onPreDisable(CRM_Extension_Info $info) {
90 $customSearchesByName = $this->getCustomSearchesByName();
91 $cs = $this->getCustomSearchesById();
92 $id = $cs[$customSearchesByName[$info->key]];
93 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
94 }
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 protected function getCustomSearchesByName() {
104 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
105 }
106
107 protected function getCustomSearchesById() {
108 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
109 }
110 }