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