3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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(
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
173 * @param $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 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 * Function to 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
268 static function lookupValues(&$params, &$names, $flip = FALSE) {
269 foreach ($names as $postName => $value) {
270 // See if $params field is in $names array (i.e. is a value that we need to lookup)
271 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
272 $postValues = array();
273 // params[$postName] may be a Ctrl+A separated value list
274 if (is_string($postalName) &&
275 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
277 // eliminate the ^A frm the beginning and end if present
278 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
279 $params[$postName] = substr($params[$postName], 1, -1);
281 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
283 elseif (is_array($postalName)) {
284 $postValues = $postalName;
287 foreach ($postValues as $postValue) {
293 $p = array(1 => array($postValue, 'String'));
294 $lookupBy = 'v.label= %1';
298 $p = array(1 => array($postValue, 'Integer'));
299 $lookupBy = 'v.value = %1';
303 $p[2] = array($value['groupName'], 'String');
306 FROM civicrm_option_value v,
307 civicrm_option_group g
308 WHERE v.option_group_id = g.id
312 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
313 $newValue = str_replace(',', '_', $newValue);
315 $params[$value['newName']] = implode(', ', $newValue);
323 * @param bool $onlyActiveValue
327 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
328 if (empty($groupName) ||
335 SELECT v.label as label ,v.value as value
336 FROM civicrm_option_value v,
337 civicrm_option_group g
338 WHERE v.option_group_id = g.id
343 if ($onlyActiveValue) {
344 $query .= " AND v.is_active = 1 ";
346 $p = array(1 => array($groupName, 'String'),
347 2 => array($value, 'Integer'),
349 $dao = CRM_Core_DAO
::executeQuery($query, $p);
359 * @param string $labelField
360 * @param string $labelType
361 * @param string $valueField
365 static function getValue($groupName,
367 $labelField = 'label',
368 $labelType = 'String',
369 $valueField = 'value'
376 SELECT v.label as label ,v.{$valueField} as value
377 FROM civicrm_option_value v,
378 civicrm_option_group g
379 WHERE v.option_group_id = g.id
383 AND v.$labelField = %2
386 $p = array(1 => array($groupName, 'String'),
387 2 => array($label, $labelType),
389 $dao = CRM_Core_DAO
::executeQuery($query, $p);
399 * Get option_value.value from default option_value row for an option group
401 * @param string $groupName the name of the option group
406 * @return string the value from the row where is_default = true
408 static function getDefaultValue($groupName) {
409 if (empty($groupName)) {
414 FROM civicrm_option_value v,
415 civicrm_option_group g
416 WHERE v.option_group_id = g.id
422 if (in_array($groupName, self
::$_domainIDGroups)) {
423 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
426 $p = array(1 => array($groupName, 'String'));
427 return CRM_Core_DAO
::singleValueQuery($query, $p);
431 * Creates a new option group with the passed in values
432 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
433 * not messed up. Currently deletes the old group
435 * @param string $groupName the name of the option group - make sure there is no conflict
436 * @param array $values the associative array that has information on the option values
437 * the keys of this array are:
438 * string 'title' (required)
439 * string 'value' (required)
440 * string 'name' (optional)
441 * string 'description' (optional)
442 * int 'weight' (optional) - the order in which the value are displayed
443 * bool 'is_default' (optional) - is this the default one to display when rendered in form
444 * bool 'is_active' (optional) - should this element be rendered
445 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
446 * @param null $groupTitle
448 * @internal param string $groupLabel - the optional label of the option group else set to group name
453 * @return int the option group ID
455 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
456 self
::deleteAssoc($groupName);
457 if (!empty($values)) {
458 $group = new CRM_Core_DAO_OptionGroup();
459 $group->name
= $groupName;
460 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
461 $group->is_reserved
= 1;
462 $group->is_active
= 1;
465 foreach ($values as $v) {
466 $value = new CRM_Core_DAO_OptionValue();
467 $value->option_group_id
= $group->id
;
468 $value->label
= $v['label'];
469 $value->value
= $v['value'];
470 $value->name
= CRM_Utils_Array
::value('name', $v);
471 $value->description
= CRM_Utils_Array
::value('description', $v);
472 $value->weight
= CRM_Utils_Array
::value('weight', $v);
473 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
474 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
477 if ($value->is_default
) {
478 $defaultID = $value->id
;
483 return $defaultID = 'null';
493 * @param string $field
495 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
497 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
498 FROM civicrm_option_group g,
499 civicrm_option_value v
500 WHERE g.id = v.option_group_id
504 $params = array(1 => array($groupName, 'String'));
505 $dao = CRM_Core_DAO
::executeQuery($query, $params);
507 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
512 foreach ($fields as $field) {
513 $values[$field] = array();
518 while ($dao->fetch()) {
521 foreach ($fields as $field) {
522 $value[$field] = $dao->$field;
524 $values[$dao->amount_id
] = $value;
527 foreach ($fields as $field) {
528 $values[$field][$index] = $dao->$field;
537 * @param string $operator
539 static function deleteAssoc($groupName, $operator = "=") {
542 FROM civicrm_option_group g,
543 civicrm_option_value v
544 WHERE g.id = v.option_group_id
545 AND g.name {$operator} %1";
547 $params = array(1 => array($groupName, 'String'));
549 $dao = CRM_Core_DAO
::executeQuery($query, $params);
556 * @return null|string
558 static function optionLabel($groupName, $value) {
561 FROM civicrm_option_group g,
562 civicrm_option_value v
563 WHERE g.id = v.option_group_id
566 $params = array(1 => array($groupName, 'String'),
567 2 => array($value, 'String'),
569 return CRM_Core_DAO
::singleValueQuery($query, $params);
575 * @param string $field
576 * @param string $fieldType
577 * @param bool $active
581 static function getRowValues($groupName, $fieldValue, $field = 'name',
582 $fieldType = 'String', $active = TRUE
585 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
586 FROM civicrm_option_value v,
587 civicrm_option_group g
588 WHERE v.option_group_id = g.id
595 $query .= " AND v.is_active = 1";
598 $p = array(1 => array($groupName, 'String'),
599 2 => array($fieldValue, $fieldType),
601 $dao = CRM_Core_DAO
::executeQuery($query, $p);
606 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
607 $row[$fld] = $dao->$fld;
614 * Wrapper for calling values with fresh set to true to empty the given value
616 * Since there appears to be some inconsistency
617 * (@todo remove inconsistency) around the pseudoconstant operations
618 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
619 * which is part of the cache key
620 * will do a couple of variations & aspire to someone cleaning it up later
624 * @param array $params
626 static function flush($name, $params = array()){
632 'labelColumnName' => 'label',
635 $params = array_merge($defaults, $params);
641 $params['condition'],
642 $params['labelColumnName'],
651 $params['condition'],
652 $params['labelColumnName'],
658 static function flushAll() {
659 self
::$_values = array();
660 self
::$_cache = array();
661 CRM_Utils_Cache
::singleton()->flush();