OptionGroup.php 20 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
4
 | Copyright CiviCRM LLC. All rights reserved.                        |
totten's avatar
totten committed
5
 |                                                                    |
6 7 8
 | This work is published under the GNU AGPLv3 license with some      |
 | permitted exceptions and without any warranty. For full license    |
 | and copyright information, see https://civicrm.org/licensing       |
totten's avatar
totten committed
9
 +--------------------------------------------------------------------+
10
 */
totten's avatar
totten committed
11 12 13 14

/**
 *
 * @package CRM
15
 * @copyright CiviCRM LLC https://civicrm.org/licensing
totten's avatar
totten committed
16 17
 */
class CRM_Core_OptionGroup {
18 19
  public static $_values = [];
  public static $_cache = [];
totten's avatar
totten committed
20

21
  /**
totten's avatar
totten committed
22 23
   * $_domainIDGroups array maintains the list of option groups for whom
   * domainID is to be considered.
24
   * @var array
totten's avatar
totten committed
25
   */
26
  public static $_domainIDGroups = [
totten's avatar
totten committed
27 28
    'from_email_address',
    'grant_type',
29
  ];
totten's avatar
totten committed
30

31
  /**
32
   * @param CRM_Core_DAO $dao
33 34 35 36 37 38 39
   * @param bool $flip
   * @param bool $grouping
   * @param bool $localize
   * @param string $valueColumnName
   *
   * @return array
   */
40
  public static function &valuesCommon(
totten's avatar
totten committed
41 42 43
    $dao, $flip = FALSE, $grouping = FALSE,
    $localize = FALSE, $valueColumnName = 'label'
  ) {
44
    self::$_values = [];
totten's avatar
totten committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    while ($dao->fetch()) {
      if ($flip) {
        if ($grouping) {
          self::$_values[$dao->value] = $dao->grouping;
        }
        else {
          self::$_values[$dao->{$valueColumnName}] = $dao->value;
        }
      }
      else {
        if ($grouping) {
          self::$_values[$dao->{$valueColumnName}] = $dao->grouping;
        }
        else {
          self::$_values[$dao->value] = $dao->{$valueColumnName};
        }
      }
    }
    if ($localize) {
      $i18n = CRM_Core_I18n::singleton();
      $i18n->localizeArray(self::$_values);
    }
    return self::$_values;
  }

  /**
   * This function retrieves all the values for the specific option group by name
   * this is primarily used to create various html based form elements
   * (radio, select, checkbox etc). OptionGroups for most cases have the
Dmitry Smirnov's avatar
Dmitry Smirnov committed
75
   * 'label' in the label column and the 'id' or 'name' in the value column
totten's avatar
totten committed
76
   *
colemanw's avatar
colemanw committed
77 78 79 80
   * @param string $name
   *   name of the option group.
   * @param bool $flip
   *   results are return in id => label format if false.
totten's avatar
totten committed
81
   *                            if true, the results are reversed
colemanw's avatar
colemanw committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
   * @param bool $grouping
   *   if true, return the value in 'grouping' column.
   * @param bool $localize
   *   if true, localize the results before returning.
   * @param string $condition
   *   add another condition to the sql query.
   * @param string $labelColumnName
   *   the column to use for 'label'.
   * @param bool $onlyActive
   *   return only the action option values.
   * @param bool $fresh
   *   ignore cache entries and go back to DB.
   * @param string $keyColumnName
   *   the column to use for 'key'.
eileen's avatar
eileen committed
96
   * @param string $orderBy
colemanw's avatar
colemanw committed
97
   *   the column to use for ordering.
totten's avatar
totten committed
98
   *
99
   * @return array
eileen's avatar
eileen committed
100
   *   The values as specified by the params
totten's avatar
totten committed
101
   */
102
  public static function &values(
totten's avatar
totten committed
103 104
    $name, $flip = FALSE, $grouping = FALSE,
    $localize = FALSE, $condition = NULL,
105 106
    $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value',
    $orderBy = 'weight'
totten's avatar
totten committed
107
  ) {
108
    $cache = CRM_Utils_Cache::singleton();
109 110 111 112 113 114
    if (in_array($name, self::$_domainIDGroups)) {
      $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy, CRM_Core_Config::domainID());
    }
    else {
      $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName, $orderBy);
    }
