Remove deprecated componentPaymentFields function
[civicrm-core.git] / CRM / Core / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * Stores all constants and pseudo constants for CRM application.
31 *
32 * examples of constants are "Contact Type" which will always be either
33 * 'Individual', 'Household', 'Organization'.
34 *
35 * pseudo constants are entities from the database whose values rarely
36 * change. examples are list of countries, states, location types,
37 * relationship types.
38 *
39 * currently we're getting the data from the underlying database. this
40 * will be reworked to use caching.
41 *
42 * Note: All pseudoconstants should be uninitialized or default to NULL.
43 * This provides greater consistency/predictability after flushing.
44 *
45 * @package CRM
6b83d5bd 46 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
47 */
48class CRM_Core_PseudoConstant {
49
887a4028 50 /**
d09edf64 51 * Static cache for pseudoconstant arrays.
887a4028 52 * @var array
887a4028
A
53 */
54 private static $cache;
55
6a488035
TO
56 /**
57 * activity type
58 * @var array
518fa0ee 59 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
60 */
61 private static $activityType;
62
6a488035 63 /**
100fef9d 64 * States, provinces
6a488035 65 * @var array
6a488035
TO
66 */
67 private static $stateProvince;
68
69 /**
d09edf64 70 * Counties.
6a488035 71 * @var array
6a488035
TO
72 */
73 private static $county;
74
75 /**
100fef9d 76 * States/provinces abbreviations
6a488035 77 * @var array
6a488035 78 */
be2fb01f 79 private static $stateProvinceAbbreviation = [];
6a488035
TO
80
81 /**
d09edf64 82 * Country.
6a488035 83 * @var array
6a488035
TO
84 */
85 private static $country;
86
87 /**
d09edf64 88 * CountryIsoCode.
6a488035 89 * @var array
6a488035
TO
90 */
91 private static $countryIsoCode;
92
6a488035
TO
93 /**
94 * group
95 * @var array
518fa0ee 96 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
97 */
98 private static $group;
99
6a488035 100 /**
100fef9d 101 * RelationshipType
6a488035 102 * @var array
6a488035
TO
103 */
104 private static $relationshipType;
105
106 /**
100fef9d 107 * Civicrm groups that are not smart groups
6a488035 108 * @var array
6a488035
TO
109 */
110 private static $staticGroup;
111
6a488035 112 /**
100fef9d 113 * Currency codes
6a488035 114 * @var array
6a488035
TO
115 */
116 private static $currencyCode;
117
6a488035 118 /**
100fef9d 119 * Payment processor
6a488035 120 * @var array
6a488035
TO
121 */
122 private static $paymentProcessor;
123
124 /**
100fef9d 125 * Payment processor types
6a488035 126 * @var array
6a488035
TO
127 */
128 private static $paymentProcessorType;
129
130 /**
131 * World Region
132 * @var array
6a488035
TO
133 */
134 private static $worldRegions;
135
6a488035
TO
136 /**
137 * activity status
138 * @var array
518fa0ee 139 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
140 */
141 private static $activityStatus;
142
6a488035
TO
143 /**
144 * Visibility
145 * @var array
6a488035
TO
146 */
147 private static $visibility;
148
6a488035
TO
149 /**
150 * Greetings
151 * @var array
6a488035
TO
152 */
153 private static $greeting;
154
6a488035
TO
155 /**
156 * Extensions of type module
157 * @var array
6a488035
TO
158 */
159 private static $extensions;
160
f743a6eb
CW
161 /**
162 * Financial Account Type
163 * @var array
f743a6eb
CW
164 */
165 private static $accountOptionValues;
166
887a4028 167 /**
6d68a4cb
CW
168 * Low-level option getter, rarely accessed directly.
169 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
7eaff122 170 * @see https://docs.civicrm.org/dev/en/latest/framework/pseudoconstant/
6d68a4cb 171 *
76068c6a
TO
172 * NOTE: If someone undertakes a refactoring of this, please consider the use-case of
173 * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
174 * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
175 * between $field lookup and the $pseudoconstant evaluation.
176 *
6a0b768e
TO
177 * @param string $daoName
178 * @param string $fieldName
179 * @param array $params
a42ef93c
CW
180 * - name string name of the option group
181 * - flip boolean results are return in id => label format if false
182 * if true, the results are reversed
183 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
184 * - localize boolean if true, localize the results before returning
f743a6eb 185 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
091fe2a8
CW
186 * - keyColumn string the column to use for 'id'
187 * - labelColumn string the column to use for 'label'
6d68a4cb 188 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
a42ef93c
CW
189 * - onlyActive boolean return only the action option values
190 * - fresh boolean ignore cache entries and go back to DB
353ffa53 191 * @param string $context : Context string
5b22d1b8 192 * @see CRM_Core_DAO::buildOptionsContext
887a4028 193 *
8d7a9d07 194 * @return array|bool
72b3a70c 195 * array on success, FALSE on error.
a42ef93c 196 *
887a4028 197 */
be2fb01f 198 public static function get($daoName, $fieldName, $params = [], $context = NULL) {
786ad6e1 199 CRM_Core_DAO::buildOptionsContext($context);
f2b53f26 200 $flip = !empty($params['flip']);
65c86f7d 201 // Historically this was 'false' but according to the notes in
202 // CRM_Core_DAO::buildOptionsContext it should be context dependent.
203 // timidly changing for 'search' only to fix world_region in search options.
204 $localizeDefault = in_array($context, ['search']) ? TRUE : FALSE;
786ad6e1 205 // Merge params with defaults
be2fb01f 206 $params += [
786ad6e1 207 'grouping' => FALSE,
65c86f7d 208 'localize' => $localizeDefault,
c203a3d6 209 'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE,
786ad6e1 210 'fresh' => FALSE,
33e61cb8 211 'context' => $context,
be2fb01f 212 ];
33e61cb8 213 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getCanonicalClassName($daoName));
f2b53f26
CW
214
215 // Custom fields are not in the schema
786ad6e1 216 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
2921fb78 217 $customField = new CRM_Core_BAO_CustomField();
a3d8b390 218 $customField->id = (int) substr($fieldName, 7);
832d5c06 219 $options = $customField->getOptions($context);
2fea9ed9
CW
220 if ($options && $flip) {
221 $options = array_flip($options);
a3d8b390 222 }
786ad6e1 223 return $options;
f2b53f26
CW
224 }
225
226 // Core field: load schema
8d7a9d07 227 $dao = new $daoName();
5fafc9b0 228 $fieldSpec = $dao->getFieldSpec($fieldName);
4268623e 229
33e61cb8
CW
230 // Ensure we have the canonical name for this field
231 $fieldName = CRM_Utils_Array::value('name', $fieldSpec, $fieldName);
4268623e 232
33e61cb8 233 // Return false if field doesn't exist.
5fafc9b0 234 if (empty($fieldSpec)) {
ecc63dd6
A
235 return FALSE;
236 }
887a4028 237
887a4028
A
238 elseif (!empty($fieldSpec['pseudoconstant'])) {
239 $pseudoconstant = $fieldSpec['pseudoconstant'];
cc6443c4 240
241 // if callback is specified..
22e263ad 242 if (!empty($pseudoconstant['callback'])) {
466fce54 243 $fieldOptions = call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']), $context);
ca0f1c4b
TL
244 //CRM-18223: Allow additions to field options via hook.
245 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $fieldOptions, $params);
246 return $fieldOptions;
cc6443c4 247 }
248
786ad6e1 249 // Merge params with schema defaults
be2fb01f
CW
250 $params += [
251 'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, []),
091fe2a8
CW
252 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant),
253 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant),
be2fb01f 254 ];
091fe2a8 255
f6635f33
AH
256 if ($context == 'abbreviate') {
257 switch ($fieldName) {
258 case 'state_province_id':
259 $params['labelColumn'] = 'abbreviation';
260 break;
2e1050f4 261
f6635f33
AH
262 case 'country_id':
263 $params['labelColumn'] = 'iso_code';
264 break;
2e1050f4 265
f6635f33
AH
266 default:
267 }
268 }
269
091fe2a8 270 // Fetch option group from option_value table
22e263ad 271 if (!empty($pseudoconstant['optionGroupName'])) {
786ad6e1
CW
272 if ($context == 'validate') {
273 $params['labelColumn'] = 'name';
274 }
b432ddaa
CW
275 if ($context == 'match') {
276 $params['keyColumn'] = 'name';
277 }
a42ef93c 278 // Call our generic fn for retrieving from the option_value table
33e61cb8 279 $options = CRM_Core_OptionGroup::values(
887a4028 280 $pseudoconstant['optionGroupName'],
a42ef93c
CW
281 $flip,
282 $params['grouping'],
283 $params['localize'],
f743a6eb 284 $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
091fe2a8 285 $params['labelColumn'] ? $params['labelColumn'] : 'label',
a42ef93c 286 $params['onlyActive'],
c0c9cd82 287 $params['fresh'],
fb011779
PN
288 $params['keyColumn'] ? $params['keyColumn'] : 'value',
289 !empty($params['orderColumn']) ? $params['orderColumn'] : 'weight'
887a4028 290 );
33e61cb8
CW
291 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params);
292 return $options;
887a4028 293 }
091fe2a8
CW
294
295 // Fetch options from other tables
887a4028 296 if (!empty($pseudoconstant['table'])) {
a42ef93c
CW
297 // Normalize params so the serialized cache string will be consistent.
298 CRM_Utils_Array::remove($params, 'flip', 'fresh');
887a4028 299 ksort($params);
a1ef51e2 300 $cacheKey = $daoName . $fieldName . serialize($params);
887a4028 301
a1ef51e2 302 // Retrieve cached options
a42ef93c 303 if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
a1ef51e2 304 $output = self::$cache[$cacheKey];
887a4028 305 }
a1ef51e2 306 else {
4539198e 307 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
2158332a
CW
308 if (!class_exists($daoName)) {
309 return FALSE;
310 }
311 // Get list of fields for the option table
8d7a9d07 312 $dao = new $daoName();
7ae0389f 313 $availableFields = array_keys($dao->fieldKeys());
887a4028 314
a1ef51e2
CW
315 $select = "SELECT %1 AS id, %2 AS label";
316 $from = "FROM %3";
be2fb01f 317 $wheres = [];
a1ef51e2 318 $order = "ORDER BY %2";
786ad6e1 319
b432ddaa
CW
320 // Use machine name in certain contexts
321 if ($context == 'validate' || $context == 'match') {
322 $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn';
a38a89fc 323 if (!empty($pseudoconstant['nameColumn'])) {
b432ddaa 324 $params[$nameField] = $pseudoconstant['nameColumn'];
a38a89fc
CW
325 }
326 elseif (in_array('name', $availableFields)) {
b432ddaa 327 $params[$nameField] = 'name';
a38a89fc 328 }
786ad6e1 329 }
6d68a4cb 330 // Condition param can be passed as an sql clause string or an array of clauses
a1ef51e2 331 if (!empty($params['condition'])) {
6d68a4cb 332 $wheres[] = implode(' AND ', (array) $params['condition']);
a1ef51e2 333 }
6d68a4cb 334 // onlyActive param will automatically filter on common flags
a1ef51e2 335 if (!empty($params['onlyActive'])) {
be2fb01f 336 foreach (['is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0] as $flag => $val) {
6d68a4cb
CW
337 if (in_array($flag, $availableFields)) {
338 $wheres[] = "$flag = $val";
339 }
a1ef51e2
CW
340 }
341 }
6d68a4cb
CW
342 // Filter domain specific options
343 if (in_array('domain_id', $availableFields)) {
344 $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID();
345 }
be2fb01f
CW
346 $queryParams = [
347 1 => [$params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
348 2 => [$params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
349 3 => [$pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
350 ];
a1ef51e2
CW
351 // Add orderColumn param
352 if (!empty($params['orderColumn'])) {
be2fb01f 353 $queryParams[4] = [$params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES];
a1ef51e2
CW
354 $order = "ORDER BY %4";
355 }
356 // Support no sorting if $params[orderColumn] is FALSE
357 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
358 $order = '';
359 }
360 // Default to 'weight' if that column exists
361 elseif (in_array('weight', $availableFields)) {
362 $order = "ORDER BY weight";
363 }
a42ef93c 364
be2fb01f 365 $output = [];
a1ef51e2
CW
366 $query = "$select $from";
367 if ($wheres) {
368 $query .= " WHERE " . implode($wheres, ' AND ');
369 }
370 $query .= ' ' . $order;
371 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
372 while ($dao->fetch()) {
373 $output[$dao->id] = $dao->label;
374 }
a3d8b390
CW
375 // Localize results
376 if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
be2fb01f 377 $I18nParams = [];
65c86f7d 378 if (isset($fieldSpec['localize_context'])) {
379 $I18nParams['context'] = $fieldSpec['localize_context'];
a3d8b390 380 }
a1ef51e2 381 $i18n = CRM_Core_I18n::singleton();
a3d8b390 382 $i18n->localizeArray($output, $I18nParams);
a1ef51e2
CW
383 // Maintain sort by label
384 if ($order == "ORDER BY %2") {
385 CRM_Utils_Array::asort($output);
386 }
387 }
33e61cb8 388 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
a1ef51e2
CW
389 self::$cache[$cacheKey] = $output;
390 }
391 return $flip ? array_flip($output) : $output;
887a4028
A
392 }
393 }
a3d8b390
CW
394
395 // Return "Yes" and "No" for boolean fields
396 elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
be2fb01f 397 $output = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
33e61cb8 398 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
a3d8b390
CW
399 return $flip ? array_flip($output) : $output;
400 }
887a4028
A
401 // If we're still here, it's an error. Return FALSE.
402 return FALSE;
403 }
404
6a488035 405 /**
d09edf64 406 * Fetch the translated label for a field given its key.
dcda1cd5 407 *
6a0b768e
TO
408 * @param string $baoName
409 * @param string $fieldName
e97c66ff 410 * @param string|int $key
a8c23526
CW
411 *
412 * TODO: Accept multivalued input?
dcda1cd5 413 *
2a3f958d
CW
414 * @return bool|null|string
415 * FALSE if the given field has no associated option list
416 * NULL if the given key has no corresponding option
417 * String if label is found
dcda1cd5 418 */
00be9182 419 public static function getLabel($baoName, $fieldName, $key) {
a8c23526
CW
420 $values = $baoName::buildOptions($fieldName, 'get');
421 if ($values === FALSE) {
422 return FALSE;
423 }
424 return CRM_Utils_Array::value($key, $values);
425 }
426
427 /**
d09edf64 428 * Fetch the machine name for a field given its key.
a8c23526 429 *
6a0b768e
TO
430 * @param string $baoName
431 * @param string $fieldName
e97c66ff 432 * @param string|int $key
a8c23526
CW
433 *
434 * @return bool|null|string
435 * FALSE if the given field has no associated option list
436 * NULL if the given key has no corresponding option
437 * String if label is found
438 */
00be9182 439 public static function getName($baoName, $fieldName, $key) {
a8c23526 440 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
441 if ($values === FALSE) {
442 return FALSE;
443 }
444 return CRM_Utils_Array::value($key, $values);
445 }
dcda1cd5
CW
446
447 /**
d09edf64 448 * Fetch the key for a field option given its name.
dcda1cd5 449 *
6a0b768e
TO
450 * @param string $baoName
451 * @param string $fieldName
e97c66ff 452 * @param string|int $value
dcda1cd5 453 *
8d7a9d07 454 * @return bool|null|string|int
2a3f958d
CW
455 * FALSE if the given field has no associated option list
456 * NULL if the given key has no corresponding option
457 * String|Number if key is found
dcda1cd5 458 */
00be9182 459 public static function getKey($baoName, $fieldName, $value) {
a8c23526 460 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
461 if ($values === FALSE) {
462 return FALSE;
463 }
464 return CRM_Utils_Array::key($value, $values);
465 }
dcda1cd5 466
e869b07d
CW
467 /**
468 * Lookup the admin page at which a field's option list can be edited
469 * @param $fieldSpec
470 * @return string|null
471 */
00be9182 472 public static function getOptionEditUrl($fieldSpec) {
e869b07d
CW
473 // If it's an option group, that's easy
474 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
475 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
476 }
477 // For everything else...
478 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
479 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
480 if (!$daoName) {
481 return NULL;
482 }
483 // We don't have good mapping so have to do a bit of guesswork from the menu
e1462487 484 list(, $parent, , $child) = explode('_', $daoName);
e869b07d 485 $sql = "SELECT path FROM civicrm_menu
e1462487
CW
486 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
487 ORDER BY page_callback
e869b07d 488 LIMIT 1";
46710ea6 489 return CRM_Core_DAO::singleValueQuery($sql);
e869b07d
CW
490 }
491 return NULL;
492 }
493
dcda1cd5 494 /**
887688b9 495 * @deprecated generic populate method.
c490a46a 496 * All pseudoconstant functions that use this method are also @deprecated
6a488035
TO
497 *
498 * The static array $var is populated from the db
499 * using the <b>$name DAO</b>.
500 *
501 * Note: any database errors will be trapped by the DAO.
502 *
6a0b768e
TO
503 * @param array $var
504 * The associative array we will fill.
505 * @param string $name
506 * The name of the DAO.
507 * @param bool $all
508 * Get all objects. default is to get only active ones.
509 * @param string $retrieve
510 * The field that we are interested in (normally name, differs in some objects).
511 * @param string $filter
512 * The field that we want to filter the result set with.
513 * @param string $condition
514 * The condition that gets passed to the final query as the WHERE clause.
745b795a 515 *
8eedd10a 516 * @param bool $orderby
745b795a 517 * @param string $key
8eedd10a 518 * @param bool $force
6a488035 519 *
8eedd10a 520 * @return array
6a488035 521 */
8360b701
DL
522 public static function populate(
523 &$var,
524 $name,
525 $all = FALSE,
526 $retrieve = 'name',
527 $filter = 'is_active',
528 $condition = NULL,
529 $orderby = NULL,
530 $key = 'id',
531 $force = NULL
532 ) {
77f080cb 533 $cacheKey = CRM_Utils_Cache::cleanKey("CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}");
353ffa53
TO
534 $cache = CRM_Utils_Cache::singleton();
535 $var = $cache->get($cacheKey);
4f468a50 536 if ($var !== NULL && empty($force)) {
6a488035
TO
537 return $var;
538 }
539
5836c35a 540 /* @var CRM_Core_DAO $object */
8d7a9d07 541 $object = new $name();
6a488035
TO
542
543 $object->selectAdd();
544 $object->selectAdd("$key, $retrieve");
545 if ($condition) {
546 $object->whereAdd($condition);
547 }
548
549 if (!$orderby) {
550 $object->orderBy($retrieve);
551 }
552 else {
553 $object->orderBy($orderby);
554 }
555
556 if (!$all) {
557 $object->$filter = 1;
5836c35a
CW
558 $aclClauses = array_filter($name::getSelectWhereClause());
559 foreach ($aclClauses as $clause) {
560 $object->whereAdd($clause);
561 }
6a488035
TO
562 }
563
564 $object->find();
be2fb01f 565 $var = [];
6a488035
TO
566 while ($object->fetch()) {
567 $var[$object->$key] = $object->$retrieve;
568 }
569
570 $cache->set($cacheKey, $var);
571 }
572
573 /**
d09edf64 574 * Flush given pseudoconstant so it can be reread from db.
6a488035
TO
575 * nex time it's requested.
576 *
2a6da8d7 577 * @param bool|string $name pseudoconstant to be flushed
6a488035 578 */
2683ce94 579 public static function flush($name = 'cache') {
80085473 580 if (isset(self::$$name)) {
fa56270d
CW
581 self::$$name = NULL;
582 }
80085473
CW
583 if ($name == 'cache') {
584 CRM_Core_OptionGroup::flushAll();
7c990617 585 if (isset(\Civi::$statics[__CLASS__])) {
586 unset(\Civi::$statics[__CLASS__]);
587 }
80085473 588 }
6a488035
TO
589 }
590
6a488035 591 /**
c490a46a 592 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 593 *
6a488035
TO
594 * Get all Activty types.
595 *
596 * The static array activityType is returned
597 *
6a488035 598 *
a6c01b45
CW
599 * @return array
600 * array reference of all activity types.
6a488035
TO
601 */
602 public static function &activityType() {
603 $args = func_get_args();
604 $all = CRM_Utils_Array::value(0, $args, TRUE);
605 $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
606 $reset = CRM_Utils_Array::value(2, $args, FALSE);
607 $returnColumn = CRM_Utils_Array::value(3, $args, 'label');
608 $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
609 $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
610 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
611 $index .= '_' . (int) $includeCampaignActivities;
612 $index .= '_' . (int) $onlyComponentActivities;
613
614 if (NULL === self::$activityType) {
be2fb01f 615 self::$activityType = [];
6a488035
TO
616 }
617
618 if (!isset(self::$activityType[$index]) || $reset) {
619 $condition = NULL;
620 if (!$all) {
621 $condition = 'AND filter = 0';
622 }
623 $componentClause = " v.component_id IS NULL";
624 if ($onlyComponentActivities) {
625 $componentClause = " v.component_id IS NOT NULL";
626 }
627
be2fb01f 628 $componentIds = [];
6a488035
TO
629 $compInfo = CRM_Core_Component::getEnabledComponents();
630
631 // build filter for listing activity types only if their
632 // respective components are enabled
633 foreach ($compInfo as $compName => $compObj) {
634 if ($compName == 'CiviCase') {
635 if ($includeCaseActivities) {
636 $componentIds[] = $compObj->componentID;
637 }
638 }
639 elseif ($compName == 'CiviCampaign') {
640 if ($includeCampaignActivities) {
641 $componentIds[] = $compObj->componentID;
642 }
643 }
644 else {
645 $componentIds[] = $compObj->componentID;
646 }
647 }
648
649 if (count($componentIds)) {
650 $componentIds = implode(',', $componentIds);
651 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
652 if ($onlyComponentActivities) {
653 $componentClause = " ( v.component_id IN ($componentIds ) )";
654 }
655 }
bdd7b275 656 $condition = $condition . ' AND ' . $componentClause;
6a488035
TO
657
658 self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
659 }
660 return self::$activityType[$index];
661 }
0e6e8724 662
6a488035
TO
663 /**
664 * Get all the State/Province from database.
665 *
666 * The static array stateProvince is returned, and if it's
667 * called the first time, the <b>State Province DAO</b> is used
668 * to get all the States.
669 *
670 * Note: any database errors will be trapped by the DAO.
671 *
6a488035 672 *
6a0b768e 673 * @param bool|int $id - Optional id to return
6a488035 674 *
2a6da8d7 675 * @param bool $limit
6a488035 676 *
a6c01b45
CW
677 * @return array
678 * array reference of all State/Provinces.
6a488035
TO
679 */
680 public static function &stateProvince($id = FALSE, $limit = TRUE) {
681 if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) {
682 $whereClause = FALSE;
6a488035
TO
683 if ($limit) {
684 $countryIsoCodes = self::countryIsoCode();
0acb7f15 685 $limitCodes = CRM_Core_BAO_Country::provinceLimit();
be2fb01f 686 $limitIds = [];
6a488035
TO
687 foreach ($limitCodes as $code) {
688 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
689 }
690 if (!empty($limitIds)) {
691 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
692 }
693 else {
694 $whereClause = FALSE;
695 }
696 }
697 self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
698
699 // localise the province names if in an non-en_US locale
98466ff9 700 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
701 if ($tsLocale != '' and $tsLocale != 'en_US') {
702 $i18n = CRM_Core_I18n::singleton();
be2fb01f 703 $i18n->localizeArray(self::$stateProvince, [
353ffa53 704 'context' => 'province',
be2fb01f 705 ]);
6a488035
TO
706 self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
707 }
708 }
709 if ($id) {
710 if (array_key_exists($id, self::$stateProvince)) {
711 return self::$stateProvince[$id];
712 }
713 else {
714 $result = NULL;
715 return $result;
716 }
717 }
718 return self::$stateProvince;
719 }
720
721 /**
722 * Get all the State/Province abbreviations from the database.
723 *
724 * Same as above, except gets the abbreviations instead of the names.
725 *
6a488035 726 *
6a0b768e 727 * @param bool|int $id - Optional id to return
2a6da8d7
EM
728 *
729 * @param bool $limit
6a488035 730 *
a6c01b45
CW
731 * @return array
732 * array reference of all State/Province abbreviations.
6a488035 733 */
a8215a8d 734 public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
05a8d245 735 if ($id && is_numeric($id)) {
736 if (!array_key_exists($id, (array) self::$stateProvinceAbbreviation)) {
737 $query = "SELECT abbreviation
6a488035
TO
738FROM civicrm_state_province
739WHERE id = %1";
be2fb01f
CW
740 $params = [
741 1 => [
05a8d245 742 $id,
743 'Integer',
be2fb01f
CW
744 ],
745 ];
05a8d245 746 self::$stateProvinceAbbreviation[$id] = CRM_Core_DAO::singleValueQuery($query, $params);
747 }
748 return self::$stateProvinceAbbreviation[$id];
6a488035 749 }
05a8d245 750 else {
6a488035
TO
751 $whereClause = FALSE;
752
753 if ($limit) {
6a488035 754 $countryIsoCodes = self::countryIsoCode();
0acb7f15 755 $limitCodes = CRM_Core_BAO_Country::provinceLimit();
be2fb01f 756 $limitIds = [];
6a488035
TO
757 foreach ($limitCodes as $code) {
758 $tmpArray = array_keys($countryIsoCodes, $code);
759
760 if (!empty($tmpArray)) {
761 $limitIds[] = array_shift($tmpArray);
762 }
763 }
764 if (!empty($limitIds)) {
765 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
766 }
767 }
768 self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
769 }
770
6a488035
TO
771 return self::$stateProvinceAbbreviation;
772 }
773
4352bd72 774 /**
775 * Get all the State/Province abbreviations from the database for the specified country.
776 *
777 * @param int $countryID
778 *
779 * @return array
780 * array of all State/Province abbreviations for the given country.
781 */
782 public static function stateProvinceAbbreviationForCountry($countryID) {
783 if (!isset(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID])) {
be2fb01f 784 \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID] = [];
4352bd72 785 }
786 self::populate(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation');
787 return \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID];
788 }
789
790 /**
791 * Get all the State/Province abbreviations from the database for the default country.
792 *
793 * @return array
794 * array of all State/Province abbreviations for the given country.
795 */
796 public static function stateProvinceAbbreviationForDefaultCountry() {
797 return CRM_Core_PseudoConstant::stateProvinceAbbreviationForCountry(Civi::settings()->get('defaultContactCountry'));
798 }
799
6a488035
TO
800 /**
801 * Get all the countries from database.
802 *
803 * The static array country is returned, and if it's
804 * called the first time, the <b>Country DAO</b> is used
805 * to get all the countries.
806 *
807 * Note: any database errors will be trapped by the DAO.
808 *
6a488035 809 *
dd244018 810 * @param bool|int $id - Optional id to return
6a488035 811 *
dd244018 812 * @param bool $applyLimit
6a488035 813 *
1273d77c 814 * @return array|null
a6c01b45 815 * array reference of all countries.
6a488035
TO
816 */
817 public static function country($id = FALSE, $applyLimit = TRUE) {
818 if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) {
819
820 $config = CRM_Core_Config::singleton();
be2fb01f 821 $limitCodes = [];
6a488035
TO
822
823 if ($applyLimit) {
824 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
825 // (ensuring it's a subset of the legal values)
826 // K/P: We need to fix this, i dont think it works with new setting files
0acb7f15 827 $limitCodes = CRM_Core_BAO_Country::countryLimit();
6a488035 828 if (!is_array($limitCodes)) {
be2fb01f 829 $limitCodes = [
6a488035 830 $config->countryLimit => 1,
be2fb01f 831 ];
6a488035
TO
832 }
833
834 $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
835 }
836
837 if (count($limitCodes)) {
838 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
839 }
840 else {
841 $whereClause = NULL;
842 }
843
844 self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
845
846 // if default country is set, percolate it to the top
847 if ($config->defaultContactCountry()) {
848 $countryIsoCodes = self::countryIsoCode();
849 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
850 if ($defaultID !== FALSE) {
851 $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country);
852 self::$country = $default + self::$country;
853 }
854 }
855
856 // localise the country names if in an non-en_US locale
98466ff9 857 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
858 if ($tsLocale != '' and $tsLocale != 'en_US') {
859 $i18n = CRM_Core_I18n::singleton();
be2fb01f 860 $i18n->localizeArray(self::$country, [
353ffa53 861 'context' => 'country',
be2fb01f 862 ]);
6a488035
TO
863 self::$country = CRM_Utils_Array::asort(self::$country);
864 }
865 }
866 if ($id) {
867 if (array_key_exists($id, self::$country)) {
868 return self::$country[$id];
869 }
870 else {
1273d77c 871 return NULL;
6a488035
TO
872 }
873 }
874 return self::$country;
875 }
876
877 /**
878 * Get all the country ISO Code abbreviations from the database.
879 *
880 * The static array countryIsoCode is returned, and if it's
881 * called the first time, the <b>Country DAO</b> is used
882 * to get all the countries' ISO codes.
883 *
884 * Note: any database errors will be trapped by the DAO.
885 *
6a488035 886 *
fd31fa4c 887 * @param bool $id
6a488035 888 *
a6c01b45
CW
889 * @return array
890 * array reference of all country ISO codes.
6a488035
TO
891 */
892 public static function &countryIsoCode($id = FALSE) {
893 if (!self::$countryIsoCode) {
894 self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
895 }
896 if ($id) {
897 if (array_key_exists($id, self::$countryIsoCode)) {
898 return self::$countryIsoCode[$id];
899 }
900 else {
7cdc9e9f 901 return CRM_Core_DAO::$_nullObject;
6a488035
TO
902 }
903 }
904 return self::$countryIsoCode;
905 }
906
6a488035 907 /**
c490a46a 908 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 909 *
6a488035
TO
910 * Get all groups from database
911 *
912 * The static array group is returned, and if it's
913 * called the first time, the <b>Group DAO</b> is used
914 * to get all the groups.
915 *
916 * Note: any database errors will be trapped by the DAO.
917 *
6a0b768e
TO
918 * @param string $groupType
919 * Type of group(Access/Mailing).
920 * @param bool $excludeHidden
921 * Exclude hidden groups.
6a488035 922 *
6a488035 923 *
a6c01b45
CW
924 * @return array
925 * array reference of all groups.
6a488035 926 */
addbec40 927 public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
1678a63b 928 if ($groupType === 'validate') {
929 // validate gets passed through from getoptions. Handle in the deprecated
930 // fn rather than change the new pattern.
931 $groupType = NULL;
932 }
6a488035 933 $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
addbec40 934 $groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden);
6a488035 935
6d054a8e 936 if (!isset(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey])) {
937 self::populate(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
6a488035 938 }
6d054a8e 939 return Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey];
6a488035
TO
940 }
941
6a488035 942 /**
d09edf64 943 * Get all permissioned groups from database.
6a488035
TO
944 *
945 * The static array group is returned, and if it's
946 * called the first time, the <b>Group DAO</b> is used
947 * to get all the groups.
948 *
949 * Note: any database errors will be trapped by the DAO.
950 *
6a0b768e
TO
951 * @param string $groupType
952 * Type of group(Access/Mailing).
953 * @param bool $excludeHidden
954 * Exclude hidden groups.
dd244018 955 *
6a488035 956 *
a6c01b45
CW
957 * @return array
958 * array reference of all groups.
6a488035
TO
959 */
960 public static function group($groupType = NULL, $excludeHidden = TRUE) {
961 return CRM_Core_Permission::group($groupType, $excludeHidden);
962 }
963
24431f7b 964 /**
d09edf64 965 * Fetch groups in a nested format suitable for use in select form element.
24431f7b
CW
966 * @param bool $checkPermissions
967 * @param string|null $groupType
968 * @param bool $excludeHidden
969 * @return array
970 */
971 public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
972 $groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden);
973 return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, '&nbsp;&nbsp;', TRUE);
974 }
975
6a488035 976 /**
d09edf64 977 * Get all permissioned groups from database.
6a488035
TO
978 *
979 * The static array group is returned, and if it's
980 * called the first time, the <b>Group DAO</b> is used
981 * to get all the groups.
982 *
983 * Note: any database errors will be trapped by the DAO.
984 *
6a488035 985 *
dd244018
EM
986 * @param bool $onlyPublic
987 * @param null $groupType
988 * @param bool $excludeHidden
6a488035 989 *
a6c01b45
CW
990 * @return array
991 * array reference of all groups.
6a488035
TO
992 */
993 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
994 if (!self::$staticGroup) {
995 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
996 if ($onlyPublic) {
997 $condition .= " AND visibility != 'User and User Admin Only'";
998 }
999
1000 if ($groupType) {
1001 $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
1002 }
1003
1004 if ($excludeHidden) {
1005 $condition .= ' AND is_hidden != 1 ';
1006 }
1007
1008 self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1009 }
1010
1011 return self::$staticGroup;
1012 }
1013
6a488035
TO
1014 /**
1015 * Get all Relationship Types from database.
1016 *
1017 * The static array group is returned, and if it's
1018 * called the first time, the <b>RelationshipType DAO</b> is used
1019 * to get all the relationship types.
1020 *
1021 * Note: any database errors will be trapped by the DAO.
1022 *
6a0b768e
TO
1023 * @param string $valueColumnName
1024 * Db column name/label.
1025 * @param bool $reset
1026 * Reset relationship types if true.
bf48aa29 1027 * @param bool $isActive
fa8e67b8 1028 * Filter by is_active. NULL to disable.
6a488035 1029 *
a6c01b45
CW
1030 * @return array
1031 * array reference of all relationship types.
6a488035 1032 */
fa8e67b8
TO
1033 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) {
1034 $cacheKey = $valueColumnName . '::' . $isActive;
1035 if (!CRM_Utils_Array::value($cacheKey, self::$relationshipType) || $reset) {
be2fb01f 1036 self::$relationshipType[$cacheKey] = [];
6a488035
TO
1037
1038 //now we have name/label columns CRM-3336
1039 $column_a_b = "{$valueColumnName}_a_b";
1040 $column_b_a = "{$valueColumnName}_b_a";
1041
1042 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1043 $relationshipTypeDAO->selectAdd();
1044 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
fa8e67b8
TO
1045 if ($isActive !== NULL) {
1046 $relationshipTypeDAO->is_active = $isActive;
1047 }
6a488035
TO
1048 $relationshipTypeDAO->find();
1049 while ($relationshipTypeDAO->fetch()) {
1050
be2fb01f 1051 self::$relationshipType[$cacheKey][$relationshipTypeDAO->id] = [
05802b8e 1052 'id' => $relationshipTypeDAO->id,
6a488035
TO
1053 $column_a_b => $relationshipTypeDAO->$column_a_b,
1054 $column_b_a => $relationshipTypeDAO->$column_b_a,
1055 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1056 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1057 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1058 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
be2fb01f 1059 ];
6a488035
TO
1060 }
1061 }
1062
fa8e67b8 1063 return self::$relationshipType[$cacheKey];
6a488035
TO
1064 }
1065
6a488035 1066 /**
100fef9d 1067 * Get all the ISO 4217 currency codes
6a488035
TO
1068 *
1069 * so far, we use this for validation only, so there's no point of putting this into the database
1070 *
6a488035 1071 *
a6c01b45
CW
1072 * @return array
1073 * array reference of all currency codes
6a488035
TO
1074 */
1075 public static function &currencyCode() {
1076 if (!self::$currencyCode) {
3b5223ee
HF
1077
1078 $query = "SELECT name FROM civicrm_currency";
1079 $dao = CRM_Core_DAO::executeQuery($query);
be2fb01f 1080 $currencyCode = [];
3b5223ee
HF
1081 while ($dao->fetch()) {
1082 self::$currencyCode[] = $dao->name;
1083 }
6a488035
TO
1084 }
1085 return self::$currencyCode;
1086 }
1087
1088 /**
1089 * Get all the County from database.
1090 *
1091 * The static array county is returned, and if it's
1092 * called the first time, the <b>County DAO</b> is used
1093 * to get all the Counties.
1094 *
1095 * Note: any database errors will be trapped by the DAO.
1096 *
6a488035 1097 *
6a0b768e 1098 * @param bool|int $id - Optional id to return
6a488035 1099 *
a6c01b45
CW
1100 * @return array
1101 * array reference of all Counties
6a488035
TO
1102 */
1103 public static function &county($id = FALSE) {
1104 if (!self::$county) {
1105
1106 $config = CRM_Core_Config::singleton();
1107 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1108 self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1109 }
1110 if ($id) {
1111 if (array_key_exists($id, self::$county)) {
1112 return self::$county[$id];
1113 }
1114 else {
7cdc9e9f 1115 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1116 }
1117 }
1118 return self::$county;
1119 }
1120
6a488035 1121 /**
c490a46a 1122 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1123 * Get all active payment processors
1124 *
1125 * The static array paymentProcessor is returned
1126 *
6a488035 1127 *
6a0b768e
TO
1128 * @param bool $all
1129 * Get payment processors - default is to get only active ones.
1130 * @param bool $test
1131 * Get test payment processors.
6a488035 1132 *
77b97be7 1133 * @param null $additionalCond
6a488035 1134 *
a6c01b45
CW
1135 * @return array
1136 * array of all payment processors
6a488035 1137 */
d6944518 1138 public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
6a488035
TO
1139 $condition = "is_test = ";
1140 $condition .= ($test) ? '1' : '0';
1141
1142 if ($additionalCond) {
1143 $condition .= " AND ( $additionalCond ) ";
1144 }
1145
b44e3f84 1146 // CRM-7178. Make sure we only include payment processors valid in this
6a488035
TO
1147 // domain
1148 $condition .= " AND domain_id = " . CRM_Core_Config::domainID();
1149
1150 $cacheKey = $condition . '_' . (int) $all;
1151 if (!isset(self::$paymentProcessor[$cacheKey])) {
1152 self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1153 }
1154
1155 return self::$paymentProcessor[$cacheKey];
1156 }
1157
1158 /**
c490a46a 1159 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1160 *
1161 * The static array paymentProcessorType is returned
1162 *
6a488035 1163 *
6a0b768e
TO
1164 * @param bool $all
1165 * Get payment processors - default is to get only active ones.
6a488035 1166 *
100fef9d 1167 * @param int $id
77b97be7 1168 * @param string $return
6a488035 1169 *
a6c01b45
CW
1170 * @return array
1171 * array of all payment processor types
6a488035
TO
1172 */
1173 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
86bfa4f6 1174 $cacheKey = $id . '_' . $return;
6a488035
TO
1175 if (empty(self::$paymentProcessorType[$cacheKey])) {
1176 self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1177 }
1178 if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) {
1179 return self::$paymentProcessorType[$cacheKey][$id];
1180 }
1181 return self::$paymentProcessorType[$cacheKey];
1182 }
1183
1184 /**
d09edf64 1185 * Get all the World Regions from Database.
6a488035 1186 *
6a488035 1187 *
77b97be7
EM
1188 * @param bool $id
1189 *
a6c01b45
CW
1190 * @return array
1191 * array reference of all World Regions
6a488035
TO
1192 */
1193 public static function &worldRegion($id = FALSE) {
1194 if (!self::$worldRegions) {
1195 self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1196 }
1197
1198 if ($id) {
1199 if (array_key_exists($id, self::$worldRegions)) {
1200 return self::$worldRegions[$id];
1201 }
1202 else {
7cdc9e9f 1203 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1204 }
1205 }
1206
1207 return self::$worldRegions;
1208 }
1209
6a488035 1210 /**
c490a46a 1211 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 1212 *
6a488035
TO
1213 * Get all Activity Statuses.
1214 *
1215 * The static array activityStatus is returned
1216 *
6a488035 1217 *
77b97be7
EM
1218 * @param string $column
1219 *
a6c01b45
CW
1220 * @return array
1221 * array reference of all activity statuses
6a488035
TO
1222 */
1223 public static function &activityStatus($column = 'label') {
1224 if (NULL === self::$activityStatus) {
be2fb01f 1225 self::$activityStatus = [];
6a488035
TO
1226 }
1227 if (!array_key_exists($column, self::$activityStatus)) {
be2fb01f 1228 self::$activityStatus[$column] = [];
6a488035
TO
1229
1230 self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1231 }
1232
1233 return self::$activityStatus[$column];
1234 }
1235
6a488035 1236 /**
c490a46a 1237 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 1238 *
6a488035
TO
1239 * Get all Visibility levels.
1240 *
1241 * The static array visibility is returned
1242 *
6a488035 1243 *
77b97be7 1244 * @param string $column
6a488035 1245 *
a6c01b45
CW
1246 * @return array
1247 * array reference of all Visibility levels.
6a488035
TO
1248 */
1249 public static function &visibility($column = 'label') {
1250 if (!isset(self::$visibility)) {
be2fb01f 1251 self::$visibility = [];
6a488035
TO
1252 }
1253
1254 if (!isset(self::$visibility[$column])) {
1255 self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
2aa397bc 1256 }
6a488035
TO
1257
1258 return self::$visibility[$column];
1259 }
1260
a0ee3941 1261 /**
100fef9d 1262 * @param int $countryID
a0ee3941
EM
1263 * @param string $field
1264 *
1265 * @return array
1266 */
6a488035
TO
1267 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1268 static $_cache = NULL;
1269
1270 $cacheKey = "{$countryID}_{$field}";
1271 if (!$_cache) {
be2fb01f 1272 $_cache = [];
6a488035
TO
1273 }
1274
1275 if (!empty($_cache[$cacheKey])) {
1276 return $_cache[$cacheKey];
1277 }
1278
1279 $query = "
1280SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1281 FROM civicrm_state_province
1282WHERE country_id = %1
1283ORDER BY name";
be2fb01f
CW
1284 $params = [
1285 1 => [
6a488035
TO
1286 $countryID,
1287 'Integer',
be2fb01f
CW
1288 ],
1289 ];
6a488035
TO
1290
1291 $dao = CRM_Core_DAO::executeQuery($query, $params);
1292
be2fb01f 1293 $result = [];
6a488035
TO
1294 while ($dao->fetch()) {
1295 $result[$dao->id] = $dao->name;
1296 }
1297
1298 // localise the stateProvince names if in an non-en_US locale
1299 $config = CRM_Core_Config::singleton();
98466ff9 1300 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
1301 if ($tsLocale != '' and $tsLocale != 'en_US') {
1302 $i18n = CRM_Core_I18n::singleton();
be2fb01f 1303 $i18n->localizeArray($result, [
6a488035 1304 'context' => 'province',
be2fb01f 1305 ]);
6a488035
TO
1306 $result = CRM_Utils_Array::asort($result);
1307 }
1308
1309 $_cache[$cacheKey] = $result;
1310
1311 CRM_Utils_Hook::buildStateProvinceForCountry($countryID, $result);
1312
1313 return $result;
1314 }
1315
a0ee3941 1316 /**
100fef9d 1317 * @param int $stateID
a0ee3941
EM
1318 *
1319 * @return array
1320 */
6a488035
TO
1321 public static function &countyForState($stateID) {
1322 if (is_array($stateID)) {
1323 $states = implode(", ", $stateID);
1324 $query = "
1325 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1326 FROM civicrm_county
1327 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1328 WHERE civicrm_county.state_province_id in ( $states )
1329 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1330
1331 $dao = CRM_Core_DAO::executeQuery($query);
1332
be2fb01f 1333 $result = [];
6a488035
TO
1334 while ($dao->fetch()) {
1335 $result[$dao->id] = $dao->abbreviation . ': ' . $dao->name;
1336 }
1337 }
1338 else {
1339
1340 static $_cache = NULL;
1341
1342 $cacheKey = "{$stateID}_name";
1343 if (!$_cache) {
be2fb01f 1344 $_cache = [];
6a488035
TO
1345 }
1346
1347 if (!empty($_cache[$cacheKey])) {
1348 return $_cache[$cacheKey];
1349 }
1350
1351 $query = "
1352 SELECT civicrm_county.name name, civicrm_county.id id
1353 FROM civicrm_county
1354 WHERE state_province_id = %1
1355 ORDER BY name";
be2fb01f
CW
1356 $params = [
1357 1 => [
6a488035
TO
1358 $stateID,
1359 'Integer',
be2fb01f
CW
1360 ],
1361 ];
6a488035
TO
1362
1363 $dao = CRM_Core_DAO::executeQuery($query, $params);
1364
be2fb01f 1365 $result = [];
6a488035
TO
1366 while ($dao->fetch()) {
1367 $result[$dao->id] = $dao->name;
1368 }
1369 }
1370
1371 return $result;
1372 }
1373
b05e28de
DL
1374 /**
1375 * Given a state ID return the country ID, this allows
1376 * us to populate forms and values for downstream code
1377 *
5a4f6742 1378 * @param int $stateID
b05e28de 1379 *
1273d77c 1380 * @return int|null
a6c01b45 1381 * the country id that the state belongs to
b05e28de 1382 */
00be9182 1383 public static function countryIDForStateID($stateID) {
b05e28de 1384 if (empty($stateID)) {
1273d77c 1385 return NULL;
b05e28de
DL
1386 }
1387
1388 $query = "
1389SELECT country_id
1390FROM civicrm_state_province
1391WHERE id = %1
1392";
be2fb01f 1393 $params = [1 => [$stateID, 'Integer']];
b05e28de
DL
1394
1395 return CRM_Core_DAO::singleValueQuery($query, $params);
1396 }
1397
6a488035
TO
1398 /**
1399 * Get all types of Greetings.
1400 *
1401 * The static array of greeting is returned
1402 *
6a488035 1403 *
6a0b768e
TO
1404 * @param $filter
1405 * Get All Email Greetings - default is to get only active ones.
6a488035 1406 *
77b97be7 1407 * @param string $columnName
6a488035 1408 *
a6c01b45
CW
1409 * @return array
1410 * array reference of all greetings.
6a488035
TO
1411 */
1412 public static function greeting($filter, $columnName = 'label') {
5f35a2d9 1413 if (!isset(Civi::$statics[__CLASS__]['greeting'])) {
be2fb01f 1414 Civi::$statics[__CLASS__]['greeting'] = [];
5f35a2d9 1415 }
1416
6a488035
TO
1417 $index = $filter['greeting_type'] . '_' . $columnName;
1418
1419 // also add contactType to the array
1420 $contactType = CRM_Utils_Array::value('contact_type', $filter);
1421 if ($contactType) {
1422 $index .= '_' . $contactType;
1423 }
1424
5f35a2d9 1425 if (!CRM_Utils_Array::value($index, Civi::$statics[__CLASS__]['greeting'])) {
6a488035
TO
1426 $filterCondition = NULL;
1427 if ($contactType) {
1428 $filterVal = 'v.filter =';
1429 switch ($contactType) {
1430 case 'Individual':
1431 $filterVal .= "1";
1432 break;
1433
1434 case 'Household':
1435 $filterVal .= "2";
1436 break;
1437
1438 case 'Organization':
1439 $filterVal .= "3";
1440 break;
1441 }
1442 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1443 }
1444
5f35a2d9 1445 Civi::$statics[__CLASS__]['greeting'][$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
6a488035
TO
1446 }
1447
5f35a2d9 1448 return Civi::$statics[__CLASS__]['greeting'][$index];
6a488035
TO
1449 }
1450
6a488035 1451 /**
d09edf64 1452 * Get all extensions.
6a488035
TO
1453 *
1454 * The static array extensions
1455 *
1456 * FIXME: This is called by civix but not by any core code. We
1457 * should provide an API call which civix can use instead.
1458 *
6a488035 1459 *
a6c01b45
CW
1460 * @return array
1461 * array($fullyQualifiedName => $label) list of extensions
6a488035
TO
1462 */
1463 public static function &getExtensions() {
1464 if (!self::$extensions) {
be2fb01f 1465 self::$extensions = [];
6a488035
TO
1466 $sql = '
1467 SELECT full_name, label
1468 FROM civicrm_extension
1469 WHERE is_active = 1
1470 ';
1471 $dao = CRM_Core_DAO::executeQuery($sql);
1472 while ($dao->fetch()) {
1473 self::$extensions[$dao->full_name] = $dao->label;
1474 }
1475 }
1476
1477 return self::$extensions;
1478 }
1479
f743a6eb 1480 /**
d09edf64 1481 * Get all options values.
f743a6eb
CW
1482 *
1483 * The static array option values is returned
1484 *
f743a6eb 1485 *
6a0b768e
TO
1486 * @param bool $optionGroupName
1487 * Get All Option Group values- default is to get only active ones.
f743a6eb 1488 *
100fef9d 1489 * @param int $id
77b97be7 1490 * @param null $condition
f743a6eb 1491 *
a6c01b45
CW
1492 * @return array
1493 * array reference of all Option Group Name
f743a6eb 1494 */
2aa397bc 1495 public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) {
f743a6eb
CW
1496 $cacheKey = $optionGroupName . '_' . $condition;
1497 if (empty(self::$accountOptionValues[$cacheKey])) {
2aa397bc 1498 self::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup::values($optionGroupName, FALSE, FALSE, FALSE, $condition);
f743a6eb
CW
1499 }
1500 if ($id) {
1501 return CRM_Utils_Array::value($id, self::$accountOptionValues[$cacheKey]);
1502 }
1503
1504 return self::$accountOptionValues[$cacheKey];
1505 }
1506
6a488035
TO
1507 /**
1508 * Fetch the list of active extensions of type 'module'
1509 *
5a4f6742
CW
1510 * @param bool $fresh
1511 * Whether to forcibly reload extensions list from canonical store.
6a488035 1512 *
a6c01b45
CW
1513 * @return array
1514 * array(array('prefix' => $, 'file' => $))
6a488035
TO
1515 */
1516 public static function getModuleExtensions($fresh = FALSE) {
1517 return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh);
1518 }
dc428161 1519
dc428161 1520 /**
d09edf64 1521 * Get all tax rates.
dc428161 1522 *
1523 * The static array tax rates is returned
1524 *
a6c01b45
CW
1525 * @return array
1526 * array list of tax rates with the financial type
dc428161 1527 */
1528 public static function getTaxRates() {
27864d8a 1529 if (!isset(Civi::$statics[__CLASS__]['taxRates'])) {
be2fb01f
CW
1530 Civi::$statics[__CLASS__]['taxRates'] = [];
1531 $option = civicrm_api3('option_value', 'get', [
a38ebe6a
SL
1532 'sequential' => 1,
1533 'option_group_id' => 'account_relationship',
1534 'name' => 'Sales Tax Account is',
be2fb01f
CW
1535 ]);
1536 $value = [];
a38ebe6a 1537 if ($option['count'] !== 0) {
02b2d071
SL
1538 if ($option['count'] > 1) {
1539 foreach ($option['values'] as $opt) {
1540 $value[] = $opt['value'];
1541 }
1542 }
1543 else {
1544 $value[] = $option['values'][0]['value'];
1545 }
518fa0ee 1546 $where = 'AND efa.account_relationship IN (' . implode(', ', $value) . ' )';
a38ebe6a
SL
1547 }
1548 else {
1549 $where = '';
1550 }
dc428161 1551 $sql = "
1552 SELECT fa.tax_rate, efa.entity_id
1553 FROM civicrm_entity_financial_account efa
1554 INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
dc428161 1555 WHERE efa.entity_table = 'civicrm_financial_type'
a38ebe6a 1556 {$where}
dc428161 1557 AND fa.is_active = 1";
1558 $dao = CRM_Core_DAO::executeQuery($sql);
1559 while ($dao->fetch()) {
27864d8a 1560 Civi::$statics[__CLASS__]['taxRates'][$dao->entity_id] = $dao->tax_rate;
dc428161 1561 }
1562 }
1563
27864d8a 1564 return Civi::$statics[__CLASS__]['taxRates'];
dc428161 1565 }
96025800 1566
f61d1b83
AS
1567 /**
1568 * Get participant status class options.
1569 *
1570 * @return array
1571 */
1572 public static function emailOnHoldOptions() {
be2fb01f 1573 return [
f61d1b83
AS
1574 '0' => ts('No'),
1575 '1' => ts('On Hold Bounce'),
1576 '2' => ts('On Hold Opt Out'),
be2fb01f 1577 ];
f61d1b83
AS
1578 }
1579
6a488035 1580}