3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Core_OptionGroup
{
34 static $_values = array();
35 static $_cache = array();
38 * $_domainIDGroups array maintains the list of option groups for whom
39 * domainID is to be considered.
41 static $_domainIDGroups = array(
47 * @param CRM_Core_DAO $dao
49 * @param bool $grouping
50 * @param bool $localize
51 * @param string $valueColumnName
55 public static function &valuesCommon(
56 $dao, $flip = FALSE, $grouping = FALSE,
57 $localize = FALSE, $valueColumnName = 'label'
59 self
::$_values = array();
61 while ($dao->fetch()) {
64 self
::$_values[$dao->value
] = $dao->grouping
;
67 self
::$_values[$dao->{$valueColumnName}] = $dao->value
;
72 self
::$_values[$dao->{$valueColumnName}] = $dao->grouping
;
75 self
::$_values[$dao->value
] = $dao->{$valueColumnName};
80 $i18n = CRM_Core_I18n
::singleton();
81 $i18n->localizeArray(self
::$_values);
83 return self
::$_values;
87 * This function retrieves all the values for the specific option group by name
88 * this is primarily used to create various html based form elements
89 * (radio, select, checkbox etc). OptionGroups for most cases have the
90 * 'label' in the label column and the 'id' or 'name' in the value column
93 * name of the option group.
95 * results are return in id => label format if false.
96 * if true, the results are reversed
97 * @param bool $grouping
98 * if true, return the value in 'grouping' column.
99 * @param bool $localize
100 * if true, localize the results before returning.
101 * @param string $condition
102 * add another condition to the sql query.
103 * @param string $labelColumnName
104 * the column to use for 'label'.
105 * @param bool $onlyActive
106 * return only the action option values.
108 * ignore cache entries and go back to DB.
109 * @param string $keyColumnName
110 * the column to use for 'key'.
111 * @param string $orderBy
114 * The values as specified by the params
116 public static function &values(
117 $name, $flip = FALSE, $grouping = FALSE,
118 $localize = FALSE, $condition = NULL,
119 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value',
122 $cache = CRM_Utils_Cache
::singleton();
123 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
126 // Fetch from static var
127 if (array_key_exists($cacheKey, self
::$_cache)) {
128 return self
::$_cache[$cacheKey];
130 // Fetch from main cache
131 $var = $cache->get($cacheKey);
138 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
139 FROM civicrm_option_value v,
140 civicrm_option_group g
141 WHERE v.option_group_id = g.id
143 AND g.is_active = 1 ";
146 $query .= " AND v.is_active = 1 ";
147 $enabledComponents = '"' . implode('","', CRM_Core_Config
::singleton()->enableComponents
) . '"';
148 $query .= " AND (v.component_id IS NULL OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents))) ";
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.{$orderBy}";
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 column 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 * Array of values as specified by the above params
225 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
226 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
228 $cache = CRM_Utils_Cache
::singleton();
230 $var = $cache->get($cacheKey);
236 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
237 FROM civicrm_option_value v,
238 civicrm_option_group g
239 WHERE v.option_group_id = g.id
244 $query .= " AND v.is_active = 1 ";
246 $query .= " ORDER BY v.weight, v.label";
248 $p = array(1 => array($id, 'Integer'));
249 $dao = CRM_Core_DAO
::executeQuery($query, $p);
251 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
252 $cache->set($cacheKey, $var);
258 * Lookup titles OR ids for a set of option_value populated fields. The retrieved value
259 * is assigned a new fieldname by id or id's by title
260 * (each within a specificied option_group)
262 * @param array $params
263 * Reference array of values submitted by the form. Based on.
264 * $flip, creates new elements in $params for each field in
266 * If $flip = false, adds root field name => title
267 * If $flip = true, adds actual field name => id
269 * @param array $names
270 * Reference array of fieldnames we want transformed.
271 * Array key = 'postName' (field name submitted by form in $params).
272 * Array value = array('newName' => $newName, 'groupName' => $groupName).
277 public static function lookupValues(&$params, &$names, $flip = FALSE) {
278 foreach ($names as $postName => $value) {
279 // See if $params field is in $names array (i.e. is a value that we need to lookup)
280 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
281 $postValues = array();
282 // params[$postName] may be a Ctrl+A separated value list
283 if (is_string($postalName) &&
284 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
286 // eliminate the ^A frm the beginning and end if present
287 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
288 $params[$postName] = substr($params[$postName], 1, -1);
290 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
292 elseif (is_array($postalName)) {
293 $postValues = $postalName;
296 foreach ($postValues as $postValue) {
302 $p = array(1 => array($postValue, 'String'));
303 $lookupBy = 'v.label= %1';
307 $p = array(1 => array($postValue, 'Integer'));
308 $lookupBy = 'v.value = %1';
312 $p[2] = array($value['groupName'], 'String');
315 FROM civicrm_option_value v,
316 civicrm_option_group g
317 WHERE v.option_group_id = g.id
321 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
322 $newValue = str_replace(',', '_', $newValue);
324 $params[$value['newName']] = implode(', ', $newValue);
330 * @deprecated - use CRM_Core_Pseudoconstant::getLabel
332 * @param string $groupName
334 * @param bool $onlyActiveValue
338 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
339 if (empty($groupName) ||
346 SELECT v.label as label ,v.value as value
347 FROM civicrm_option_value v,
348 civicrm_option_group g
349 WHERE v.option_group_id = g.id
354 if ($onlyActiveValue) {
355 $query .= " AND v.is_active = 1 ";
358 1 => array($groupName, 'String'),
359 2 => array($value, 'Integer'),
361 $dao = CRM_Core_DAO
::executeQuery($query, $p);
371 * This function is not cached.
373 * @param string $groupName
375 * @param string $labelField
376 * @param string $labelType
377 * @param string $valueField
381 public 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.
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.
472 * the option group ID
474 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
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;
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);
496 if ($value->is_default
) {
497 $defaultID = $value->id
;
502 return $defaultID = 'null';
509 * @param string $groupName
512 * @param string $field
514 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
516 SELECT 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
523 $params = array(1 => array($groupName, 'String'));
524 $dao = CRM_Core_DAO
::executeQuery($query, $params);
526 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
531 foreach ($fields as $field) {
532 $values[$field] = array();
537 while ($dao->fetch()) {
540 foreach ($fields as $field) {
541 $value[$field] = $dao->$field;
543 $values[$dao->amount_id
] = $value;
546 foreach ($fields as $field) {
547 $values[$field][$index] = $dao->$field;
555 * @param string $groupName
556 * @param string $operator
558 public static function deleteAssoc($groupName, $operator = "=") {
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";
566 $params = array(1 => array($groupName, 'String'));
568 $dao = CRM_Core_DAO
::executeQuery($query, $params);
572 * @param string $groupName
574 * @param string $field
575 * @param string $fieldType
576 * @param bool $active
577 * @param bool $localize
578 * if true, localize the results before returning.
582 public static function getRowValues(
583 $groupName, $fieldValue, $field = 'name',
584 $fieldType = 'String', $active = TRUE, $localize = FALSE
587 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
588 FROM civicrm_option_value v,
589 civicrm_option_group g
590 WHERE v.option_group_id = g.id
597 $query .= " AND v.is_active = 1";
601 1 => array($groupName, 'String'),
602 2 => array($fieldValue, $fieldType),
604 $dao = CRM_Core_DAO
::executeQuery($query, $p);
616 $row[$fld] = $dao->$fld;
617 if ($localize && in_array($fld, array('label', 'description'))) {
618 $row[$fld] = ts($row[$fld]);
627 * Wrapper for calling values with fresh set to true to empty the given value.
629 * Since there appears to be some inconsistency
630 * (@todo remove inconsistency) around the pseudoconstant operations
631 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
632 * which is part of the cache key
633 * will do a couple of variations & aspire to someone cleaning it up later
635 * @param string $name
636 * @param array $params
638 public static function flush($name, $params = array()) {
644 'labelColumnName' => 'label',
647 $params = array_merge($defaults, $params);
653 $params['condition'],
654 $params['labelColumnName'],
663 $params['condition'],
664 $params['labelColumnName'],
670 public static function flushAll() {
671 self
::$_values = array();
672 self
::$_cache = array();
673 CRM_Utils_Cache
::singleton()->flush();