PseudoConstant.php 48.5 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/**
 *
 * Stores all constants and pseudo constants for CRM application.
 *
 * examples of constants are "Contact Type" which will always be either
 * 'Individual', 'Household', 'Organization'.
 *
 * pseudo constants are entities from the database whose values rarely
 * change. examples are list of countries, states, location types,
 * relationship types.
 *
 * currently we're getting the data from the underlying database. this
 * will be reworked to use caching.
 *
 * Note: All pseudoconstants should be uninitialized or default to NULL.
 * This provides greater consistency/predictability after flushing.
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
46
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
47 48 49
 */
class CRM_Core_PseudoConstant {

50
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
51
   * Static cache for pseudoconstant arrays.
52 53 54 55
   * @var array
   */
  private static $cache;

totten's avatar
totten committed
56 57 58
  /**
   * activity type
   * @var array
59
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
totten's avatar
totten committed
60 61 62 63
   */
  private static $activityType;

  /**
colemanw's avatar
colemanw committed
64
   * States, provinces
totten's avatar
totten committed
65 66 67 68 69
   * @var array
   */
  private static $stateProvince;

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
70
   * Counties.
totten's avatar
totten committed
71 72 73 74 75
   * @var array
   */
  private static $county;

  /**
colemanw's avatar
colemanw committed
76
   * States/provinces abbreviations
totten's avatar
totten committed
77 78
   * @var array
   */
79
  private static $stateProvinceAbbreviation = [];
totten's avatar
totten committed
80 81

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
82
   * Country.
totten's avatar
totten committed
83 84 85 86 87
   * @var array
   */
  private static $country;

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
88
   * CountryIsoCode.
totten's avatar
totten committed
89 90 91 92 93 94 95
   * @var array
   */
  private static $countryIsoCode;

  /**
   * group
   * @var array
96
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
totten's avatar
totten committed
97 98 99 100
   */
  private static $group;

  /**
colemanw's avatar
colemanw committed
101
   * RelationshipType
totten's avatar
totten committed
102 103 104 105 106
   * @var array
   */
  private static $relationshipType;

  /**
colemanw's avatar
colemanw committed
107
   * Civicrm groups that are not smart groups
totten's avatar
totten committed
108 109 110 111 112
   * @var array
   */
  private static $staticGroup;

  /**
colemanw's avatar
colemanw committed
113
   * Currency codes
totten's avatar
totten committed
114 115 116 117 118
   * @var array
   */
  private static $currencyCode;

  /**
colemanw's avatar
colemanw committed
119
   * Payment processor
totten's avatar
totten committed
120 121 122 123 124
   * @var array
   */
  private static $paymentProcessor;

  /**
colemanw's avatar
colemanw committed
125
   * Payment processor types
totten's avatar
totten committed
126 127 128 129 130 131 132 133 134 135 136 137 138
   * @var array
   */
  private static $paymentProcessorType;

  /**
   * World Region
   * @var array
   */
  private static $worldRegions;

  /**
   * activity status
   * @var array
139
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
totten's avatar
totten committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
   */
  private static $activityStatus;

  /**
   * Visibility
   * @var array
   */
  private static $visibility;

  /**
   * Greetings
   * @var array
   */
  private static $greeting;

  /**
   * Extensions of type module
   * @var array
   */
  private static $extensions;

161 162 163 164 165 166
  /**
   * Financial Account Type
   * @var array
   */
  private static $accountOptionValues;

167
  /**
168 169
   * Low-level option getter, rarely accessed directly.
   * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
eileen's avatar
eileen committed
170
   * @see https://docs.civicrm.org/dev/en/latest/framework/pseudoconstant/
171
   *
172 173 174 175 176
   * NOTE: If someone undertakes a refactoring of this, please consider the use-case of
   * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
   * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
   * between $field lookup and the $pseudoconstant evaluation.
   *
177 178 179
   * @param string $daoName
   * @param string $fieldName
   * @param array $params
180 181 182 183 184
   * - name       string  name of the option group
   * - flip       boolean results are return in id => label format if false
   *                            if true, the results are reversed
   * - grouping   boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
   * - localize   boolean if true, localize the results before returning
185
   * - condition  string|array add condition(s) to the sql query - will be concatenated using 'AND'
186 187
   * - keyColumn  string the column to use for 'id'
   * - labelColumn string the column to use for 'label'
188
   * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
189 190
   * - onlyActive boolean return only the action option values
   * - fresh      boolean ignore cache entries and go back to DB
totten's avatar
totten committed
191
   * @param string $context : Context string
192
   * @see CRM_Core_DAO::buildOptionsContext
193
   *
xurizaemon's avatar
xurizaemon committed
194
   * @return array|bool
195
   *   array on success, FALSE on error.
196
   *
197
   */
198
  public static function get($daoName, $fieldName, $params = [], $context = NULL) {
199
    CRM_Core_DAO::buildOptionsContext($context);
colemanw's avatar
colemanw committed
200
    $flip = !empty($params['flip']);
201 202 203 204
    // Historically this was 'false' but according to the notes in
    // CRM_Core_DAO::buildOptionsContext it should be context dependent.
    // timidly changing for 'search' only to fix world_region in search options.
    $localizeDefault = in_array($context, ['search']) ? TRUE : FALSE;
205
    // Merge params with defaults
206
    $params += [
207
      'grouping' => FALSE,
208
      'localize' => $localizeDefault,
209
      'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE,
210
      'fresh' => FALSE,
211
      'context' => $context,
212
    ];
213
    $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getCanonicalClassName($daoName));
colemanw's avatar
colemanw committed
214 215