totten's avatar
totten committed
115

116 117 118 119 120 121
    if (!$fresh) {
      // Fetch from static var
      if (array_key_exists($cacheKey, self::$_cache)) {
        return self::$_cache[$cacheKey];
      }
      // Fetch from main cache
eileen's avatar
eileen committed
122 123 124
      self::$_cache[$cacheKey] = $cache->get($cacheKey);
      if (self::$_cache[$cacheKey] !== NULL) {
        return self::$_cache[$cacheKey];
125
      }
totten's avatar
totten committed
126 127 128
    }

    $query = "
129
SELECT  v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as `grouping`
totten's avatar
totten committed
130 131 132 133 134 135 136 137
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.name            = %1
  AND  g.is_active       = 1 ";

    if ($onlyActive) {
      $query .= " AND  v.is_active = 1 ";
138 139 140 141 142 143 144 145
      // Only show options for enabled components
      $componentClause = ' v.component_id IS NULL ';
      $enabledComponents = CRM_Core_Config::singleton()->enableComponents;
      if ($enabledComponents) {
        $enabledComponents = '"' . implode('","', $enabledComponents) . '"';
        $componentClause .= " OR v.component_id IN (SELECT id FROM civicrm_component WHERE name IN ($enabledComponents)) ";
      }
      $query .= " AND ($componentClause) ";
totten's avatar
totten committed
146 147 148 149 150 151 152 153 154
    }
    if (in_array($name, self::$_domainIDGroups)) {
      $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
    }

    if ($condition) {
      $query .= $condition;
    }

155
    $query .= " ORDER BY v.{$orderBy}";
totten's avatar
totten committed
156

157
    $p = [1 => [$name, 'String']];
totten's avatar
totten committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    $dao = CRM_Core_DAO::executeQuery($query, $p);

    $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);

    // call option value hook
    CRM_Utils_Hook::optionValues($var, $name);

    self::$_cache[$cacheKey] = $var;
    $cache->set($cacheKey, $var);

    return $var;
  }

  /**
   * Counterpart to values() which removes the item from the cache
   *
colemanw's avatar
colemanw committed
174
   * @param string $name
totten's avatar
totten committed
175 176 177 178
   * @param $flip
   * @param $grouping
   * @param $localize
   * @param $condition
colemanw's avatar
colemanw committed
179
   * @param string $labelColumnName
totten's avatar
totten committed
180
   * @param $onlyActive
Eileen McNaughton's avatar
Eileen McNaughton committed
181
   * @param string $keyColumnName
totten's avatar
totten committed
182
   */
183 184
  protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') {
    $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName);
totten's avatar
totten committed
185 186 187 188 189
    $cache = CRM_Utils_Cache::singleton();
    $cache->delete($cacheKey);
    unset(self::$_cache[$cacheKey]);
  }

190 191 192
  /**
   * @return string
   */
193
  protected static function createCacheKey($id) {
194
    return "CRM_OG_" . preg_replace('/[^a-zA-Z0-9]/', '', $id) . '_' . md5(serialize(func_get_args()));
totten's avatar
totten committed
195 196 197
  }

  /**
198
   * This function retrieves all the values for the specific option group by id.
totten's avatar
totten committed
199 200
   * this is primarily used to create various html based form elements
   * (radio, select, checkbox etc). OptionGroups for most cases have the
Dmitry Smirnov's avatar
Dmitry Smirnov committed
201
   * 'label' in the label column and the 'id' or 'name' in the value column
totten's avatar
totten committed
202
   *
colemanw's avatar
colemanw committed
203 204 205 206 207 208 209 210 211 212 213
   * @param int $id
   *   id of the option group.
   * @param bool $flip
   *   results are return in id => label format if false.
   *   if true, the results are reversed
   * @param bool $grouping
   *   if true, return the value in 'grouping' column.
   * @param bool $localize
   *   if true, localize the results before returning.
   * @param string $labelColumnName
   *   the column to use for 'label'.
Eileen McNaughton's avatar
Eileen McNaughton committed
214 215 216
   * @param bool $onlyActive
   * @param bool $fresh
   *
217
   * @return array
218
   *   Array of values as specified by the above params
totten's avatar
totten committed
219 220
   * @void
   */
