CRM-19693 - Don't show options for disabled components
[civicrm-core.git] / CRM / Core / OptionGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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'.
bf48aa29 111 * @param string $orderBy
6a488035 112 *
a6c01b45 113 * @return array
bf48aa29 114 * The values as specified by the params
6a488035 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 ";
7797abe1
CW
147 $enabledComponents = '"' . implode('","', CRM_Core_Config::singleton()->enableComponents) . '"';
148 $query .= " AND (v.component_id IS NULL OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents))) ";
6a488035
TO
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
148136ae 158 $query .= " ORDER BY v.{$orderBy}";
6a488035
TO
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 *
100fef9d 177 * @param string $name
6a488035
TO
178 * @param $flip
179 * @param $grouping
180 * @param $localize
181 * @param $condition
100fef9d 182 * @param string $labelColumnName
6a488035 183 * @param $onlyActive
da6b46f4 184 * @param string $keyColumnName
6a488035 185 */
c0c9cd82
CW
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);
6a488035
TO
188 $cache = CRM_Utils_Cache::singleton();
189 $cache->delete($cacheKey);
190 unset(self::$_cache[$cacheKey]);
191 }
192
a0ee3941
EM
193 /**
194 * @return string
195 */
c0c9cd82
CW
196 protected static function createCacheKey() {
197 $cacheKey = "CRM_OG_" . serialize(func_get_args());
6a488035
TO
198 return $cacheKey;
199 }
200
201 /**
fe482240 202 * This function retrieves all the values for the specific option group by id.
6a488035
TO
203 * this is primarily used to create various html based form elements
204 * (radio, select, checkbox etc). OptionGroups for most cases have the
b44e3f84 205 * 'label' in the label column and the 'id' or 'name' in the value column
6a488035 206 *
5a4f6742
CW
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'.
da6b46f4
EM
218 * @param bool $onlyActive
219 * @param bool $fresh
220 *
a6c01b45 221 * @return array
16b10e64 222 * Array of values as specified by the above params
6a488035
TO
223 * @void
224 */
00be9182 225 public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
786ad6e1 226 $cacheKey = self::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
6a488035
TO
227
228 $cache = CRM_Utils_Cache::singleton();
786ad6e1
CW
229 if (!$fresh) {
230 $var = $cache->get($cacheKey);
231 if ($var) {
232 return $var;
233 }
6a488035
TO
234 }
235 $query = "
236SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping
237FROM civicrm_option_value v,
238 civicrm_option_group g
239WHERE v.option_group_id = g.id
240 AND g.id = %1
6a488035 241 AND g.is_active = 1
6a488035 242";
786ad6e1
CW
243 if ($onlyActive) {
244 $query .= " AND v.is_active = 1 ";
245 }
246 $query .= " ORDER BY v.weight, v.label";
247
6a488035
TO
248 $p = array(1 => array($id, 'Integer'));
249 $dao = CRM_Core_DAO::executeQuery($query, $p);
250
251 $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
252 $cache->set($cacheKey, $var);
253
254 return $var;
255 }
256
257 /**
100fef9d 258 * Lookup titles OR ids for a set of option_value populated fields. The retrieved value
6a488035
TO
259 * is assigned a new fieldname by id or id's by title
260 * (each within a specificied option_group)
261 *
6a0b768e
TO
262 * @param array $params
263 * Reference array of values submitted by the form. Based on.
5a4f6742
CW
264 * $flip, creates new elements in $params for each field in
265 * the $names array.
266 * If $flip = false, adds root field name => title
267 * If $flip = true, adds actual field name => id
6a488035 268 *
6a0b768e
TO
269 * @param array $names
270 * Reference array of fieldnames we want transformed.
5a4f6742
CW
271 * Array key = 'postName' (field name submitted by form in $params).
272 * Array value = array('newName' => $newName, 'groupName' => $groupName).
6a488035
TO
273 *
274 *
2aa397bc 275 * @param bool $flip
6a488035 276 */
00be9182 277 public static function lookupValues(&$params, &$names, $flip = FALSE) {
6a488035
TO
278 foreach ($names as $postName => $value) {
279 // See if $params field is in $names array (i.e. is a value that we need to lookup)
280 if ($postalName = CRM_Utils_Array::value($postName, $params)) {
281 $postValues = array();
282 // params[$postName] may be a Ctrl+A separated value list
283 if (is_string($postalName) &&
284 strpos($postalName, CRM_Core_DAO::VALUE_SEPARATOR) == FALSE
285 ) {
286 // eliminate the ^A frm the beginning and end if present
287 if (substr($postalName, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) {
288 $params[$postName] = substr($params[$postName], 1, -1);
289 }
290 $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]);
291 }
292 elseif (is_array($postalName)) {
293 $postValues = $postalName;
294 }
295 $newValue = array();
296 foreach ($postValues as $postValue) {
297 if (!$postValue) {
298 continue;
299 }
300
301 if ($flip) {
353ffa53 302 $p = array(1 => array($postValue, 'String'));
6a488035 303 $lookupBy = 'v.label= %1';
353ffa53 304 $select = "v.value";
6a488035
TO
305 }
306 else {
353ffa53 307 $p = array(1 => array($postValue, 'Integer'));
6a488035 308 $lookupBy = 'v.value = %1';
353ffa53 309 $select = "v.label";
6a488035
TO
310 }
311
312 $p[2] = array($value['groupName'], 'String');
313 $query = "
314 SELECT $select
315 FROM civicrm_option_value v,
316 civicrm_option_group g
317 WHERE v.option_group_id = g.id
318 AND g.name = %2
319 AND $lookupBy";
320
321 $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p);
322 $newValue = str_replace(',', '_', $newValue);
323 }
324 $params[$value['newName']] = implode(', ', $newValue);
325 }
326 }
327 }
328
a0ee3941 329 /**
d6f7dc03 330 * @deprecated - use CRM_Core_Pseudoconstant::getLabel
331 *
100fef9d 332 * @param string $groupName
a0ee3941
EM
333 * @param $value
334 * @param bool $onlyActiveValue
335 *
336 * @return null
337 */
00be9182 338 public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
6a488035
TO
339 if (empty($groupName) ||
340 empty($value)
341 ) {
342 return NULL;
343 }
344
345 $query = "
346SELECT v.label as label ,v.value as value
347FROM civicrm_option_value v,
348 civicrm_option_group g
349WHERE v.option_group_id = g.id
350 AND g.name = %1
351 AND g.is_active = 1
352 AND v.value = %2
353";
354 if ($onlyActiveValue) {
355 $query .= " AND v.is_active = 1 ";
356 }
2aa397bc 357 $p = array(
353ffa53 358 1 => array($groupName, 'String'),
6a488035
TO
359 2 => array($value, 'Integer'),
360 );
361 $dao = CRM_Core_DAO::executeQuery($query, $p);
362 if ($dao->fetch()) {
363 return $dao->label;
364 }
365 return NULL;
366 }
367
a0ee3941 368 /**
76c28c8d
DG
369 * @deprecated
370 *
371 * This function is not cached.
372 *
100fef9d 373 * @param string $groupName
a0ee3941
EM
374 * @param $label
375 * @param string $labelField
376 * @param string $labelType
377 * @param string $valueField
378 *
379 * @return null
380 */
2da40d21 381 public static function getValue(
f9f40af3 382 $groupName,
6a488035
TO
383 $label,
384 $labelField = 'label',
2aa397bc 385 $labelType = 'String',
6a488035
TO
386 $valueField = 'value'
387 ) {
388 if (empty($label)) {
389 return NULL;
390 }
391
392 $query = "
393SELECT v.label as label ,v.{$valueField} as value
394FROM civicrm_option_value v,
395 civicrm_option_group g
396WHERE v.option_group_id = g.id
397 AND g.name = %1
398 AND v.is_active = 1
399 AND g.is_active = 1
400 AND v.$labelField = %2
401";
402
2aa397bc 403 $p = array(
353ffa53 404 1 => array($groupName, 'String'),
6a488035
TO
405 2 => array($label, $labelType),
406 );
407 $dao = CRM_Core_DAO::executeQuery($query, $p);
408 if ($dao->fetch()) {
409 $dao->free();
410 return $dao->value;
411 }
412 $dao->free();
413 return NULL;
414 }
415
343d84fa
DG
416 /**
417 * Get option_value.value from default option_value row for an option group
418 *
6a0b768e
TO
419 * @param string $groupName
420 * The name of the option group.
343d84fa 421 *
343d84fa 422 *
a6c01b45
CW
423 * @return string
424 * the value from the row where is_default = true
8ef12e64 425 */
00be9182 426 public static function getDefaultValue($groupName) {
343d84fa
DG
427 if (empty($groupName)) {
428 return NULL;
429 }
430 $query = "
431SELECT v.value
432FROM civicrm_option_value v,
433 civicrm_option_group g
434WHERE 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 }
8ef12e64 447
6a488035 448 /**
d09edf64 449 * Creates a new option group with the passed in values.
6a488035
TO
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 *
6a0b768e
TO
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.
6a488035
TO
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
6a0b768e
TO
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.
6a488035 469 *
6a488035 470 *
a6c01b45
CW
471 * @return int
472 * the option group ID
6a488035 473 */
00be9182 474 public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
6a488035
TO
475 self::deleteAssoc($groupName);
476 if (!empty($values)) {
353ffa53
TO
477 $group = new CRM_Core_DAO_OptionGroup();
478 $group->name = $groupName;
479 $group->title = empty($groupTitle) ? $groupName : $groupTitle;
6a488035 480 $group->is_reserved = 1;
353ffa53 481 $group->is_active = 1;
6a488035
TO
482 $group->save();
483
484 foreach ($values as $v) {
485 $value = new CRM_Core_DAO_OptionValue();
486 $value->option_group_id = $group->id;
487 $value->label = $v['label'];
488 $value->value = $v['value'];
489 $value->name = CRM_Utils_Array::value('name', $v);
490 $value->description = CRM_Utils_Array::value('description', $v);
491 $value->weight = CRM_Utils_Array::value('weight', $v);
492 $value->is_default = CRM_Utils_Array::value('is_default', $v);
493 $value->is_active = CRM_Utils_Array::value('is_active', $v);
494 $value->save();
495
496 if ($value->is_default) {
497 $defaultID = $value->id;
498 }
499 }
500 }
501 else {
502 return $defaultID = 'null';
503 }
504
505 return $group->id;
506 }
507
a0ee3941 508 /**
100fef9d 509 * @param string $groupName
a0ee3941
EM
510 * @param $values
511 * @param bool $flip
512 * @param string $field
513 */
00be9182 514 public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
6a488035
TO
515 $query = "
516SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
517 FROM civicrm_option_group g,
518 civicrm_option_value v
519 WHERE g.id = v.option_group_id
520 AND g.$field = %1
521ORDER BY v.weight
522";
523 $params = array(1 => array($groupName, 'String'));
524 $dao = CRM_Core_DAO::executeQuery($query, $params);
525
526 $fields = array('value', 'label', 'name', 'description', 'amount_id', 'weight');
527 if ($flip) {
528 $values = array();
529 }
530 else {
531 foreach ($fields as $field) {
532 $values[$field] = array();
533 }
534 }
535 $index = 1;
536
537 while ($dao->fetch()) {
538 if ($flip) {
539 $value = array();
540 foreach ($fields as $field) {
541 $value[$field] = $dao->$field;
542 }
543 $values[$dao->amount_id] = $value;
544 }
545 else {
546 foreach ($fields as $field) {
547 $values[$field][$index] = $dao->$field;
548 }
549 $index++;
550 }
551 }
552 }
553
a0ee3941 554 /**
100fef9d 555 * @param string $groupName
a0ee3941
EM
556 * @param string $operator
557 */
00be9182 558 public static function deleteAssoc($groupName, $operator = "=") {
6a488035
TO
559 $query = "
560DELETE g, v
561 FROM civicrm_option_group g,
562 civicrm_option_value v
563 WHERE g.id = v.option_group_id
564 AND g.name {$operator} %1";
565
566 $params = array(1 => array($groupName, 'String'));
567
568 $dao = CRM_Core_DAO::executeQuery($query, $params);
569 }
570
a0ee3941 571 /**
100fef9d 572 * @param string $groupName
a0ee3941
EM
573 * @param $fieldValue
574 * @param string $field
575 * @param string $fieldType
576 * @param bool $active
bc3364a9
ML
577 * @param bool $localize
578 * if true, localize the results before returning.
a0ee3941
EM
579 *
580 * @return array
581 */
2da40d21 582 public static function getRowValues(
f9f40af3 583 $groupName, $fieldValue, $field = 'name',
bc3364a9 584 $fieldType = 'String', $active = TRUE, $localize = FALSE
6a488035
TO
585 ) {
586 $query = "
587SELECT v.id, v.label, v.value, v.name, v.weight, v.description
588FROM civicrm_option_value v,
589 civicrm_option_group g
590WHERE v.option_group_id = g.id
591 AND g.name = %1
592 AND g.is_active = 1
593 AND v.$field = %2
594";
595
596 if ($active) {
597 $query .= " AND v.is_active = 1";
598 }
599
2aa397bc 600 $p = array(
353ffa53 601 1 => array($groupName, 'String'),
6a488035
TO
602 2 => array($fieldValue, $fieldType),
603 );
604 $dao = CRM_Core_DAO::executeQuery($query, $p);
605 $row = array();
606
607 if ($dao->fetch()) {
608 foreach (array(
353ffa53
TO
609 'id',
610 'name',
611 'value',
612 'label',
613 'weight',
af9b09df 614 'description',
353ffa53 615 ) as $fld) {
6a488035 616 $row[$fld] = $dao->$fld;
f623b0e8 617 if ($localize && in_array($fld, array('label', 'description'))) {
03c70eeb 618 $row[$fld] = ts($row[$fld]);
619 }
bc3364a9
ML
620 }
621 }
622
6a488035
TO
623 return $row;
624 }
625
d424ffde 626 /**
d09edf64 627 * Wrapper for calling values with fresh set to true to empty the given value.
6a488035
TO
628 *
629 * Since there appears to be some inconsistency
630 * (@todo remove inconsistency) around the pseudoconstant operations
631 * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
632 * which is part of the cache key
633 * will do a couple of variations & aspire to someone cleaning it up later
d424ffde
CW
634 *
635 * @param string $name
a0ee3941
EM
636 * @param array $params
637 */
9b873358 638 public static function flush($name, $params = array()) {
6a488035
TO
639 $defaults = array(
640 'flip' => FALSE,
641 'grouping' => FALSE,
642 'localize' => FALSE,
643 'condition' => NULL,
644 'labelColumnName' => 'label',
645 );
646
647 $params = array_merge($defaults, $params);
648 self::flushValues(
649 $name,
650 $params['flip'],
651 $params['grouping'],
652 $params['localize'],
653 $params['condition'],
654 $params['labelColumnName'],
655 TRUE,
656 TRUE
657 );
658 self::flushValues(
659 $name,
660 $params['flip'],
661 $params['grouping'],
662 $params['localize'],
663 $params['condition'],
664 $params['labelColumnName'],
665 FALSE,
666 TRUE
667 );
668 }
669
00be9182 670 public static function flushAll() {
6a488035
TO
671 self::$_values = array();
672 self::$_cache = array();
a4a33486 673 CRM_Utils_Cache::singleton()->flush();
6a488035 674 }
96025800 675
6a488035 676}