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