Merge pull request #4627 from colemanw/docblocks
[civicrm-core.git] / CRM / Core / OptionGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 *
168 * @param $name
169 * @param $flip
170 * @param $grouping
171 * @param $localize
172 * @param $condition
173 * @param $labelColumnName
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 */
786ad6e1
CW
212 static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
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 /**
c490a46a 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 *
265 * @access public
266 * @static
267 */
268 static function lookupValues(&$params, &$names, $flip = FALSE) {
269 foreach ($names as $postName => $value) {
270 // See if $params field is in $names array (i.e. is a value that we need to lookup)
271 if ($postalName = CRM_Utils_Array::value($postName, $params)) {
272 $postValues = array();
273 // params[$postName] may be a Ctrl+A separated value list
274 if (is_string($postalName) &&
275 strpos($postalName, CRM_Core_DAO::VALUE_SEPARATOR) == FALSE
276 ) {
277 // eliminate the ^A frm the beginning and end if present
278 if (substr($postalName, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) {
279 $params[$postName] = substr($params[$postName], 1, -1);
280 }
281 $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]);
282 }
283 elseif (is_array($postalName)) {
284 $postValues = $postalName;
285 }
286 $newValue = array();
287 foreach ($postValues as $postValue) {
288 if (!$postValue) {
289 continue;
290 }
291
292 if ($flip) {
293 $p = array(1 => array($postValue, 'String'));
294 $lookupBy = 'v.label= %1';
295 $select = "v.value";
296 }
297 else {
298 $p = array(1 => array($postValue, 'Integer'));
299 $lookupBy = 'v.value = %1';
300 $select = "v.label";
301 }
302
303 $p[2] = array($value['groupName'], 'String');
304 $query = "
305 SELECT $select
306 FROM civicrm_option_value v,
307 civicrm_option_group g
308 WHERE v.option_group_id = g.id
309 AND g.name = %2
310 AND $lookupBy";
311
312 $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p);
313 $newValue = str_replace(',', '_', $newValue);
314 }
315 $params[$value['newName']] = implode(', ', $newValue);
316 }
317 }
318 }
319
a0ee3941
EM
320 /**
321 * @param $groupName
322 * @param $value
323 * @param bool $onlyActiveValue
324 *
325 * @return null
326 */
6a488035
TO
327 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
328 if (empty($groupName) ||
329 empty($value)
330 ) {
331 return NULL;
332 }
333
334 $query = "
335SELECT v.label as label ,v.value as value
336FROM civicrm_option_value v,
337 civicrm_option_group g
338WHERE v.option_group_id = g.id
339 AND g.name = %1
340 AND g.is_active = 1
341 AND v.value = %2
342";
343 if ($onlyActiveValue) {
344 $query .= " AND v.is_active = 1 ";
345 }
346 $p = array(1 => array($groupName, 'String'),
347 2 => array($value, 'Integer'),
348 );
349 $dao = CRM_Core_DAO::executeQuery($query, $p);
350 if ($dao->fetch()) {
351 return $dao->label;
352 }
353 return NULL;
354 }
355
a0ee3941
EM
356 /**
357 * @param $groupName
358 * @param $label
359 * @param string $labelField
360 * @param string $labelType
361 * @param string $valueField
362 *
363 * @return null
364 */
6a488035
TO
365 static function getValue($groupName,
366 $label,
367 $labelField = 'label',
368 $labelType = 'String',
369 $valueField = 'value'
370 ) {
371 if (empty($label)) {
372 return NULL;
373 }
374
375 $query = "
376SELECT v.label as label ,v.{$valueField} as value
377FROM civicrm_option_value v,
378 civicrm_option_group g
379WHERE v.option_group_id = g.id
380 AND g.name = %1
381 AND v.is_active = 1
382 AND g.is_active = 1
383 AND v.$labelField = %2
384";
385
386 $p = array(1 => array($groupName, 'String'),
387 2 => array($label, $labelType),
388 );
389 $dao = CRM_Core_DAO::executeQuery($query, $p);
390 if ($dao->fetch()) {
391 $dao->free();
392 return $dao->value;
393 }
394 $dao->free();
395 return NULL;
396 }
397
343d84fa
DG
398 /**
399 * Get option_value.value from default option_value row for an option group
400 *
401 * @param string $groupName the name of the option group
402 *
403 * @access public
404 * @static
405 *
406 * @return string the value from the row where is_default = true
8ef12e64 407 */
343d84fa
DG
408 static function getDefaultValue($groupName) {
409 if (empty($groupName)) {
410 return NULL;
411 }
412 $query = "
413SELECT v.value
414FROM civicrm_option_value v,
415 civicrm_option_group g
416WHERE v.option_group_id = g.id
417 AND g.name = %1
418 AND v.is_active = 1
419 AND g.is_active = 1
420 AND v.is_default = 1
421";
422 if (in_array($groupName, self::$_domainIDGroups)) {
423 $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
424 }
425
426 $p = array(1 => array($groupName, 'String'));
427 return CRM_Core_DAO::singleValueQuery($query, $p);
428 }
8ef12e64 429
6a488035
TO
430 /**
431 * Creates a new option group with the passed in values
432 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
433 * not messed up. Currently deletes the old group
434 *
435 * @param string $groupName the name of the option group - make sure there is no conflict
2a6da8d7 436 * @param array $values the associative array that has information on the option values
6a488035
TO
437 * the keys of this array are:
438 * string 'title' (required)
439 * string 'value' (required)
440 * string 'name' (optional)
441 * string 'description' (optional)
442 * int 'weight' (optional) - the order in which the value are displayed
443 * bool 'is_default' (optional) - is this the default one to display when rendered in form
444 * bool 'is_active' (optional) - should this element be rendered
2a6da8d7 445 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
c490a46a 446 * @param null $groupTitle the optional label of the option group else set to group name
6a488035
TO
447 *
448 * @access public
449 * @static
450 *
451 * @return int the option group ID
6a488035
TO
452 */
453 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
454 self::deleteAssoc($groupName);
455 if (!empty($values)) {
456 $group = new CRM_Core_DAO_OptionGroup();
457 $group->name = $groupName;
458 $group->title = empty($groupTitle) ? $groupName : $groupTitle;
459 $group->is_reserved = 1;
460 $group->is_active = 1;
461 $group->save();
462
463 foreach ($values as $v) {
464 $value = new CRM_Core_DAO_OptionValue();
465 $value->option_group_id = $group->id;
466 $value->label = $v['label'];
467 $value->value = $v['value'];
468 $value->name = CRM_Utils_Array::value('name', $v);
469 $value->description = CRM_Utils_Array::value('description', $v);
470 $value->weight = CRM_Utils_Array::value('weight', $v);
471 $value->is_default = CRM_Utils_Array::value('is_default', $v);
472 $value->is_active = CRM_Utils_Array::value('is_active', $v);
473 $value->save();
474
475 if ($value->is_default) {
476 $defaultID = $value->id;
477 }
478 }
479 }
480 else {
481 return $defaultID = 'null';
482 }
483
484 return $group->id;
485 }
486
a0ee3941
EM
487 /**
488 * @param $groupName
489 * @param $values
490 * @param bool $flip
491 * @param string $field
492 */
6a488035
TO
493 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
494 $query = "
495SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
496 FROM civicrm_option_group g,
497 civicrm_option_value v
498 WHERE g.id = v.option_group_id
499 AND g.$field = %1
500ORDER BY v.weight
501";
502 $params = array(1 => array($groupName, 'String'));
503 $dao = CRM_Core_DAO::executeQuery($query, $params);
504
505 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
506 if ($flip) {
507 $values = array();
508 }
509 else {
510 foreach ($fields as $field) {
511 $values[$field] = array();
512 }
513 }
514 $index = 1;
515
516 while ($dao->fetch()) {
517 if ($flip) {
518 $value = array();
519 foreach ($fields as $field) {
520 $value[$field] = $dao->$field;
521 }
522 $values[$dao->amount_id] = $value;
523 }
524 else {
525 foreach ($fields as $field) {
526 $values[$field][$index] = $dao->$field;
527 }
528 $index++;
529 }
530 }
531 }
532
a0ee3941
EM
533 /**
534 * @param $groupName
535 * @param string $operator
536 */
6a488035
TO
537 static function deleteAssoc($groupName, $operator = "=") {
538 $query = "
539DELETE g, v
540 FROM civicrm_option_group g,
541 civicrm_option_value v
542 WHERE g.id = v.option_group_id
543 AND g.name {$operator} %1";
544
545 $params = array(1 => array($groupName, 'String'));
546
547 $dao = CRM_Core_DAO::executeQuery($query, $params);
548 }
549
a0ee3941
EM
550 /**
551 * @param $groupName
552 * @param $value
553 *
554 * @return null|string
555 */
6a488035
TO
556 static function optionLabel($groupName, $value) {
557 $query = "
558SELECT v.label
559 FROM civicrm_option_group g,
560 civicrm_option_value v
561 WHERE g.id = v.option_group_id
562 AND g.name = %1
563 AND v.value = %2";
564 $params = array(1 => array($groupName, 'String'),
565 2 => array($value, 'String'),
566 );
567 return CRM_Core_DAO::singleValueQuery($query, $params);
568 }
569
a0ee3941
EM
570 /**
571 * @param $groupName
572 * @param $fieldValue
573 * @param string $field
574 * @param string $fieldType
575 * @param bool $active
576 *
577 * @return array
578 */
6a488035
TO
579 static function getRowValues($groupName, $fieldValue, $field = 'name',
580 $fieldType = 'String', $active = TRUE
581 ) {
582 $query = "
583SELECT v.id, v.label, v.value, v.name, v.weight, v.description
584FROM civicrm_option_value v,
585 civicrm_option_group g
586WHERE v.option_group_id = g.id
587 AND g.name = %1
588 AND g.is_active = 1
589 AND v.$field = %2
590";
591
592 if ($active) {
593 $query .= " AND v.is_active = 1";
594 }
595
596 $p = array(1 => array($groupName, 'String'),
597 2 => array($fieldValue, $fieldType),
598 );
599 $dao = CRM_Core_DAO::executeQuery($query, $p);
600 $row = array();
601
602 if ($dao->fetch()) {
603 foreach (array(
604 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
605 $row[$fld] = $dao->$fld;
606 }
607 }
608 return $row;
609 }
610
611 /*
612 * Wrapper for calling values with fresh set to true to empty the given value
613 *
614 * Since there appears to be some inconsistency
615 * (@todo remove inconsistency) around the pseudoconstant operations
616 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
617 * which is part of the cache key
618 * will do a couple of variations & aspire to someone cleaning it up later
619 */
a0ee3941
EM
620 /**
621 * @param $name
622 * @param array $params
623 */
6a488035
TO
624 static function flush($name, $params = array()){
625 $defaults = array(
626 'flip' => FALSE,
627 'grouping' => FALSE,
628 'localize' => FALSE,
629 'condition' => NULL,
630 'labelColumnName' => 'label',
631 );
632
633 $params = array_merge($defaults, $params);
634 self::flushValues(
635 $name,
636 $params['flip'],
637 $params['grouping'],
638 $params['localize'],
639 $params['condition'],
640 $params['labelColumnName'],
641 TRUE,
642 TRUE
643 );
644 self::flushValues(
645 $name,
646 $params['flip'],
647 $params['grouping'],
648 $params['localize'],
649 $params['condition'],
650 $params['labelColumnName'],
651 FALSE,
652 TRUE
653 );
654 }
655
656 static function flushAll() {
657 self::$_values = array();
658 self::$_cache = array();
a4a33486 659 CRM_Utils_Cache::singleton()->flush();
6a488035
TO
660 }
661}
662