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