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