    // Custom fields are not in the schema
216
    if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
217
      $customField = new CRM_Core_BAO_CustomField();
218
      $customField->id = (int) substr($fieldName, 7);
219
      $options = $customField->getOptions($context);
220 221
      if ($options && $flip) {
        $options = array_flip($options);
222 223
      }
      $customField->free();
224
      return $options;
colemanw's avatar
colemanw committed
225 226 227
    }

    // Core field: load schema
xurizaemon's avatar
xurizaemon committed
228
    $dao = new $daoName();
229
    $fieldSpec = $dao->getFieldSpec($fieldName);
230
    $dao->free();
colemanw's avatar
colemanw committed
231

232 233
    // Ensure we have the canonical name for this field
    $fieldName = CRM_Utils_Array::value('name', $fieldSpec, $fieldName);
colemanw's avatar
colemanw committed
234

235
    // Return false if field doesn't exist.
236
    if (empty($fieldSpec)) {
237 238
      return FALSE;
    }
239 240 241

    elseif (!empty($fieldSpec['pseudoconstant'])) {
      $pseudoconstant = $fieldSpec['pseudoconstant'];
242 243

      // if callback is specified..
244
      if (!empty($pseudoconstant['callback'])) {
245
        $fieldOptions = call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']), $context);
246 247 248
        //CRM-18223: Allow additions to field options via hook.
        CRM_Utils_Hook::fieldOptions($entity, $fieldName, $fieldOptions, $params);
        return $fieldOptions;
249 250
      }

251
      // Merge params with schema defaults
252 253
      $params += [
        'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, []),
254 255
        'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant),
        'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant),
256
      ];
257

258 259 260 261 262
      if ($context == 'abbreviate') {
        switch ($fieldName) {
          case 'state_province_id':
            $params['labelColumn'] = 'abbreviation';
            break;
Andrew Hunt's avatar
Andrew Hunt committed
263

264 265 266
          case 'country_id':
            $params['labelColumn'] = 'iso_code';
            break;
Andrew Hunt's avatar
Andrew Hunt committed
267

268 269 270 271
          default:
        }
      }

272
      // Fetch option group from option_value table
273
      if (!empty($pseudoconstant['optionGroupName'])) {
274 275 276
        if ($context == 'validate') {
          $params['labelColumn'] = 'name';
        }
277 278 279
        if ($context == 'match') {
          $params['keyColumn'] = 'name';
        }
280
        // Call our generic fn for retrieving from the option_value table
281
        $options = CRM_Core_OptionGroup::values(
282
          $pseudoconstant['optionGroupName'],
283 284 285
          $flip,
          $params['grouping'],
          $params['localize'],
286
          $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
287
          $params['labelColumn'] ? $params['labelColumn'] : 'label',
288
          $params['onlyActive'],
289
          $params['fresh'],
290 291
          $params['keyColumn'] ? $params['keyColumn'] : 'value',
          !empty($params['orderColumn']) ? $params['orderColumn'] : 'weight'
292
        );
293 294
        CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params);
        return $options;
295
      }
296 297

      // Fetch options from other tables
298
      if (!empty($pseudoconstant['table'])) {
299 300
        // Normalize params so the serialized cache string will be consistent.
        CRM_Utils_Array::remove($params, 'flip', 'fresh');
301
        ksort($params);
302
        $cacheKey = $daoName . $fieldName . serialize($params);
303

304
        // Retrieve cached options
305
        if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
306
          $output = self::$cache[$cacheKey];
307
        }
