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 * Creates a new option group with the passed in values
359 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
360 * not messed up. Currently deletes the old group
362 * @param string $groupName the name of the option group - make sure there is no conflict
363 * @param array $values the associative array that has information on the option values
364 * the keys of this array are:
365 * string 'title' (required)
366 * string 'value' (required)
367 * string 'name' (optional)
368 * string 'description' (optional)
369 * int 'weight' (optional) - the order in which the value are displayed
370 * bool 'is_default' (optional) - is this the default one to display when rendered in form
371 * bool 'is_active' (optional) - should this element be rendered
372 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
373 * @param string $groupLabel - the optional label of the option group else set to group name
378 * @return int the option group ID
381 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
382 self
::deleteAssoc($groupName);
383 if (!empty($values)) {
384 $group = new CRM_Core_DAO_OptionGroup();
385 $group->name
= $groupName;
386 $group->title
= empty($groupTitle) ?
$groupName : $groupTitle;
387 $group->is_reserved
= 1;
388 $group->is_active
= 1;
391 foreach ($values as $v) {
392 $value = new CRM_Core_DAO_OptionValue();
393 $value->option_group_id
= $group->id
;
394 $value->label
= $v['label'];
395 $value->value
= $v['value'];
396 $value->name
= CRM_Utils_Array
::value('name', $v);
397 $value->description
= CRM_Utils_Array
::value('description', $v);
398 $value->weight
= CRM_Utils_Array
::value('weight', $v);
399 $value->is_default
= CRM_Utils_Array
::value('is_default', $v);
400 $value->is_active
= CRM_Utils_Array
::value('is_active', $v);
403 if ($value->is_default
) {
404 $defaultID = $value->id
;
409 return $defaultID = 'null';
415 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
417 SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
418 FROM civicrm_option_group g,
419 civicrm_option_value v
420 WHERE g.id = v.option_group_id
424 $params = array(1 => array($groupName, 'String'));
425 $dao = CRM_Core_DAO
::executeQuery($query, $params);
427 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
432 foreach ($fields as $field) {
433 $values[$field] = array();
438 while ($dao->fetch()) {
441 foreach ($fields as $field) {
442 $value[$field] = $dao->$field;
444 $values[$dao->amount_id
] = $value;
447 foreach ($fields as $field) {
448 $values[$field][$index] = $dao->$field;
455 static function deleteAssoc($groupName, $operator = "=") {
458 FROM civicrm_option_group g,
459 civicrm_option_value v
460 WHERE g.id = v.option_group_id
461 AND g.name {$operator} %1";
463 $params = array(1 => array($groupName, 'String'));
465 $dao = CRM_Core_DAO
::executeQuery($query, $params);
468 static function optionLabel($groupName, $value) {
471 FROM civicrm_option_group g,
472 civicrm_option_value v
473 WHERE g.id = v.option_group_id
476 $params = array(1 => array($groupName, 'String'),
477 2 => array($value, 'String'),
479 return CRM_Core_DAO
::singleValueQuery($query, $params);
482 static function getRowValues($groupName, $fieldValue, $field = 'name',
483 $fieldType = 'String', $active = TRUE
486 SELECT v.id, v.label, v.value, v.name, v.weight, v.description
487 FROM civicrm_option_value v,
488 civicrm_option_group g
489 WHERE v.option_group_id = g.id
496 $query .= " AND v.is_active = 1";
499 $p = array(1 => array($groupName, 'String'),
500 2 => array($fieldValue, $fieldType),
502 $dao = CRM_Core_DAO
::executeQuery($query, $p);
507 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
508 $row[$fld] = $dao->$fld;
515 * Wrapper for calling values with fresh set to true to empty the given value
517 * Since there appears to be some inconsistency
518 * (@todo remove inconsistency) around the pseudoconstant operations
519 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
520 * which is part of the cache key
521 * will do a couple of variations & aspire to someone cleaning it up later
523 static function flush($name, $params = array()){
529 'labelColumnName' => 'label',
532 $params = array_merge($defaults, $params);
538 $params['condition'],
539 $params['labelColumnName'],
548 $params['condition'],
549 $params['labelColumnName'],
555 static function flushAll() {
556 self
::$_values = array();
557 self
::$_cache = array();