Commit | Line | Data |
---|---|---|
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 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup { | |
20 | ||
21 | /** | |
fe482240 | 22 | * Class constructor. |
6a488035 | 23 | */ |
00be9182 | 24 | public function __construct() { |
6a488035 TO |
25 | parent::__construct(); |
26 | } | |
27 | ||
28 | /** | |
fe482240 | 29 | * Fetch object based on array of properties. |
6a488035 | 30 | * |
6a0b768e TO |
31 | * @param array $params |
32 | * (reference ) an assoc array of name/value pairs. | |
33 | * @param array $defaults | |
34 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 35 | * |
16b10e64 | 36 | * @return CRM_Core_BAO_OptionGroup |
6a488035 | 37 | */ |
00be9182 | 38 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
39 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
40 | $optionGroup->copyValues($params); | |
41 | if ($optionGroup->find(TRUE)) { | |
42 | CRM_Core_DAO::storeValues($optionGroup, $defaults); | |
43 | return $optionGroup; | |
44 | } | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | /** | |
fe482240 | 49 | * Update the is_active flag in the db. |
6a488035 | 50 | * |
6a0b768e TO |
51 | * @param int $id |
52 | * Id of the database record. | |
53 | * @param bool $is_active | |
54 | * Value we want to set the is_active field. | |
6a488035 | 55 | * |
8a4fede3 | 56 | * @return bool |
57 | * true if we found and updated the object, else false | |
6a488035 | 58 | */ |
00be9182 | 59 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
60 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active); |
61 | } | |
62 | ||
63 | /** | |
fe482240 | 64 | * Add the Option Group. |
6a488035 | 65 | * |
6a0b768e TO |
66 | * @param array $params |
67 | * Reference array contains the values submitted by the form. | |
68 | * @param array $ids | |
69 | * Reference array contains the id. | |
6a488035 | 70 | * |
6a488035 TO |
71 | * |
72 | * @return object | |
73 | */ | |
be2fb01f | 74 | public static function add(&$params, $ids = []) { |
25a8e8c2 | 75 | if (empty($params['id']) && !empty($ids['optionGroup'])) { |
76 | CRM_Core_Error::deprecatedFunctionWarning('no $ids array'); | |
77 | $params['id'] = $ids['optionGroup']; | |
4033343a | 78 | } |
6a488035 TO |
79 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
80 | $optionGroup->copyValues($params);; | |
6a488035 TO |
81 | $optionGroup->save(); |
82 | return $optionGroup; | |
83 | } | |
84 | ||
85 | /** | |
fe482240 | 86 | * Delete Option Group. |
6a488035 | 87 | * |
6a0b768e TO |
88 | * @param int $optionGroupId |
89 | * Id of the Option Group to be deleted. | |
6a488035 | 90 | */ |
00be9182 | 91 | public static function del($optionGroupId) { |
6a488035 TO |
92 | // need to delete all option value field before deleting group |
93 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
94 | $optionValue->option_group_id = $optionGroupId; | |
95 | $optionValue->delete(); | |
96 | ||
97 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
98 | $optionGroup->id = $optionGroupId; | |
99 | $optionGroup->delete(); | |
100 | } | |
101 | ||
102 | /** | |
fe482240 | 103 | * Get title of the option group. |
6a488035 | 104 | * |
6a0b768e TO |
105 | * @param int $optionGroupId |
106 | * Id of the Option Group. | |
6a488035 | 107 | * |
ae5ffbb7 | 108 | * @return string |
a6c01b45 | 109 | * title |
6a488035 | 110 | */ |
00be9182 | 111 | public static function getTitle($optionGroupId) { |
6a488035 TO |
112 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
113 | $optionGroup->id = $optionGroupId; | |
114 | $optionGroup->find(TRUE); | |
115 | return $optionGroup->name; | |
116 | } | |
96025800 | 117 | |
eaecfa20 SL |
118 | /** |
119 | * Get DataType for a specified option Group | |
120 | * | |
121 | * @param int $optionGroupId | |
122 | * Id of the Option Group. | |
123 | * | |
124 | * @return string|null | |
125 | * Data Type | |
126 | */ | |
127 | public static function getDataType($optionGroupId) { | |
128 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
129 | $optionGroup->id = $optionGroupId; | |
130 | $optionGroup->find(TRUE); | |
131 | return $optionGroup->data_type; | |
132 | } | |
133 | ||
801bafd7 | 134 | /** |
135 | * Ensure an option group exists. | |
136 | * | |
137 | * This function is intended to be called from the upgrade script to ensure | |
138 | * that an option group exists, without hitting an error if it already exists. | |
139 | * | |
140 | * This is sympathetic to sites who might pre-add it. | |
141 | * | |
142 | * @param array $params | |
143 | * | |
144 | * @return int | |
145 | * ID of the option group. | |
146 | */ | |
147 | public static function ensureOptionGroupExists($params) { | |
be2fb01f | 148 | $existingValues = civicrm_api3('OptionGroup', 'get', [ |
801bafd7 | 149 | 'name' => $params['name'], |
34273b2a | 150 | 'return' => 'id', |
be2fb01f | 151 | ]); |
801bafd7 | 152 | if (!$existingValues['count']) { |
153 | $result = civicrm_api3('OptionGroup', 'create', $params); | |
154 | return $result['id']; | |
155 | } | |
156 | else { | |
157 | return $existingValues['id']; | |
158 | } | |
159 | } | |
160 | ||
9d5c7f14 | 161 | /** |
162 | * Get the title of an option group by name. | |
163 | * | |
164 | * @param string $name | |
165 | * The name value for the option group table. | |
166 | * | |
167 | * @return string | |
168 | * The relevant title. | |
169 | */ | |
170 | public static function getTitleByName($name) { | |
171 | $groups = self::getTitlesByNames(); | |
172 | return $groups[$name]; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Get a cached mapping of all group titles indexed by their unique name. | |
177 | * | |
178 | * We tend to only have a limited number of option groups so memory caching | |
179 | * makes more sense than multiple look-ups. | |
180 | * | |
181 | * @return array | |
182 | * Array of all group titles by name. | |
183 | * e.g | |
184 | * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....) | |
185 | */ | |
186 | public static function getTitlesByNames() { | |
187 | if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['titles_by_name'])) { | |
188 | $dao = CRM_Core_DAO::executeQuery("SELECT name, title FROM civicrm_option_group"); | |
189 | while ($dao->fetch()) { | |
190 | \Civi::$statics[__CLASS__]['titles_by_name'][$dao->name] = $dao->title; | |
191 | } | |
192 | } | |
193 | return \Civi::$statics[__CLASS__]['titles_by_name']; | |
194 | } | |
195 | ||
ece6501c ML |
196 | /** |
197 | * Set the given values to active, and set all other values to inactive. | |
198 | * | |
199 | * @param string $optionGroupName | |
200 | * e.g "languages" | |
041ecc95 | 201 | * @param string[] $activeValues |
ece6501c ML |
202 | * e.g. array("en_CA","fr_CA") |
203 | */ | |
204 | public static function setActiveValues($optionGroupName, $activeValues) { | |
be2fb01f CW |
205 | $params = [ |
206 | 1 => [$optionGroupName, 'String'], | |
207 | ]; | |
ece6501c ML |
208 | |
209 | // convert activeValues into placeholders / params in the query | |
be2fb01f | 210 | $placeholders = []; |
ece6501c ML |
211 | $i = count($params) + 1; |
212 | foreach ($activeValues as $value) { | |
213 | $placeholders[] = "%{$i}"; | |
be2fb01f | 214 | $params[$i] = [$value, 'String']; |
ece6501c ML |
215 | $i++; |
216 | } | |
217 | $placeholders = implode(', ', $placeholders); | |
218 | ||
219 | CRM_Core_DAO::executeQuery(" | |
220 | UPDATE civicrm_option_value cov | |
221 | LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id | |
222 | SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END | |
223 | WHERE cog.name = %1", | |
224 | $params | |
225 | ); | |
226 | } | |
227 | ||
6a488035 | 228 | } |