308
        else {
309
          $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
colemanw's avatar
colemanw committed
310 311 312 313
          if (!class_exists($daoName)) {
            return FALSE;
          }
          // Get list of fields for the option table
xurizaemon's avatar
xurizaemon committed
314
          $dao = new $daoName();
colemanw's avatar
colemanw committed
315 316
          $availableFields = array_keys($dao->fieldKeys());
          $dao->free();
317

318 319
          $select = "SELECT %1 AS id, %2 AS label";
          $from = "FROM %3";
320
          $wheres = [];
321
          $order = "ORDER BY %2";
322

323 324 325
          // Use machine name in certain contexts
          if ($context == 'validate' || $context == 'match') {
            $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn';
326
            if (!empty($pseudoconstant['nameColumn'])) {
327
              $params[$nameField] = $pseudoconstant['nameColumn'];
328 329
            }
            elseif (in_array('name', $availableFields)) {
330
              $params[$nameField] = 'name';
331
            }
332
          }
333
          // Condition param can be passed as an sql clause string or an array of clauses
334
          if (!empty($params['condition'])) {
335
            $wheres[] = implode(' AND ', (array) $params['condition']);
336
          }
337
          // onlyActive param will automatically filter on common flags
338
          if (!empty($params['onlyActive'])) {
339
            foreach (['is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0] as $flag => $val) {
340 341 342
              if (in_array($flag, $availableFields)) {
                $wheres[] = "$flag = $val";
              }
343 344
            }
          }
345 346 347 348
          // Filter domain specific options
          if (in_array('domain_id', $availableFields)) {
            $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID();
          }
349 350 351 352 353
          $queryParams = [
            1 => [$params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
            2 => [$params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
            3 => [$pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
          ];
354 355
          // Add orderColumn param
          if (!empty($params['orderColumn'])) {
356
            $queryParams[4] = [$params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES];
357 358 359 360 361 362 363 364 365 366
            $order = "ORDER BY %4";
          }
          // Support no sorting if $params[orderColumn] is FALSE
          elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
            $order = '';
          }
          // Default to 'weight' if that column exists
          elseif (in_array('weight', $availableFields)) {
            $order = "ORDER BY weight";
          }
367

368
          $output = [];
369 370 371 372 373 374 375 376 377 378
          $query = "$select $from";
          if ($wheres) {
            $query .= " WHERE " . implode($wheres, ' AND ');
          }
          $query .= ' ' . $order;
          $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
          while ($dao->fetch()) {
            $output[$dao->id] = $dao->label;
          }
          $dao->free();
379 380
          // Localize results
          if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
381
            $I18nParams = [];
382 383
            if (isset($fieldSpec['localize_context'])) {
              $I18nParams['context'] = $fieldSpec['localize_context'];
384
            }
385
            $i18n = CRM_Core_I18n::singleton();
386
            $i18n->localizeArray($output, $I18nParams);
387 388 389 390 391
            // Maintain sort by label
            if ($order == "ORDER BY %2") {
              CRM_Utils_Array::asort($output);
            }
          }
392
          CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
393 394 395
          self::$cache[$cacheKey] = $output;
        }
        return $flip ? array_flip($output) : $output;
396 397
      }
    }
398 399 400

    // Return "Yes" and "No" for boolean fields
    elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
401
      $output = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
402
      CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
403 404
      return $flip ? array_flip($output) : $output;
    }
405 406 407 408
    // If we're still here, it's an error. Return FALSE.
    return FALSE;
  }

totten's avatar
totten committed
409
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
410
   * Fetch the translated label for a field given its key.
411
   *
412 413 414
   * @param string $baoName
   * @param string $fieldName
   * @param string|Int $key
415 416
   *
   * TODO: Accept multivalued input?
417
   *
418 419 420 421
   * @return bool|null|string
   *   FALSE if the given field has no associated option list
   *   NULL if the given key has no corresponding option
   *   String if label is found
422
   */
423
  public static function getLabel($baoName, $fieldName, $key) {
424 425 426 427 428 429 430 431
    $values = $baoName::buildOptions($fieldName, 'get');
    if ($values === FALSE) {
      return FALSE;
    }
    return CRM_Utils_Array::value($key, $values);
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
432
   * Fetch the machine name for a field given its key.
433
   *
434 435 436
   * @param string $baoName
   * @param string $fieldName
   * @param string|Int $key
437 438 439 440 441 442
   *
   * @return bool|null|string
   *   FALSE if the given field has no associated option list
   *   NULL if the given key has no corresponding option
   *   String if label is found
   */
443
  public static function getName($baoName, $fieldName, $key) {
444
    $values = $baoName::buildOptions($fieldName, 'validate');
445 446 447 448 449
    if ($values === FALSE) {
      return FALSE;
    }
    return CRM_Utils_Array::value($key, $values);
  }
450 451

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
452
   * Fetch the key for a field option given its name.
453
   *
454 455 456
   * @param string $baoName
   * @param string $fieldName
   * @param string|Int $value
457
   *
xurizaemon's avatar
xurizaemon committed
458
   * @return bool|null|string|int
459 460 461
   *   FALSE if the given field has no associated option list
   *   NULL if the given key has no corresponding option
   *   String|Number if key is found
462
   */
463
  public static function getKey($baoName, $fieldName, $value) {
464
    $values = $baoName::buildOptions($fieldName, 'validate');
465 466 467 468 469
    if ($values === FALSE) {
      return FALSE;
    }
    return CRM_Utils_Array::key($value, $values);
  }
470

471 472 473 474 475
  /**
   * Lookup the admin page at which a field's option list can be edited
   * @param $fieldSpec
   * @return string|null
   */
476
  public static function getOptionEditUrl($fieldSpec) {
477 478 479 480 481 482 483 484 485 486 487
    // If it's an option group, that's easy
    if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
      return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
    }
    // For everything else...
    elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
      $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
      if (!$daoName) {
        return NULL;
      }
      // We don't have good mapping so have to do a bit of guesswork from the menu
colemanw's avatar
colemanw committed
488
      list(, $parent, , $child) = explode('_', $daoName);
489
      $sql = "SELECT path FROM civicrm_menu
colemanw's avatar
colemanw committed
490 491
        WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
        ORDER BY page_callback
492 493 494 495 496 497
        LIMIT 1";
      return CRM_Core_Dao::singleValueQuery($sql);
    }
    return NULL;
  }

