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
112 * the column to use for ordering.
115 * The values as specified by the params
117 public static function &values(
118 $name, $flip = FALSE, $grouping = FALSE,
119 $localize = FALSE, $condition = NULL,
120 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value',
123 $cache = CRM_Utils_Cache
::singleton();
124 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy);
127 // Fetch from static var
128 if (array_key_exists($cacheKey, self
::$_cache)) {
129 return self
::$_cache[$cacheKey];
131 // Fetch from main cache
132 $var = $cache->get($cacheKey);
139 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
140 FROM civicrm_option_value v,
141 civicrm_option_group g
142 WHERE v.option_group_id = g.id
144 AND g.is_active = 1 ";
147 $query .= " AND v.is_active = 1 ";
148 // Only show options for enabled components
149 $componentClause = ' v.component_id IS NULL ';
150 $enabledComponents = CRM_Core_Config
::singleton()->enableComponents
;
151 if ($enabledComponents) {
152 $enabledComponents = '"' . implode('","', $enabledComponents) . '"';
153 $componentClause .= " OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents)) ";
155 $query .= " AND ($componentClause) ";
157 if (in_array($name, self
::$_domainIDGroups)) {
158 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
162 $query .= $condition;
165 $query .= " ORDER BY v.{$orderBy}";
167 $p = array(1 => array($name, 'String'));
168 $dao = CRM_Core_DAO
::executeQuery($query, $p);
170 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
172 // call option value hook
173 CRM_Utils_Hook
::optionValues($var, $name);
175 self
::$_cache[$cacheKey] = $var;
176 $cache->set($cacheKey, $var);
182 * Counterpart to values() which removes the item from the cache
184 * @param string $name
189 * @param string $labelColumnName
191 * @param string $keyColumnName
193 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
194 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
195 $cache = CRM_Utils_Cache
::singleton();
196 $cache->delete($cacheKey);
197 unset(self
::$_cache[$cacheKey]);
203 protected static function createCacheKey() {
204 $cacheKey = "CRM_OG_" . serialize(func_get_args());
209 * This function retrieves all the values for the specific option group by id.
210 * this is primarily used to create various html based form elements
211 * (radio, select, checkbox etc). OptionGroups for most cases have the
212 * 'label' in the label column and the 'id' or 'name' in the value column
215 * id of the option group.
217 * results are return in id => label format if false.
218 * if true, the results are reversed
219 * @param bool $grouping
220 * if true, return the value in 'grouping' column.
221 * @param bool $localize
222 * if true, localize the results before returning.
223 * @param string $labelColumnName
224 * the column to use for 'label'.
225 * @param bool $onlyActive
229 * Array of values as specified by the above params
232 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
233 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
235 $cache = CRM_Utils_Cache
::singleton();
237 $var = $cache->get($cacheKey);
243 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
244 FROM civicrm_option_value v,
245 civicrm_option_group g
246 WHERE v.option_group_id = g.id
251 $query .= " AND v.is_active = 1 ";
253 $query .= " ORDER BY v.weight, v.label";
255 $p = array(1 => array($id, 'Integer'));
256 $dao = CRM_Core_DAO
::executeQuery($query, $p);
258 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
259 $cache->set($cacheKey, $var);
265 * Lookup titles OR ids for a set of option_value populated fields. The
266 * retrieved value is assigned a new field name by id or id's by title
267 * (each within a specified option_group).
269 * @param array $params
270 * Reference array of values submitted by the form. Based on.
271 * $flip, creates new elements in $params for each field in
273 * If $flip = false, adds root field name => title
274 * If $flip = true, adds actual field name => id
276 * @param array $names
277 * Array of field names we want transformed.
278 * Array key = 'postName' (field name submitted by form in $params).
279 * 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 * @deprecated - use CRM_Core_Pseudoconstant::getLabel
338 * @param string $groupName
340 * @param bool $onlyActiveValue
344 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
345 if (empty($groupName) ||
352 SELECT v.label as label ,v.value as value
353 FROM civicrm_option_value v,
354 civicrm_option_group g
355 WHERE v.option_group_id = g.id
360 if ($onlyActiveValue) {
361 $query .= " AND v.is_active = 1 ";
364 1 => array($groupName, 'String'),
365 2 => array($value, 'Integer'),
367 $dao = CRM_Core_DAO
::executeQuery($query, $p);
377 * This function is not cached.
379 * @param string $groupName
381 * @param string $labelField
382 * @param string $labelType
383 * @param string $valueField
387 public static function getValue(
390 $labelField = 'label',
391 $labelType = 'String',
392 $valueField = 'value'
399 SELECT v.label as label ,v.{$valueField} as value
400 FROM civicrm_option_value v,
401 civicrm_option_group g
402 WHERE v.option_group_id = g.id
406 AND v.$labelField = %2
410 1 => array($groupName, 'String'),
411 2 => array($label, $labelType),
413 $dao = CRM_Core_DAO
::executeQuery($query, $p);
423 * Get option_value.value from default option_value row for an option group
425 * @param string $groupName
426 * The name of the option group.
430 * the value from the row where is_default = true
432 public static function getDefaultValue($groupName) {
433 if (empty($groupName)) {
438 FROM civicrm_option_value v,
439 civicrm_option_group g
440 WHERE v.option_group_id = g.id
446 if (in_array($groupName, self
::$_domainIDGroups)) {
447 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
450 $p = array(1 => array($groupName, 'String'));
451 return CRM_Core_DAO
::singleValueQuery($query, $p);
455 * Creates a new option group with the passed in values.
456 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
457 * not messed up. Currently deletes the old group
459 * @param string $groupName
460 * The name of the option group - make sure there is no conflict.
461 * @param array $values
462 * The associative array that has information on the option values.
463 * the keys of this array are:
464 * string 'title' (required)
465 * string 'value' (required)
466 * string 'name' (optional)
467 * string 'description' (optional)
468 * int 'weight' (optional) - the order in which the value are displayed
469 * bool 'is_default' (optional) - is this the default one to display when rendered in form
470 * bool 'is_active' (optional) - should this element be rendered
471 * @param int $defaultID
472 * (reference) - the option value ID of the default element (if set) is returned else 'null'.
473 * @param null $groupTitle
474 * The optional label of the option group else set to group name.
478 * the option group ID
480 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
481 self
::deleteAssoc($groupName);
482 if (!empty($values)) {
483 $group = new CRM_Core_DAO_OptionGroup();
484 $group->name
= $groupName;
485 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
486 $group->is_reserved
= 1;
487 $group->is_active
= 1;
490 foreach ($values as $v) {
491 $value = new CRM_Core_DAO_OptionValue();
492 $value->option_group_id
= $group->id
;
493 $value->label
= $v['label'];
494 $value->value
= $v['value'];
495 $value->name
= CRM_Utils_Array
::value('name', $v);
496 $value->description
= CRM_Utils_Array
::value('description', $v);
497 $value->weight
= CRM_Utils_Array
::value('weight', $v);
498 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
499 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
502 if ($value->is_default
) {
503 $defaultID = $value->id
;
508 return $defaultID = 'null';
515 * @param string $groupName
518 * @param string $field
520 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
522 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
523 FROM civicrm_option_group g,
524 civicrm_option_value v
525 WHERE g.id = v.option_group_id
529 $params = array(1 => array($groupName, 'String'));
530 $dao = CRM_Core_DAO
::executeQuery($query, $params);
532 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
537 foreach ($fields as $field) {
538 $values[$field] = array();
543 while ($dao->fetch()) {
546 foreach ($fields as $field) {
547 $value[$field] = $dao->$field;
549 $values[$dao->amount_id
] = $value;
552 foreach ($fields as $field) {
553 $values[$field][$index] = $dao->$field;
561 * @param string $groupName
562 * @param string $operator
564 public static function deleteAssoc($groupName, $operator = "=") {
567 FROM civicrm_option_group g,
568 civicrm_option_value v
569 WHERE g.id = v.option_group_id
570 AND g.name {$operator} %1";
572 $params = array(1 => array($groupName, 'String'));
574 $dao = CRM_Core_DAO
::executeQuery($query, $params);
578 * @param string $groupName
580 * @param string $field
581 * @param string $fieldType
582 * @param bool $active
583 * @param bool $localize
584 * if true, localize the results before returning.
588 public static function getRowValues(
589 $groupName, $fieldValue, $field = 'name',
590 $fieldType = 'String', $active = TRUE, $localize = FALSE
593 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
594 FROM civicrm_option_value v,
595 civicrm_option_group g
596 WHERE v.option_group_id = g.id
603 $query .= " AND v.is_active = 1";
607 1 => array($groupName, 'String'),
608 2 => array($fieldValue, $fieldType),
610 $dao = CRM_Core_DAO
::executeQuery($query, $p);
622 $row[$fld] = $dao->$fld;
623 if ($localize && in_array($fld, array('label', 'description'))) {
624 $row[$fld] = ts($row[$fld]);
633 * Wrapper for calling values with fresh set to true to empty the given value.
635 * Since there appears to be some inconsistency
636 * (@todo remove inconsistency) around the pseudoconstant operations
637 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
638 * which is part of the cache key
639 * will do a couple of variations & aspire to someone cleaning it up later
641 * @param string $name
642 * @param array $params
644 public static function flush($name, $params = array()) {
650 'labelColumnName' => 'label',
653 $params = array_merge($defaults, $params);
659 $params['condition'],
660 $params['labelColumnName'],
669 $params['condition'],
670 $params['labelColumnName'],
676 public static function flushAll() {
677 self
::$_values = array();
678 self
::$_cache = array();
679 CRM_Utils_Cache
::singleton()->flush();