Merge pull request #16845 from jitendrapurohit/core-1663
[civicrm-core.git] / CRM / Extension / Manager / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * This class stores logic for managing CiviCRM extensions.
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
17 */
18class CRM_Extension_Manager_Search extends CRM_Extension_Manager_Base {
19
7da04cde 20 const CUSTOM_SEARCH_GROUP_NAME = 'custom_search';
6a488035 21
8246bca4 22 /**
23 * CRM_Extension_Manager_Search constructor.
24 */
6a488035
TO
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
e0ef6999
EM
32 /**
33 * @param CRM_Extension_Info $info
34 *
35 * @return bool
df9f2d1a 36 * @throws CRM_Core_Exception
e0ef6999 37 */
6a488035
TO
38 public function onPreInstall(CRM_Extension_Info $info) {
39 $customSearchesByName = $this->getCustomSearchesByName();
40 if (array_key_exists($info->key, $customSearchesByName)) {
df9f2d1a 41 throw new CRM_Core_Exception(ts('This custom search is already registered.'));
6a488035
TO
42 }
43
44 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
be2fb01f 45 ['option_group_id' => $this->groupId]
6a488035
TO
46 );
47
be2fb01f 48 $params = [
6a488035
TO
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,
be2fb01f 56 ];
6a488035 57
be2fb01f 58 $ids = [];
6a488035
TO
59 $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids);
60
61 return $optionValue ? TRUE : FALSE;
62 }
63
e0ef6999
EM
64 /**
65 * @param CRM_Extension_Info $info
66 *
67 * @return bool
68 * @throws Exception
69 */
6a488035
TO
70 public function onPreUninstall(CRM_Extension_Info $info) {
71 $customSearchesByName = $this->getCustomSearchesByName();
72 if (!array_key_exists($info->key, $customSearchesByName)) {
73 CRM_Core_Error::fatal('This custom search is not registered.');
74 }
75
353ffa53
TO
76 $cs = $this->getCustomSearchesById();
77 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
78 $optionValue = CRM_Core_BAO_OptionValue::del($id);
79
80 return TRUE;
81 }
82
e0ef6999
EM
83 /**
84 * @param CRM_Extension_Info $info
85 */
6a488035
TO
86 public function onPreDisable(CRM_Extension_Info $info) {
87 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
88 $cs = $this->getCustomSearchesById();
89 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
90 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
91 }
92
e0ef6999
EM
93 /**
94 * @param CRM_Extension_Info $info
95 */
6a488035
TO
96 public function onPreEnable(CRM_Extension_Info $info) {
97 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
98 $cs = $this->getCustomSearchesById();
99 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
100 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
101 }
102
e0ef6999
EM
103 /**
104 * @return array
105 */
6a488035
TO
106 protected function getCustomSearchesByName() {
107 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
108 }
109
e0ef6999
EM
110 /**
111 * @return array
112 */
6a488035
TO
113 protected function getCustomSearchesById() {
114 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
115 }
96025800 116
6a488035 117}