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