498
  /**
499
   * @deprecated generic populate method.
500
   * All pseudoconstant functions that use this method are also @deprecated
totten's avatar
totten committed
501 502 503 504 505 506
   *
   * The static array $var is populated from the db
   * using the <b>$name DAO</b>.
   *
   * Note: any database errors will be trapped by the DAO.
   *
507 508 509 510 511 512 513 514 515 516 517 518
   * @param array $var
   *   The associative array we will fill.
   * @param string $name
   *   The name of the DAO.
   * @param bool $all
   *   Get all objects. default is to get only active ones.
   * @param string $retrieve
   *   The field that we are interested in (normally name, differs in some objects).
   * @param string $filter
   *   The field that we want to filter the result set with.
   * @param string $condition
   *   The condition that gets passed to the final query as the WHERE clause.
519
   *
eileenmcnaugton's avatar
eileenmcnaugton committed
520
   * @param bool $orderby
521
   * @param string $key
eileenmcnaugton's avatar
eileenmcnaugton committed
522
   * @param bool $force
totten's avatar
totten committed
523
   *
eileenmcnaugton's avatar
eileenmcnaugton committed
524
   * @return array
totten's avatar
totten committed
525
   */
lobo's avatar
lobo committed
526 527 528 529 530 531 532 533 534 535 536
  public static function populate(
    &$var,
    $name,
    $all = FALSE,
    $retrieve = 'name',
    $filter = 'is_active',
    $condition = NULL,
    $orderby = NULL,
    $key = 'id',
    $force = NULL
  ) {
537
    $cacheKey = CRM_Core_BAO_Cache::cleanKey("CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}");
totten's avatar
totten committed
538 539
    $cache = CRM_Utils_Cache::singleton();
    $var = $cache->get($cacheKey);
540
    if ($var !== NULL && empty($force)) {
totten's avatar
totten committed
541 542 543
      return $var;
    }

544
    /* @var CRM_Core_DAO $object */
xurizaemon's avatar
xurizaemon committed
545
    $object = new $name();
totten's avatar
totten committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

    $object->selectAdd();
    $object->selectAdd("$key, $retrieve");
    if ($condition) {
      $object->whereAdd($condition);
    }

    if (!$orderby) {
      $object->orderBy($retrieve);
    }
    else {
      $object->orderBy($orderby);
    }

    if (!$all) {
      $object->$filter = 1;
562 563 564 565
      $aclClauses = array_filter($name::getSelectWhereClause());
      foreach ($aclClauses as $clause) {
        $object->whereAdd($clause);
      }
totten's avatar
totten committed
566 567 568
    }

    $object->find();
569
    $var = [];
totten's avatar
totten committed
570 571 572 573 574 575 576 577
    while ($object->fetch()) {
      $var[$object->$key] = $object->$retrieve;
    }

    $cache->set($cacheKey, $var);
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
578
   * Flush given pseudoconstant so it can be reread from db.
totten's avatar
totten committed
579 580
   * nex time it's requested.
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
581
   * @param bool|string $name pseudoconstant to be flushed
totten's avatar
totten committed
582
   */
colemanw's avatar
colemanw committed
583
  public static function flush($name = 'cache') {
colemanw's avatar
colemanw committed
584
    if (isset(self::$$name)) {
585 586
      self::$$name = NULL;
    }
colemanw's avatar
colemanw committed
587 588
    if ($name == 'cache') {
      CRM_Core_OptionGroup::flushAll();
589 590 591
      if (isset(\Civi::$statics[__CLASS__])) {
        unset(\Civi::$statics[__CLASS__]);
      }
colemanw's avatar
colemanw committed
592
    }
totten's avatar
totten committed
593 594 595
  }

  /**
596
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
597
   *
totten's avatar
totten committed
598 599 600 601 602
   * Get all Activty types.
   *
   * The static array activityType is returned
   *
   *
603 604
   * @return array
   *   array reference of all activity types.
totten's avatar
totten committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618
   */
  public static function &activityType() {
    $args = func_get_args();
    $all = CRM_Utils_Array::value(0, $args, TRUE);
    $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
    $reset = CRM_Utils_Array::value(2, $args, FALSE);
    $returnColumn = CRM_Utils_Array::value(3, $args, 'label');
    $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
    $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
    $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
    $index .= '_' . (int) $includeCampaignActivities;
    $index .= '_' . (int) $onlyComponentActivities;

    if (NULL === self::$activityType) {
619
      self::$activityType = [];
totten's avatar
totten committed
620 621 622 623 624 625 626 627 628 629 630 631
    }

    if (!isset(self::$activityType[$index]) || $reset) {
      $condition = NULL;
      if (!$all) {
        $condition = 'AND filter = 0';
      }
      $componentClause = " v.component_id IS NULL";
      if ($onlyComponentActivities) {
        $componentClause = " v.component_id IS NOT NULL";
      }

632
      $componentIds = [];
totten's avatar
totten committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
      $compInfo = CRM_Core_Component::getEnabledComponents();

      // build filter for listing activity types only if their
      // respective components are enabled
      foreach ($compInfo as $compName => $compObj) {
        if ($compName == 'CiviCase') {
          if ($includeCaseActivities) {
            $componentIds[] = $compObj->componentID;
          }
        }
        elseif ($compName == 'CiviCampaign') {
          if ($includeCampaignActivities) {
            $componentIds[] = $compObj->componentID;
          }
        }
        else {
          $componentIds[] = $compObj->componentID;
        }
      }

      if (count($componentIds)) {
        $componentIds = implode(',', $componentIds);
        $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
        if ($onlyComponentActivities) {
          $componentClause = " ( v.component_id IN ($componentIds ) )";
        }
      }
yashodha's avatar
yashodha committed
660
      $condition = $condition . ' AND ' . $componentClause;
totten's avatar
totten committed
661 662 663 664 665

      self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
    }
    return self::$activityType[$index];
  }
lobo's avatar
lobo committed
666

totten's avatar
totten committed
667 668 669 670 671 672 673 674 675 676
  /**
   * Get all the State/Province from database.
   *
   * The static array stateProvince is returned, and if it's
   * called the first time, the <b>State Province DAO</b> is used
   * to get all the States.
   *
   * Note: any database errors will be trapped by the DAO.
   *
   *
677
   * @param bool|int $id - Optional id to return
totten's avatar
totten committed
678
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
679
   * @param bool $limit
totten's avatar
totten committed
680
   *
681 682
   * @return array
   *   array reference of all State/Provinces.
totten's avatar
totten committed
683 684 685 686 687 688
   */
  public static function &stateProvince($id = FALSE, $limit = TRUE) {
    if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) {
      $whereClause = FALSE;
      if ($limit) {
        $countryIsoCodes = self::countryIsoCode();
689
        $limitCodes = CRM_Core_BAO_Country::provinceLimit();
690
        $limitIds = [];
totten's avatar
totten committed
691 692 693 694 695 696 697 698 699 700 701 702 703
        foreach ($limitCodes as $code) {
          $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
        }
        if (!empty($limitIds)) {
          $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
        }
        else {
          $whereClause = FALSE;
        }
      }
      self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);

      // localise the province names if in an non-en_US locale
sunil's avatar
sunil committed
704
      $tsLocale = CRM_Core_I18n::getLocale();
totten's avatar
totten committed
705 706
      if ($tsLocale != '' and $tsLocale != 'en_US') {
        $i18n = CRM_Core_I18n::singleton();
707
        $i18n->localizeArray(self::$stateProvince, [
totten's avatar
totten committed
708
          'context' => 'province',
709
        ]);
totten's avatar
totten committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
        self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
      }
    }
    if ($id) {
      if (array_key_exists($id, self::$stateProvince)) {
        return self::$stateProvince[$id];
      }
      else {
        $result = NULL;
        return $result;
      }
    }
    return self::$stateProvince;
  }

  /**
   * Get all the State/Province abbreviations from the database.
   *
   * Same as above, except gets the abbreviations instead of the names.
   *
   *
731
   * @param bool|int $id - Optional id to return
Eileen McNaughton's avatar
Eileen McNaughton committed
732 733
   *
   * @param bool $limit
totten's avatar
totten committed
734
   *
735 736
   * @return array
   *   array reference of all State/Province abbreviations.
totten's avatar
totten committed
737
   */
