fix CRM-12007
[civicrm-core.git] / CRM / Core / OptionGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Core_OptionGroup {
36 static $_values = array();
37 static $_cache = array();
38
39 /*
40 * $_domainIDGroups array maintains the list of option groups for whom
41 * domainID is to be considered.
42 *
43 */
44 static $_domainIDGroups = array(
45 'from_email_address',
46 'grant_type',
47 );
48
49 static function &valuesCommon(
50 $dao, $flip = FALSE, $grouping = FALSE,
51 $localize = FALSE, $valueColumnName = 'label'
52 ) {
53 self::$_values = array();
54
55 while ($dao->fetch()) {
56 if ($flip) {
57 if ($grouping) {
58 self::$_values[$dao->value] = $dao->grouping;
59 }
60 else {
61 self::$_values[$dao->{$valueColumnName}] = $dao->value;
62 }
63 }
64 else {
65 if ($grouping) {
66 self::$_values[$dao->{$valueColumnName}] = $dao->grouping;
67 }
68 else {
69 self::$_values[$dao->value] = $dao->{$valueColumnName};
70 }
71 }
72 }
73 if ($localize) {
74 $i18n = CRM_Core_I18n::singleton();
75 $i18n->localizeArray(self::$_values);
76 }
77 return self::$_values;
78 }
79
80 /**
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
85 *
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 *
96 * @return array the values as specified by the above params
97 * @static
98 * @void
99 */
100 static function &values(
101 $name, $flip = FALSE, $grouping = FALSE,
102 $localize = FALSE, $condition = NULL,
103 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE
104 ) {
105 $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive);
106
107 if (array_key_exists($cacheKey, self::$_cache) && !$fresh) {
108 return self::$_cache[$cacheKey];
109 }
110
111 $cache = CRM_Utils_Cache::singleton();
112 $var = $cache->get($cacheKey);
113 if ($var && !$fresh) {
114 return $var;
115 }
116
117 $query = "
118SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
119FROM civicrm_option_value v,
120 civicrm_option_group g
121WHERE v.option_group_id = g.id
122 AND g.name = %1
123 AND g.is_active = 1 ";
124
125 if ($onlyActive) {
126 $query .= " AND v.is_active = 1 ";
127 }
128 if (in_array($name, self::$_domainIDGroups)) {
129 $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
130 }
131
132 if ($condition) {
133 $query .= $condition;
134 }
135
136 $query .= " ORDER BY v.weight";
137
138 $p = array(1 => array($name, 'String'));
139 $dao = CRM_Core_DAO::executeQuery($query, $p);
140
141 $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
142
143 // call option value hook
144 CRM_Utils_Hook::optionValues($var, $name);
145
146 self::$_cache[$cacheKey] = $var;
147 $cache->set($cacheKey, $var);
148
149 return $var;
150 }
151
152 /**
153 * Counterpart to values() which removes the item from the cache
154 *
155 * @param $name
156 * @param $flip
157 * @param $grouping
158 * @param $localize
159 * @param $condition
160 * @param $labelColumnName
161 * @param $onlyActive
162 */
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]);
168 }
169
170 protected static function createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) {
171 $cacheKey = "CRM_OG_{$name}_{$flip}_{$grouping}_{$localize}_{$condition}_{$labelColumnName}_{$onlyActive}";
172 return $cacheKey;
173 }
174
175 /**
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
180 *
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'
187 *
188 * @return array the values as specified by the above params
189 * @static
190 * @void
191 */
192 static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label') {
193 $cacheKey = "CRM_OG_ID_{$id}_{$flip}_{$grouping}_{$localize}_{$labelColumnName}";
194
195 $cache = CRM_Utils_Cache::singleton();
196 $var = $cache->get($cacheKey);
197 if ($var) {
198 return $var;
199 }
200 $query = "
201SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
202FROM civicrm_option_value v,
203 civicrm_option_group g
204WHERE v.option_group_id = g.id
205 AND g.id = %1
206 AND v.is_active = 1
207 AND g.is_active = 1
208 ORDER BY v.weight, v.label;
209";
210 $p = array(1 => array($id, 'Integer'));
211 $dao = CRM_Core_DAO::executeQuery($query, $p);
212
213 $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
214 $cache->set($cacheKey, $var);
215
216 return $var;
217 }
218
219 /**
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)
223 *
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
226 * the $names array.
227 * If $flip = false, adds root field name => title
228 * If $flip = true, adds actual field name => id
229 *
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).
234 *
235 *
236 * @param boolean $flip
237 *
238 * @return void
239 *
240 * @access public
241 * @static
242 */
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
251 ) {
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);
255 }
256 $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]);
257 }
258 elseif (is_array($postalName)) {
259 $postValues = $postalName;
260 }
261 $newValue = array();
262 foreach ($postValues as $postValue) {
263 if (!$postValue) {
264 continue;
265 }
266
267 if ($flip) {
268 $p = array(1 => array($postValue, 'String'));
269 $lookupBy = 'v.label= %1';
270 $select = "v.value";
271 }
272 else {
273 $p = array(1 => array($postValue, 'Integer'));
274 $lookupBy = 'v.value = %1';
275 $select = "v.label";
276 }
277
278 $p[2] = array($value['groupName'], 'String');
279 $query = "
280 SELECT $select
281 FROM civicrm_option_value v,
282 civicrm_option_group g
283 WHERE v.option_group_id = g.id
284 AND g.name = %2
285 AND $lookupBy";
286
287 $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p);
288 $newValue = str_replace(',', '_', $newValue);
289 }
290 $params[$value['newName']] = implode(', ', $newValue);
291 }
292 }
293 }
294
295 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
296 if (empty($groupName) ||
297 empty($value)
298 ) {
299 return NULL;
300 }
301
302 $query = "
303SELECT v.label as label ,v.value as value
304FROM civicrm_option_value v,
305 civicrm_option_group g
306WHERE v.option_group_id = g.id
307 AND g.name = %1
308 AND g.is_active = 1
309 AND v.value = %2
310";
311 if ($onlyActiveValue) {
312 $query .= " AND v.is_active = 1 ";
313 }
314 $p = array(1 => array($groupName, 'String'),
315 2 => array($value, 'Integer'),
316 );
317 $dao = CRM_Core_DAO::executeQuery($query, $p);
318 if ($dao->fetch()) {
319 return $dao->label;
320 }
321 return NULL;
322 }
323
324 static function getValue($groupName,
325 $label,
326 $labelField = 'label',
327 $labelType = 'String',
328 $valueField = 'value'
329 ) {
330 if (empty($label)) {
331 return NULL;
332 }
333
334 $query = "
335SELECT v.label as label ,v.{$valueField} as value
336FROM civicrm_option_value v,
337 civicrm_option_group g
338WHERE v.option_group_id = g.id
339 AND g.name = %1
340 AND v.is_active = 1
341 AND g.is_active = 1
342 AND v.$labelField = %2
343";
344
345 $p = array(1 => array($groupName, 'String'),
346 2 => array($label, $labelType),
347 );
348 $dao = CRM_Core_DAO::executeQuery($query, $p);
349 if ($dao->fetch()) {
350 $dao->free();
351 return $dao->value;
352 }
353 $dao->free();
354 return NULL;
355 }
356
357 /**
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
361 *
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
374 *
375 * @access public
376 * @static
377 *
378 * @return int the option group ID
379 *
380 */
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;
389 $group->save();
390
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);
401 $value->save();
402
403 if ($value->is_default) {
404 $defaultID = $value->id;
405 }
406 }
407 }
408 else {
409 return $defaultID = 'null';
410 }
411
412 return $group->id;
413 }
414
415 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
416 $query = "
417SELECT 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
421 AND g.$field = %1
422ORDER BY v.weight
423";
424 $params = array(1 => array($groupName, 'String'));
425 $dao = CRM_Core_DAO::executeQuery($query, $params);
426
427 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
428 if ($flip) {
429 $values = array();
430 }
431 else {
432 foreach ($fields as $field) {
433 $values[$field] = array();
434 }
435 }
436 $index = 1;
437
438 while ($dao->fetch()) {
439 if ($flip) {
440 $value = array();
441 foreach ($fields as $field) {
442 $value[$field] = $dao->$field;
443 }
444 $values[$dao->amount_id] = $value;
445 }
446 else {
447 foreach ($fields as $field) {
448 $values[$field][$index] = $dao->$field;
449 }
450 $index++;
451 }
452 }
453 }
454
455 static function deleteAssoc($groupName, $operator = "=") {
456 $query = "
457DELETE g, v
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";
462
463 $params = array(1 => array($groupName, 'String'));
464
465 $dao = CRM_Core_DAO::executeQuery($query, $params);
466 }
467
468 static function optionLabel($groupName, $value) {
469 $query = "
470SELECT v.label
471 FROM civicrm_option_group g,
472 civicrm_option_value v
473 WHERE g.id = v.option_group_id
474 AND g.name = %1
475 AND v.value = %2";
476 $params = array(1 => array($groupName, 'String'),
477 2 => array($value, 'String'),
478 );
479 return CRM_Core_DAO::singleValueQuery($query, $params);
480 }
481
482 static function getRowValues($groupName, $fieldValue, $field = 'name',
483 $fieldType = 'String', $active = TRUE
484 ) {
485 $query = "
486SELECT v.id, v.label, v.value, v.name, v.weight, v.description
487FROM civicrm_option_value v,
488 civicrm_option_group g
489WHERE v.option_group_id = g.id
490 AND g.name = %1
491 AND g.is_active = 1
492 AND v.$field = %2
493";
494
495 if ($active) {
496 $query .= " AND v.is_active = 1";
497 }
498
499 $p = array(1 => array($groupName, 'String'),
500 2 => array($fieldValue, $fieldType),
501 );
502 $dao = CRM_Core_DAO::executeQuery($query, $p);
503 $row = array();
504
505 if ($dao->fetch()) {
506 foreach (array(
507 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
508 $row[$fld] = $dao->$fld;
509 }
510 }
511 return $row;
512 }
513
514 /*
515 * Wrapper for calling values with fresh set to true to empty the given value
516 *
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
522 */
523 static function flush($name, $params = array()){
524 $defaults = array(
525 'flip' => FALSE,
526 'grouping' => FALSE,
527 'localize' => FALSE,
528 'condition' => NULL,
529 'labelColumnName' => 'label',
530 );
531
532 $params = array_merge($defaults, $params);
533 self::flushValues(
534 $name,
535 $params['flip'],
536 $params['grouping'],
537 $params['localize'],
538 $params['condition'],
539 $params['labelColumnName'],
540 TRUE,
541 TRUE
542 );
543 self::flushValues(
544 $name,
545 $params['flip'],
546 $params['grouping'],
547 $params['localize'],
548 $params['condition'],
549 $params['labelColumnName'],
550 FALSE,
551 TRUE
552 );
553 }
554
555 static function flushAll() {
556 self::$_values = array();
557 self::$_cache = array();
558 }
559}
560