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
95 * @param $name string name of the option group
96 * @param $flip boolean results are return in id => label format if false
97 * if true, the results are reversed
98 * @param $grouping boolean if true, return the value in 'grouping' column
99 * @param $localize boolean if true, localize the results before returning
100 * @param $condition string add another condition to the sql query
101 * @param $labelColumnName string the column to use for 'label'
102 * @param $onlyActive boolean return only the action option values
103 * @param $fresh boolean ignore cache entries and go back to DB
104 * @param $keyColumnName string the column to use for 'key'
106 * @return array the values as specified by the above params
110 static function &values(
111 $name, $flip = FALSE, $grouping = FALSE,
112 $localize = FALSE, $condition = NULL,
113 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
115 $cache = CRM_Utils_Cache
::singleton();
116 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
119 // Fetch from static var
120 if (array_key_exists($cacheKey, self
::$_cache)) {
121 return self
::$_cache[$cacheKey];
123 // Fetch from main cache
124 $var = $cache->get($cacheKey);
131 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
132 FROM civicrm_option_value v,
133 civicrm_option_group g
134 WHERE v.option_group_id = g.id
136 AND g.is_active = 1 ";
139 $query .= " AND v.is_active = 1 ";
141 if (in_array($name, self
::$_domainIDGroups)) {
142 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
146 $query .= $condition;
149 $query .= " ORDER BY v.weight";
151 $p = array(1 => array($name, 'String'));
152 $dao = CRM_Core_DAO
::executeQuery($query, $p);
154 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
156 // call option value hook
157 CRM_Utils_Hook
::optionValues($var, $name);
159 self
::$_cache[$cacheKey] = $var;
160 $cache->set($cacheKey, $var);
166 * Counterpart to values() which removes the item from the cache
168 * @param string $name
173 * @param string $labelColumnName
175 * @param string $keyColumnName
177 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
178 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
179 $cache = CRM_Utils_Cache
::singleton();
180 $cache->delete($cacheKey);
181 unset(self
::$_cache[$cacheKey]);
187 protected static function createCacheKey() {
188 $cacheKey = "CRM_OG_" . serialize(func_get_args());
193 * This function retrieves all the values for the specific option group by id
194 * this is primarily used to create various html based form elements
195 * (radio, select, checkbox etc). OptionGroups for most cases have the
196 * 'label' in the label colum and the 'id' or 'name' in the value column
198 * @param $id integer id of the option group
199 * @param $flip boolean results are return in id => label format if false
200 * if true, the results are reversed
201 * @param $grouping boolean if true, return the value in 'grouping' column
202 * @param $localize boolean if true, localize the results before returning
203 * @param $labelColumnName string the column to use for 'label'
205 * @param bool $onlyActive
208 * @return array the values as specified by the above params
212 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
213 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
215 $cache = CRM_Utils_Cache
::singleton();
217 $var = $cache->get($cacheKey);
223 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
224 FROM civicrm_option_value v,
225 civicrm_option_group g
226 WHERE v.option_group_id = g.id
231 $query .= " AND v.is_active = 1 ";
233 $query .= " ORDER BY v.weight, v.label";
235 $p = array(1 => array($id, 'Integer'));
236 $dao = CRM_Core_DAO
::executeQuery($query, $p);
238 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
239 $cache->set($cacheKey, $var);
245 * Lookup titles OR ids for a set of option_value populated fields. The retrieved value
246 * is assigned a new fieldname by id or id's by title
247 * (each within a specificied option_group)
249 * @param array $params Reference array of values submitted by the form. Based on
250 * $flip, creates new elements in $params for each field in
252 * If $flip = false, adds root field name => title
253 * If $flip = true, adds actual field name => id
255 * @param array $names Reference array of fieldnames we want transformed.
256 * Array key = 'postName' (field name submitted by form in $params).
257 * Array value = array(
258 'newName' => $newName, 'groupName' => $groupName).
261 * @param boolean $flip
267 public static function lookupValues(&$params, &$names, $flip = FALSE) {
268 foreach ($names as $postName => $value) {
269 // See if $params field is in $names array (i.e. is a value that we need to lookup)
270 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
271 $postValues = array();
272 // params[$postName] may be a Ctrl+A separated value list
273 if (is_string($postalName) &&
274 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
276 // eliminate the ^A frm the beginning and end if present
277 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
278 $params[$postName] = substr($params[$postName], 1, -1);
280 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
282 elseif (is_array($postalName)) {
283 $postValues = $postalName;
286 foreach ($postValues as $postValue) {
292 $p = array(1 => array($postValue, 'String'));
293 $lookupBy = 'v.label= %1';
297 $p = array(1 => array($postValue, 'Integer'));
298 $lookupBy = 'v.value = %1';
302 $p[2] = array($value['groupName'], 'String');
305 FROM civicrm_option_value v,
306 civicrm_option_group g
307 WHERE v.option_group_id = g.id
311 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
312 $newValue = str_replace(',', '_', $newValue);
314 $params[$value['newName']] = implode(', ', $newValue);
320 * @param string $groupName
322 * @param bool $onlyActiveValue
326 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
327 if (empty($groupName) ||
334 SELECT v.label as label ,v.value as value
335 FROM civicrm_option_value v,
336 civicrm_option_group g
337 WHERE v.option_group_id = g.id
342 if ($onlyActiveValue) {
343 $query .= " AND v.is_active = 1 ";
345 $p = array(1 => array($groupName, 'String'),
346 2 => array($value, 'Integer'),
348 $dao = CRM_Core_DAO
::executeQuery($query, $p);
356 * @param string $groupName
358 * @param string $labelField
359 * @param string $labelType
360 * @param string $valueField
364 static function getValue($groupName,
366 $labelField = 'label',
367 $labelType = 'String',
368 $valueField = 'value'
375 SELECT v.label as label ,v.{$valueField} as value
376 FROM civicrm_option_value v,
377 civicrm_option_group g
378 WHERE v.option_group_id = g.id
382 AND v.$labelField = %2
385 $p = array(1 => array($groupName, 'String'),
386 2 => array($label, $labelType),
388 $dao = CRM_Core_DAO
::executeQuery($query, $p);
398 * Get option_value.value from default option_value row for an option group
400 * @param string $groupName the name of the option group
404 * @return string the value from the row where is_default = true
406 public static function getDefaultValue($groupName) {
407 if (empty($groupName)) {
412 FROM civicrm_option_value v,
413 civicrm_option_group g
414 WHERE v.option_group_id = g.id
420 if (in_array($groupName, self
::$_domainIDGroups)) {
421 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
424 $p = array(1 => array($groupName, 'String'));
425 return CRM_Core_DAO
::singleValueQuery($query, $p);
429 * Creates a new option group with the passed in values
430 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
431 * not messed up. Currently deletes the old group
433 * @param string $groupName the name of the option group - make sure there is no conflict
434 * @param array $values the associative array that has information on the option values
435 * the keys of this array are:
436 * string 'title' (required)
437 * string 'value' (required)
438 * string 'name' (optional)
439 * string 'description' (optional)
440 * int 'weight' (optional) - the order in which the value are displayed
441 * bool 'is_default' (optional) - is this the default one to display when rendered in form
442 * bool 'is_active' (optional) - should this element be rendered
443 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
444 * @param null $groupTitle the optional label of the option group else set to group name
448 * @return int the option group ID
450 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
451 self
::deleteAssoc($groupName);
452 if (!empty($values)) {
453 $group = new CRM_Core_DAO_OptionGroup();
454 $group->name
= $groupName;
455 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
456 $group->is_reserved
= 1;
457 $group->is_active
= 1;
460 foreach ($values as $v) {
461 $value = new CRM_Core_DAO_OptionValue();
462 $value->option_group_id
= $group->id
;
463 $value->label
= $v['label'];
464 $value->value
= $v['value'];
465 $value->name
= CRM_Utils_Array
::value('name', $v);
466 $value->description
= CRM_Utils_Array
::value('description', $v);
467 $value->weight
= CRM_Utils_Array
::value('weight', $v);
468 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
469 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
472 if ($value->is_default
) {
473 $defaultID = $value->id
;
478 return $defaultID = 'null';
485 * @param string $groupName
488 * @param string $field
490 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
492 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
493 FROM civicrm_option_group g,
494 civicrm_option_value v
495 WHERE g.id = v.option_group_id
499 $params = array(1 => array($groupName, 'String'));
500 $dao = CRM_Core_DAO
::executeQuery($query, $params);
502 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
507 foreach ($fields as $field) {
508 $values[$field] = array();
513 while ($dao->fetch()) {
516 foreach ($fields as $field) {
517 $value[$field] = $dao->$field;
519 $values[$dao->amount_id
] = $value;
522 foreach ($fields as $field) {
523 $values[$field][$index] = $dao->$field;
531 * @param string $groupName
532 * @param string $operator
534 public static function deleteAssoc($groupName, $operator = "=") {
537 FROM civicrm_option_group g,
538 civicrm_option_value v
539 WHERE g.id = v.option_group_id
540 AND g.name {$operator} %1";
542 $params = array(1 => array($groupName, 'String'));
544 $dao = CRM_Core_DAO
::executeQuery($query, $params);
548 * @param string $groupName
551 * @return null|string
553 public static function optionLabel($groupName, $value) {
556 FROM civicrm_option_group g,
557 civicrm_option_value v
558 WHERE g.id = v.option_group_id
561 $params = array(1 => array($groupName, 'String'),
562 2 => array($value, 'String'),
564 return CRM_Core_DAO
::singleValueQuery($query, $params);
568 * @param string $groupName
570 * @param string $field
571 * @param string $fieldType
572 * @param bool $active
576 static function getRowValues($groupName, $fieldValue, $field = 'name',
577 $fieldType = 'String', $active = TRUE
580 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
581 FROM civicrm_option_value v,
582 civicrm_option_group g
583 WHERE v.option_group_id = g.id
590 $query .= " AND v.is_active = 1";
593 $p = array(1 => array($groupName, 'String'),
594 2 => array($fieldValue, $fieldType),
596 $dao = CRM_Core_DAO
::executeQuery($query, $p);
601 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
602 $row[$fld] = $dao->$fld;
609 * Wrapper for calling values with fresh set to true to empty the given value
611 * Since there appears to be some inconsistency
612 * (@todo remove inconsistency) around the pseudoconstant operations
613 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
614 * which is part of the cache key
615 * will do a couple of variations & aspire to someone cleaning it up later
619 * @param array $params
621 public static function flush($name, $params = array()){
627 'labelColumnName' => 'label',
630 $params = array_merge($defaults, $params);
636 $params['condition'],
637 $params['labelColumnName'],
646 $params['condition'],
647 $params['labelColumnName'],
653 public static function flushAll() {
654 self
::$_values = array();
655 self
::$_cache = array();
656 CRM_Utils_Cache
::singleton()->flush();