738
  public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
739 740 741
    if ($id && is_numeric($id)) {
      if (!array_key_exists($id, (array) self::$stateProvinceAbbreviation)) {
        $query = "SELECT abbreviation
totten's avatar
totten committed
742 743
FROM   civicrm_state_province
WHERE  id = %1";
744 745
        $params = [
          1 => [
746 747
            $id,
            'Integer',
748 749
          ],
        ];
750 751 752
        self::$stateProvinceAbbreviation[$id] = CRM_Core_DAO::singleValueQuery($query, $params);
      }
      return self::$stateProvinceAbbreviation[$id];
totten's avatar
totten committed
753
    }
754
    else {
totten's avatar
totten committed
755 756 757 758
      $whereClause = FALSE;

      if ($limit) {
        $countryIsoCodes = self::countryIsoCode();
759
        $limitCodes = CRM_Core_BAO_Country::provinceLimit();
760
        $limitIds = [];
totten's avatar
totten committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        foreach ($limitCodes as $code) {
          $tmpArray = array_keys($countryIsoCodes, $code);

          if (!empty($tmpArray)) {
            $limitIds[] = array_shift($tmpArray);
          }
        }
        if (!empty($limitIds)) {
          $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
        }
      }
      self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
    }

    return self::$stateProvinceAbbreviation;
  }

778 779 780 781 782 783 784 785 786 787
  /**
   * Get all the State/Province abbreviations from the database for the specified country.
   *
   * @param int $countryID
   *
   * @return array
   *   array of all State/Province abbreviations for the given country.
   */
  public static function stateProvinceAbbreviationForCountry($countryID) {
    if (!isset(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID])) {
788
      \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID] = [];
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    }
    self::populate(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation');
    return \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID];
  }

  /**
   * Get all the State/Province abbreviations from the database for the default country.
   *
   * @return array
   *   array of all State/Province abbreviations for the given country.
   */
  public static function stateProvinceAbbreviationForDefaultCountry() {
    return CRM_Core_PseudoConstant::stateProvinceAbbreviationForCountry(Civi::settings()->get('defaultContactCountry'));
  }

