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