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 | */ |
17 | class CRM_Core_OptionGroup { | |
518fa0ee SL |
18 | public static $_values = []; |
19 | public static $_cache = []; | |
6a488035 | 20 | |
d424ffde | 21 | /** |
6a488035 TO |
22 | * $_domainIDGroups array maintains the list of option groups for whom |
23 | * domainID is to be considered. | |
518fa0ee | 24 | * @var array |
6a488035 | 25 | */ |
518fa0ee | 26 | public static $_domainIDGroups = [ |
6a488035 TO |
27 | 'from_email_address', |
28 | 'grant_type', | |
be2fb01f | 29 | ]; |
6a488035 | 30 | |
a0ee3941 | 31 | /** |
c490a46a | 32 | * @param CRM_Core_DAO $dao |
a0ee3941 EM |
33 | * @param bool $flip |
34 | * @param bool $grouping | |
35 | * @param bool $localize | |
36 | * @param string $valueColumnName | |
37 | * | |
38 | * @return array | |
39 | */ | |
2da40d21 | 40 | public static function &valuesCommon( |
6a488035 TO |
41 | $dao, $flip = FALSE, $grouping = FALSE, |
42 | $localize = FALSE, $valueColumnName = 'label' | |
43 | ) { | |
be2fb01f | 44 | self::$_values = []; |
6a488035 TO |
45 | |
46 | while ($dao->fetch()) { | |
47 | if ($flip) { | |
48 | if ($grouping) { | |
49 | self::$_values[$dao->value] = $dao->grouping; | |
50 | } | |
51 | else { | |
52 | self::$_values[$dao->{$valueColumnName}] = $dao->value; | |
53 | } | |
54 | } | |
55 | else { | |
56 | if ($grouping) { | |
57 | self::$_values[$dao->{$valueColumnName}] = $dao->grouping; | |
58 | } | |
59 | else { | |
60 | self::$_values[$dao->value] = $dao->{$valueColumnName}; | |
61 | } | |
62 | } | |
63 | } | |
64 | if ($localize) { | |
65 | $i18n = CRM_Core_I18n::singleton(); | |
66 | $i18n->localizeArray(self::$_values); | |
67 | } | |
68 | return self::$_values; | |
69 | } | |
70 | ||
71 | /** | |
72 | * This function retrieves all the values for the specific option group by name | |
73 | * this is primarily used to create various html based form elements | |
74 | * (radio, select, checkbox etc). OptionGroups for most cases have the | |
b44e3f84 | 75 | * 'label' in the label column and the 'id' or 'name' in the value column |
6a488035 | 76 | * |
5a4f6742 CW |
77 | * @param string $name |
78 | * name of the option group. | |
79 | * @param bool $flip | |
80 | * results are return in id => label format if false. | |
6a488035 | 81 | * if true, the results are reversed |
5a4f6742 CW |
82 | * @param bool $grouping |
83 | * if true, return the value in 'grouping' column. | |
84 | * @param bool $localize | |
85 | * if true, localize the results before returning. | |
86 | * @param string $condition | |
87 | * add another condition to the sql query. | |
88 | * @param string $labelColumnName | |
89 | * the column to use for 'label'. | |
90 | * @param bool $onlyActive | |
91 | * return only the action option values. | |
92 | * @param bool $fresh | |
93 | * ignore cache entries and go back to DB. | |
94 | * @param string $keyColumnName | |
95 | * the column to use for 'key'. | |
bf48aa29 | 96 | * @param string $orderBy |
03ba3ef1 | 97 | * the column to use for ordering. |
6a488035 | 98 | * |
a6c01b45 | 99 | * @return array |
bf48aa29 | 100 | * The values as specified by the params |
6a488035 | 101 | */ |
2da40d21 | 102 | public static function &values( |
6a488035 TO |
103 | $name, $flip = FALSE, $grouping = FALSE, |
104 | $localize = FALSE, $condition = NULL, | |
148136ae PN |
105 | $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value', |
106 | $orderBy = 'weight' | |
6a488035 | 107 | ) { |
be80e977 | 108 | $cache = CRM_Utils_Cache::singleton(); |
370d56eb | 109 | $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy); |
6a488035 | 110 | |
c0c9cd82 CW |
111 | if (!$fresh) { |
112 | // Fetch from static var | |
113 | if (array_key_exists($cacheKey, self::$_cache)) { | |
114 | return self::$_cache[$cacheKey]; | |
115 | } | |
116 | // Fetch from main cache | |
e2ba3ae8 | 117 | self::$_cache[$cacheKey] = $cache->get($cacheKey); |
118 | if (self::$_cache[$cacheKey] !== NULL) { | |
119 | return self::$_cache[$cacheKey]; | |
c0c9cd82 | 120 | } |
6a488035 TO |
121 | } |
122 | ||
123 | $query = " | |
9e4925a0 | 124 | SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as `grouping` |
6a488035 TO |
125 | FROM civicrm_option_value v, |
126 | civicrm_option_group g | |
127 | WHERE v.option_group_id = g.id | |
128 | AND g.name = %1 | |
129 | AND g.is_active = 1 "; | |
130 | ||
131 | if ($onlyActive) { | |
132 | $query .= " AND v.is_active = 1 "; | |
98d41b7d CW |
133 | // Only show options for enabled components |
134 | $componentClause = ' v.component_id IS NULL '; | |
135 | $enabledComponents = CRM_Core_Config::singleton()->enableComponents; | |
136 | if ($enabledComponents) { | |
137 | $enabledComponents = '"' . implode('","', $enabledComponents) . '"'; | |
138 | $componentClause .= " OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents)) "; | |
139 | } | |
140 | $query .= " AND ($componentClause) "; | |
6a488035 TO |
141 | } |
142 | if (in_array($name, self::$_domainIDGroups)) { | |
143 | $query .= " AND v.domain_id = " . CRM_Core_Config::domainID(); | |
144 | } | |
145 | ||
146 | if ($condition) { | |
147 | $query .= $condition; | |
148 | } | |
149 | ||
148136ae | 150 | $query .= " ORDER BY v.{$orderBy}"; |
6a488035 | 151 | |
be2fb01f | 152 | $p = [1 => [$name, 'String']]; |
6a488035 TO |
153 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
154 | ||
155 | $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName); | |
156 | ||
157 | // call option value hook | |
158 | CRM_Utils_Hook::optionValues($var, $name); | |
159 | ||
160 | self::$_cache[$cacheKey] = $var; | |
161 | $cache->set($cacheKey, $var); | |
162 | ||
163 | return $var; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Counterpart to values() which removes the item from the cache | |
168 | * | |
100fef9d | 169 | * @param string $name |
6a488035 TO |
170 | * @param $flip |
171 | * @param $grouping | |
172 | * @param $localize | |
173 | * @param $condition | |
100fef9d | 174 | * @param string $labelColumnName |
6a488035 | 175 | * @param $onlyActive |
da6b46f4 | 176 | * @param string $keyColumnName |
6a488035 | 177 | */ |
c0c9cd82 CW |
178 | protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') { |
179 | $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName); | |
6a488035 TO |
180 | $cache = CRM_Utils_Cache::singleton(); |
181 | $cache->delete($cacheKey); | |
182 | unset(self::$_cache[$cacheKey]); | |
183 | } | |
184 | ||
a0ee3941 EM |
185 | /** |
186 | * @return string | |
187 | */ | |
9647286d TO |
188 | protected static function createCacheKey($id) { |
189 | $cacheKey = "CRM_OG_" . preg_replace('/[^a-zA-Z0-9]/', '', $id) . '_' . md5(serialize(func_get_args())); | |
6a488035 TO |
190 | return $cacheKey; |
191 | } | |
192 | ||
193 | /** | |
fe482240 | 194 | * This function retrieves all the values for the specific option group by id. |
6a488035 TO |
195 | * this is primarily used to create various html based form elements |
196 | * (radio, select, checkbox etc). OptionGroups for most cases have the | |
b44e3f84 | 197 | * 'label' in the label column and the 'id' or 'name' in the value column |
6a488035 | 198 | * |
5a4f6742 CW |
199 | * @param int $id |
200 | * id of the option group. | |
201 | * @param bool $flip | |
202 | * results are return in id => label format if false. | |
203 | * if true, the results are reversed | |
204 | * @param bool $grouping | |
205 | * if true, return the value in 'grouping' column. | |
206 | * @param bool $localize | |
207 | * if true, localize the results before returning. | |
208 | * @param string $labelColumnName | |
209 | * the column to use for 'label'. | |
da6b46f4 EM |
210 | * @param bool $onlyActive |
211 | * @param bool $fresh | |
212 | * | |
a6c01b45 | 213 | * @return array |
16b10e64 | 214 | * Array of values as specified by the above params |
6a488035 TO |
215 | * @void |
216 | */ | |
00be9182 | 217 | public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) { |
786ad6e1 | 218 | $cacheKey = self::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive); |
6a488035 TO |
219 | |
220 | $cache = CRM_Utils_Cache::singleton(); | |
786ad6e1 | 221 | if (!$fresh) { |
86c265cf | 222 | self::$_cache[$cacheKey] = $cache->get($cacheKey); |
223 | if (self::$_cache[$cacheKey] !== NULL) { | |
224 | return self::$_cache[$cacheKey]; | |
786ad6e1 | 225 | } |
6a488035 TO |
226 | } |
227 | $query = " | |
9e4925a0 | 228 | SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as `grouping` |
6a488035 TO |
229 | FROM civicrm_option_value v, |
230 | civicrm_option_group g | |
231 | WHERE v.option_group_id = g.id | |
232 | AND g.id = %1 | |
6a488035 | 233 | AND g.is_active = 1 |
6a488035 | 234 | "; |
786ad6e1 CW |
235 | if ($onlyActive) { |
236 | $query .= " AND v.is_active = 1 "; | |
237 | } | |
238 | $query .= " ORDER BY v.weight, v.label"; | |
239 | ||
be2fb01f | 240 | $p = [1 => [$id, 'Integer']]; |
6a488035 TO |
241 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
242 | ||
243 | $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName); | |
244 | $cache->set($cacheKey, $var); | |
245 | ||
246 | return $var; | |
247 | } | |
248 | ||
249 | /** | |
59cba00c J |
250 | * Lookup titles OR ids for a set of option_value populated fields. The |
251 | * retrieved value is assigned a new field name by id or id's by title | |
252 | * (each within a specified option_group). | |
6a488035 | 253 | * |
6a0b768e TO |
254 | * @param array $params |
255 | * Reference array of values submitted by the form. Based on. | |
5a4f6742 CW |
256 | * $flip, creates new elements in $params for each field in |
257 | * the $names array. | |
258 | * If $flip = false, adds root field name => title | |
259 | * If $flip = true, adds actual field name => id | |
6a488035 | 260 | * |
6a0b768e | 261 | * @param array $names |
59cba00c | 262 | * Array of field names we want transformed. |
5a4f6742 CW |
263 | * Array key = 'postName' (field name submitted by form in $params). |
264 | * Array value = array('newName' => $newName, 'groupName' => $groupName). | |
6a488035 | 265 | * |
2aa397bc | 266 | * @param bool $flip |
6a488035 | 267 | */ |
f2e3129e | 268 | public static function lookupValues(&$params, $names, $flip = FALSE) { |
6a488035 TO |
269 | foreach ($names as $postName => $value) { |
270 | // See if $params field is in $names array (i.e. is a value that we need to lookup) | |
271 | if ($postalName = CRM_Utils_Array::value($postName, $params)) { | |
be2fb01f | 272 | $postValues = []; |
6a488035 TO |
273 | // params[$postName] may be a Ctrl+A separated value list |
274 | if (is_string($postalName) && | |
275 | strpos($postalName, CRM_Core_DAO::VALUE_SEPARATOR) == FALSE | |
276 | ) { | |
277 | // eliminate the ^A frm the beginning and end if present | |
278 | if (substr($postalName, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) { | |
279 | $params[$postName] = substr($params[$postName], 1, -1); | |
280 | } | |
281 | $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]); | |
282 | } | |
283 | elseif (is_array($postalName)) { | |
284 | $postValues = $postalName; | |
285 | } | |
be2fb01f | 286 | $newValue = []; |
6a488035 TO |
287 | foreach ($postValues as $postValue) { |
288 | if (!$postValue) { | |
289 | continue; | |
290 | } | |
291 | ||
292 | if ($flip) { | |
be2fb01f | 293 | $p = [1 => [$postValue, 'String']]; |
6a488035 | 294 | $lookupBy = 'v.label= %1'; |
353ffa53 | 295 | $select = "v.value"; |
6a488035 TO |
296 | } |
297 | else { | |
be2fb01f | 298 | $p = [1 => [$postValue, 'Integer']]; |
6a488035 | 299 | $lookupBy = 'v.value = %1'; |
353ffa53 | 300 | $select = "v.label"; |
6a488035 TO |
301 | } |
302 | ||
be2fb01f | 303 | $p[2] = [$value['groupName'], 'String']; |
6a488035 TO |
304 | $query = " |
305 | SELECT $select | |
306 | FROM civicrm_option_value v, | |
307 | civicrm_option_group g | |
308 | WHERE v.option_group_id = g.id | |
309 | AND g.name = %2 | |
310 | AND $lookupBy"; | |
311 | ||
312 | $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p); | |
313 | $newValue = str_replace(',', '_', $newValue); | |
314 | } | |
315 | $params[$value['newName']] = implode(', ', $newValue); | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
a0ee3941 | 320 | /** |
4a413eb6 | 321 | * @deprecated - use CRM_Core_PseudoConstant::getLabel |
d6f7dc03 | 322 | * |
100fef9d | 323 | * @param string $groupName |
a0ee3941 EM |
324 | * @param $value |
325 | * @param bool $onlyActiveValue | |
326 | * | |
327 | * @return null | |
328 | */ | |
00be9182 | 329 | public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) { |
496320c3 | 330 | CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_PseudoConstant::getLabel'); |
6a488035 TO |
331 | if (empty($groupName) || |
332 | empty($value) | |
333 | ) { | |
334 | return NULL; | |
335 | } | |
336 | ||
337 | $query = " | |
338 | SELECT v.label as label ,v.value as value | |
339 | FROM civicrm_option_value v, | |
340 | civicrm_option_group g | |
341 | WHERE v.option_group_id = g.id | |
342 | AND g.name = %1 | |
343 | AND g.is_active = 1 | |
344 | AND v.value = %2 | |
345 | "; | |
346 | if ($onlyActiveValue) { | |
347 | $query .= " AND v.is_active = 1 "; | |
348 | } | |
be2fb01f CW |
349 | $p = [ |
350 | 1 => [$groupName, 'String'], | |
351 | 2 => [$value, 'Integer'], | |
352 | ]; | |
6a488035 TO |
353 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
354 | if ($dao->fetch()) { | |
355 | return $dao->label; | |
356 | } | |
357 | return NULL; | |
358 | } | |
359 | ||
a0ee3941 | 360 | /** |
76c28c8d DG |
361 | * @deprecated |
362 | * | |
363 | * This function is not cached. | |
364 | * | |
100fef9d | 365 | * @param string $groupName |
a0ee3941 EM |
366 | * @param $label |
367 | * @param string $labelField | |
368 | * @param string $labelType | |
369 | * @param string $valueField | |
370 | * | |
371 | * @return null | |
372 | */ | |
2da40d21 | 373 | public static function getValue( |
f9f40af3 | 374 | $groupName, |
6a488035 TO |
375 | $label, |
376 | $labelField = 'label', | |
2aa397bc | 377 | $labelType = 'String', |
6a488035 TO |
378 | $valueField = 'value' |
379 | ) { | |
380 | if (empty($label)) { | |
381 | return NULL; | |
382 | } | |
383 | ||
496320c3 | 384 | CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_PseudoConstant::getKey'); |
6a59e510 | 385 | |
6a488035 TO |
386 | $query = " |
387 | SELECT v.label as label ,v.{$valueField} as value | |
388 | FROM civicrm_option_value v, | |
389 | civicrm_option_group g | |
390 | WHERE v.option_group_id = g.id | |
391 | AND g.name = %1 | |
392 | AND v.is_active = 1 | |
393 | AND g.is_active = 1 | |
394 | AND v.$labelField = %2 | |
395 | "; | |
396 | ||
be2fb01f CW |
397 | $p = [ |
398 | 1 => [$groupName, 'String'], | |
399 | 2 => [$label, $labelType], | |
400 | ]; | |
6a488035 TO |
401 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
402 | if ($dao->fetch()) { | |
6a488035 TO |
403 | return $dao->value; |
404 | } | |
6a488035 TO |
405 | return NULL; |
406 | } | |
407 | ||
343d84fa DG |
408 | /** |
409 | * Get option_value.value from default option_value row for an option group | |
410 | * | |
6a0b768e TO |
411 | * @param string $groupName |
412 | * The name of the option group. | |
343d84fa | 413 | * |
343d84fa | 414 | * |
a6c01b45 CW |
415 | * @return string |
416 | * the value from the row where is_default = true | |
8ef12e64 | 417 | */ |
00be9182 | 418 | public static function getDefaultValue($groupName) { |
343d84fa DG |
419 | if (empty($groupName)) { |
420 | return NULL; | |
421 | } | |
422 | $query = " | |
423 | SELECT v.value | |
424 | FROM civicrm_option_value v, | |
425 | civicrm_option_group g | |
426 | WHERE v.option_group_id = g.id | |
427 | AND g.name = %1 | |
428 | AND v.is_active = 1 | |
429 | AND g.is_active = 1 | |
430 | AND v.is_default = 1 | |
431 | "; | |
432 | if (in_array($groupName, self::$_domainIDGroups)) { | |
433 | $query .= " AND v.domain_id = " . CRM_Core_Config::domainID(); | |
434 | } | |
435 | ||
be2fb01f | 436 | $p = [1 => [$groupName, 'String']]; |
343d84fa DG |
437 | return CRM_Core_DAO::singleValueQuery($query, $p); |
438 | } | |
8ef12e64 | 439 | |
6a488035 | 440 | /** |
d09edf64 | 441 | * Creates a new option group with the passed in values. |
6a488035 TO |
442 | * @TODO: Should update the group if it already exists intelligently, so multi-lingual is |
443 | * not messed up. Currently deletes the old group | |
444 | * | |
6a0b768e TO |
445 | * @param string $groupName |
446 | * The name of the option group - make sure there is no conflict. | |
447 | * @param array $values | |
448 | * The associative array that has information on the option values. | |
6a488035 TO |
449 | * the keys of this array are: |
450 | * string 'title' (required) | |
451 | * string 'value' (required) | |
452 | * string 'name' (optional) | |
453 | * string 'description' (optional) | |
454 | * int 'weight' (optional) - the order in which the value are displayed | |
455 | * bool 'is_default' (optional) - is this the default one to display when rendered in form | |
456 | * bool 'is_active' (optional) - should this element be rendered | |
6a0b768e TO |
457 | * @param int $defaultID |
458 | * (reference) - the option value ID of the default element (if set) is returned else 'null'. | |
459 | * @param null $groupTitle | |
460 | * The optional label of the option group else set to group name. | |
6a488035 | 461 | * |
6a488035 | 462 | * |
a6c01b45 CW |
463 | * @return int |
464 | * the option group ID | |
6a488035 | 465 | */ |
00be9182 | 466 | public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) { |
6a488035 TO |
467 | self::deleteAssoc($groupName); |
468 | if (!empty($values)) { | |
353ffa53 TO |
469 | $group = new CRM_Core_DAO_OptionGroup(); |
470 | $group->name = $groupName; | |
471 | $group->title = empty($groupTitle) ? $groupName : $groupTitle; | |
6a488035 | 472 | $group->is_reserved = 1; |
353ffa53 | 473 | $group->is_active = 1; |
6a488035 TO |
474 | $group->save(); |
475 | ||
476 | foreach ($values as $v) { | |
477 | $value = new CRM_Core_DAO_OptionValue(); | |
478 | $value->option_group_id = $group->id; | |
479 | $value->label = $v['label']; | |
480 | $value->value = $v['value']; | |
9c1bc317 CW |
481 | $value->name = $v['name'] ?? NULL; |
482 | $value->description = $v['description'] ?? NULL; | |
483 | $value->weight = $v['weight'] ?? NULL; | |
484 | $value->is_default = $v['is_default'] ?? NULL; | |
485 | $value->is_active = $v['is_active'] ?? NULL; | |
6a488035 TO |
486 | $value->save(); |
487 | ||
488 | if ($value->is_default) { | |
489 | $defaultID = $value->id; | |
490 | } | |
491 | } | |
492 | } | |
493 | else { | |
494 | return $defaultID = 'null'; | |
495 | } | |
496 | ||
497 | return $group->id; | |
498 | } | |
499 | ||
a0ee3941 | 500 | /** |
100fef9d | 501 | * @param string $groupName |
a0ee3941 EM |
502 | * @param $values |
503 | * @param bool $flip | |
504 | * @param string $field | |
505 | */ | |
00be9182 | 506 | public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') { |
6a488035 TO |
507 | $query = " |
508 | SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight | |
509 | FROM civicrm_option_group g, | |
510 | civicrm_option_value v | |
511 | WHERE g.id = v.option_group_id | |
512 | AND g.$field = %1 | |
513 | ORDER BY v.weight | |
514 | "; | |
be2fb01f | 515 | $params = [1 => [$groupName, 'String']]; |
6a488035 TO |
516 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
517 | ||
be2fb01f | 518 | $fields = ['value', 'label', 'name', 'description', 'amount_id', 'weight']; |
6a488035 | 519 | if ($flip) { |
be2fb01f | 520 | $values = []; |
6a488035 TO |
521 | } |
522 | else { | |
523 | foreach ($fields as $field) { | |
be2fb01f | 524 | $values[$field] = []; |
6a488035 TO |
525 | } |
526 | } | |
527 | $index = 1; | |
528 | ||
529 | while ($dao->fetch()) { | |
530 | if ($flip) { | |
be2fb01f | 531 | $value = []; |
6a488035 TO |
532 | foreach ($fields as $field) { |
533 | $value[$field] = $dao->$field; | |
534 | } | |
535 | $values[$dao->amount_id] = $value; | |
536 | } | |
537 | else { | |
538 | foreach ($fields as $field) { | |
539 | $values[$field][$index] = $dao->$field; | |
540 | } | |
541 | $index++; | |
542 | } | |
543 | } | |
544 | } | |
545 | ||
a0ee3941 | 546 | /** |
100fef9d | 547 | * @param string $groupName |
a0ee3941 EM |
548 | * @param string $operator |
549 | */ | |
00be9182 | 550 | public static function deleteAssoc($groupName, $operator = "=") { |
6a488035 TO |
551 | $query = " |
552 | DELETE g, v | |
553 | FROM civicrm_option_group g, | |
554 | civicrm_option_value v | |
555 | WHERE g.id = v.option_group_id | |
556 | AND g.name {$operator} %1"; | |
557 | ||
be2fb01f | 558 | $params = [1 => [$groupName, 'String']]; |
6a488035 TO |
559 | |
560 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
561 | } | |
562 | ||
a0ee3941 | 563 | /** |
100fef9d | 564 | * @param string $groupName |
a0ee3941 EM |
565 | * @param $fieldValue |
566 | * @param string $field | |
567 | * @param string $fieldType | |
568 | * @param bool $active | |
bc3364a9 ML |
569 | * @param bool $localize |
570 | * if true, localize the results before returning. | |
a0ee3941 EM |
571 | * |
572 | * @return array | |
573 | */ | |
2da40d21 | 574 | public static function getRowValues( |
f9f40af3 | 575 | $groupName, $fieldValue, $field = 'name', |
bc3364a9 | 576 | $fieldType = 'String', $active = TRUE, $localize = FALSE |
6a488035 TO |
577 | ) { |
578 | $query = " | |
579 | SELECT v.id, v.label, v.value, v.name, v.weight, v.description | |
580 | FROM civicrm_option_value v, | |
581 | civicrm_option_group g | |
582 | WHERE v.option_group_id = g.id | |
583 | AND g.name = %1 | |
584 | AND g.is_active = 1 | |
585 | AND v.$field = %2 | |
586 | "; | |
587 | ||
588 | if ($active) { | |
589 | $query .= " AND v.is_active = 1"; | |
590 | } | |
591 | ||
be2fb01f CW |
592 | $p = [ |
593 | 1 => [$groupName, 'String'], | |
594 | 2 => [$fieldValue, $fieldType], | |
595 | ]; | |
6a488035 | 596 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
be2fb01f | 597 | $row = []; |
6a488035 TO |
598 | |
599 | if ($dao->fetch()) { | |
be2fb01f | 600 | foreach ([ |
518fa0ee SL |
601 | 'id', |
602 | 'name', | |
603 | 'value', | |
604 | 'label', | |
605 | 'weight', | |
606 | 'description', | |
607 | ] as $fld) { | |
6a488035 | 608 | $row[$fld] = $dao->$fld; |
be2fb01f | 609 | if ($localize && in_array($fld, ['label', 'description'])) { |
03c70eeb | 610 | $row[$fld] = ts($row[$fld]); |
611 | } | |
bc3364a9 ML |
612 | } |
613 | } | |
614 | ||
6a488035 TO |
615 | return $row; |
616 | } | |
617 | ||
d424ffde | 618 | /** |
d09edf64 | 619 | * Wrapper for calling values with fresh set to true to empty the given value. |
6a488035 TO |
620 | * |
621 | * Since there appears to be some inconsistency | |
622 | * (@todo remove inconsistency) around the pseudoconstant operations | |
623 | * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive | |
624 | * which is part of the cache key | |
625 | * will do a couple of variations & aspire to someone cleaning it up later | |
d424ffde CW |
626 | * |
627 | * @param string $name | |
a0ee3941 EM |
628 | * @param array $params |
629 | */ | |
be2fb01f CW |
630 | public static function flush($name, $params = []) { |
631 | $defaults = [ | |
6a488035 TO |
632 | 'flip' => FALSE, |
633 | 'grouping' => FALSE, | |
634 | 'localize' => FALSE, | |
635 | 'condition' => NULL, | |
636 | 'labelColumnName' => 'label', | |
be2fb01f | 637 | ]; |
6a488035 TO |
638 | |
639 | $params = array_merge($defaults, $params); | |
640 | self::flushValues( | |
641 | $name, | |
642 | $params['flip'], | |
643 | $params['grouping'], | |
644 | $params['localize'], | |
645 | $params['condition'], | |
646 | $params['labelColumnName'], | |
647 | TRUE, | |
648 | TRUE | |
649 | ); | |
650 | self::flushValues( | |
651 | $name, | |
652 | $params['flip'], | |
653 | $params['grouping'], | |
654 | $params['localize'], | |
655 | $params['condition'], | |
656 | $params['labelColumnName'], | |
657 | FALSE, | |
658 | TRUE | |
659 | ); | |
660 | } | |
661 | ||
7c990617 | 662 | /** |
663 | * Flush all the places where option values are cached. | |
664 | * | |
665 | * Note that this is called from CRM_Core_PseudoConstant::flush() so we should resist | |
666 | * the intuitive urge to flush that class. | |
667 | */ | |
00be9182 | 668 | public static function flushAll() { |
be2fb01f CW |
669 | self::$_values = []; |
670 | self::$_cache = []; | |
a4a33486 | 671 | CRM_Utils_Cache::singleton()->flush(); |
6a488035 | 672 | } |
96025800 | 673 | |
6a488035 | 674 | } |