INFRA-132 - Docblock cleanup
[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 */
f9f40af3
TO
381 static function getValue(
382 $groupName,
6a488035
TO
383 $label,
384 $labelField = 'label',
2aa397bc 385 $labelType = 'String',
6a488035
TO
386 $valueField = 'value'
387 ) {
388 if (empty($label)) {
389 return NULL;
390 }
391
392 $query = "
393SELECT v.label as label ,v.{$valueField} as value
394FROM civicrm_option_value v,
395 civicrm_option_group g
396WHERE v.option_group_id = g.id
397 AND g.name = %1
398 AND v.is_active = 1
399 AND g.is_active = 1
400 AND v.$labelField = %2
401";
402
2aa397bc
TO
403 $p = array(
404 1 => array($groupName, 'String'),
6a488035
TO
405 2 => array($label, $labelType),
406 );
407 $dao = CRM_Core_DAO::executeQuery($query, $p);
408 if ($dao->fetch()) {
409 $dao->free();
410 return $dao->value;
411 }
412 $dao->free();
413 return NULL;
414 }
415
343d84fa
DG
416 /**
417 * Get option_value.value from default option_value row for an option group
418 *
6a0b768e
TO
419 * @param string $groupName
420 * The name of the option group.
343d84fa 421 *
343d84fa
DG
422 * @static
423 *
424 * @return string the value from the row where is_default = true
8ef12e64 425 */
00be9182 426 public static function getDefaultValue($groupName) {
343d84fa
DG
427 if (empty($groupName)) {
428 return NULL;
429 }
430 $query = "
431SELECT v.value
432FROM civicrm_option_value v,
433 civicrm_option_group g
434WHERE v.option_group_id = g.id
435 AND g.name = %1
436 AND v.is_active = 1
437 AND g.is_active = 1
438 AND v.is_default = 1
439";
440 if (in_array($groupName, self::$_domainIDGroups)) {
441 $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
442 }
443
444 $p = array(1 => array($groupName, 'String'));
445 return CRM_Core_DAO::singleValueQuery($query, $p);
446 }
8ef12e64 447
6a488035
TO
448 /**
449 * Creates a new option group with the passed in values
450 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
451 * not messed up. Currently deletes the old group
452 *
6a0b768e
TO
453 * @param string $groupName
454 * The name of the option group - make sure there is no conflict.
455 * @param array $values
456 * The associative array that has information on the option values.
6a488035
TO
457 * the keys of this array are:
458 * string 'title' (required)
459 * string 'value' (required)
460 * string 'name' (optional)
461 * string 'description' (optional)
462 * int 'weight' (optional) - the order in which the value are displayed
463 * bool 'is_default' (optional) - is this the default one to display when rendered in form
464 * bool 'is_active' (optional) - should this element be rendered
6a0b768e
TO
465 * @param int $defaultID
466 * (reference) - the option value ID of the default element (if set) is returned else 'null'.
467 * @param null $groupTitle
468 * The optional label of the option group else set to group name.
6a488035 469 *
6a488035
TO
470 * @static
471 *
472 * @return int the option group ID
6a488035 473 */
00be9182 474 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
6a488035
TO
475 self::deleteAssoc($groupName);
476 if (!empty($values)) {
477 $group = new CRM_Core_DAO_OptionGroup();
478 $group->name = $groupName;
479 $group->title = empty($groupTitle) ? $groupName : $groupTitle;
480 $group->is_reserved = 1;
481 $group->is_active = 1;
482 $group->save();
483
484 foreach ($values as $v) {
485 $value = new CRM_Core_DAO_OptionValue();
486 $value->option_group_id = $group->id;
487 $value->label = $v['label'];
488 $value->value = $v['value'];
489 $value->name = CRM_Utils_Array::value('name', $v);
490 $value->description = CRM_Utils_Array::value('description', $v);
491 $value->weight = CRM_Utils_Array::value('weight', $v);
492 $value->is_default = CRM_Utils_Array::value('is_default', $v);
493 $value->is_active = CRM_Utils_Array::value('is_active', $v);
494 $value->save();
495
496 if ($value->is_default) {
497 $defaultID = $value->id;
498 }
499 }
500 }
501 else {
502 return $defaultID = 'null';
503 }
504
505 return $group->id;
506 }
507
a0ee3941 508 /**
100fef9d 509 * @param string $groupName
a0ee3941
EM
510 * @param $values
511 * @param bool $flip
512 * @param string $field
513 */
00be9182 514 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
6a488035
TO
515 $query = "
516SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
517 FROM civicrm_option_group g,
518 civicrm_option_value v
519 WHERE g.id = v.option_group_id
520 AND g.$field = %1
521ORDER BY v.weight
522";
523 $params = array(1 => array($groupName, 'String'));
524 $dao = CRM_Core_DAO::executeQuery($query, $params);
525
526 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
527 if ($flip) {
528 $values = array();
529 }
530 else {
531 foreach ($fields as $field) {
532 $values[$field] = array();
533 }
534 }
535 $index = 1;
536
537 while ($dao->fetch()) {
538 if ($flip) {
539 $value = array();
540 foreach ($fields as $field) {
541 $value[$field] = $dao->$field;
542 }
543 $values[$dao->amount_id] = $value;
544 }
545 else {
546 foreach ($fields as $field) {
547 $values[$field][$index] = $dao->$field;
548 }
549 $index++;
550 }
551 }
552 }
553
a0ee3941 554 /**
100fef9d 555 * @param string $groupName
a0ee3941
EM
556 * @param string $operator
557 */
00be9182 558 public static function deleteAssoc($groupName, $operator = "=") {
6a488035
TO
559 $query = "
560DELETE g, v
561 FROM civicrm_option_group g,
562 civicrm_option_value v
563 WHERE g.id = v.option_group_id
564 AND g.name {$operator} %1";
565
566 $params = array(1 => array($groupName, 'String'));
567
568 $dao = CRM_Core_DAO::executeQuery($query, $params);
569 }
570
a0ee3941 571 /**
100fef9d 572 * @param string $groupName
a0ee3941
EM
573 * @param $value
574 *
575 * @return null|string
576 */
00be9182 577 public static function optionLabel($groupName, $value) {
6a488035
TO
578 $query = "
579SELECT v.label
580 FROM civicrm_option_group g,
581 civicrm_option_value v
582 WHERE g.id = v.option_group_id
583 AND g.name = %1
584 AND v.value = %2";
2aa397bc
TO
585 $params = array(
586 1 => array($groupName, 'String'),
6a488035
TO
587 2 => array($value, 'String'),
588 );
589 return CRM_Core_DAO::singleValueQuery($query, $params);
590 }
591
a0ee3941 592 /**
100fef9d 593 * @param string $groupName
a0ee3941
EM
594 * @param $fieldValue
595 * @param string $field
596 * @param string $fieldType
597 * @param bool $active
598 *
599 * @return array
600 */
f9f40af3
TO
601 static function getRowValues(
602 $groupName, $fieldValue, $field = 'name',
6a488035
TO
603 $fieldType = 'String', $active = TRUE
604 ) {
605 $query = "
606SELECT v.id, v.label, v.value, v.name, v.weight, v.description
607FROM civicrm_option_value v,
608 civicrm_option_group g
609WHERE v.option_group_id = g.id
610 AND g.name = %1
611 AND g.is_active = 1
612 AND v.$field = %2
613";
614
615 if ($active) {
616 $query .= " AND v.is_active = 1";
617 }
618
2aa397bc
TO
619 $p = array(
620 1 => array($groupName, 'String'),
6a488035
TO
621 2 => array($fieldValue, $fieldType),
622 );
623 $dao = CRM_Core_DAO::executeQuery($query, $p);
624 $row = array();
625
626 if ($dao->fetch()) {
627 foreach (array(
628 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
629 $row[$fld] = $dao->$fld;
630 }
631 }
632 return $row;
633 }
634
635 /*
636 * Wrapper for calling values with fresh set to true to empty the given value
637 *
638 * Since there appears to be some inconsistency
639 * (@todo remove inconsistency) around the pseudoconstant operations
640 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
641 * which is part of the cache key
642 * will do a couple of variations & aspire to someone cleaning it up later
643 */
a0ee3941
EM
644 /**
645 * @param $name
646 * @param array $params
647 */
9b873358 648 public static function flush($name, $params = array()) {
6a488035
TO
649 $defaults = array(
650 'flip' => FALSE,
651 'grouping' => FALSE,
652 'localize' => FALSE,
653 'condition' => NULL,
654 'labelColumnName' => 'label',
655 );
656
657 $params = array_merge($defaults, $params);
658 self::flushValues(
659 $name,
660 $params['flip'],
661 $params['grouping'],
662 $params['localize'],
663 $params['condition'],
664 $params['labelColumnName'],
665 TRUE,
666 TRUE
667 );
668 self::flushValues(
669 $name,
670 $params['flip'],
671 $params['grouping'],
672 $params['localize'],
673 $params['condition'],
674 $params['labelColumnName'],
675 FALSE,
676 TRUE
677 );
678 }
679
00be9182 680 public static function flushAll() {
6a488035
TO
681 self::$_values = array();
682 self::$_cache = array();
a4a33486 683 CRM_Utils_Cache::singleton()->flush();
6a488035
TO
684 }
685}