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