totten's avatar
totten committed
804 805 806 807 808 809 810 811 812 813
  /**
   * Get all the countries from database.
   *
   * The static array country is returned, and if it's
   * called the first time, the <b>Country DAO</b> is used
   * to get all the countries.
   *
   * Note: any database errors will be trapped by the DAO.
   *
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
814
   * @param bool|int $id - Optional id to return
totten's avatar
totten committed
815
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
816
   * @param bool $applyLimit
totten's avatar
totten committed
817
   *
818
   * @return array|null
819
   *   array reference of all countries.
totten's avatar
totten committed
820 821 822 823 824
   */
  public static function country($id = FALSE, $applyLimit = TRUE) {
    if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) {

      $config = CRM_Core_Config::singleton();
825
      $limitCodes = [];
totten's avatar
totten committed
826 827 828 829 830

      if ($applyLimit) {
        // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
        // (ensuring it's a subset of the legal values)
        // K/P: We need to fix this, i dont think it works with new setting files
831
        $limitCodes = CRM_Core_BAO_Country::countryLimit();
totten's avatar
totten committed
832
        if (!is_array($limitCodes)) {
833
          $limitCodes = [
totten's avatar
totten committed
834
            $config->countryLimit => 1,
835
          ];
totten's avatar
totten committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
        }

        $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
      }

      if (count($limitCodes)) {
        $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
      }
      else {
        $whereClause = NULL;
      }

      self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);

      // if default country is set, percolate it to the top
      if ($config->defaultContactCountry()) {
        $countryIsoCodes = self::countryIsoCode();
        $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
        if ($defaultID !== FALSE) {
          $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country);
          self::$country = $default + self::$country;
        }
      }

      // localise the country names if in an non-en_US locale
sunil's avatar
sunil committed
861
      $tsLocale = CRM_Core_I18n::getLocale();
totten's avatar
totten committed
862 863
      if ($tsLocale != '' and $tsLocale != 'en_US') {
        $i18n = CRM_Core_I18n::singleton();
864
        $i18n->localizeArray(self::$country, [
totten's avatar
totten committed
865
          'context' => 'country',
866
        ]);
totten's avatar
totten committed
867 868 869 870 871 872 873 874
        self::$country = CRM_Utils_Array::asort(self::$country);
      }
    }
    if ($id) {
      if (array_key_exists($id, self::$country)) {
        return self::$country[$id];
      }
      else {
875
        return NULL;
totten's avatar
totten committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
      }
    }
    return self::$country;
  }

  /**
   * Get all the country ISO Code abbreviations from the database.
   *
   * The static array countryIsoCode is returned, and if it's
   * called the first time, the <b>Country DAO</b> is used
   * to get all the countries' ISO codes.
   *
   * Note: any database errors will be trapped by the DAO.
   *
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
891
   * @param bool $id
totten's avatar
totten committed
892
   *
893 894
   * @return array
   *   array reference of all country ISO codes.
totten's avatar
totten committed
895 896 897 898 899 900 901 902 903 904
   */
  public static function &countryIsoCode($id = FALSE) {
    if (!self::$countryIsoCode) {
      self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
    }
    if ($id) {
      if (array_key_exists($id, self::$countryIsoCode)) {
        return self::$countryIsoCode[$id];
      }
      else {
lobo's avatar
lobo committed
905
        return CRM_Core_DAO::$_nullObject;
totten's avatar
totten committed
906 907 908 909 910 911
      }
    }
    return self::$countryIsoCode;
  }

  /**
912
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
913
   *
totten's avatar
totten committed
914 915 916 917 918 919 920 921
   * Get all groups from database
   *
   * The static array group is returned, and if it's
   * called the first time, the <b>Group DAO</b> is used
   * to get all the groups.
   *
   * Note: any database errors will be trapped by the DAO.
   *
922 923 924 925
   * @param string $groupType
   *   Type of group(Access/Mailing).
   * @param bool $excludeHidden
   *   Exclude hidden groups.
totten's avatar
totten committed
926 927
   *
   *
928 929
   * @return array
   *   array reference of all groups.
totten's avatar
totten committed
930
   */
931
  public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
932 933 934 935 936
    if ($groupType === 'validate') {
      // validate gets passed through from getoptions. Handle in the deprecated
      // fn rather than change the new pattern.
      $groupType = NULL;
    }
totten's avatar
totten committed
937
    $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
938
    $groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden);
totten's avatar
totten committed
939

940 941
    if (!isset(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey])) {
      self::populate(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
totten's avatar
totten committed
942
    }
943
    return Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey];
totten's avatar
totten committed
944 945 946
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
947
   * Get all permissioned groups from database.
totten's avatar
totten committed
948 949 950 951 952 953 954
   *
   * The static array group is returned, and if it's
   * called the first time, the <b>Group DAO</b> is used
   * to get all the groups.
   *
   * Note: any database errors will be trapped by the DAO.
   *
