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