Fix api_v3_LocBlockTest
[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
3f3a3ba0 95 * @param $keyColumnName string the column to use for 'key'
6a488035
TO
96 *
97 * @return array the values as specified by the above params
98 * @static
99 * @void
100 */
101 static function &values(
102 $name, $flip = FALSE, $grouping = FALSE,
103 $localize = FALSE, $condition = NULL,
c0c9cd82 104 $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value'
6a488035 105 ) {
be80e977 106 $cache = CRM_Utils_Cache::singleton();
c0c9cd82 107 $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
6a488035 108
c0c9cd82
CW
109 if (!$fresh) {
110 // Fetch from static var
111 if (array_key_exists($cacheKey, self::$_cache)) {
112 return self::$_cache[$cacheKey];
113 }
114 // Fetch from main cache
c0c9cd82
CW
115 $var = $cache->get($cacheKey);
116 if ($var) {
117 return $var;
118 }
6a488035
TO
119 }
120
121 $query = "
c0c9cd82 122SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping
6a488035
TO
123FROM civicrm_option_value v,
124 civicrm_option_group g
125WHERE v.option_group_id = g.id
126 AND g.name = %1
127 AND g.is_active = 1 ";
128
129 if ($onlyActive) {
130 $query .= " AND v.is_active = 1 ";
131 }
132 if (in_array($name, self::$_domainIDGroups)) {
133 $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
134 }
135
136 if ($condition) {
137 $query .= $condition;
138 }
139
c0c9cd82 140 $query .= " ORDER BY v.weight";
6a488035
TO
141
142 $p = array(1 => array($name, 'String'));
143 $dao = CRM_Core_DAO::executeQuery($query, $p);
144
145 $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
146
147 // call option value hook
148 CRM_Utils_Hook::optionValues($var, $name);
149
150 self::$_cache[$cacheKey] = $var;
151 $cache->set($cacheKey, $var);
152
153 return $var;
154 }
155
156 /**
157 * Counterpart to values() which removes the item from the cache
158 *
159 * @param $name
160 * @param $flip
161 * @param $grouping
162 * @param $localize
163 * @param $condition
164 * @param $labelColumnName
165 * @param $onlyActive
166 */
c0c9cd82
CW
167 protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
168 $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
6a488035
TO
169 $cache = CRM_Utils_Cache::singleton();
170 $cache->delete($cacheKey);
171 unset(self::$_cache[$cacheKey]);
172 }
173
c0c9cd82
CW
174 protected static function createCacheKey() {
175 $cacheKey = "CRM_OG_" . serialize(func_get_args());
6a488035
TO
176 return $cacheKey;
177 }
178
179 /**
180 * This function retrieves all the values for the specific option group by id
181 * this is primarily used to create various html based form elements
182 * (radio, select, checkbox etc). OptionGroups for most cases have the
183 * 'label' in the label colum and the 'id' or 'name' in the value column
184 *
185 * @param $id integer id of the option group
186 * @param $flip boolean results are return in id => label format if false
187 * if true, the results are reversed
188 * @param $grouping boolean if true, return the value in 'grouping' column
189 * @param $localize boolean if true, localize the results before returning
190 * @param $labelColumnName string the column to use for 'label'
191 *
192 * @return array the values as specified by the above params
193 * @static
194 * @void
195 */
786ad6e1
CW
196 static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
197 $cacheKey = self::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
6a488035
TO
198
199 $cache = CRM_Utils_Cache::singleton();
786ad6e1
CW
200 if (!$fresh) {
201 $var = $cache->get($cacheKey);
202 if ($var) {
203 return $var;
204 }
6a488035
TO
205 }
206 $query = "
207SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
208FROM civicrm_option_value v,
209 civicrm_option_group g
210WHERE v.option_group_id = g.id
211 AND g.id = %1
6a488035 212 AND g.is_active = 1
6a488035 213";
786ad6e1
CW
214 if ($onlyActive) {
215 $query .= " AND v.is_active = 1 ";
216 }
217 $query .= " ORDER BY v.weight, v.label";
218
6a488035
TO
219 $p = array(1 => array($id, 'Integer'));
220 $dao = CRM_Core_DAO::executeQuery($query, $p);
221
222 $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
223 $cache->set($cacheKey, $var);
224
225 return $var;
226 }
227
228 /**
229 * Function to lookup titles OR ids for a set of option_value populated fields. The retrieved value
230 * is assigned a new fieldname by id or id's by title
231 * (each within a specificied option_group)
232 *
233 * @param array $params Reference array of values submitted by the form. Based on
234 * $flip, creates new elements in $params for each field in
235 * the $names array.
236 * If $flip = false, adds root field name => title
237 * If $flip = true, adds actual field name => id
238 *
239 * @param array $names Reference array of fieldnames we want transformed.
240 * Array key = 'postName' (field name submitted by form in $params).
241 * Array value = array(
242 'newName' => $newName, 'groupName' => $groupName).
243 *
244 *
245 * @param boolean $flip
246 *
247 * @return void
248 *
249 * @access public
250 * @static
251 */
252 static function lookupValues(&$params, &$names, $flip = FALSE) {
253 foreach ($names as $postName => $value) {
254 // See if $params field is in $names array (i.e. is a value that we need to lookup)
255 if ($postalName = CRM_Utils_Array::value($postName, $params)) {
256 $postValues = array();
257 // params[$postName] may be a Ctrl+A separated value list
258 if (is_string($postalName) &&
259 strpos($postalName, CRM_Core_DAO::VALUE_SEPARATOR) == FALSE
260 ) {
261 // eliminate the ^A frm the beginning and end if present
262 if (substr($postalName, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) {
263 $params[$postName] = substr($params[$postName], 1, -1);
264 }
265 $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]);
266 }
267 elseif (is_array($postalName)) {
268 $postValues = $postalName;
269 }
270 $newValue = array();
271 foreach ($postValues as $postValue) {
272 if (!$postValue) {
273 continue;
274 }
275
276 if ($flip) {
277 $p = array(1 => array($postValue, 'String'));
278 $lookupBy = 'v.label= %1';
279 $select = "v.value";
280 }
281 else {
282 $p = array(1 => array($postValue, 'Integer'));
283 $lookupBy = 'v.value = %1';
284 $select = "v.label";
285 }
286
287 $p[2] = array($value['groupName'], 'String');
288 $query = "
289 SELECT $select
290 FROM civicrm_option_value v,
291 civicrm_option_group g
292 WHERE v.option_group_id = g.id
293 AND g.name = %2
294 AND $lookupBy";
295
296 $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p);
297 $newValue = str_replace(',', '_', $newValue);
298 }
299 $params[$value['newName']] = implode(', ', $newValue);
300 }
301 }
302 }
303
304 static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
305 if (empty($groupName) ||
306 empty($value)
307 ) {
308 return NULL;
309 }
310
311 $query = "
312SELECT v.label as label ,v.value as value
313FROM civicrm_option_value v,
314 civicrm_option_group g
315WHERE v.option_group_id = g.id
316 AND g.name = %1
317 AND g.is_active = 1
318 AND v.value = %2
319";
320 if ($onlyActiveValue) {
321 $query .= " AND v.is_active = 1 ";
322 }
323 $p = array(1 => array($groupName, 'String'),
324 2 => array($value, 'Integer'),
325 );
326 $dao = CRM_Core_DAO::executeQuery($query, $p);
327 if ($dao->fetch()) {
328 return $dao->label;
329 }
330 return NULL;
331 }
332
333 static function getValue($groupName,
334 $label,
335 $labelField = 'label',
336 $labelType = 'String',
337 $valueField = 'value'
338 ) {
339 if (empty($label)) {
340 return NULL;
341 }
342
343 $query = "
344SELECT v.label as label ,v.{$valueField} as value
345FROM civicrm_option_value v,
346 civicrm_option_group g
347WHERE v.option_group_id = g.id
348 AND g.name = %1
349 AND v.is_active = 1
350 AND g.is_active = 1
351 AND v.$labelField = %2
352";
353
354 $p = array(1 => array($groupName, 'String'),
355 2 => array($label, $labelType),
356 );
357 $dao = CRM_Core_DAO::executeQuery($query, $p);
358 if ($dao->fetch()) {
359 $dao->free();
360 return $dao->value;
361 }
362 $dao->free();
363 return NULL;
364 }
365
343d84fa
DG
366 /**
367 * Get option_value.value from default option_value row for an option group
368 *
369 * @param string $groupName the name of the option group
370 *
371 * @access public
372 * @static
373 *
374 * @return string the value from the row where is_default = true
375 */
376 static function getDefaultValue($groupName) {
377 if (empty($groupName)) {
378 return NULL;
379 }
380 $query = "
381SELECT v.value
382FROM civicrm_option_value v,
383 civicrm_option_group g
384WHERE v.option_group_id = g.id
385 AND g.name = %1
386 AND v.is_active = 1
387 AND g.is_active = 1
388 AND v.is_default = 1
389";
390 if (in_array($groupName, self::$_domainIDGroups)) {
391 $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
392 }
393
394 $p = array(1 => array($groupName, 'String'));
395 return CRM_Core_DAO::singleValueQuery($query, $p);
396 }
397
6a488035
TO
398 /**
399 * Creates a new option group with the passed in values
400 * @TODO: Should update the group if it already exists intelligently, so multi-lingual is
401 * not messed up. Currently deletes the old group
402 *
403 * @param string $groupName the name of the option group - make sure there is no conflict
404 * @param array $values the associative array that has information on the option values
405 * the keys of this array are:
406 * string 'title' (required)
407 * string 'value' (required)
408 * string 'name' (optional)
409 * string 'description' (optional)
410 * int 'weight' (optional) - the order in which the value are displayed
411 * bool 'is_default' (optional) - is this the default one to display when rendered in form
412 * bool 'is_active' (optional) - should this element be rendered
413 * @param int $defaultID (reference) - the option value ID of the default element (if set) is returned else 'null'
414 * @param string $groupLabel - the optional label of the option group else set to group name
415 *
416 * @access public
417 * @static
418 *
419 * @return int the option group ID
420 *
421 */
422 static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
423 self::deleteAssoc($groupName);
424 if (!empty($values)) {
425 $group = new CRM_Core_DAO_OptionGroup();
426 $group->name = $groupName;
427 $group->title = empty($groupTitle) ? $groupName : $groupTitle;
428 $group->is_reserved = 1;
429 $group->is_active = 1;
430 $group->save();
431
432 foreach ($values as $v) {
433 $value = new CRM_Core_DAO_OptionValue();
434 $value->option_group_id = $group->id;
435 $value->label = $v['label'];
436 $value->value = $v['value'];
437 $value->name = CRM_Utils_Array::value('name', $v);
438 $value->description = CRM_Utils_Array::value('description', $v);
439 $value->weight = CRM_Utils_Array::value('weight', $v);
440 $value->is_default = CRM_Utils_Array::value('is_default', $v);
441 $value->is_active = CRM_Utils_Array::value('is_active', $v);
442 $value->save();
443
444 if ($value->is_default) {
445 $defaultID = $value->id;
446 }
447 }
448 }
449 else {
450 return $defaultID = 'null';
451 }
452
453 return $group->id;
454 }
455
456 static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
457 $query = "
458SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
459 FROM civicrm_option_group g,
460 civicrm_option_value v
461 WHERE g.id = v.option_group_id
462 AND g.$field = %1
463ORDER BY v.weight
464";
465 $params = array(1 => array($groupName, 'String'));
466 $dao = CRM_Core_DAO::executeQuery($query, $params);
467
468 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
469 if ($flip) {
470 $values = array();
471 }
472 else {
473 foreach ($fields as $field) {
474 $values[$field] = array();
475 }
476 }
477 $index = 1;
478
479 while ($dao->fetch()) {
480 if ($flip) {
481 $value = array();
482 foreach ($fields as $field) {
483 $value[$field] = $dao->$field;
484 }
485 $values[$dao->amount_id] = $value;
486 }
487 else {
488 foreach ($fields as $field) {
489 $values[$field][$index] = $dao->$field;
490 }
491 $index++;
492 }
493 }
494 }
495
496 static function deleteAssoc($groupName, $operator = "=") {
497 $query = "
498DELETE g, v
499 FROM civicrm_option_group g,
500 civicrm_option_value v
501 WHERE g.id = v.option_group_id
502 AND g.name {$operator} %1";
503
504 $params = array(1 => array($groupName, 'String'));
505
506 $dao = CRM_Core_DAO::executeQuery($query, $params);
507 }
508
509 static function optionLabel($groupName, $value) {
510 $query = "
511SELECT v.label
512 FROM civicrm_option_group g,
513 civicrm_option_value v
514 WHERE g.id = v.option_group_id
515 AND g.name = %1
516 AND v.value = %2";
517 $params = array(1 => array($groupName, 'String'),
518 2 => array($value, 'String'),
519 );
520 return CRM_Core_DAO::singleValueQuery($query, $params);
521 }
522
523 static function getRowValues($groupName, $fieldValue, $field = 'name',
524 $fieldType = 'String', $active = TRUE
525 ) {
526 $query = "
527SELECT v.id, v.label, v.value, v.name, v.weight, v.description
528FROM civicrm_option_value v,
529 civicrm_option_group g
530WHERE v.option_group_id = g.id
531 AND g.name = %1
532 AND g.is_active = 1
533 AND v.$field = %2
534";
535
536 if ($active) {
537 $query .= " AND v.is_active = 1";
538 }
539
540 $p = array(1 => array($groupName, 'String'),
541 2 => array($fieldValue, $fieldType),
542 );
543 $dao = CRM_Core_DAO::executeQuery($query, $p);
544 $row = array();
545
546 if ($dao->fetch()) {
547 foreach (array(
548 'id', 'name', 'value', 'label', 'weight', 'description') as $fld) {
549 $row[$fld] = $dao->$fld;
550 }
551 }
552 return $row;
553 }
554
555 /*
556 * Wrapper for calling values with fresh set to true to empty the given value
557 *
558 * Since there appears to be some inconsistency
559 * (@todo remove inconsistency) around the pseudoconstant operations
560 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
561 * which is part of the cache key
562 * will do a couple of variations & aspire to someone cleaning it up later
563 */
564 static function flush($name, $params = array()){
565 $defaults = array(
566 'flip' => FALSE,
567 'grouping' => FALSE,
568 'localize' => FALSE,
569 'condition' => NULL,
570 'labelColumnName' => 'label',
571 );
572
573 $params = array_merge($defaults, $params);
574 self::flushValues(
575 $name,
576 $params['flip'],
577 $params['grouping'],
578 $params['localize'],
579 $params['condition'],
580 $params['labelColumnName'],
581 TRUE,
582 TRUE
583 );
584 self::flushValues(
585 $name,
586 $params['flip'],
587 $params['grouping'],
588 $params['localize'],
589 $params['condition'],
590 $params['labelColumnName'],
591 FALSE,
592 TRUE
593 );
594 }
595
596 static function flushAll() {
597 self::$_values = array();
598 self::$_cache = array();
599 }
600}
601