221
  public static function &valuesByID($id, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE) {
222
    $cacheKey = self::createCacheKey($id, $flip, $grouping, $localize, $labelColumnName, $onlyActive);
totten's avatar
totten committed
223 224

    $cache = CRM_Utils_Cache::singleton();
225
    if (!$fresh) {
eileen's avatar
eileen committed
226 227 228
      self::$_cache[$cacheKey] = $cache->get($cacheKey);
      if (self::$_cache[$cacheKey] !== NULL) {
        return self::$_cache[$cacheKey];
229
      }
totten's avatar
totten committed
230 231
    }
    $query = "
232
SELECT  v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as `grouping`
totten's avatar
totten committed
233 234 235 236 237 238
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.id              = %1
  AND  g.is_active       = 1
";
239 240 241 242 243
    if ($onlyActive) {
      $query .= " AND  v.is_active = 1 ";
    }
    $query .= " ORDER BY v.weight, v.label";

244
    $p = [1 => [$id, 'Integer']];
totten's avatar
totten committed
245 246 247 248 249 250 251 252 253
    $dao = CRM_Core_DAO::executeQuery($query, $p);

    $var = self::valuesCommon($dao, $flip, $grouping, $localize, $labelColumnName);
    $cache->set($cacheKey, $var);

    return $var;
  }

  /**
254 255 256
   * Lookup titles OR ids for a set of option_value populated fields. The
   * retrieved value is assigned a new field name by id or id's by title
   * (each within a specified option_group).
totten's avatar
totten committed
257
   *
258 259
   * @param array $params
   *   Reference array of values submitted by the form. Based on.
colemanw's avatar
colemanw committed
260 261 262 263
   *   $flip, creates new elements in $params for each field in
   *   the $names array.
   *   If $flip = false, adds root field name => title
   *   If $flip = true, adds actual field name => id
totten's avatar
totten committed
264
   *
265
   * @param array $names
266
   *   Array of field names we want transformed.
colemanw's avatar
colemanw committed
267 268
   *   Array key = 'postName' (field name submitted by form in $params).
   *   Array value = array('newName' => $newName, 'groupName' => $groupName).
totten's avatar
totten committed
269
   *
totten's avatar
totten committed
270
   * @param bool $flip
totten's avatar
totten committed
271
   */
272
  public static function lookupValues(&$params, $names, $flip = FALSE) {
totten's avatar
totten committed
273 274 275
    foreach ($names as $postName => $value) {
      // See if $params field is in $names array (i.e. is a value that we need to lookup)
      if ($postalName = CRM_Utils_Array::value($postName, $params)) {
276
        $postValues = [];
totten's avatar
totten committed
277 278 279 280 281 282 283 284 285 286 287 288 289
        // params[$postName] may be a Ctrl+A separated value list
        if (is_string($postalName) &&
          strpos($postalName, CRM_Core_DAO::VALUE_SEPARATOR) == FALSE
        ) {
          // eliminate the ^A frm the beginning and end if present
          if (substr($postalName, 0, 1) == CRM_Core_DAO::VALUE_SEPARATOR) {
            $params[$postName] = substr($params[$postName], 1, -1);
          }
          $postValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $params[$postName]);
        }
        elseif (is_array($postalName)) {
          $postValues = $postalName;
        }
290
        $newValue = [];
totten's avatar
totten committed
291 292 293 294 295 296
        foreach ($postValues as $postValue) {
          if (!$postValue) {
            continue;
          }

          if ($flip) {
297
            $p = [1 => [$postValue, 'String']];
totten's avatar
totten committed
298
            $lookupBy = 'v.label= %1';
totten's avatar
totten committed
299
            $select = "v.value";
totten's avatar
totten committed
300 301
          }
          else {
302
            $p = [1 => [$postValue, 'Integer']];
totten's avatar
totten committed
303
            $lookupBy = 'v.value = %1';
totten's avatar
totten committed
304
            $select = "v.label";
totten's avatar
totten committed
305 306
          }

307
          $p[2] = [$value['groupName'], 'String'];
totten's avatar
totten committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
          $query = "
                        SELECT $select
                        FROM   civicrm_option_value v,
                               civicrm_option_group g
                        WHERE  v.option_group_id = g.id
                        AND    g.name            = %2
                        AND    $lookupBy";

          $newValue[] = CRM_Core_DAO::singleValueQuery($query, $p);
          $newValue = str_replace(',', '_', $newValue);
        }
        $params[$value['newName']] = implode(', ', $newValue);
      }
    }
  }