955 956 957 958
   * @param string $groupType
   *   Type of group(Access/Mailing).
   * @param bool $excludeHidden
   *   Exclude hidden groups.
Eileen McNaughton's avatar
Eileen McNaughton committed
959
   *
totten's avatar
totten committed
960
   *
961 962
   * @return array
   *   array reference of all groups.
totten's avatar
totten committed
963 964 965 966 967
   */
  public static function group($groupType = NULL, $excludeHidden = TRUE) {
    return CRM_Core_Permission::group($groupType, $excludeHidden);
  }

968
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
969
   * Fetch groups in a nested format suitable for use in select form element.
970 971 972 973 974 975 976 977 978 979
   * @param bool $checkPermissions
   * @param string|null $groupType
   * @param bool $excludeHidden
   * @return array
   */
  public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
    $groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden);
    return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, '&nbsp;&nbsp;', TRUE);
  }

totten's avatar
totten committed
980
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
981
   * Get all permissioned groups from database.
totten's avatar
totten committed
982 983 984 985 986 987 988 989
   *
   * The static array group is returned, and if it's
   * called the first time, the <b>Group DAO</b> is used
   * to get all the groups.
   *
   * Note: any database errors will be trapped by the DAO.
   *
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
990 991 992
   * @param bool $onlyPublic
   * @param null $groupType
   * @param bool $excludeHidden
totten's avatar
totten committed
993
   *
994 995
   * @return array
   *   array reference of all groups.
totten's avatar
totten committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
   */
  public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
    if (!self::$staticGroup) {
      $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
      if ($onlyPublic) {
        $condition .= " AND visibility != 'User and User Admin Only'";
      }

      if ($groupType) {
        $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
      }

      if ($excludeHidden) {
        $condition .= ' AND is_hidden != 1 ';
      }

      self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
    }

    return self::$staticGroup;
  }

  /**
   * Get all Relationship Types  from database.
   *
   * The static array group is returned, and if it's
   * called the first time, the <b>RelationshipType DAO</b> is used
   * to get all the relationship types.
   *
   * Note: any database errors will be trapped by the DAO.
   *
1027 1028 1029 1030
   * @param string $valueColumnName
   *   Db column name/label.
   * @param bool $reset
   *   Reset relationship types if true.
eileen's avatar
eileen committed
1031
   * @param bool $isActive
1032
   *   Filter by is_active. NULL to disable.
totten's avatar
totten committed
1033
   *
1034 1035
   * @return array
   *   array reference of all relationship types.
totten's avatar
totten committed
1036
   */
1037 1038 1039
  public static function &relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) {
    $cacheKey = $valueColumnName . '::' . $isActive;
    if (!CRM_Utils_Array::value($cacheKey, self::$relationshipType) || $reset) {
1040
      self::$relationshipType[$cacheKey] = [];
totten's avatar
totten committed
1041 1042 1043 1044 1045 1046 1047 1048

      //now we have name/label columns CRM-3336
      $column_a_b = "{$valueColumnName}_a_b";
      $column_b_a = "{$valueColumnName}_b_a";

      $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
      $relationshipTypeDAO->selectAdd();
      $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1049 1050 1051
      if ($isActive !== NULL) {
        $relationshipTypeDAO->is_active = $isActive;
      }
totten's avatar
totten committed
1052 1053 1054
      $relationshipTypeDAO->find();
      while ($relationshipTypeDAO->fetch()) {

1055
        self::$relationshipType[$cacheKey][$relationshipTypeDAO->id] = [
1056
          'id' => $relationshipTypeDAO->id,
totten's avatar
totten committed
1057 1058 1059 1060 1061 1062
          $column_a_b => $relationshipTypeDAO->$column_a_b,
          $column_b_a => $relationshipTypeDAO->$column_b_a,
          'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
          'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
          'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
          'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1063
        ];
totten's avatar
totten committed
1064 1065 1066
      }
    }

1067
    return self::$relationshipType[$cacheKey];
totten's avatar
totten committed
1068 1069 1070
  }

  /**
colemanw's avatar
colemanw committed
1071
   * Get all the ISO 4217 currency codes
totten's avatar
totten committed
1072 1073 1074 1075
   *
   * so far, we use this for validation only, so there's no point of putting this into the database
   *
   *
1076 1077
   * @return array
   *   array reference of all currency codes
totten's avatar
totten committed
1078 1079 1080
   */
  public static function &currencyCode() {
    if (!self::$currencyCode) {
1081 1082 1083

      $query = "SELECT name FROM civicrm_currency";
      $dao = CRM_Core_DAO::executeQuery($query);
1084
      $currencyCode = [];
1085 1086 1087
      while ($dao->fetch()) {
        self::$currencyCode[] = $dao->name;
      }
totten's avatar
totten committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    }
    return self::$currencyCode;
  }

  /**
   * Get all the County from database.
   *
   * The static array county is returned, and if it's
   * called the first time, the <b>County DAO</b> is used
   * to get all the Counties.
   *
   * Note: any database errors will be trapped by the DAO.
   *
   *
1102
   * @param bool|int $id - Optional id to return
totten's avatar
totten committed
1103
   *
1104 1105
   * @return array
   *   array reference of all Counties
totten's avatar
totten committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
   */
  public static function &county($id = FALSE) {
    if (!self::$county) {

      $config = CRM_Core_Config::singleton();
      // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
      self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
    }
    if ($id) {
      if (array_key_exists($id, self::$county)) {
        return self::$county[$id];
      }
      else {
lobo's avatar
lobo committed
1119
        return CRM_Core_DAO::$_nullObject;
totten's avatar
totten committed
1120 1121 1122 1123 1124 1125
      }
    }
    return self::$county;
  }

  /**
1126
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
totten's avatar
totten committed
1127 1128 1129 1130 1131
   * Get all active payment processors
   *
   * The static array paymentProcessor is returned
   *
   *
1132 1133 1134 1135
   * @param bool $all
   *   Get payment processors - default is to get only active ones.
   * @param bool $test
   *   Get test payment processors.
totten's avatar
totten committed
1136
   *
1137
   * @param null $additionalCond
totten's avatar
totten committed
1138
   *
1139 1140
   * @return array
   *   array of all payment processors
totten's avatar
totten committed
1141
   */
