Merge pull request #16626 from pradpnayak/lineItemFixes
[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
d8efe404 58 $optionValue = CRM_Core_BAO_OptionValue::add($params);
6a488035
TO
59
60 return $optionValue ? TRUE : FALSE;
61 }
62
e0ef6999
EM
63 /**
64 * @param CRM_Extension_Info $info
65 *
66 * @return bool
67 * @throws Exception
68 */
6a488035
TO
69 public function onPreUninstall(CRM_Extension_Info $info) {
70 $customSearchesByName = $this->getCustomSearchesByName();
71 if (!array_key_exists($info->key, $customSearchesByName)) {
ba968e38 72 throw new CRM_Core_Exception('This custom search is not registered.');
6a488035
TO
73 }
74
353ffa53
TO
75 $cs = $this->getCustomSearchesById();
76 $id = $cs[$customSearchesByName[$info->key]];
ba968e38 77 CRM_Core_BAO_OptionValue::del($id);
6a488035
TO
78
79 return TRUE;
80 }
81
e0ef6999
EM
82 /**
83 * @param CRM_Extension_Info $info
84 */
6a488035
TO
85 public function onPreDisable(CRM_Extension_Info $info) {
86 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
87 $cs = $this->getCustomSearchesById();
88 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
89 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 0);
90 }
91
e0ef6999
EM
92 /**
93 * @param CRM_Extension_Info $info
94 */
6a488035
TO
95 public function onPreEnable(CRM_Extension_Info $info) {
96 $customSearchesByName = $this->getCustomSearchesByName();
353ffa53
TO
97 $cs = $this->getCustomSearchesById();
98 $id = $cs[$customSearchesByName[$info->key]];
6a488035
TO
99 $optionValue = CRM_Core_BAO_OptionValue::setIsActive($id, 1);
100 }
101
e0ef6999
EM
102 /**
103 * @return array
104 */
6a488035
TO
105 protected function getCustomSearchesByName() {
106 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, TRUE, FALSE, FALSE, NULL, 'name', FALSE, TRUE);
107 }
108
e0ef6999
EM
109 /**
110 * @return array
111 */
6a488035
TO
112 protected function getCustomSearchesById() {
113 return CRM_Core_OptionGroup::values(self::CUSTOM_SEARCH_GROUP_NAME, FALSE, FALSE, FALSE, NULL, 'id', FALSE, TRUE);
114 }
96025800 115
6a488035 116}