Merge pull request #7595 from monishdeb/CRM-17807
[civicrm-core.git] / CRM / Core / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 46 * @copyright CiviCRM LLC (c) 2004-2015
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
TO
79 */
80 private static $stateProvinceAbbreviation;
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
102 /**
100fef9d 103 * GroupIterator
6a488035 104 * @var mixed
6a488035
TO
105 */
106 private static $groupIterator;
107
108 /**
100fef9d 109 * RelationshipType
6a488035 110 * @var array
6a488035
TO
111 */
112 private static $relationshipType;
113
114 /**
100fef9d 115 * Civicrm groups that are not smart groups
6a488035 116 * @var array
6a488035
TO
117 */
118 private static $staticGroup;
119
6a488035 120 /**
100fef9d 121 * Currency codes
6a488035 122 * @var array
6a488035
TO
123 */
124 private static $currencyCode;
125
6a488035 126 /**
100fef9d 127 * Payment processor
6a488035 128 * @var array
6a488035
TO
129 */
130 private static $paymentProcessor;
131
132 /**
100fef9d 133 * Payment processor types
6a488035 134 * @var array
6a488035
TO
135 */
136 private static $paymentProcessorType;
137
138 /**
139 * World Region
140 * @var array
6a488035
TO
141 */
142 private static $worldRegions;
143
6a488035 144 /**
c490a46a 145 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 146 *
6a488035
TO
147 * activity status
148 * @var array
6a488035
TO
149 */
150 private static $activityStatus;
151
6a488035
TO
152 /**
153 * Visibility
154 * @var array
6a488035
TO
155 */
156 private static $visibility;
157
6a488035
TO
158 /**
159 * Greetings
160 * @var array
6a488035
TO
161 */
162 private static $greeting;
163
164 /**
165 * Default Greetings
166 * @var array
6a488035
TO
167 */
168 private static $greetingDefaults;
169
170 /**
171 * Extensions of type module
172 * @var array
6a488035
TO
173 */
174 private static $extensions;
175
f743a6eb
CW
176 /**
177 * Financial Account Type
178 * @var array
f743a6eb
CW
179 */
180 private static $accountOptionValues;
181
887a4028 182 /**
6d68a4cb
CW
183 * Low-level option getter, rarely accessed directly.
184 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
07c365da 185 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
6d68a4cb 186 *
76068c6a
TO
187 * NOTE: If someone undertakes a refactoring of this, please consider the use-case of
188 * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
189 * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
190 * between $field lookup and the $pseudoconstant evaluation.
191 *
6a0b768e
TO
192 * @param string $daoName
193 * @param string $fieldName
194 * @param array $params
a42ef93c
CW
195 * - name string name of the option group
196 * - flip boolean results are return in id => label format if false
197 * if true, the results are reversed
198 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
199 * - localize boolean if true, localize the results before returning
f743a6eb 200 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
091fe2a8
CW
201 * - keyColumn string the column to use for 'id'
202 * - labelColumn string the column to use for 'label'
6d68a4cb 203 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
a42ef93c
CW
204 * - onlyActive boolean return only the action option values
205 * - fresh boolean ignore cache entries and go back to DB
353ffa53 206 * @param string $context : Context string
887a4028 207 *
8d7a9d07 208 * @return array|bool
72b3a70c 209 * array on success, FALSE on error.
a42ef93c 210 *
887a4028 211 */
786ad6e1
CW
212 public static function get($daoName, $fieldName, $params = array(), $context = NULL) {
213 CRM_Core_DAO::buildOptionsContext($context);
f2b53f26 214 $flip = !empty($params['flip']);
786ad6e1
CW
215 // Merge params with defaults
216 $params += array(
217 'grouping' => FALSE,
218 'localize' => FALSE,
c203a3d6 219 'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE,
786ad6e1 220 'fresh' => FALSE,
33e61cb8 221 'context' => $context,
786ad6e1 222 );
33e61cb8 223 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getCanonicalClassName($daoName));
f2b53f26
CW
224
225 // Custom fields are not in the schema
786ad6e1 226 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
2921fb78 227 $customField = new CRM_Core_BAO_CustomField();
a3d8b390 228 $customField->id = (int) substr($fieldName, 7);
2921fb78 229 $options = $customField->getOptions();
2fea9ed9
CW
230 if ($options && $flip) {
231 $options = array_flip($options);
a3d8b390
CW
232 }
233 $customField->free();
786ad6e1 234 return $options;
f2b53f26
CW
235 }
236
237 // Core field: load schema
8d7a9d07 238 $dao = new $daoName();
5fafc9b0 239 $fieldSpec = $dao->getFieldSpec($fieldName);
091fe2a8 240 $dao->free();
4268623e 241
33e61cb8
CW
242 // Ensure we have the canonical name for this field
243 $fieldName = CRM_Utils_Array::value('name', $fieldSpec, $fieldName);
4268623e 244
33e61cb8 245 // Return false if field doesn't exist.
5fafc9b0 246 if (empty($fieldSpec)) {
ecc63dd6
A
247 return FALSE;
248 }
887a4028 249
887a4028
A
250 elseif (!empty($fieldSpec['pseudoconstant'])) {
251 $pseudoconstant = $fieldSpec['pseudoconstant'];
cc6443c4 252
253 // if callback is specified..
22e263ad 254 if (!empty($pseudoconstant['callback'])) {
c8074a93 255 return call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']));
cc6443c4 256 }
257
786ad6e1 258 // Merge params with schema defaults
091fe2a8 259 $params += array(
786ad6e1 260 'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()),
091fe2a8
CW
261 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant),
262 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant),
091fe2a8
CW
263 );
264
f6635f33
AH
265 if ($context == 'abbreviate') {
266 switch ($fieldName) {
267 case 'state_province_id':
268 $params['labelColumn'] = 'abbreviation';
269 break;
2e1050f4 270
f6635f33
AH
271 case 'country_id':
272 $params['labelColumn'] = 'iso_code';
273 break;
2e1050f4 274
f6635f33
AH
275 default:
276 }
277 }
278
091fe2a8 279 // Fetch option group from option_value table
22e263ad 280 if (!empty($pseudoconstant['optionGroupName'])) {
786ad6e1
CW
281 if ($context == 'validate') {
282 $params['labelColumn'] = 'name';
283 }
b432ddaa
CW
284 if ($context == 'match') {
285 $params['keyColumn'] = 'name';
286 }
a42ef93c 287 // Call our generic fn for retrieving from the option_value table
33e61cb8 288 $options = CRM_Core_OptionGroup::values(
887a4028 289 $pseudoconstant['optionGroupName'],
a42ef93c
CW
290 $flip,
291 $params['grouping'],
292 $params['localize'],
f743a6eb 293 $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
091fe2a8 294 $params['labelColumn'] ? $params['labelColumn'] : 'label',
a42ef93c 295 $params['onlyActive'],
c0c9cd82
CW
296 $params['fresh'],
297 $params['keyColumn'] ? $params['keyColumn'] : 'value'
887a4028 298 );
33e61cb8
CW
299 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params);
300 return $options;
887a4028 301 }
091fe2a8
CW
302
303 // Fetch options from other tables
887a4028 304 if (!empty($pseudoconstant['table'])) {
a42ef93c
CW
305 // Normalize params so the serialized cache string will be consistent.
306 CRM_Utils_Array::remove($params, 'flip', 'fresh');
887a4028 307 ksort($params);
a1ef51e2 308 $cacheKey = $daoName . $fieldName . serialize($params);
887a4028 309
a1ef51e2 310 // Retrieve cached options
a42ef93c 311 if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
a1ef51e2 312 $output = self::$cache[$cacheKey];
887a4028 313 }
a1ef51e2 314 else {
4539198e 315 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
2158332a
CW
316 if (!class_exists($daoName)) {
317 return FALSE;
318 }
319 // Get list of fields for the option table
8d7a9d07 320 $dao = new $daoName();
7ae0389f
CW
321 $availableFields = array_keys($dao->fieldKeys());
322 $dao->free();
887a4028 323
a1ef51e2
CW
324 $select = "SELECT %1 AS id, %2 AS label";
325 $from = "FROM %3";
326 $wheres = array();
327 $order = "ORDER BY %2";
786ad6e1 328
b432ddaa
CW
329 // Use machine name in certain contexts
330 if ($context == 'validate' || $context == 'match') {
331 $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn';
a38a89fc 332 if (!empty($pseudoconstant['nameColumn'])) {
b432ddaa 333 $params[$nameField] = $pseudoconstant['nameColumn'];
a38a89fc
CW
334 }
335 elseif (in_array('name', $availableFields)) {
b432ddaa 336 $params[$nameField] = 'name';
a38a89fc 337 }
786ad6e1 338 }
6d68a4cb 339 // Condition param can be passed as an sql clause string or an array of clauses
a1ef51e2 340 if (!empty($params['condition'])) {
6d68a4cb 341 $wheres[] = implode(' AND ', (array) $params['condition']);
a1ef51e2 342 }
6d68a4cb 343 // onlyActive param will automatically filter on common flags
a1ef51e2 344 if (!empty($params['onlyActive'])) {
21d4b9c1 345 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0) as $flag => $val) {
6d68a4cb
CW
346 if (in_array($flag, $availableFields)) {
347 $wheres[] = "$flag = $val";
348 }
a1ef51e2
CW
349 }
350 }
6d68a4cb
CW
351 // Filter domain specific options
352 if (in_array('domain_id', $availableFields)) {
353 $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID();
354 }
a1ef51e2 355 $queryParams = array(
353ffa53
TO
356 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
357 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
358 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
a1ef51e2
CW
359 );
360 // Add orderColumn param
361 if (!empty($params['orderColumn'])) {
362 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES);
363 $order = "ORDER BY %4";
364 }
365 // Support no sorting if $params[orderColumn] is FALSE
366 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
367 $order = '';
368 }
369 // Default to 'weight' if that column exists
370 elseif (in_array('weight', $availableFields)) {
371 $order = "ORDER BY weight";
372 }
a42ef93c 373
a1ef51e2
CW
374 $output = array();
375 $query = "$select $from";
376 if ($wheres) {
377 $query .= " WHERE " . implode($wheres, ' AND ');
378 }
379 $query .= ' ' . $order;
380 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
381 while ($dao->fetch()) {
382 $output[$dao->id] = $dao->label;
383 }
384 $dao->free();
a3d8b390
CW
385 // Localize results
386 if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
387 $I18nParams = array();
388 if ($pseudoconstant['table'] == 'civicrm_country') {
389 $I18nParams['context'] = 'country';
390 }
391 if ($pseudoconstant['table'] == 'civicrm_state_province') {
392 $I18nParams['context'] = 'province';
393 }
a1ef51e2 394 $i18n = CRM_Core_I18n::singleton();
a3d8b390 395 $i18n->localizeArray($output, $I18nParams);
a1ef51e2
CW
396 // Maintain sort by label
397 if ($order == "ORDER BY %2") {
398 CRM_Utils_Array::asort($output);
399 }
400 }
33e61cb8 401 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
a1ef51e2
CW
402 self::$cache[$cacheKey] = $output;
403 }
404 return $flip ? array_flip($output) : $output;
887a4028
A
405 }
406 }
a3d8b390
CW
407
408 // Return "Yes" and "No" for boolean fields
409 elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
3b66f502 410 $output = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
33e61cb8 411 CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
a3d8b390
CW
412 return $flip ? array_flip($output) : $output;
413 }
887a4028
A
414 // If we're still here, it's an error. Return FALSE.
415 return FALSE;
416 }
417
6a488035 418 /**
d09edf64 419 * Fetch the translated label for a field given its key.
dcda1cd5 420 *
6a0b768e
TO
421 * @param string $baoName
422 * @param string $fieldName
423 * @param string|Int $key
a8c23526
CW
424 *
425 * TODO: Accept multivalued input?
dcda1cd5 426 *
2a3f958d
CW
427 * @return bool|null|string
428 * FALSE if the given field has no associated option list
429 * NULL if the given key has no corresponding option
430 * String if label is found
dcda1cd5 431 */
00be9182 432 public static function getLabel($baoName, $fieldName, $key) {
a8c23526
CW
433 $values = $baoName::buildOptions($fieldName, 'get');
434 if ($values === FALSE) {
435 return FALSE;
436 }
437 return CRM_Utils_Array::value($key, $values);
438 }
439
440 /**
d09edf64 441 * Fetch the machine name for a field given its key.
a8c23526 442 *
6a0b768e
TO
443 * @param string $baoName
444 * @param string $fieldName
445 * @param string|Int $key
a8c23526
CW
446 *
447 * @return bool|null|string
448 * FALSE if the given field has no associated option list
449 * NULL if the given key has no corresponding option
450 * String if label is found
451 */
00be9182 452 public static function getName($baoName, $fieldName, $key) {
a8c23526 453 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
454 if ($values === FALSE) {
455 return FALSE;
456 }
457 return CRM_Utils_Array::value($key, $values);
458 }
dcda1cd5
CW
459
460 /**
d09edf64 461 * Fetch the key for a field option given its name.
dcda1cd5 462 *
6a0b768e
TO
463 * @param string $baoName
464 * @param string $fieldName
465 * @param string|Int $value
dcda1cd5 466 *
8d7a9d07 467 * @return bool|null|string|int
2a3f958d
CW
468 * FALSE if the given field has no associated option list
469 * NULL if the given key has no corresponding option
470 * String|Number if key is found
dcda1cd5 471 */
00be9182 472 public static function getKey($baoName, $fieldName, $value) {
a8c23526 473 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
474 if ($values === FALSE) {
475 return FALSE;
476 }
477 return CRM_Utils_Array::key($value, $values);
478 }
dcda1cd5 479
e869b07d
CW
480 /**
481 * Lookup the admin page at which a field's option list can be edited
482 * @param $fieldSpec
483 * @return string|null
484 */
00be9182 485 public static function getOptionEditUrl($fieldSpec) {
e869b07d
CW
486 // If it's an option group, that's easy
487 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
488 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
489 }
490 // For everything else...
491 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
492 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
493 if (!$daoName) {
494 return NULL;
495 }
496 // We don't have good mapping so have to do a bit of guesswork from the menu
e1462487 497 list(, $parent, , $child) = explode('_', $daoName);
e869b07d 498 $sql = "SELECT path FROM civicrm_menu
e1462487
CW
499 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
500 ORDER BY page_callback
e869b07d
CW
501 LIMIT 1";
502 return CRM_Core_Dao::singleValueQuery($sql);
503 }
504 return NULL;
505 }
506
dcda1cd5 507 /**
d09edf64 508 * DEPRECATED generic populate method.
c490a46a 509 * All pseudoconstant functions that use this method are also @deprecated
6a488035
TO
510 *
511 * The static array $var is populated from the db
512 * using the <b>$name DAO</b>.
513 *
514 * Note: any database errors will be trapped by the DAO.
515 *
6a0b768e
TO
516 * @param array $var
517 * The associative array we will fill.
518 * @param string $name
519 * The name of the DAO.
520 * @param bool $all
521 * Get all objects. default is to get only active ones.
522 * @param string $retrieve
523 * The field that we are interested in (normally name, differs in some objects).
524 * @param string $filter
525 * The field that we want to filter the result set with.
526 * @param string $condition
527 * The condition that gets passed to the final query as the WHERE clause.
745b795a 528 *
8eedd10a 529 * @param bool $orderby
745b795a 530 * @param string $key
8eedd10a 531 * @param bool $force
6a488035 532 *
8eedd10a 533 * @return array
6a488035 534 */
8360b701
DL
535 public static function populate(
536 &$var,
537 $name,
538 $all = FALSE,
539 $retrieve = 'name',
540 $filter = 'is_active',
541 $condition = NULL,
542 $orderby = NULL,
543 $key = 'id',
544 $force = NULL
545 ) {
6a488035 546 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
353ffa53
TO
547 $cache = CRM_Utils_Cache::singleton();
548 $var = $cache->get($cacheKey);
6a488035
TO
549 if ($var && empty($force)) {
550 return $var;
551 }
552
8d7a9d07 553 $object = new $name();
6a488035
TO
554
555 $object->selectAdd();
556 $object->selectAdd("$key, $retrieve");
557 if ($condition) {
558 $object->whereAdd($condition);
559 }
560
561 if (!$orderby) {
562 $object->orderBy($retrieve);
563 }
564 else {
565 $object->orderBy($orderby);
566 }
567
568 if (!$all) {
569 $object->$filter = 1;
570 }
571
572 $object->find();
573 $var = array();
574 while ($object->fetch()) {
575 $var[$object->$key] = $object->$retrieve;
576 }
577
578 $cache->set($cacheKey, $var);
579 }
580
581 /**
d09edf64 582 * Flush given pseudoconstant so it can be reread from db.
6a488035
TO
583 * nex time it's requested.
584 *
6a488035 585 *
2a6da8d7 586 * @param bool|string $name pseudoconstant to be flushed
6a488035 587 */
2683ce94 588 public static function flush($name = 'cache') {
80085473 589 if (isset(self::$$name)) {
fa56270d
CW
590 self::$$name = NULL;
591 }
80085473
CW
592 if ($name == 'cache') {
593 CRM_Core_OptionGroup::flushAll();
594 }
6a488035
TO
595 }
596
6a488035 597 /**
c490a46a 598 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 599 *
6a488035
TO
600 * Get all Activty types.
601 *
602 * The static array activityType is returned
603 *
6a488035 604 *
a6c01b45
CW
605 * @return array
606 * array reference of all activity types.
6a488035
TO
607 */
608 public static function &activityType() {
609 $args = func_get_args();
610 $all = CRM_Utils_Array::value(0, $args, TRUE);
611 $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
612 $reset = CRM_Utils_Array::value(2, $args, FALSE);
613 $returnColumn = CRM_Utils_Array::value(3, $args, 'label');
614 $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
615 $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
616 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
617 $index .= '_' . (int) $includeCampaignActivities;
618 $index .= '_' . (int) $onlyComponentActivities;
619
620 if (NULL === self::$activityType) {
621 self::$activityType = array();
622 }
623
624 if (!isset(self::$activityType[$index]) || $reset) {
625 $condition = NULL;
626 if (!$all) {
627 $condition = 'AND filter = 0';
628 }
629 $componentClause = " v.component_id IS NULL";
630 if ($onlyComponentActivities) {
631 $componentClause = " v.component_id IS NOT NULL";
632 }
633
634 $componentIds = array();
635 $compInfo = CRM_Core_Component::getEnabledComponents();
636
637 // build filter for listing activity types only if their
638 // respective components are enabled
639 foreach ($compInfo as $compName => $compObj) {
640 if ($compName == 'CiviCase') {
641 if ($includeCaseActivities) {
642 $componentIds[] = $compObj->componentID;
643 }
644 }
645 elseif ($compName == 'CiviCampaign') {
646 if ($includeCampaignActivities) {
647 $componentIds[] = $compObj->componentID;
648 }
649 }
650 else {
651 $componentIds[] = $compObj->componentID;
652 }
653 }
654
655 if (count($componentIds)) {
656 $componentIds = implode(',', $componentIds);
657 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
658 if ($onlyComponentActivities) {
659 $componentClause = " ( v.component_id IN ($componentIds ) )";
660 }
661 }
bdd7b275 662 $condition = $condition . ' AND ' . $componentClause;
6a488035
TO
663
664 self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
665 }
666 return self::$activityType[$index];
667 }
0e6e8724 668
6a488035
TO
669 /**
670 * Get all the State/Province from database.
671 *
672 * The static array stateProvince is returned, and if it's
673 * called the first time, the <b>State Province DAO</b> is used
674 * to get all the States.
675 *
676 * Note: any database errors will be trapped by the DAO.
677 *
6a488035 678 *
6a0b768e 679 * @param bool|int $id - Optional id to return
6a488035 680 *
2a6da8d7 681 * @param bool $limit
6a488035 682 *
a6c01b45
CW
683 * @return array
684 * array reference of all State/Provinces.
6a488035
TO
685 */
686 public static function &stateProvince($id = FALSE, $limit = TRUE) {
687 if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) {
688 $whereClause = FALSE;
6a488035
TO
689 if ($limit) {
690 $countryIsoCodes = self::countryIsoCode();
0acb7f15 691 $limitCodes = CRM_Core_BAO_Country::provinceLimit();
353ffa53 692 $limitIds = array();
6a488035
TO
693 foreach ($limitCodes as $code) {
694 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
695 }
696 if (!empty($limitIds)) {
697 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
698 }
699 else {
700 $whereClause = FALSE;
701 }
702 }
703 self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
704
705 // localise the province names if in an non-en_US locale
706 global $tsLocale;
707 if ($tsLocale != '' and $tsLocale != 'en_US') {
708 $i18n = CRM_Core_I18n::singleton();
709 $i18n->localizeArray(self::$stateProvince, array(
353ffa53
TO
710 'context' => 'province',
711 ));
6a488035
TO
712 self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
713 }
714 }
715 if ($id) {
716 if (array_key_exists($id, self::$stateProvince)) {
717 return self::$stateProvince[$id];
718 }
719 else {
720 $result = NULL;
721 return $result;
722 }
723 }
724 return self::$stateProvince;
725 }
726
727 /**
728 * Get all the State/Province abbreviations from the database.
729 *
730 * Same as above, except gets the abbreviations instead of the names.
731 *
6a488035 732 *
6a0b768e 733 * @param bool|int $id - Optional id to return
2a6da8d7
EM
734 *
735 * @param bool $limit
6a488035 736 *
a6c01b45
CW
737 * @return array
738 * array reference of all State/Province abbreviations.
6a488035 739 */
a8215a8d 740 public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
6a488035
TO
741 if ($id > 1) {
742 $query = "
743SELECT abbreviation
744FROM civicrm_state_province
745WHERE id = %1";
746 $params = array(
747 1 => array(
748 $id,
749 'Integer',
750 ),
751 );
752 return CRM_Core_DAO::singleValueQuery($query, $params);
753 }
754
755 if (!self::$stateProvinceAbbreviation || !$id) {
756
757 $whereClause = FALSE;
758
759 if ($limit) {
6a488035 760 $countryIsoCodes = self::countryIsoCode();
0acb7f15 761 $limitCodes = CRM_Core_BAO_Country::provinceLimit();
353ffa53 762 $limitIds = array();
6a488035
TO
763 foreach ($limitCodes as $code) {
764 $tmpArray = array_keys($countryIsoCodes, $code);
765
766 if (!empty($tmpArray)) {
767 $limitIds[] = array_shift($tmpArray);
768 }
769 }
770 if (!empty($limitIds)) {
771 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
772 }
773 }
774 self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
775 }
776
777 if ($id) {
778 if (array_key_exists($id, self::$stateProvinceAbbreviation)) {
779 return self::$stateProvinceAbbreviation[$id];
780 }
781 else {
782 $result = NULL;
783 return $result;
784 }
785 }
786 return self::$stateProvinceAbbreviation;
787 }
788
789 /**
790 * Get all the countries from database.
791 *
792 * The static array country is returned, and if it's
793 * called the first time, the <b>Country DAO</b> is used
794 * to get all the countries.
795 *
796 * Note: any database errors will be trapped by the DAO.
797 *
6a488035 798 *
dd244018 799 * @param bool|int $id - Optional id to return
6a488035 800 *
dd244018 801 * @param bool $applyLimit
6a488035 802 *
a6c01b45
CW
803 * @return array
804 * array reference of all countries.
6a488035
TO
805 */
806 public static function country($id = FALSE, $applyLimit = TRUE) {
807 if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) {
808
809 $config = CRM_Core_Config::singleton();
810 $limitCodes = array();
811
812 if ($applyLimit) {
813 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
814 // (ensuring it's a subset of the legal values)
815 // K/P: We need to fix this, i dont think it works with new setting files
0acb7f15 816 $limitCodes = CRM_Core_BAO_Country::countryLimit();
6a488035
TO
817 if (!is_array($limitCodes)) {
818 $limitCodes = array(
819 $config->countryLimit => 1,
820 );
821 }
822
823 $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
824 }
825
826 if (count($limitCodes)) {
827 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
828 }
829 else {
830 $whereClause = NULL;
831 }
832
833 self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
834
835 // if default country is set, percolate it to the top
836 if ($config->defaultContactCountry()) {
837 $countryIsoCodes = self::countryIsoCode();
838 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
839 if ($defaultID !== FALSE) {
840 $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country);
841 self::$country = $default + self::$country;
842 }
843 }
844
845 // localise the country names if in an non-en_US locale
846 global $tsLocale;
847 if ($tsLocale != '' and $tsLocale != 'en_US') {
848 $i18n = CRM_Core_I18n::singleton();
849 $i18n->localizeArray(self::$country, array(
353ffa53
TO
850 'context' => 'country',
851 ));
6a488035
TO
852 self::$country = CRM_Utils_Array::asort(self::$country);
853 }
854 }
855 if ($id) {
856 if (array_key_exists($id, self::$country)) {
857 return self::$country[$id];
858 }
859 else {
7cdc9e9f 860 return CRM_Core_DAO::$_nullObject;
6a488035
TO
861 }
862 }
863 return self::$country;
864 }
865
866 /**
867 * Get all the country ISO Code abbreviations from the database.
868 *
869 * The static array countryIsoCode is returned, and if it's
870 * called the first time, the <b>Country DAO</b> is used
871 * to get all the countries' ISO codes.
872 *
873 * Note: any database errors will be trapped by the DAO.
874 *
6a488035 875 *
fd31fa4c 876 * @param bool $id
6a488035 877 *
a6c01b45
CW
878 * @return array
879 * array reference of all country ISO codes.
6a488035
TO
880 */
881 public static function &countryIsoCode($id = FALSE) {
882 if (!self::$countryIsoCode) {
883 self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
884 }
885 if ($id) {
886 if (array_key_exists($id, self::$countryIsoCode)) {
887 return self::$countryIsoCode[$id];
888 }
889 else {
7cdc9e9f 890 return CRM_Core_DAO::$_nullObject;
6a488035
TO
891 }
892 }
893 return self::$countryIsoCode;
894 }
895
6a488035 896 /**
c490a46a 897 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 898 *
6a488035
TO
899 * Get all groups from database
900 *
901 * The static array group is returned, and if it's
902 * called the first time, the <b>Group DAO</b> is used
903 * to get all the groups.
904 *
905 * Note: any database errors will be trapped by the DAO.
906 *
6a0b768e
TO
907 * @param string $groupType
908 * Type of group(Access/Mailing).
909 * @param bool $excludeHidden
910 * Exclude hidden groups.
6a488035 911 *
6a488035 912 *
a6c01b45
CW
913 * @return array
914 * array reference of all groups.
6a488035 915 */
addbec40 916 public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
6a488035
TO
917 $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
918
919 if (!self::$group) {
920 self::$group = array();
921 }
922
addbec40 923 $groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden);
6a488035
TO
924
925 if (!isset(self::$group[$groupKey])) {
926 self::$group[$groupKey] = NULL;
927 self::populate(self::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
928 }
929 return self::$group[$groupKey];
930 }
931
932 /**
933 * Create or get groups iterator (iterates over nested groups in a
934 * logical fashion)
935 *
936 * The GroupNesting instance is returned; it's created if this is being
937 * called for the first time
938 *
939 *
6a488035 940 *
dd244018 941 * @param bool $styledLabels
6a488035 942 *
72b3a70c 943 * @return CRM_Contact_BAO_GroupNesting
6a488035
TO
944 */
945 public static function &groupIterator($styledLabels = FALSE) {
946 if (!self::$groupIterator) {
d424ffde
CW
947 // When used as an object, GroupNesting implements Iterator
948 // and iterates nested groups in a logical manner for us
6a488035
TO
949 self::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
950 }
951 return self::$groupIterator;
952 }
953
954 /**
d09edf64 955 * Get all permissioned groups from database.
6a488035
TO
956 *
957 * The static array group is returned, and if it's
958 * called the first time, the <b>Group DAO</b> is used
959 * to get all the groups.
960 *
961 * Note: any database errors will be trapped by the DAO.
962 *
6a0b768e
TO
963 * @param string $groupType
964 * Type of group(Access/Mailing).
965 * @param bool $excludeHidden
966 * Exclude hidden groups.
dd244018 967 *
6a488035 968 *
a6c01b45
CW
969 * @return array
970 * array reference of all groups.
6a488035
TO
971 */
972 public static function group($groupType = NULL, $excludeHidden = TRUE) {
973 return CRM_Core_Permission::group($groupType, $excludeHidden);
974 }
975
24431f7b 976 /**
d09edf64 977 * Fetch groups in a nested format suitable for use in select form element.
24431f7b
CW
978 * @param bool $checkPermissions
979 * @param string|null $groupType
980 * @param bool $excludeHidden
981 * @return array
982 */
983 public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
984 $groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden);
985 return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, '&nbsp;&nbsp;', TRUE);
986 }
987
6a488035 988 /**
d09edf64 989 * Get all permissioned groups from database.
6a488035
TO
990 *
991 * The static array group is returned, and if it's
992 * called the first time, the <b>Group DAO</b> is used
993 * to get all the groups.
994 *
995 * Note: any database errors will be trapped by the DAO.
996 *
6a488035 997 *
dd244018
EM
998 * @param bool $onlyPublic
999 * @param null $groupType
1000 * @param bool $excludeHidden
6a488035 1001 *
a6c01b45
CW
1002 * @return array
1003 * array reference of all groups.
6a488035
TO
1004 */
1005 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1006 if (!self::$staticGroup) {
1007 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1008 if ($onlyPublic) {
1009 $condition .= " AND visibility != 'User and User Admin Only'";
1010 }
1011
1012 if ($groupType) {
1013 $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
1014 }
1015
1016 if ($excludeHidden) {
1017 $condition .= ' AND is_hidden != 1 ';
1018 }
1019
1020 self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1021 }
1022
1023 return self::$staticGroup;
1024 }
1025
6a488035
TO
1026 /**
1027 * Get all Relationship Types from database.
1028 *
1029 * The static array group is returned, and if it's
1030 * called the first time, the <b>RelationshipType DAO</b> is used
1031 * to get all the relationship types.
1032 *
1033 * Note: any database errors will be trapped by the DAO.
1034 *
6a0b768e
TO
1035 * @param string $valueColumnName
1036 * Db column name/label.
1037 * @param bool $reset
1038 * Reset relationship types if true.
fa8e67b8
TO
1039 * @param bool|NULL $isActive
1040 * Filter by is_active. NULL to disable.
6a488035 1041 *
a6c01b45
CW
1042 * @return array
1043 * array reference of all relationship types.
6a488035 1044 */
fa8e67b8
TO
1045 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) {
1046 $cacheKey = $valueColumnName . '::' . $isActive;
1047 if (!CRM_Utils_Array::value($cacheKey, self::$relationshipType) || $reset) {
1048 self::$relationshipType[$cacheKey] = array();
6a488035
TO
1049
1050 //now we have name/label columns CRM-3336
1051 $column_a_b = "{$valueColumnName}_a_b";
1052 $column_b_a = "{$valueColumnName}_b_a";
1053
1054 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1055 $relationshipTypeDAO->selectAdd();
1056 $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
1057 if ($isActive !== NULL) {
1058 $relationshipTypeDAO->is_active = $isActive;
1059 }
6a488035
TO
1060 $relationshipTypeDAO->find();
1061 while ($relationshipTypeDAO->fetch()) {
1062
fa8e67b8 1063 self::$relationshipType[$cacheKey][$relationshipTypeDAO->id] = array(
05802b8e 1064 'id' => $relationshipTypeDAO->id,
6a488035
TO
1065 $column_a_b => $relationshipTypeDAO->$column_a_b,
1066 $column_b_a => $relationshipTypeDAO->$column_b_a,
1067 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1068 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1069 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1070 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1071 );
1072 }
1073 }
1074
fa8e67b8 1075 return self::$relationshipType[$cacheKey];
6a488035
TO
1076 }
1077
6a488035 1078 /**
100fef9d 1079 * Get all the ISO 4217 currency codes
6a488035
TO
1080 *
1081 * so far, we use this for validation only, so there's no point of putting this into the database
1082 *
6a488035 1083 *
a6c01b45
CW
1084 * @return array
1085 * array reference of all currency codes
6a488035
TO
1086 */
1087 public static function &currencyCode() {
1088 if (!self::$currencyCode) {
1089 self::$currencyCode = array(
1090 'AFN',
1091 'ALL',
1092 'DZD',
1093 'USD',
1094 'EUR',
1095 'AOA',
1096 'XCD',
1097 'XCD',
1098 'ARS',
1099 'AMD',
1100 'AWG',
1101 'AUD',
1102 'EUR',
1103 'AZM',
1104 'BSD',
1105 'BHD',
1106 'BDT',
1107 'BBD',
1108 'BYR',
1109 'EUR',
1110 'BZD',
1111 'XOF',
1112 'BMD',
1113 'INR',
1114 'BTN',
1115 'BOB',
1116 'BOV',
1117 'BAM',
1118 'BWP',
1119 'NOK',
1120 'BRL',
1121 'USD',
1122 'BND',
1123 'BGN',
1124 'XOF',
1125 'BIF',
1126 'KHR',
1127 'XAF',
1128 'CAD',
1129 'CVE',
1130 'KYD',
1131 'XAF',
1132 'XAF',
1133 'CLP',
1134 'CLF',
1135 'CNY',
1136 'AUD',
1137 'AUD',
1138 'COP',
1139 'COU',
1140 'KMF',
1141 'XAF',
1142 'CDF',
1143 'NZD',
1144 'CRC',
1145 'XOF',
1146 'HRK',
1147 'CUP',
1148 'CYP',
1149 'CZK',
1150 'DKK',
1151 'DJF',
1152 'XCD',
1153 'DOP',
1154 'USD',
1155 'EGP',
1156 'SVC',
1157 'USD',
1158 'XAF',
1159 'ERN',
1160 'EEK',
1161 'ETB',
1162 'FKP',
1163 'DKK',
1164 'FJD',
1165 'EUR',
1166 'EUR',
1167 'EUR',
1168 'XPF',
1169 'EUR',
1170 'XAF',
1171 'GMD',
1172 'GEL',
1173 'EUR',
1174 'GHC',
1175 'GIP',
1176 'EUR',
1177 'DKK',
1178 'XCD',
1179 'EUR',
1180 'USD',
1181 'GTQ',
1182 'GNF',
1183 'GWP',
1184 'XOF',
1185 'GYD',
1186 'HTG',
1187 'USD',
1188 'AUD',
1189 'EUR',
1190 'HNL',
1191 'HKD',
1192 'HUF',
1193 'ISK',
1194 'INR',
1195 'IDR',
1196 'XDR',
1197 'IRR',
1198 'IQD',
1199 'EUR',
1200 'ILS',
1201 'EUR',
1202 'JMD',
1203 'JPY',
1204 'JOD',
1205 'KZT',
1206 'KES',
1207 'AUD',
1208 'KPW',
1209 'KRW',
1210 'KWD',
1211 'KGS',
1212 'LAK',
1213 'LVL',
1214 'LBP',
1215 'ZAR',
1216 'LSL',
1217 'LRD',
1218 'LYD',
1219 'CHF',
1220 'LTL',
1221 'EUR',
1222 'MOP',
1223 'MKD',
1224 'MGA',
1225 'MWK',
1226 'MYR',
1227 'MVR',
1228 'XOF',
1229 'MTL',
1230 'USD',
1231 'EUR',
1232 'MRO',
1233 'MUR',
1234 'EUR',
1235 'MXN',
1236 'MXV',
1237 'USD',
1238 'MDL',
1239 'EUR',
1240 'MNT',
1241 'XCD',
1242 'MAD',
1243 'MZM',
1244 'MMK',
1245 'ZAR',
1246 'NAD',
1247 'AUD',
1248 'NPR',
1249 'EUR',
1250 'ANG',
1251 'XPF',
1252 'NZD',
1253 'NIO',
1254 'XOF',
1255 'NGN',
1256 'NZD',
1257 'AUD',
1258 'USD',
1259 'NOK',
1260 'OMR',
1261 'PKR',
1262 'USD',
1263 'PAB',
1264 'USD',
1265 'PGK',
1266 'PYG',
1267 'PEN',
1268 'PHP',
1269 'NZD',
1270 'PLN',
1271 'EUR',
1272 'USD',
1273 'QAR',
1274 'EUR',
1275 'ROL',
1276 'RON',
1277 'RUB',
1278 'RWF',
1279 'SHP',
1280 'XCD',
1281 'XCD',
1282 'EUR',
1283 'XCD',
1284 'WST',
1285 'EUR',
1286 'STD',
1287 'SAR',
1288 'XOF',
1289 'CSD',
1290 'EUR',
1291 'SCR',
1292 'SLL',
1293 'SGD',
1294 'SKK',
1295 'SIT',
1296 'SBD',
1297 'SOS',
1298 'ZAR',
1299 'EUR',
1300 'LKR',
1301 'SDD',
1302 'SRD',
1303 'NOK',
1304 'SZL',
1305 'SEK',
1306 'CHF',
1307 'CHW',
1308 'CHE',
1309 'SYP',
1310 'TWD',
1311 'TJS',
1312 'TZS',
1313 'THB',
1314 'USD',
1315 'XOF',
1316 'NZD',
1317 'TOP',
1318 'TTD',
1319 'TND',
1320 'TRY',
1321 'TRL',
1322 'TMM',
1323 'USD',
1324 'AUD',
1325 'UGX',
1326 'UAH',
1327 'AED',
1328 'GBP',
1329 'USD',
1330 'USS',
1331 'USN',
1332 'USD',
1333 'UYU',
1334 'UZS',
1335 'VUV',
1336 'VEB',
1337 'VND',
1338 'USD',
1339 'USD',
1340 'XPF',
1341 'MAD',
1342 'YER',
1343 'ZMK',
1344 'ZWD',
1345 'XAU',
1346 'XBA',
1347 'XBB',
1348 'XBC',
1349 'XBD',
1350 'XPD',
1351 'XPT',
1352 'XAG',
1353 'XFU',
1354 'XFO',
1355 'XTS',
1356 'XXX',
1357 );
1358 }
1359 return self::$currencyCode;
1360 }
1361
1362 /**
1363 * Get all the County from database.
1364 *
1365 * The static array county is returned, and if it's
1366 * called the first time, the <b>County DAO</b> is used
1367 * to get all the Counties.
1368 *
1369 * Note: any database errors will be trapped by the DAO.
1370 *
6a488035 1371 *
6a0b768e 1372 * @param bool|int $id - Optional id to return
6a488035 1373 *
a6c01b45
CW
1374 * @return array
1375 * array reference of all Counties
6a488035
TO
1376 */
1377 public static function &county($id = FALSE) {
1378 if (!self::$county) {
1379
1380 $config = CRM_Core_Config::singleton();
1381 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1382 self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1383 }
1384 if ($id) {
1385 if (array_key_exists($id, self::$county)) {
1386 return self::$county[$id];
1387 }
1388 else {
7cdc9e9f 1389 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1390 }
1391 }
1392 return self::$county;
1393 }
1394
6a488035 1395 /**
c490a46a 1396 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1397 * Get all active payment processors
1398 *
1399 * The static array paymentProcessor is returned
1400 *
6a488035 1401 *
6a0b768e
TO
1402 * @param bool $all
1403 * Get payment processors - default is to get only active ones.
1404 * @param bool $test
1405 * Get test payment processors.
6a488035 1406 *
77b97be7 1407 * @param null $additionalCond
6a488035 1408 *
a6c01b45
CW
1409 * @return array
1410 * array of all payment processors
6a488035 1411 */
d6944518 1412 public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
6a488035
TO
1413 $condition = "is_test = ";
1414 $condition .= ($test) ? '1' : '0';
1415
1416 if ($additionalCond) {
1417 $condition .= " AND ( $additionalCond ) ";
1418 }
1419
b44e3f84 1420 // CRM-7178. Make sure we only include payment processors valid in this
6a488035
TO
1421 // domain
1422 $condition .= " AND domain_id = " . CRM_Core_Config::domainID();
1423
1424 $cacheKey = $condition . '_' . (int) $all;
1425 if (!isset(self::$paymentProcessor[$cacheKey])) {
1426 self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1427 }
1428
1429 return self::$paymentProcessor[$cacheKey];
1430 }
1431
1432 /**
c490a46a 1433 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1434 *
1435 * The static array paymentProcessorType is returned
1436 *
6a488035 1437 *
6a0b768e
TO
1438 * @param bool $all
1439 * Get payment processors - default is to get only active ones.
6a488035 1440 *
100fef9d 1441 * @param int $id
77b97be7 1442 * @param string $return
6a488035 1443 *
a6c01b45
CW
1444 * @return array
1445 * array of all payment processor types
6a488035
TO
1446 */
1447 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
86bfa4f6 1448 $cacheKey = $id . '_' . $return;
6a488035
TO
1449 if (empty(self::$paymentProcessorType[$cacheKey])) {
1450 self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1451 }
1452 if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) {
1453 return self::$paymentProcessorType[$cacheKey][$id];
1454 }
1455 return self::$paymentProcessorType[$cacheKey];
1456 }
1457
1458 /**
d09edf64 1459 * Get all the World Regions from Database.
6a488035 1460 *
6a488035 1461 *
77b97be7
EM
1462 * @param bool $id
1463 *
a6c01b45
CW
1464 * @return array
1465 * array reference of all World Regions
6a488035
TO
1466 */
1467 public static function &worldRegion($id = FALSE) {
1468 if (!self::$worldRegions) {
1469 self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1470 }
1471
1472 if ($id) {
1473 if (array_key_exists($id, self::$worldRegions)) {
1474 return self::$worldRegions[$id];
1475 }
1476 else {
7cdc9e9f 1477 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1478 }
1479 }
1480
1481 return self::$worldRegions;
1482 }
1483
6a488035 1484 /**
c490a46a 1485 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 1486 *
6a488035
TO
1487 * Get all Activity Statuses.
1488 *
1489 * The static array activityStatus is returned
1490 *
6a488035 1491 *
77b97be7
EM
1492 * @param string $column
1493 *
a6c01b45
CW
1494 * @return array
1495 * array reference of all activity statuses
6a488035
TO
1496 */
1497 public static function &activityStatus($column = 'label') {
1498 if (NULL === self::$activityStatus) {
1499 self::$activityStatus = array();
1500 }
1501 if (!array_key_exists($column, self::$activityStatus)) {
1502 self::$activityStatus[$column] = array();
1503
1504 self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1505 }
1506
1507 return self::$activityStatus[$column];
1508 }
1509
6a488035 1510 /**
c490a46a 1511 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 1512 *
6a488035
TO
1513 * Get all Visibility levels.
1514 *
1515 * The static array visibility is returned
1516 *
6a488035 1517 *
77b97be7 1518 * @param string $column
6a488035 1519 *
a6c01b45
CW
1520 * @return array
1521 * array reference of all Visibility levels.
6a488035
TO
1522 */
1523 public static function &visibility($column = 'label') {
1524 if (!isset(self::$visibility)) {
2aa397bc 1525 self::$visibility = array();
6a488035
TO
1526 }
1527
1528 if (!isset(self::$visibility[$column])) {
1529 self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
2aa397bc 1530 }
6a488035
TO
1531
1532 return self::$visibility[$column];
1533 }
1534
a0ee3941 1535 /**
100fef9d 1536 * @param int $countryID
a0ee3941
EM
1537 * @param string $field
1538 *
1539 * @return array
1540 */
6a488035
TO
1541 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1542 static $_cache = NULL;
1543
1544 $cacheKey = "{$countryID}_{$field}";
1545 if (!$_cache) {
1546 $_cache = array();
1547 }
1548
1549 if (!empty($_cache[$cacheKey])) {
1550 return $_cache[$cacheKey];
1551 }
1552
1553 $query = "
1554SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1555 FROM civicrm_state_province
1556WHERE country_id = %1
1557ORDER BY name";
1558 $params = array(
1559 1 => array(
1560 $countryID,
1561 'Integer',
1562 ),
1563 );
1564
1565 $dao = CRM_Core_DAO::executeQuery($query, $params);
1566
1567 $result = array();
1568 while ($dao->fetch()) {
1569 $result[$dao->id] = $dao->name;
1570 }
1571
1572 // localise the stateProvince names if in an non-en_US locale
1573 $config = CRM_Core_Config::singleton();
1574 global $tsLocale;
1575 if ($tsLocale != '' and $tsLocale != 'en_US') {
1576 $i18n = CRM_Core_I18n::singleton();
1577 $i18n->localizeArray($result, array(
1578 'context' => 'province',
1579 ));
1580 $result = CRM_Utils_Array::asort($result);
1581 }
1582
1583 $_cache[$cacheKey] = $result;
1584
1585 CRM_Utils_Hook::buildStateProvinceForCountry($countryID, $result);
1586
1587 return $result;
1588 }
1589
a0ee3941 1590 /**
100fef9d 1591 * @param int $stateID
a0ee3941
EM
1592 *
1593 * @return array
1594 */
6a488035
TO
1595 public static function &countyForState($stateID) {
1596 if (is_array($stateID)) {
1597 $states = implode(", ", $stateID);
1598 $query = "
1599 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1600 FROM civicrm_county
1601 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1602 WHERE civicrm_county.state_province_id in ( $states )
1603 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1604
1605 $dao = CRM_Core_DAO::executeQuery($query);
1606
1607 $result = array();
1608 while ($dao->fetch()) {
1609 $result[$dao->id] = $dao->abbreviation . ': ' . $dao->name;
1610 }
1611 }
1612 else {
1613
1614 static $_cache = NULL;
1615
1616 $cacheKey = "{$stateID}_name";
1617 if (!$_cache) {
1618 $_cache = array();
1619 }
1620
1621 if (!empty($_cache[$cacheKey])) {
1622 return $_cache[$cacheKey];
1623 }
1624
1625 $query = "
1626 SELECT civicrm_county.name name, civicrm_county.id id
1627 FROM civicrm_county
1628 WHERE state_province_id = %1
1629 ORDER BY name";
1630 $params = array(
1631 1 => array(
1632 $stateID,
1633 'Integer',
1634 ),
1635 );
1636
1637 $dao = CRM_Core_DAO::executeQuery($query, $params);
1638
1639 $result = array();
1640 while ($dao->fetch()) {
1641 $result[$dao->id] = $dao->name;
1642 }
1643 }
1644
1645 return $result;
1646 }
1647
b05e28de
DL
1648 /**
1649 * Given a state ID return the country ID, this allows
1650 * us to populate forms and values for downstream code
1651 *
5a4f6742 1652 * @param int $stateID
b05e28de 1653 *
a6c01b45
CW
1654 * @return int
1655 * the country id that the state belongs to
b05e28de 1656 */
00be9182 1657 public static function countryIDForStateID($stateID) {
b05e28de 1658 if (empty($stateID)) {
7cdc9e9f 1659 return CRM_Core_DAO::$_nullObject;
b05e28de
DL
1660 }
1661
1662 $query = "
1663SELECT country_id
1664FROM civicrm_state_province
1665WHERE id = %1
1666";
1667 $params = array(1 => array($stateID, 'Integer'));
1668
1669 return CRM_Core_DAO::singleValueQuery($query, $params);
1670 }
1671
6a488035
TO
1672 /**
1673 * Get all types of Greetings.
1674 *
1675 * The static array of greeting is returned
1676 *
6a488035 1677 *
6a0b768e
TO
1678 * @param $filter
1679 * Get All Email Greetings - default is to get only active ones.
6a488035 1680 *
77b97be7 1681 * @param string $columnName
6a488035 1682 *
a6c01b45
CW
1683 * @return array
1684 * array reference of all greetings.
6a488035
TO
1685 */
1686 public static function greeting($filter, $columnName = 'label') {
1687 $index = $filter['greeting_type'] . '_' . $columnName;
1688
1689 // also add contactType to the array
1690 $contactType = CRM_Utils_Array::value('contact_type', $filter);
1691 if ($contactType) {
1692 $index .= '_' . $contactType;
1693 }
1694
1695 if (NULL === self::$greeting) {
1696 self::$greeting = array();
1697 }
1698
1699 if (!CRM_Utils_Array::value($index, self::$greeting)) {
1700 $filterCondition = NULL;
1701 if ($contactType) {
1702 $filterVal = 'v.filter =';
1703 switch ($contactType) {
1704 case 'Individual':
1705 $filterVal .= "1";
1706 break;
1707
1708 case 'Household':
1709 $filterVal .= "2";
1710 break;
1711
1712 case 'Organization':
1713 $filterVal .= "3";
1714 break;
1715 }
1716 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1717 }
1718
1719 self::$greeting[$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1720 }
1721
1722 return self::$greeting[$index];
1723 }
1724
1725 /**
d09edf64 1726 * Construct array of default greeting values for contact type.
6a488035 1727 *
6a488035 1728 *
a6c01b45
CW
1729 * @return array
1730 * array reference of default greetings.
6a488035
TO
1731 */
1732 public static function &greetingDefaults() {
1733 if (!self::$greetingDefaults) {
1734 $defaultGreetings = array();
353ffa53
TO
1735 $contactTypes = self::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1736 'keyColumn' => 'id',
8d7a9d07 1737 'labelColumn' => 'name',
353ffa53 1738 ));
6a488035 1739
091fe2a8 1740 foreach ($contactTypes as $filter => $contactType) {
6a488035
TO
1741 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1742
1743 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
1744 $tokenVal = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1745 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1746 }
1747 }
1748
1749 self::$greetingDefaults = $defaultGreetings;
1750 }
1751
1752 return self::$greetingDefaults;
1753 }
1754
6a488035 1755 /**
d09edf64 1756 * Get all extensions.
6a488035
TO
1757 *
1758 * The static array extensions
1759 *
1760 * FIXME: This is called by civix but not by any core code. We
1761 * should provide an API call which civix can use instead.
1762 *
6a488035 1763 *
a6c01b45
CW
1764 * @return array
1765 * array($fullyQualifiedName => $label) list of extensions
6a488035
TO
1766 */
1767 public static function &getExtensions() {
1768 if (!self::$extensions) {
1769 self::$extensions = array();
1770 $sql = '
1771 SELECT full_name, label
1772 FROM civicrm_extension
1773 WHERE is_active = 1
1774 ';
1775 $dao = CRM_Core_DAO::executeQuery($sql);
1776 while ($dao->fetch()) {
1777 self::$extensions[$dao->full_name] = $dao->label;
1778 }
1779 }
1780
1781 return self::$extensions;
1782 }
1783
f743a6eb 1784 /**
d09edf64 1785 * Get all options values.
f743a6eb
CW
1786 *
1787 * The static array option values is returned
1788 *
f743a6eb 1789 *
6a0b768e
TO
1790 * @param bool $optionGroupName
1791 * Get All Option Group values- default is to get only active ones.
f743a6eb 1792 *
100fef9d 1793 * @param int $id
77b97be7 1794 * @param null $condition
f743a6eb 1795 *
a6c01b45
CW
1796 * @return array
1797 * array reference of all Option Group Name
f743a6eb 1798 */
2aa397bc 1799 public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) {
f743a6eb
CW
1800 $cacheKey = $optionGroupName . '_' . $condition;
1801 if (empty(self::$accountOptionValues[$cacheKey])) {
2aa397bc 1802 self::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup::values($optionGroupName, FALSE, FALSE, FALSE, $condition);
f743a6eb
CW
1803 }
1804 if ($id) {
1805 return CRM_Utils_Array::value($id, self::$accountOptionValues[$cacheKey]);
1806 }
1807
1808 return self::$accountOptionValues[$cacheKey];
1809 }
1810
6a488035
TO
1811 /**
1812 * Fetch the list of active extensions of type 'module'
1813 *
5a4f6742
CW
1814 * @param bool $fresh
1815 * Whether to forcibly reload extensions list from canonical store.
6a488035 1816 *
a6c01b45
CW
1817 * @return array
1818 * array(array('prefix' => $, 'file' => $))
6a488035
TO
1819 */
1820 public static function getModuleExtensions($fresh = FALSE) {
1821 return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh);
1822 }
dc428161 1823
1824
1825 /**
d09edf64 1826 * Get all tax rates.
dc428161 1827 *
1828 * The static array tax rates is returned
1829 *
a6c01b45
CW
1830 * @return array
1831 * array list of tax rates with the financial type
dc428161 1832 */
1833 public static function getTaxRates() {
27864d8a 1834 if (!isset(Civi::$statics[__CLASS__]['taxRates'])) {
1835 Civi::$statics[__CLASS__]['taxRates'] = array();
dc428161 1836 $sql = "
1837 SELECT fa.tax_rate, efa.entity_id
1838 FROM civicrm_entity_financial_account efa
1839 INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
1840 INNER JOIN civicrm_option_value cov ON cov.value = efa.account_relationship
1841 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
1842 WHERE efa.entity_table = 'civicrm_financial_type'
1843 AND cov.name = 'Sales Tax Account is'
1844 AND cog.name = 'account_relationship'
1845 AND fa.is_active = 1";
1846 $dao = CRM_Core_DAO::executeQuery($sql);
1847 while ($dao->fetch()) {
27864d8a 1848 Civi::$statics[__CLASS__]['taxRates'][$dao->entity_id] = $dao->tax_rate;
dc428161 1849 }
1850 }
1851
27864d8a 1852 return Civi::$statics[__CLASS__]['taxRates'];
dc428161 1853 }
96025800 1854
6a488035 1855}