324
  /**
325
   * @deprecated - use CRM_Core_PseudoConstant::getLabel
eileenmcnaugton's avatar
eileenmcnaugton committed
326
   *
colemanw's avatar
colemanw committed
327
   * @param string $groupName
328 329 330 331 332
   * @param $value
   * @param bool $onlyActiveValue
   *
   * @return null
   */
333
  public static function getLabel($groupName, $value, $onlyActiveValue = TRUE) {
334
    CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_PseudoConstant::getLabel');
totten's avatar
totten committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    if (empty($groupName) ||
      empty($value)
    ) {
      return NULL;
    }

    $query = "
SELECT  v.label as label ,v.value as value
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.name            = %1
  AND  g.is_active       = 1
  AND  v.value           = %2
";
    if ($onlyActiveValue) {
      $query .= " AND  v.is_active = 1 ";
    }
353 354 355 356
    $p = [
      1 => [$groupName, 'String'],
      2 => [$value, 'Integer'],
    ];
totten's avatar
totten committed
357 358 359 360 361 362 363
    $dao = CRM_Core_DAO::executeQuery($query, $p);
    if ($dao->fetch()) {
      return $dao->label;
    }
    return NULL;
  }

364
  /**
365 366 367 368
   * @deprecated
   *
   * This function is not cached.
   *
colemanw's avatar
colemanw committed
369
   * @param string $groupName
370 371 372 373 374 375 376
   * @param $label
   * @param string $labelField
   * @param string $labelType
   * @param string $valueField
   *
   * @return null
   */
377
  public static function getValue(
totten's avatar
totten committed
378
    $groupName,
totten's avatar
totten committed
379 380
    $label,
    $labelField = 'label',
totten's avatar
totten committed
381
    $labelType = 'String',
totten's avatar
totten committed
382 383 384 385 386 387
    $valueField = 'value'
  ) {
    if (empty($label)) {
      return NULL;
    }

388
    CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_PseudoConstant::getKey');
389

totten's avatar
totten committed
390 391 392 393 394 395 396 397 398 399 400
    $query = "
SELECT  v.label as label ,v.{$valueField} as value
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.name            = %1
  AND  v.is_active       = 1
  AND  g.is_active       = 1
  AND  v.$labelField     = %2
";

401 402 403 404
    $p = [
      1 => [$groupName, 'String'],
      2 => [$label, $labelType],
    ];
totten's avatar
totten committed
405 406 407 408 409 410 411
    $dao = CRM_Core_DAO::executeQuery($query, $p);
    if ($dao->fetch()) {
      return $dao->value;
    }
    return NULL;
  }

412 413 414
  /**
   * Get option_value.value from default option_value row for an option group
   *
415 416
   * @param string $groupName
   *   The name of the option group.
417 418
   *
   *
419 420
   * @return string
   *   the value from the row where is_default = true
eileen's avatar
eileen committed
421
   */
422
  public static function getDefaultValue($groupName) {
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    if (empty($groupName)) {
      return NULL;
    }
    $query = "
SELECT v.value
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.name            = %1
  AND  v.is_active       = 1
  AND  g.is_active       = 1
  AND  v.is_default      = 1
";
    if (in_array($groupName, self::$_domainIDGroups)) {
      $query .= " AND v.domain_id = " . CRM_Core_Config::domainID();
    }

440
    $p = [1 => [$groupName, 'String']];
441 442
    return CRM_Core_DAO::singleValueQuery($query, $p);
  }
eileen's avatar
eileen committed
443

