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(
49 static function &valuesCommon(
50 $dao, $flip = FALSE, $grouping = FALSE,
51 $localize = FALSE, $valueColumnName = 'label'
53 self
::$_values = array();
55 while ($dao->fetch()) {
58 self
::$_values[$dao->value
] = $dao->grouping
;
61 self
::$_values[$dao->{$valueColumnName}] = $dao->value
;
66 self
::$_values[$dao->{$valueColumnName}] = $dao->grouping
;
69 self
::$_values[$dao->value
] = $dao->{$valueColumnName};
74 $i18n = CRM_Core_I18n
::singleton();
75 $i18n->localizeArray(self
::$_values);
77 return self
::$_values;
81 * This function retrieves all the values for the specific option group by name
82 * this is primarily used to create various html based form elements
83 * (radio, select, checkbox etc). OptionGroups for most cases have the
84 * 'label' in the label colum and the 'id' or 'name' in the value column
86 * @param $name string name of the option group
87 * @param $flip boolean results are return in id => label format if false
88 * if true, the results are reversed
89 * @param $grouping boolean if true, return the value in 'grouping' column
90 * @param $localize boolean if true, localize the results before returning
91 * @param $condition string add another condition to the sql query
92 * @param $labelColumnName string the column to use for 'label'
93 * @param $onlyActive boolean return only the action option values
94 * @param $fresh boolean ignore cache entries and go back to DB
95 * @param $keyColumnName string the column to use for 'key'
97 * @return array the values as specified by the above params
101 static function &values(
102 $name, $flip = FALSE, $grouping = FALSE,
103 $localize = FALSE, $condition = NULL,
104 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
106 $cache = CRM_Utils_Cache
::singleton();
107 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
110 // Fetch from static var
111 if (array_key_exists($cacheKey, self
::$_cache)) {
112 return self
::$_cache[$cacheKey];
114 // Fetch from main cache
115 $var = $cache->get($cacheKey);
122 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
123 FROM civicrm_option_value v,
124 civicrm_option_group g
125 WHERE v.option_group_id = g.id
127 AND g.is_active = 1 ";
130 $query .= " AND v.is_active = 1 ";
132 if (in_array($name, self
::$_domainIDGroups)) {
133 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
137 $query .= $condition;
140 $query .= " ORDER BY v.weight";
142 $p = array(1 => array($name, 'String'));
143 $dao = CRM_Core_DAO
::executeQuery($query, $p);
145 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
147 // call option value hook
148 CRM_Utils_Hook
::optionValues($var, $name);
150 self
::$_cache[$cacheKey] = $var;
151 $cache->set($cacheKey, $var);
157 * Counterpart to values() which removes the item from the cache
164 * @param $labelColumnName
166 * @param string $keyColumnName
168 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
169 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
170 $cache = CRM_Utils_Cache
::singleton();
171 $cache->delete($cacheKey);
172 unset(self
::$_cache[$cacheKey]);
175 protected static function createCacheKey() {
176 $cacheKey = "CRM_OG_" . serialize(func_get_args());
181 * This function retrieves all the values for the specific option group by id
182 * this is primarily used to create various html based form elements
183 * (radio, select, checkbox etc). OptionGroups for most cases have the
184 * 'label' in the label colum and the 'id' or 'name' in the value column
186 * @param $id integer id of the option group
187 * @param $flip boolean results are return in id => label format if false
188 * if true, the results are reversed
189 * @param $grouping boolean if true, return the value in 'grouping' column
190 * @param $localize boolean if true, localize the results before returning
191 * @param $labelColumnName string the column to use for 'label'
193 * @param bool $onlyActive
196 * @return array the values as specified by the above params
200 static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
201 $cacheKey = self
::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
203 $cache = CRM_Utils_Cache
::singleton();
205 $var = $cache->get($cacheKey);
211 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
212 FROM civicrm_option_value v,
213 civicrm_option_group g
214 WHERE v.option_group_id = g.id
219 $query .= " AND v.is_active = 1 ";
221 $query .= " ORDER BY v.weight, v.label";
223 $p = array(1 => array($id, 'Integer'));
224 $dao = CRM_Core_DAO
::executeQuery($query, $p);
226 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
227 $cache->set($cacheKey, $var);
233 * Function to lookup titles OR ids for a set of option_value populated fields. The retrieved value
234 * is assigned a new fieldname by id or id's by title
235 * (each within a specificied option_group)
237 * @param array $params Reference array of values submitted by the form. Based on
238 * $flip, creates new elements in $params for each field in
240 * If $flip = false, adds root field name => title
241 * If $flip = true, adds actual field name => id
243 * @param array $names Reference array of fieldnames we want transformed.
244 * Array key = 'postName' (field name submitted by form in $params).
245 * Array value = array(
246 'newName' => $newName, 'groupName' => $groupName).
249 * @param boolean $flip
256 static function lookupValues(&$params, &$names, $flip = FALSE) {
257 foreach ($names as $postName => $value) {
258 // See if $params field is in $names array (i.e. is a value that we need to lookup)
259 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
260 $postValues = array();
261 // params[$postName] may be a Ctrl+A separated value list
262 if (is_string($postalName) &&
263 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
265 // eliminate the ^A frm the beginning and end if present
266 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
267 $params[$postName] = substr($params[$postName], 1, -1);
269 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
271 elseif (is_array($postalName)) {
272 $postValues = $postalName;
275 foreach ($postValues as $postValue) {
281 $p = array(1 => array($postValue, 'String'));
282 $lookupBy = 'v.label= %1';
286 $p = array(1 => array($postValue, 'Integer'));
287 $lookupBy = 'v.value = %1';
291 $p[2] = array($value['groupName'], 'String');
294 FROM civicrm_option_value v,
295 civicrm_option_group g
296 WHERE v.option_group_id = g.id
300 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
301 $newValue = str_replace(',', '_', $newValue);
303 $params[$value['newName']] = implode(', ', $newValue);
308 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
309 if (empty($groupName) ||
316 SELECT v.label as label ,v.value as value
317 FROM civicrm_option_value v,
318 civicrm_option_group g
319 WHERE v.option_group_id = g.id
324 if ($onlyActiveValue) {
325 $query .= " AND v.is_active = 1 ";
327 $p = array(1 => array($groupName, 'String'),
328 2 => array($value, 'Integer'),
330 $dao = CRM_Core_DAO
::executeQuery($query, $p);
337 static function getValue($groupName,
339 $labelField = 'label',
340 $labelType = 'String',
341 $valueField = 'value'
348 SELECT v.label as label ,v.{$valueField} as value
349 FROM civicrm_option_value v,
350 civicrm_option_group g
351 WHERE v.option_group_id = g.id
355 AND v.$labelField = %2
358 $p = array(1 => array($groupName, 'String'),
359 2 => array($label, $labelType),
361 $dao = CRM_Core_DAO
::executeQuery($query, $p);
371 * Get option_value.value from default option_value row for an option group
373 * @param string $groupName the name of the option group
378 * @return string the value from the row where is_default = true
380 static function getDefaultValue($groupName) {
381 if (empty($groupName)) {
386 FROM civicrm_option_value v,
387 civicrm_option_group g
388 WHERE v.option_group_id = g.id
394 if (in_array($groupName, self
::$_domainIDGroups)) {
395 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
398 $p = array(1 => array($groupName, 'String'));
399 return CRM_Core_DAO
::singleValueQuery($query, $p);
403 * Creates a new option group with the passed in values
404 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
405 * not messed up. Currently deletes the old group
407 * @param string $groupName the name of the option group - make sure there is no conflict
408 * @param array $values the associative array that has information on the option values
409 * the keys of this array are:
410 * string 'title' (required)
411 * string 'value' (required)
412 * string 'name' (optional)
413 * string 'description' (optional)
414 * int 'weight' (optional) - the order in which the value are displayed
415 * bool 'is_default' (optional) - is this the default one to display when rendered in form
416 * bool 'is_active' (optional) - should this element be rendered
417 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
418 * @param null $groupTitle
420 * @internal param string $groupLabel - the optional label of the option group else set to group name
425 * @return int the option group ID
427 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
428 self
::deleteAssoc($groupName);
429 if (!empty($values)) {
430 $group = new CRM_Core_DAO_OptionGroup();
431 $group->name
= $groupName;
432 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
433 $group->is_reserved
= 1;
434 $group->is_active
= 1;
437 foreach ($values as $v) {
438 $value = new CRM_Core_DAO_OptionValue();
439 $value->option_group_id
= $group->id
;
440 $value->label
= $v['label'];
441 $value->value
= $v['value'];
442 $value->name
= CRM_Utils_Array
::value('name', $v);
443 $value->description
= CRM_Utils_Array
::value('description', $v);
444 $value->weight
= CRM_Utils_Array
::value('weight', $v);
445 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
446 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
449 if ($value->is_default
) {
450 $defaultID = $value->id
;
455 return $defaultID = 'null';
461 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
463 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
464 FROM civicrm_option_group g,
465 civicrm_option_value v
466 WHERE g.id = v.option_group_id
470 $params = array(1 => array($groupName, 'String'));
471 $dao = CRM_Core_DAO
::executeQuery($query, $params);
473 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
478 foreach ($fields as $field) {
479 $values[$field] = array();
484 while ($dao->fetch()) {
487 foreach ($fields as $field) {
488 $value[$field] = $dao->$field;
490 $values[$dao->amount_id
] = $value;
493 foreach ($fields as $field) {
494 $values[$field][$index] = $dao->$field;
501 static function deleteAssoc($groupName, $operator = "=") {
504 FROM civicrm_option_group g,
505 civicrm_option_value v
506 WHERE g.id = v.option_group_id
507 AND g.name {$operator} %1";
509 $params = array(1 => array($groupName, 'String'));
511 $dao = CRM_Core_DAO
::executeQuery($query, $params);
514 static function optionLabel($groupName, $value) {
517 FROM civicrm_option_group g,
518 civicrm_option_value v
519 WHERE g.id = v.option_group_id
522 $params = array(1 => array($groupName, 'String'),
523 2 => array($value, 'String'),
525 return CRM_Core_DAO
::singleValueQuery($query, $params);
528 static function getRowValues($groupName, $fieldValue, $field = 'name',
529 $fieldType = 'String', $active = TRUE
532 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
533 FROM civicrm_option_value v,
534 civicrm_option_group g
535 WHERE v.option_group_id = g.id
542 $query .= " AND v.is_active = 1";
545 $p = array(1 => array($groupName, 'String'),
546 2 => array($fieldValue, $fieldType),
548 $dao = CRM_Core_DAO
::executeQuery($query, $p);
553 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
554 $row[$fld] = $dao->$fld;
561 * Wrapper for calling values with fresh set to true to empty the given value
563 * Since there appears to be some inconsistency
564 * (@todo remove inconsistency) around the pseudoconstant operations
565 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
566 * which is part of the cache key
567 * will do a couple of variations & aspire to someone cleaning it up later
569 static function flush($name, $params = array()){
575 'labelColumnName' => 'label',
578 $params = array_merge($defaults, $params);
584 $params['condition'],
585 $params['labelColumnName'],
594 $params['condition'],
595 $params['labelColumnName'],
601 static function flushAll() {
602 self
::$_values = array();
603 self
::$_cache = array();
604 CRM_Utils_Cache
::singleton()->flush();