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