totten's avatar
totten committed
444
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
445
   * Creates a new option group with the passed in values.
totten's avatar
totten committed
446
   *
447 448 449 450
   * @param string $groupName
   *   The name of the option group - make sure there is no conflict.
   * @param array $values
   *   The associative array that has information on the option values.
totten's avatar
totten committed
451 452 453 454 455 456 457 458
   *                          the keys of this array are:
   *                          string 'title'       (required)
   *                          string 'value'       (required)
   *                          string 'name'        (optional)
   *                          string 'description' (optional)
   *                          int    'weight'      (optional) - the order in which the value are displayed
   *                          bool   'is_default'  (optional) - is this the default one to display when rendered in form
   *                          bool   'is_active'   (optional) - should this element be rendered
459 460 461 462
   * @param int $defaultID
   *   (reference) - the option value ID of the default element (if set) is returned else 'null'.
   * @param null $groupTitle
   *   The optional label of the option group else set to group name.
totten's avatar
totten committed
463 464
   *
   *
465 466
   * @return int
   *   the option group ID
totten's avatar
totten committed
467
   */
468
  public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) {
totten's avatar
totten committed
469
    if (!empty($values)) {
totten's avatar
totten committed
470 471
      $group = new CRM_Core_DAO_OptionGroup();
      $group->name = $groupName;
472
      $group->find(TRUE);
totten's avatar
totten committed
473
      $group->title = empty($groupTitle) ? $groupName : $groupTitle;
totten's avatar
totten committed
474
      $group->is_reserved = 1;
totten's avatar
totten committed
475
      $group->is_active = 1;
totten's avatar
totten committed
476 477 478 479 480 481
      $group->save();

      foreach ($values as $v) {
        $value = new CRM_Core_DAO_OptionValue();
        $value->option_group_id = $group->id;
        $value->value = $v['value'];
482 483
        $value->find(TRUE);
        $value->label = $v['label'];
484 485 486 487 488
        $value->name = $v['name'] ?? NULL;
        $value->description = $v['description'] ?? NULL;
        $value->weight = $v['weight'] ?? NULL;
        $value->is_default = $v['is_default'] ?? NULL;
        $value->is_active = $v['is_active'] ?? NULL;
totten's avatar
totten committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502
        $value->save();

        if ($value->is_default) {
          $defaultID = $value->id;
        }
      }
    }
    else {
      return $defaultID = 'null';
    }

    return $group->id;
  }

503
  /**
colemanw's avatar
colemanw committed
504
   * @param string $groupName
505 506 507
   * @param $values
   * @param bool $flip
   * @param string $field
508 509
   *
   * @deprecated
510
   */
511
  public static function getAssoc($groupName, &$values, $flip = FALSE, $field = 'name') {
512
    CRM_Core_Error::deprecatedFunctionWarning('unused function');
totten's avatar
totten committed
513 514 515 516 517 518 519 520
    $query = "
SELECT v.id as amount_id, v.value, v.label, v.name, v.description, v.weight
  FROM civicrm_option_group g,
       civicrm_option_value v
 WHERE g.id = v.option_group_id
   AND g.$field = %1
ORDER BY v.weight
";
521
    $params = [1 => [$groupName, 'String']];
totten's avatar
totten committed
522 523
    $dao = CRM_Core_DAO::executeQuery($query, $params);

524
    $fields = ['value', 'label', 'name', 'description', 'amount_id', 'weight'];
totten's avatar
totten committed
525
    if ($flip) {
526
      $values = [];
totten's avatar
totten committed
527 528 529
    }
    else {
      foreach ($fields as $field) {
530
        $values[$field] = [];
totten's avatar
totten committed
531 532 533 534 535 536
      }
    }
    $index = 1;

    while ($dao->fetch()) {
      if ($flip) {
537
        $value = [];
totten's avatar
totten committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551
        foreach ($fields as $field) {
          $value[$field] = $dao->$field;
        }
        $values[$dao->amount_id] = $value;
      }
      else {
        foreach ($fields as $field) {
          $values[$field][$index] = $dao->$field;
        }
        $index++;
      }
    }
  }