Eileen McNaughton's avatar
Eileen McNaughton committed
1142
  public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
totten's avatar
totten committed
1143 1144 1145 1146 1147 1148 1149
    $condition = "is_test = ";
    $condition .= ($test) ? '1' : '0';

    if ($additionalCond) {
      $condition .= " AND ( $additionalCond ) ";
    }

Dmitry Smirnov's avatar
Dmitry Smirnov committed
1150
    // CRM-7178. Make sure we only include payment processors valid in this
totten's avatar
totten committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
    // domain
    $condition .= " AND domain_id = " . CRM_Core_Config::domainID();

    $cacheKey = $condition . '_' . (int) $all;
    if (!isset(self::$paymentProcessor[$cacheKey])) {
      self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
    }

    return self::$paymentProcessor[$cacheKey];
  }

  /**
1163
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
totten's avatar
totten committed
1164 1165 1166 1167
   *
   * The static array paymentProcessorType is returned
   *
   *
1168 1169
   * @param bool $all
   *   Get payment processors - default is to get only active ones.
totten's avatar
totten committed
1170
   *
colemanw's avatar
colemanw committed
1171
   * @param int $id
1172
   * @param string $return
totten's avatar
totten committed
1173
   *
1174 1175
   * @return array
   *   array of all payment processor types
totten's avatar
totten committed
1176 1177
   */
  public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1178
    $cacheKey = $id . '_' . $return;
totten's avatar
totten committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    if (empty(self::$paymentProcessorType[$cacheKey])) {
      self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
    }
    if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) {
      return self::$paymentProcessorType[$cacheKey][$id];
    }
    return self::$paymentProcessorType[$cacheKey];
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
1189
   * Get all the World Regions from Database.
totten's avatar
totten committed
1190 1191
   *
   *
1192 1193
   * @param bool $id
   *
1194 1195
   * @return array
   *   array reference of all World Regions
totten's avatar
totten committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
   */
  public static function &worldRegion($id = FALSE) {
    if (!self::$worldRegions) {
      self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
    }

    if ($id) {
      if (array_key_exists($id, self::$worldRegions)) {
        return self::$worldRegions[$id];
      }
      else {
lobo's avatar
lobo committed
1207
        return CRM_Core_DAO::$_nullObject;
totten's avatar
totten committed
1208 1209 1210 1211 1212 1213 1214
      }
    }

    return self::$worldRegions;
  }

  /**
1215
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1216
   *
totten's avatar
totten committed
1217 1218 1219 1220 1221
   * Get all Activity Statuses.
   *
   * The static array activityStatus is returned
   *
   *
1222 1223
   * @param string $column
   *
1224 1225
   * @return array
   *   array reference of all activity statuses
totten's avatar
totten committed
1226 1227 1228
   */
  public static function &activityStatus($column = 'label') {
    if (NULL === self::$activityStatus) {
1229
      self::$activityStatus = [];
totten's avatar
totten committed
1230 1231
    }
    if (!array_key_exists($column, self::$activityStatus)) {
1232
      self::$activityStatus[$column] = [];
totten's avatar
totten committed
1233 1234 1235 1236 1237 1238 1239 1240

      self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
    }

    return self::$activityStatus[$column];
  }

  /**
1241
   * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1242
   *
totten's avatar
totten committed
1243 1244 1245 1246 1247
   * Get all Visibility levels.
   *
   * The static array visibility is returned
   *
   *
1248
   * @param string $column
totten's avatar
totten committed
1249
   *
1250 1251
   * @return array
   *   array reference of all Visibility levels.
totten's avatar
totten committed
1252 1253 1254
   */
  public static function &visibility($column = 'label') {
    if (!isset(self::$visibility)) {
1255
      self::$visibility = [];
totten's avatar
totten committed
1256 1257 1258 1259
    }

    if (!isset(self::$visibility[$column])) {
      self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column);