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