552
  /**
colemanw's avatar
colemanw committed
553
   * @param string $groupName
554
   * @param string $operator
555 556
   *
   * @deprecated
557
   */
558
  public static function deleteAssoc($groupName, $operator = "=") {
559
    CRM_Core_Error::deprecatedFunctionWarning('unused function');
totten's avatar
totten committed
560 561 562 563 564 565 566
    $query = "
DELETE g, v
  FROM civicrm_option_group g,
       civicrm_option_value v
 WHERE g.id = v.option_group_id
   AND g.name {$operator} %1";

567
    $params = [1 => [$groupName, 'String']];
totten's avatar
totten committed
568 569 570 571

    $dao = CRM_Core_DAO::executeQuery($query, $params);
  }

572
  /**
colemanw's avatar
colemanw committed
573
   * @param string $groupName
574 575 576 577
   * @param $fieldValue
   * @param string $field
   * @param string $fieldType
   * @param bool $active
578 579
   * @param bool $localize
   *   if true, localize the results before returning.
580 581 582
   *
   * @return array
   */
583
  public static function getRowValues(
totten's avatar
totten committed
584
    $groupName, $fieldValue, $field = 'name',
585
    $fieldType = 'String', $active = TRUE, $localize = FALSE
totten's avatar
totten committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
  ) {
    $query = "
SELECT v.id, v.label, v.value, v.name, v.weight, v.description
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
  AND  g.name            = %1
  AND  g.is_active       = 1
  AND  v.$field          = %2
";

    if ($active) {
      $query .= " AND  v.is_active = 1";
    }

601 602 603 604
    $p = [
      1 => [$groupName, 'String'],
      2 => [$fieldValue, $fieldType],
    ];
totten's avatar
totten committed
605
    $dao = CRM_Core_DAO::executeQuery($query, $p);
606
    $row = [];
totten's avatar
totten committed
607 608

    if ($dao->fetch()) {
609
      foreach ([
610 611 612 613 614 615 616
        'id',
        'name',
        'value',
        'label',
        'weight',
        'description',
      ] as $fld) {
totten's avatar
totten committed
617
        $row[$fld] = $dao->$fld;
618
        if ($localize && in_array($fld, ['label', 'description'])) {
619 620
          $row[$fld] = ts($row[$fld]);
        }
621 622 623
      }
    }

totten's avatar
totten committed
624 625 626
    return $row;
  }

627
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
628
   * Wrapper for calling values with fresh set to true to empty the given value.
totten's avatar
totten committed
629 630 631 632 633 634
   *
   * Since there appears to be some inconsistency
   * (@todo remove inconsistency) around the pseudoconstant operations
   * (for example CRM_Contribution_Pseudoconstant::paymentInstrument doesn't specify isActive
   * which is part of the cache key
   * will do a couple of variations & aspire to someone cleaning it up later
635 636
   *
   * @param string $name
637 638
   * @param array $params
   */
639 640
  public static function flush($name, $params = []) {
    $defaults = [
totten's avatar
totten committed
641 642 643 644 645
      'flip' => FALSE,
      'grouping' => FALSE,
      'localize' => FALSE,
      'condition' => NULL,
      'labelColumnName' => 'label',
646
    ];
totten's avatar
totten committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    $params = array_merge($defaults, $params);
    self::flushValues(
      $name,
      $params['flip'],
      $params['grouping'],
      $params['localize'],
      $params['condition'],
      $params['labelColumnName'],
      TRUE,
      TRUE
    );
    self::flushValues(
      $name,
      $params['flip'],
      $params['grouping'],
      $params['localize'],
      $params['condition'],
      $params['labelColumnName'],
      FALSE,
      TRUE
    );
  }

671 672 673 674 675 676
  /**
   * Flush all the places where option values are cached.
   *
   * Note that this is called from CRM_Core_PseudoConstant::flush() so we should resist
   * the intuitive urge to flush that class.
   */
677
  public static function flushAll() {
678 679
    self::$_values = [];
    self::$_cache = [];
colemanw's avatar
colemanw committed
680
    CRM_Utils_Cache::singleton()->flush();
totten's avatar
totten committed
681
  }
682

totten's avatar
totten committed
683
}