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