Merge pull request #22683 from mattwire/paymentstatushelpersall
[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);
a7718acb
TO
27 }
28
29 public function getGroupId() {
30 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
6a488035
TO
31 self::CUSTOM_SEARCH_GROUP_NAME, 'id', 'name'
32 );
33 }
34
e0ef6999
EM
35 /**
36 * @param CRM_Extension_Info $info
37 *
38 * @return bool
df9f2d1a 39 * @throws CRM_Core_Exception
e0ef6999 40 */
6a488035
TO
41 public function onPreInstall(CRM_Extension_Info $info) {
42 $customSearchesByName = $this->getCustomSearchesByName();
43 if (array_key_exists($info->key, $customSearchesByName)) {
df9f2d1a 44 throw new CRM_Core_Exception(ts('This custom search is already registered.'));
6a488035
TO
45 }
46
47 $weight = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
a7718acb 48 ['option_group_id' => $this->getGroupId()]
6a488035
TO
49 );
50
be2fb01f 51 $params = [
a7718acb 52 'option_group_id' => $this->getGroupId(),
6a488035
TO
53 'weight' => $weight,
54 'description' => $info->label . ' (' . $info->key . ')',
55 'name' => $info->key,
56 'value' => max($customSearchesByName) + 1,
57 'label' => $info->key,
58 'is_active' => 1,
be2fb01f 59 ];
6a488035 60
d8efe404 61 $optionValue = CRM_Core_BAO_OptionValue::add($params);
6a488035
TO
62
63 return $optionValue ? TRUE : FALSE;
64 }
65
e0ef6999
EM
66 /**
67 * @param CRM_Extension_Info $info
68 *
69 * @return bool
70 * @throws Exception
71 */
6a488035
TO
72 public function onPreUninstall(CRM_Extension_Info $info) {
73 $customSearchesByName = $this->getCustomSearchesByName();
74 if (!array_key_exists($info->key, $customSearchesByName)) {
ba968e38 75 throw new CRM_Core_Exception('This custom search is not registered.');
6a488035
TO
76 }
77
353ffa53
TO
78 $cs = $this->getCustomSearchesById();
79 $id = $cs[$customSearchesByName[$info->key]];
ba968e38 80 CRM_Core_BAO_OptionValue::del($id);
6a488035
TO
81
82 return TRUE;
83 }
84
e0ef6999
EM
85 /**
86 * @param CRM_Extension_Info $info
87 */
6a488035
TO
88 public function onPreDisable(CRM_Extension_Info $info) {
89 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
90 $cs = $this->getCustomSearchesById();
91 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
92 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
93 }
94
e0ef6999
EM
95 /**
96 * @param CRM_Extension_Info $info
97 */
6a488035
TO
98 public function onPreEnable(CRM_Extension_Info $info) {
99 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
100 $cs = $this->getCustomSearchesById();
101 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
102 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
103 }
104
e0ef6999
EM
105 /**
106 * @return array
107 */
6a488035
TO
108 protected function getCustomSearchesByName() {
109 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
110 }
111
e0ef6999
EM
112 /**
113 * @return array
114 */
6a488035
TO
115 protected function getCustomSearchesById() {
116 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
117 }
96025800 118
6a488035 119}