3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Core_OptionGroup
{
36 static $_values = array();
37 static $_cache = array();
40 * $_domainIDGroups array maintains the list of option groups for whom
41 * domainID is to be considered.
44 static $_domainIDGroups = array(
50 * @param CRM_Core_DAO $dao
52 * @param bool $grouping
53 * @param bool $localize
54 * @param string $valueColumnName
58 static function &valuesCommon(
59 $dao, $flip = FALSE, $grouping = FALSE,
60 $localize = FALSE, $valueColumnName = 'label'
62 self
::$_values = array();
64 while ($dao->fetch()) {
67 self
::$_values[$dao->value
] = $dao->grouping
;
70 self
::$_values[$dao->{$valueColumnName}] = $dao->value
;
75 self
::$_values[$dao->{$valueColumnName}] = $dao->grouping
;
78 self
::$_values[$dao->value
] = $dao->{$valueColumnName};
83 $i18n = CRM_Core_I18n
::singleton();
84 $i18n->localizeArray(self
::$_values);
86 return self
::$_values;
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
96 * name of the option group.
98 * results are return in id => label format if false.
99 * if true, the results are reversed
100 * @param bool $grouping
101 * if true, return the value in 'grouping' column.
102 * @param bool $localize
103 * if true, localize the results before returning.
104 * @param string $condition
105 * add another condition to the sql query.
106 * @param string $labelColumnName
107 * the column to use for 'label'.
108 * @param bool $onlyActive
109 * return only the action option values.
111 * ignore cache entries and go back to DB.
112 * @param string $keyColumnName
113 * the column to use for 'key'.
116 * the values as specified by the above params
120 static function &values(
121 $name, $flip = FALSE, $grouping = FALSE,
122 $localize = FALSE, $condition = NULL,
123 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
125 $cache = CRM_Utils_Cache
::singleton();
126 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
129 // Fetch from static var
130 if (array_key_exists($cacheKey, self
::$_cache)) {
131 return self
::$_cache[$cacheKey];
133 // Fetch from main cache
134 $var = $cache->get($cacheKey);
141 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
142 FROM civicrm_option_value v,
143 civicrm_option_group g
144 WHERE v.option_group_id = g.id
146 AND g.is_active = 1 ";
149 $query .= " AND v.is_active = 1 ";
151 if (in_array($name, self
::$_domainIDGroups)) {
152 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
156 $query .= $condition;
159 $query .= " ORDER BY v.weight";
161 $p = array(1 => array($name, 'String'));
162 $dao = CRM_Core_DAO
::executeQuery($query, $p);
164 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
166 // call option value hook
167 CRM_Utils_Hook
::optionValues($var, $name);
169 self
::$_cache[$cacheKey] = $var;
170 $cache->set($cacheKey, $var);
176 * Counterpart to values() which removes the item from the cache
178 * @param string $name
183 * @param string $labelColumnName
185 * @param string $keyColumnName
187 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
188 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
189 $cache = CRM_Utils_Cache
::singleton();
190 $cache->delete($cacheKey);
191 unset(self
::$_cache[$cacheKey]);
197 protected static function createCacheKey() {
198 $cacheKey = "CRM_OG_" . serialize(func_get_args());
203 * This function retrieves all the values for the specific option group by id
204 * this is primarily used to create various html based form elements
205 * (radio, select, checkbox etc). OptionGroups for most cases have the
206 * 'label' in the label colum and the 'id' or 'name' in the value column
209 * id of the option group.
211 * results are return in id => label format if false.
212 * if true, the results are reversed
213 * @param bool $grouping
214 * if true, return the value in 'grouping' column.
215 * @param bool $localize
216 * if true, localize the results before returning.
217 * @param string $labelColumnName
218 * the column to use for 'label'.
219 * @param bool $onlyActive
223 * of values as specified by the above params
227 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
228 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
230 $cache = CRM_Utils_Cache
::singleton();
232 $var = $cache->get($cacheKey);
238 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
239 FROM civicrm_option_value v,
240 civicrm_option_group g
241 WHERE v.option_group_id = g.id
246 $query .= " AND v.is_active = 1 ";
248 $query .= " ORDER BY v.weight, v.label";
250 $p = array(1 => array($id, 'Integer'));
251 $dao = CRM_Core_DAO
::executeQuery($query, $p);
253 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
254 $cache->set($cacheKey, $var);
260 * Lookup titles OR ids for a set of option_value populated fields. The retrieved value
261 * is assigned a new fieldname by id or id's by title
262 * (each within a specificied option_group)
264 * @param array $params
265 * Reference array of values submitted by the form. Based on.
266 * $flip, creates new elements in $params for each field in
268 * If $flip = false, adds root field name => title
269 * If $flip = true, adds actual field name => id
271 * @param array $names
272 * Reference array of fieldnames we want transformed.
273 * Array key = 'postName' (field name submitted by form in $params).
274 * Array value = array('newName' => $newName, 'groupName' => $groupName).
283 public static function lookupValues(&$params, &$names, $flip = FALSE) {
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
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);
296 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
298 elseif (is_array($postalName)) {
299 $postValues = $postalName;
302 foreach ($postValues as $postValue) {
308 $p = array(1 => array($postValue, 'String'));
309 $lookupBy = 'v.label= %1';
313 $p = array(1 => array($postValue, 'Integer'));
314 $lookupBy = 'v.value = %1';
318 $p[2] = array($value['groupName'], 'String');
321 FROM civicrm_option_value v,
322 civicrm_option_group g
323 WHERE v.option_group_id = g.id
327 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
328 $newValue = str_replace(',', '_', $newValue);
330 $params[$value['newName']] = implode(', ', $newValue);
336 * @param string $groupName
338 * @param bool $onlyActiveValue
342 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
343 if (empty($groupName) ||
350 SELECT v.label as label ,v.value as value
351 FROM civicrm_option_value v,
352 civicrm_option_group g
353 WHERE v.option_group_id = g.id
358 if ($onlyActiveValue) {
359 $query .= " AND v.is_active = 1 ";
362 1 => array($groupName, 'String'),
363 2 => array($value, 'Integer'),
365 $dao = CRM_Core_DAO
::executeQuery($query, $p);
373 * @param string $groupName
375 * @param string $labelField
376 * @param string $labelType
377 * @param string $valueField
381 static function getValue(
384 $labelField = 'label',
385 $labelType = 'String',
386 $valueField = 'value'
393 SELECT v.label as label ,v.{$valueField} as value
394 FROM civicrm_option_value v,
395 civicrm_option_group g
396 WHERE v.option_group_id = g.id
400 AND v.$labelField = %2
404 1 => array($groupName, 'String'),
405 2 => array($label, $labelType),
407 $dao = CRM_Core_DAO
::executeQuery($query, $p);
417 * Get option_value.value from default option_value row for an option group
419 * @param string $groupName
420 * The name of the option group.
425 * the value from the row where is_default = true
427 public static function getDefaultValue($groupName) {
428 if (empty($groupName)) {
433 FROM civicrm_option_value v,
434 civicrm_option_group g
435 WHERE v.option_group_id = g.id
441 if (in_array($groupName, self
::$_domainIDGroups)) {
442 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
445 $p = array(1 => array($groupName, 'String'));
446 return CRM_Core_DAO
::singleValueQuery($query, $p);
450 * Creates a new option group with the passed in values
451 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
452 * not messed up. Currently deletes the old group
454 * @param string $groupName
455 * The name of the option group - make sure there is no conflict.
456 * @param array $values
457 * The associative array that has information on the option values.
458 * the keys of this array are:
459 * string 'title' (required)
460 * string 'value' (required)
461 * string 'name' (optional)
462 * string 'description' (optional)
463 * int 'weight' (optional) - the order in which the value are displayed
464 * bool 'is_default' (optional) - is this the default one to display when rendered in form
465 * bool 'is_active' (optional) - should this element be rendered
466 * @param int $defaultID
467 * (reference) - the option value ID of the default element (if set) is returned else 'null'.
468 * @param null $groupTitle
469 * The optional label of the option group else set to group name.
474 * the option group ID
476 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
477 self
::deleteAssoc($groupName);
478 if (!empty($values)) {
479 $group = new CRM_Core_DAO_OptionGroup();
480 $group->name
= $groupName;
481 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
482 $group->is_reserved
= 1;
483 $group->is_active
= 1;
486 foreach ($values as $v) {
487 $value = new CRM_Core_DAO_OptionValue();
488 $value->option_group_id
= $group->id
;
489 $value->label
= $v['label'];
490 $value->value
= $v['value'];
491 $value->name
= CRM_Utils_Array
::value('name', $v);
492 $value->description
= CRM_Utils_Array
::value('description', $v);
493 $value->weight
= CRM_Utils_Array
::value('weight', $v);
494 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
495 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
498 if ($value->is_default
) {
499 $defaultID = $value->id
;
504 return $defaultID = 'null';
511 * @param string $groupName
514 * @param string $field
516 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
518 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
519 FROM civicrm_option_group g,
520 civicrm_option_value v
521 WHERE g.id = v.option_group_id
525 $params = array(1 => array($groupName, 'String'));
526 $dao = CRM_Core_DAO
::executeQuery($query, $params);
528 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
533 foreach ($fields as $field) {
534 $values[$field] = array();
539 while ($dao->fetch()) {
542 foreach ($fields as $field) {
543 $value[$field] = $dao->$field;
545 $values[$dao->amount_id
] = $value;
548 foreach ($fields as $field) {
549 $values[$field][$index] = $dao->$field;
557 * @param string $groupName
558 * @param string $operator
560 public static function deleteAssoc($groupName, $operator = "=") {
563 FROM civicrm_option_group g,
564 civicrm_option_value v
565 WHERE g.id = v.option_group_id
566 AND g.name {$operator} %1";
568 $params = array(1 => array($groupName, 'String'));
570 $dao = CRM_Core_DAO
::executeQuery($query, $params);
574 * @param string $groupName
577 * @return null|string
579 public static function optionLabel($groupName, $value) {
582 FROM civicrm_option_group g,
583 civicrm_option_value v
584 WHERE g.id = v.option_group_id
588 1 => array($groupName, 'String'),
589 2 => array($value, 'String'),
591 return CRM_Core_DAO
::singleValueQuery($query, $params);
595 * @param string $groupName
597 * @param string $field
598 * @param string $fieldType
599 * @param bool $active
603 static function getRowValues(
604 $groupName, $fieldValue, $field = 'name',
605 $fieldType = 'String', $active = TRUE
608 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
609 FROM civicrm_option_value v,
610 civicrm_option_group g
611 WHERE v.option_group_id = g.id
618 $query .= " AND v.is_active = 1";
622 1 => array($groupName, 'String'),
623 2 => array($fieldValue, $fieldType),
625 $dao = CRM_Core_DAO
::executeQuery($query, $p);
630 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
631 $row[$fld] = $dao->$fld;
638 * Wrapper for calling values with fresh set to true to empty the given value
640 * Since there appears to be some inconsistency
641 * (@todo remove inconsistency) around the pseudoconstant operations
642 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
643 * which is part of the cache key
644 * will do a couple of variations & aspire to someone cleaning it up later
648 * @param array $params
650 public static function flush($name, $params = array()) {
656 'labelColumnName' => 'label',
659 $params = array_merge($defaults, $params);
665 $params['condition'],
666 $params['labelColumnName'],
675 $params['condition'],
676 $params['labelColumnName'],
682 public static function flushAll() {
683 self
::$_values = array();
684 self
::$_cache = array();
685 CRM_Utils_Cache
::singleton()->flush();