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 * String name of the option group.
98 * Boolean results are return in id => label format if false.
99 * if true, the results are reversed
101 * Boolean if true, return the value in 'grouping' column.
103 * Boolean if true, localize the results before returning.
105 * String add another condition to the sql query.
106 * @param $labelColumnName
107 * String the column to use for 'label'.
109 * Boolean return only the action option values.
111 * Boolean ignore cache entries and go back to DB.
112 * @param $keyColumnName
113 * String the column to use for 'key'.
115 * @return array 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 * Integer id of the option group.
210 * Boolean results are return in id => label format if false.
211 * if true, the results are reversed
213 * Boolean if true, return the value in 'grouping' column.
215 * Boolean if true, localize the results before returning.
216 * @param $labelColumnName
217 * String the column to use for 'label'.
219 * @param bool $onlyActive
222 * @return array the 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(
274 '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($groupName,
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.
423 * @return string the value from the row where is_default = true
425 public static function getDefaultValue($groupName) {
426 if (empty($groupName)) {
431 FROM civicrm_option_value v,
432 civicrm_option_group g
433 WHERE v.option_group_id = g.id
439 if (in_array($groupName, self
::$_domainIDGroups)) {
440 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
443 $p = array(1 => array($groupName, 'String'));
444 return CRM_Core_DAO
::singleValueQuery($query, $p);
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
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.
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
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.
471 * @return int the option group ID
473 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
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;
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);
495 if ($value->is_default
) {
496 $defaultID = $value->id
;
501 return $defaultID = 'null';
508 * @param string $groupName
511 * @param string $field
513 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
515 SELECT 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
522 $params = array(1 => array($groupName, 'String'));
523 $dao = CRM_Core_DAO
::executeQuery($query, $params);
525 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
530 foreach ($fields as $field) {
531 $values[$field] = array();
536 while ($dao->fetch()) {
539 foreach ($fields as $field) {
540 $value[$field] = $dao->$field;
542 $values[$dao->amount_id
] = $value;
545 foreach ($fields as $field) {
546 $values[$field][$index] = $dao->$field;
554 * @param string $groupName
555 * @param string $operator
557 public static function deleteAssoc($groupName, $operator = "=") {
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";
565 $params = array(1 => array($groupName, 'String'));
567 $dao = CRM_Core_DAO
::executeQuery($query, $params);
571 * @param string $groupName
574 * @return null|string
576 public static function optionLabel($groupName, $value) {
579 FROM civicrm_option_group g,
580 civicrm_option_value v
581 WHERE g.id = v.option_group_id
585 1 => array($groupName, 'String'),
586 2 => array($value, 'String'),
588 return CRM_Core_DAO
::singleValueQuery($query, $params);
592 * @param string $groupName
594 * @param string $field
595 * @param string $fieldType
596 * @param bool $active
600 static function getRowValues($groupName, $fieldValue, $field = 'name',
601 $fieldType = 'String', $active = TRUE
604 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
605 FROM civicrm_option_value v,
606 civicrm_option_group g
607 WHERE v.option_group_id = g.id
614 $query .= " AND v.is_active = 1";
618 1 => array($groupName, 'String'),
619 2 => array($fieldValue, $fieldType),
621 $dao = CRM_Core_DAO
::executeQuery($query, $p);
626 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
627 $row[$fld] = $dao->$fld;
634 * Wrapper for calling values with fresh set to true to empty the given value
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
644 * @param array $params
646 public static function flush($name, $params = array()){
652 'labelColumnName' => 'label',
655 $params = array_merge($defaults, $params);
661 $params['condition'],
662 $params['labelColumnName'],
671 $params['condition'],
672 $params['labelColumnName'],
678 public static function flushAll() {
679 self
::$_values = array();
680 self
::$_cache = array();
681 CRM_Utils_Cache
::singleton()->flush();