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