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