3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
96 * @return array the values as specified by the above params
100 static function &values(
101 $name, $flip = FALSE, $grouping = FALSE,
102 $localize = FALSE, $condition = NULL,
103 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE
105 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive);
107 if (array_key_exists($cacheKey, self
::$_cache) && !$fresh) {
108 return self
::$_cache[$cacheKey];
111 $cache = CRM_Utils_Cache
::singleton();
112 $var = $cache->get($cacheKey);
113 if ($var && !$fresh) {
118 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
119 FROM civicrm_option_value v,
120 civicrm_option_group g
121 WHERE v.option_group_id = g.id
123 AND g.is_active = 1 ";
126 $query .= " AND v.is_active = 1 ";
128 if (in_array($name, self
::$_domainIDGroups)) {
129 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
133 $query .= $condition;
136 $query .= " ORDER BY v.weight";
138 $p = array(1 => array($name, 'String'));
139 $dao = CRM_Core_DAO
::executeQuery($query, $p);
141 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
143 // call option value hook
144 CRM_Utils_Hook
::optionValues($var, $name);
146 self
::$_cache[$cacheKey] = $var;
147 $cache->set($cacheKey, $var);
153 * Counterpart to values() which removes the item from the cache
160 * @param $labelColumnName
163 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) {
164 $cacheKey = self
::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive);
165 $cache = CRM_Utils_Cache
::singleton();
166 $cache->delete($cacheKey);
167 unset(self
::$_cache[$cacheKey]);
170 protected static function createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) {
171 $cacheKey = "CRM_OG_{$name}_{$flip}_{$grouping}_{$localize}_{$condition}_{$labelColumnName}_{$onlyActive}";
176 * This function retrieves all the values for the specific option group by id
177 * this is primarily used to create various html based form elements
178 * (radio, select, checkbox etc). OptionGroups for most cases have the
179 * 'label' in the label colum and the 'id' or 'name' in the value column
181 * @param $id integer id of the option group
182 * @param $flip boolean results are return in id => label format if false
183 * if true, the results are reversed
184 * @param $grouping boolean if true, return the value in 'grouping' column
185 * @param $localize boolean if true, localize the results before returning
186 * @param $labelColumnName string the column to use for 'label'
188 * @return array the values as specified by the above params
192 static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label') {
193 $cacheKey = "CRM_OG_ID_{$id}_{$flip}_{$grouping}_{$localize}_{$labelColumnName}";
195 $cache = CRM_Utils_Cache
::singleton();
196 $var = $cache->get($cacheKey);
201 SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
202 FROM civicrm_option_value v,
203 civicrm_option_group g
204 WHERE v.option_group_id = g.id
208 ORDER BY v.weight, v.label;
210 $p = array(1 => array($id, 'Integer'));
211 $dao = CRM_Core_DAO
::executeQuery($query, $p);
213 $var = self
::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
214 $cache->set($cacheKey, $var);
220 * Function to lookup titles OR ids for a set of option_value populated fields. The retrieved value
221 * is assigned a new fieldname by id or id's by title
222 * (each within a specificied option_group)
224 * @param array $params Reference array of values submitted by the form. Based on
225 * $flip, creates new elements in $params for each field in
227 * If $flip = false, adds root field name => title
228 * If $flip = true, adds actual field name => id
230 * @param array $names Reference array of fieldnames we want transformed.
231 * Array key = 'postName' (field name submitted by form in $params).
232 * Array value = array(
233 'newName' => $newName, 'groupName' => $groupName).
236 * @param boolean $flip
243 static function lookupValues(&$params, &$names, $flip = FALSE) {
244 foreach ($names as $postName => $value) {
245 // See if $params field is in $names array (i.e. is a value that we need to lookup)
246 if ($postalName = CRM_Utils_Array
::value($postName, $params)) {
247 $postValues = array();
248 // params[$postName] may be a Ctrl+A separated value list
249 if (is_string($postalName) &&
250 strpos($postalName, CRM_Core_DAO
::VALUE_SEPARATOR
) == FALSE
252 // eliminate the ^A frm the beginning and end if present
253 if (substr($postalName, 0, 1) == CRM_Core_DAO
::VALUE_SEPARATOR
) {
254 $params[$postName] = substr($params[$postName], 1, -1);
256 $postValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $params[$postName]);
258 elseif (is_array($postalName)) {
259 $postValues = $postalName;
262 foreach ($postValues as $postValue) {
268 $p = array(1 => array($postValue, 'String'));
269 $lookupBy = 'v.label= %1';
273 $p = array(1 => array($postValue, 'Integer'));
274 $lookupBy = 'v.value = %1';
278 $p[2] = array($value['groupName'], 'String');
281 FROM civicrm_option_value v,
282 civicrm_option_group g
283 WHERE v.option_group_id = g.id
287 $newValue[] = CRM_Core_DAO
::singleValueQuery($query, $p);
288 $newValue = str_replace(',', '_', $newValue);
290 $params[$value['newName']] = implode(', ', $newValue);
295 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
296 if (empty($groupName) ||
303 SELECT v.label as label ,v.value as value
304 FROM civicrm_option_value v,
305 civicrm_option_group g
306 WHERE v.option_group_id = g.id
311 if ($onlyActiveValue) {
312 $query .= " AND v.is_active = 1 ";
314 $p = array(1 => array($groupName, 'String'),
315 2 => array($value, 'Integer'),
317 $dao = CRM_Core_DAO
::executeQuery($query, $p);
324 static function getValue($groupName,
326 $labelField = 'label',
327 $labelType = 'String',
328 $valueField = 'value'
335 SELECT v.label as label ,v.{$valueField} as value
336 FROM civicrm_option_value v,
337 civicrm_option_group g
338 WHERE v.option_group_id = g.id
342 AND v.$labelField = %2
345 $p = array(1 => array($groupName, 'String'),
346 2 => array($label, $labelType),
348 $dao = CRM_Core_DAO
::executeQuery($query, $p);
358 * Get option_value.value from default option_value row for an option group
360 * @param string $groupName the name of the option group
365 * @return string the value from the row where is_default = true
367 static function getDefaultValue($groupName) {
368 if (empty($groupName)) {
373 FROM civicrm_option_value v,
374 civicrm_option_group g
375 WHERE v.option_group_id = g.id
381 if (in_array($groupName, self
::$_domainIDGroups)) {
382 $query .= " AND v.domain_id = " . CRM_Core_Config
::domainID();
385 $p = array(1 => array($groupName, 'String'));
386 return CRM_Core_DAO
::singleValueQuery($query, $p);
390 * Creates a new option group with the passed in values
391 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
392 * not messed up. Currently deletes the old group
394 * @param string $groupName the name of the option group - make sure there is no conflict
395 * @param array $values the associative array that has information on the option values
396 * the keys of this array are:
397 * string 'title' (required)
398 * string 'value' (required)
399 * string 'name' (optional)
400 * string 'description' (optional)
401 * int 'weight' (optional) - the order in which the value are displayed
402 * bool 'is_default' (optional) - is this the default one to display when rendered in form
403 * bool 'is_active' (optional) - should this element be rendered
404 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
405 * @param string $groupLabel - the optional label of the option group else set to group name
410 * @return int the option group ID
413 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
414 self
::deleteAssoc($groupName);
415 if (!empty($values)) {
416 $group = new CRM_Core_DAO_OptionGroup();
417 $group->name
= $groupName;
418 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
419 $group->is_reserved
= 1;
420 $group->is_active
= 1;
423 foreach ($values as $v) {
424 $value = new CRM_Core_DAO_OptionValue();
425 $value->option_group_id
= $group->id
;
426 $value->label
= $v['label'];
427 $value->value
= $v['value'];
428 $value->name
= CRM_Utils_Array
::value('name', $v);
429 $value->description
= CRM_Utils_Array
::value('description', $v);
430 $value->weight
= CRM_Utils_Array
::value('weight', $v);
431 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
432 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
435 if ($value->is_default
) {
436 $defaultID = $value->id
;
441 return $defaultID = 'null';
447 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
449 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
450 FROM civicrm_option_group g,
451 civicrm_option_value v
452 WHERE g.id = v.option_group_id
456 $params = array(1 => array($groupName, 'String'));
457 $dao = CRM_Core_DAO
::executeQuery($query, $params);
459 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
464 foreach ($fields as $field) {
465 $values[$field] = array();
470 while ($dao->fetch()) {
473 foreach ($fields as $field) {
474 $value[$field] = $dao->$field;
476 $values[$dao->amount_id
] = $value;
479 foreach ($fields as $field) {
480 $values[$field][$index] = $dao->$field;
487 static function deleteAssoc($groupName, $operator = "=") {
490 FROM civicrm_option_group g,
491 civicrm_option_value v
492 WHERE g.id = v.option_group_id
493 AND g.name {$operator} %1";
495 $params = array(1 => array($groupName, 'String'));
497 $dao = CRM_Core_DAO
::executeQuery($query, $params);
500 static function optionLabel($groupName, $value) {
503 FROM civicrm_option_group g,
504 civicrm_option_value v
505 WHERE g.id = v.option_group_id
508 $params = array(1 => array($groupName, 'String'),
509 2 => array($value, 'String'),
511 return CRM_Core_DAO
::singleValueQuery($query, $params);
514 static function getRowValues($groupName, $fieldValue, $field = 'name',
515 $fieldType = 'String', $active = TRUE
518 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
519 FROM civicrm_option_value v,
520 civicrm_option_group g
521 WHERE v.option_group_id = g.id
528 $query .= " AND v.is_active = 1";
531 $p = array(1 => array($groupName, 'String'),
532 2 => array($fieldValue, $fieldType),
534 $dao = CRM_Core_DAO
::executeQuery($query, $p);
539 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
540 $row[$fld] = $dao->$fld;
547 * Wrapper for calling values with fresh set to true to empty the given value
549 * Since there appears to be some inconsistency
550 * (@todo remove inconsistency) around the pseudoconstant operations
551 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
552 * which is part of the cache key
553 * will do a couple of variations & aspire to someone cleaning it up later
555 static function flush($name, $params = array()){
561 'labelColumnName' => 'label',
564 $params = array_merge($defaults, $params);
570 $params['condition'],
571 $params['labelColumnName'],
580 $params['condition'],
581 $params['labelColumnName'],
587 static function flushAll() {
588 self
::$_values = array();
589 self
::$_cache = array();