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.
43 static $_domainIDGroups = array(
49 * @param CRM_Core_DAO $dao
51 * @param bool $grouping
52 * @param bool $localize
53 * @param string $valueColumnName
57 static function &valuesCommon(
58 $dao, $flip = FALSE, $grouping = FALSE,
59 $localize = FALSE, $valueColumnName = 'label'
61 self
::$_values = array();
63 while ($dao->fetch()) {
66 self
::$_values[$dao->value
] = $dao->grouping
;
69 self
::$_values[$dao->{$valueColumnName}] = $dao->value
;
74 self
::$_values[$dao->{$valueColumnName}] = $dao->grouping
;
77 self
::$_values[$dao->value
] = $dao->{$valueColumnName};
82 $i18n = CRM_Core_I18n
::singleton();
83 $i18n->localizeArray(self
::$_values);
85 return self
::$_values;
89 * This function retrieves all the values for the specific option group by name
90 * this is primarily used to create various html based form elements
91 * (radio, select, checkbox etc). OptionGroups for most cases have the
92 * 'label' in the label colum and the 'id' or 'name' in the value column
95 * name of the option group.
97 * results are return in id => label format if false.
98 * if true, the results are reversed
99 * @param bool $grouping
100 * if true, return the value in 'grouping' column.
101 * @param bool $localize
102 * if true, localize the results before returning.
103 * @param string $condition
104 * add another condition to the sql query.
105 * @param string $labelColumnName
106 * the column to use for 'label'.
107 * @param bool $onlyActive
108 * return only the action option values.
110 * ignore cache entries and go back to DB.
111 * @param string $keyColumnName
112 * the column to use for 'key'.
115 * the values as specified by the above params
119 static function &values(
120 $name, $flip = FALSE, $grouping = FALSE,
121 $localize = FALSE, $condition = NULL,
122 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
124 $cache = CRM_Utils_Cache
::singleton();
125 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
128 // Fetch from static var
129 if (array_key_exists($cacheKey, self
::$_cache)) {
130 return self
::$_cache[$cacheKey];
132 // Fetch from main cache
133 $var = $cache->get($cacheKey);
140 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
141 FROM civicrm_option_value v,
142 civicrm_option_group g
143 WHERE v.option_group_id = g.id
145 AND g.is_active = 1 ";
148 $query .= " AND v.is_active = 1 ";
150 if (in_array($name, self
::$_domainIDGroups)) {
151 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
155 $query .= $condition;
158 $query .= " ORDER BY v.weight";
160 $p = array(1 => array($name, 'String'));
161 $dao = CRM_Core_DAO
::executeQuery($query, $p);
163 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
165 // call option value hook
166 CRM_Utils_Hook
::optionValues($var, $name);
168 self
::$_cache[$cacheKey] = $var;
169 $cache->set($cacheKey, $var);
175 * Counterpart to values() which removes the item from the cache
177 * @param string $name
182 * @param string $labelColumnName
184 * @param string $keyColumnName
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);
188 $cache = CRM_Utils_Cache
::singleton();
189 $cache->delete($cacheKey);
190 unset(self
::$_cache[$cacheKey]);
196 protected static function createCacheKey() {
197 $cacheKey = "CRM_OG_" . serialize(func_get_args());
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
208 * id of the option group.
210 * results are return in id => label format if false.
211 * if true, the results are reversed
212 * @param bool $grouping
213 * if true, return the value in 'grouping' column.
214 * @param bool $localize
215 * if true, localize the results before returning.
216 * @param string $labelColumnName
217 * the column to use for 'label'.
218 * @param bool $onlyActive
222 * of values as specified by the above params
226 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
227 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
229 $cache = CRM_Utils_Cache
::singleton();
231 $var = $cache->get($cacheKey);
237 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
238 FROM civicrm_option_value v,
239 civicrm_option_group g
240 WHERE v.option_group_id = g.id
245 $query .= " AND v.is_active = 1 ";
247 $query .= " ORDER BY v.weight, v.label";
249 $p = array(1 => array($id, 'Integer'));
250 $dao = CRM_Core_DAO
::executeQuery($query, $p);
252 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
253 $cache->set($cacheKey, $var);
259 * Lookup titles OR ids for a set of option_value populated fields. The retrieved value
260 * is assigned a new fieldname by id or id's by title
261 * (each within a specificied option_group)
263 * @param array $params
264 * Reference array of values submitted by the form. Based on.
265 * $flip, creates new elements in $params for each field in
267 * If $flip = false, adds root field name => title
268 * If $flip = true, adds actual field name => id
270 * @param array $names
271 * Reference array of fieldnames we want transformed.
272 * Array key = 'postName' (field name submitted by form in $params).
273 * Array value = array('newName' => $newName, 'groupName' => $groupName).
282 public static function lookupValues(&$params, &$names, $flip = FALSE) {
283 foreach ($names as $postName => $value) {
284 // See if $params field is in $names array (i.e. is a value that we need to lookup)
285 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
286 $postValues = array();
287 // params[$postName] may be a Ctrl+A separated value list
288 if (is_string($postalName) &&
289 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
291 // eliminate the ^A frm the beginning and end if present
292 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
293 $params[$postName] = substr($params[$postName], 1, -1);
295 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
297 elseif (is_array($postalName)) {
298 $postValues = $postalName;
301 foreach ($postValues as $postValue) {
307 $p = array(1 => array($postValue, 'String'));
308 $lookupBy = 'v.label= %1';
312 $p = array(1 => array($postValue, 'Integer'));
313 $lookupBy = 'v.value = %1';
317 $p[2] = array($value['groupName'], 'String');
320 FROM civicrm_option_value v,
321 civicrm_option_group g
322 WHERE v.option_group_id = g.id
326 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
327 $newValue = str_replace(',', '_', $newValue);
329 $params[$value['newName']] = implode(', ', $newValue);
335 * @param string $groupName
337 * @param bool $onlyActiveValue
341 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
342 if (empty($groupName) ||
349 SELECT v.label as label ,v.value as value
350 FROM civicrm_option_value v,
351 civicrm_option_group g
352 WHERE v.option_group_id = g.id
357 if ($onlyActiveValue) {
358 $query .= " AND v.is_active = 1 ";
361 1 => array($groupName, 'String'),
362 2 => array($value, 'Integer'),
364 $dao = CRM_Core_DAO
::executeQuery($query, $p);
372 * @param string $groupName
374 * @param string $labelField
375 * @param string $labelType
376 * @param string $valueField
380 static function getValue(
383 $labelField = 'label',
384 $labelType = 'String',
385 $valueField = 'value'
392 SELECT v.label as label ,v.{$valueField} as value
393 FROM civicrm_option_value v,
394 civicrm_option_group g
395 WHERE v.option_group_id = g.id
399 AND v.$labelField = %2
403 1 => array($groupName, 'String'),
404 2 => array($label, $labelType),
406 $dao = CRM_Core_DAO
::executeQuery($query, $p);
416 * Get option_value.value from default option_value row for an option group
418 * @param string $groupName
419 * The name of the option group.
424 * the value from the row where is_default = true
426 public static function getDefaultValue($groupName) {
427 if (empty($groupName)) {
432 FROM civicrm_option_value v,
433 civicrm_option_group g
434 WHERE v.option_group_id = g.id
440 if (in_array($groupName, self
::$_domainIDGroups)) {
441 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
444 $p = array(1 => array($groupName, 'String'));
445 return CRM_Core_DAO
::singleValueQuery($query, $p);
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
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.
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
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.
473 * the option group ID
475 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
476 self
::deleteAssoc($groupName);
477 if (!empty($values)) {
478 $group = new CRM_Core_DAO_OptionGroup();
479 $group->name
= $groupName;
480 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
481 $group->is_reserved
= 1;
482 $group->is_active
= 1;
485 foreach ($values as $v) {
486 $value = new CRM_Core_DAO_OptionValue();
487 $value->option_group_id
= $group->id
;
488 $value->label
= $v['label'];
489 $value->value
= $v['value'];
490 $value->name
= CRM_Utils_Array
::value('name', $v);
491 $value->description
= CRM_Utils_Array
::value('description', $v);
492 $value->weight
= CRM_Utils_Array
::value('weight', $v);
493 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
494 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
497 if ($value->is_default
) {
498 $defaultID = $value->id
;
503 return $defaultID = 'null';
510 * @param string $groupName
513 * @param string $field
515 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
517 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
518 FROM civicrm_option_group g,
519 civicrm_option_value v
520 WHERE g.id = v.option_group_id
524 $params = array(1 => array($groupName, 'String'));
525 $dao = CRM_Core_DAO
::executeQuery($query, $params);
527 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
532 foreach ($fields as $field) {
533 $values[$field] = array();
538 while ($dao->fetch()) {
541 foreach ($fields as $field) {
542 $value[$field] = $dao->$field;
544 $values[$dao->amount_id
] = $value;
547 foreach ($fields as $field) {
548 $values[$field][$index] = $dao->$field;
556 * @param string $groupName
557 * @param string $operator
559 public static function deleteAssoc($groupName, $operator = "=") {
562 FROM civicrm_option_group g,
563 civicrm_option_value v
564 WHERE g.id = v.option_group_id
565 AND g.name {$operator} %1";
567 $params = array(1 => array($groupName, 'String'));
569 $dao = CRM_Core_DAO
::executeQuery($query, $params);
573 * @param string $groupName
576 * @return null|string
578 public static function optionLabel($groupName, $value) {
581 FROM civicrm_option_group g,
582 civicrm_option_value v
583 WHERE g.id = v.option_group_id
587 1 => array($groupName, 'String'),
588 2 => array($value, 'String'),
590 return CRM_Core_DAO
::singleValueQuery($query, $params);
594 * @param string $groupName
596 * @param string $field
597 * @param string $fieldType
598 * @param bool $active
602 static function getRowValues(
603 $groupName, $fieldValue, $field = 'name',
604 $fieldType = 'String', $active = TRUE
607 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
608 FROM civicrm_option_value v,
609 civicrm_option_group g
610 WHERE v.option_group_id = g.id
617 $query .= " AND v.is_active = 1";
621 1 => array($groupName, 'String'),
622 2 => array($fieldValue, $fieldType),
624 $dao = CRM_Core_DAO
::executeQuery($query, $p);
629 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
630 $row[$fld] = $dao->$fld;
637 * Wrapper for calling values with fresh set to true to empty the given value
639 * Since there appears to be some inconsistency
640 * (@todo remove inconsistency) around the pseudoconstant operations
641 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
642 * which is part of the cache key
643 * will do a couple of variations & aspire to someone cleaning it up later
647 * @param array $params
649 public static function flush($name, $params = array()) {
655 'labelColumnName' => 'label',
658 $params = array_merge($defaults, $params);
664 $params['condition'],
665 $params['labelColumnName'],
674 $params['condition'],
675 $params['labelColumnName'],
681 public static function flushAll() {
682 self
::$_values = array();
683 self
::$_cache = array();
684 CRM_Utils_Cache
::singleton()->flush();