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