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