4c551a6c702a504d66298ef88dfc904e9e641dad
[civicrm-core.git] / CRM / Core / PseudoConstant.php
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 */
50 class CRM_Core_PseudoConstant {
51
52 /**
53 * static cache for pseudoconstant arrays
54 * @var array
55 * @static
56 */
57 private static $cache;
58
59 /**
60 * location type
61 * @var array
62 * @static
63 */
64 private static $locationType;
65
66 /**
67 * location vCard name
68 * @var array
69 * @static
70 */
71 private static $locationVcardName;
72
73 /**
74 * location display name
75 * @var array
76 * @static
77 */
78 private static $locationDisplayName;
79
80 /**
81 * activity type
82 * @var array
83 * @static
84 */
85 private static $activityType;
86
87 /**
88 * payment processor billing modes
89 * @var array
90 * @static
91 */
92 private static $billingMode;
93
94 /**
95 * component
96 * @var array
97 * @static
98 */
99 private static $component;
100
101 /**
102 * individual prefix
103 * @var array
104 * @static
105 */
106 private static $individualPrefix;
107
108 /**
109 * individual suffix
110 * @var array
111 * @static
112 */
113 private static $individualSuffix;
114
115 /**
116 * gender
117 * @var array
118 * @static
119 */
120 private static $gender;
121
122 /**
123 * im protocols
124 * @var array
125 * @static
126 */
127 private static $imProvider;
128
129 /**
130 * website protocols
131 * @var array
132 * @static
133 */
134 private static $websiteType;
135
136 /**
137 * im protocols
138 * @var array
139 * @static
140 */
141 private static $fromEmailAddress;
142
143 /**
144 * states, provinces
145 * @var array
146 * @static
147 */
148 private static $stateProvince;
149
150 /**
151 * counties
152 * @var array
153 * @static
154 */
155 private static $county;
156
157 /**
158 * states/provinces abbreviations
159 * @var array
160 * @static
161 */
162 private static $stateProvinceAbbreviation;
163
164 /**
165 * country
166 * @var array
167 * @static
168 */
169 private static $country;
170
171 /**
172 * countryIsoCode
173 * @var array
174 * @static
175 */
176 private static $countryIsoCode;
177
178 /**
179 * tag
180 * @var array
181 * @static
182 */
183 private static $tag;
184
185 /**
186 * group
187 * @var array
188 * @static
189 */
190 private static $group;
191
192 /**
193 * groupIterator
194 * @var mixed
195 * @static
196 */
197 private static $groupIterator;
198
199 /**
200 * relationshipType
201 * @var array
202 * @static
203 */
204 private static $relationshipType;
205
206 /**
207 * civicrm groups that are not smart groups
208 * @var array
209 * @static
210 */
211 private static $staticGroup;
212
213 /**
214 * user framework groups
215 * @var array
216 * @static
217 */
218 private static $ufGroup;
219
220 /**
221 * custom groups
222 * @var array
223 * @static
224 */
225 private static $customGroup;
226
227 /**
228 * currency codes
229 * @var array
230 * @static
231 */
232 private static $currencyCode;
233
234 /**
235 * currency Symbols
236 * @var array
237 * @static
238 */
239 private static $currencySymbols;
240
241 /**
242 * preferred communication methods
243 * @var array
244 * @static
245 */
246 private static $pcm;
247
248 /**
249 * payment processor
250 * @var array
251 * @static
252 */
253 private static $paymentProcessor;
254
255 /**
256 * payment processor types
257 * @var array
258 * @static
259 */
260 private static $paymentProcessorType;
261
262 /**
263 * World Region
264 * @var array
265 * @static
266 */
267 private static $worldRegions;
268
269 /**
270 * honorType
271 * @var array
272 * @static
273 */
274 private static $honorType;
275
276 /**
277 * activity status
278 * @var array
279 * @static
280 */
281 private static $activityStatus;
282
283 /**
284 * priority
285 * @var array
286 * @static
287 */
288 private static $priority;
289
290 /**
291 * wysiwyg Editor
292 * @var array
293 * @static
294 */
295 private static $wysiwygEditor;
296
297 /**
298 * Mapping Types
299 * @var array
300 * @static
301 */
302 private static $mappingType;
303
304 /**
305 * Phone Types
306 * @var array
307 * @static
308 */
309 private static $phoneType;
310
311 /**
312 * Visibility
313 * @var array
314 * @static
315 */
316 private static $visibility;
317
318 /**
319 * Mail Protocols
320 * @var array
321 * @static
322 */
323 private static $mailProtocol;
324
325 /**
326 * Greetings
327 * @var array
328 * @static
329 */
330 private static $greeting;
331
332 /**
333 * Default Greetings
334 * @var array
335 * @static
336 */
337 private static $greetingDefaults;
338
339 /**
340 * Extensions of type module
341 * @var array
342 * @static
343 */
344 private static $extensions;
345
346 /**
347 * activity contacts
348 * @var array
349 * @static
350 */
351 private static $activityContacts;
352
353 /**
354 * event contacts
355 * @var array
356 * @static
357 */
358 private static $eventContacts;
359
360 /**
361 * auto renew options
362 * @var array
363 * @static
364 */
365 private static $autoRenew;
366
367 /**
368 * batch mode options
369 * @var array
370 * @static
371 */
372 private static $batchModes;
373
374 /**
375 * batch type options
376 * @var array
377 * @static
378 */
379 private static $batchTypes;
380
381 /**
382 * batch status options
383 * @var array
384 * @static
385 */
386 private static $batchStatues;
387
388 /**
389 * contact Type
390 * @var array
391 * @static
392 */
393 private static $contactType;
394
395 /**
396 * Financial Account Type
397 * @var array
398 * @static
399 */
400 private static $accountOptionValues;
401
402 /**
403 * Get options for a given field.
404 * @param String $daoName
405 * @param String $fieldName
406 * @param Array $params
407 *
408 * @return Array on success, FALSE on error.
409 *
410 * @static
411 */
412 public static function get($daoName, $fieldName, $params = array()) {
413 $dao = new $daoName;
414 $fields = $dao->fields();
415 $fieldSpec = $fields[$fieldName];
416
417 // If the field is an enum, use explode the enum definition and return the array.
418 if (array_key_exists('enumValues', $fieldSpec)) {
419 // use of a space after the comma is inconsistent in xml
420 $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
421 return explode(',', $enumStr);
422 }
423 elseif (!empty($fieldSpec['pseudoconstant'])) {
424 $pseudoconstant = $fieldSpec['pseudoconstant'];
425 if(!empty($pseudoconstant['optionGroupName'])) {
426 // Translate $params array into function arguments;
427 // populate default function params if not supplied in the array.
428 $ret = CRM_Core_OptionGroup::values(
429 $pseudoconstant['optionGroupName'],
430 CRM_Utils_Array::value('flip', $params, FALSE),
431 CRM_Utils_Array::value('grouping', $params, FALSE),
432 CRM_Utils_Array::value('localize', $params, FALSE),
433 CRM_Utils_Array::value('condition', $params, NULL),
434 CRM_Utils_Array::value('labelColumnName', $params, 'label'),
435 CRM_Utils_Array::value('onlyActive', $params, TRUE),
436 CRM_Utils_Array::value('fresh', $params, FALSE)
437 );
438 return $ret;
439 }
440 if (!empty($pseudoconstant['table'])) {
441 // Sort params so the serialized string will be consistent.
442 ksort($params);
443 $cacheKey = "{$daoName}{$fieldName}" . serialize($params);
444
445 if (isset(self::$cache[$cacheKey])) {
446 return self::$cache[$cacheKey];
447 }
448
449 $query = "
450 SELECT
451 %1 AS id, %2 AS label
452 FROM
453 %3
454 ";
455 if (!empty($pseudoconstant['condition'])) {
456 $query .= " WHERE {$pseudoconstant['condition']}";
457 }
458 $query .= " ORDER BY %2";
459 $queryParams = array(
460 1 => array($pseudoconstant['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
461 2 => array($pseudoconstant['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
462 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
463 );
464
465 self::$cache[$cacheKey] = array();
466 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
467 while ($dao->fetch()) {
468 self::$cache[$cacheKey][$dao->id] = $dao->label;
469 }
470 return self::$cache[$cacheKey];
471 }
472 }
473 // If we're still here, it's an error. Return FALSE.
474 return FALSE;
475 }
476
477 /**
478 * populate the object from the database. generic populate
479 * method
480 *
481 * The static array $var is populated from the db
482 * using the <b>$name DAO</b>.
483 *
484 * Note: any database errors will be trapped by the DAO.
485 *
486 * @param array $var the associative array we will fill
487 * @param string $name the name of the DAO
488 * @param boolean $all get all objects. default is to get only active ones.
489 * @param string $retrieve the field that we are interested in (normally name, differs in some objects)
490 * @param string $filter the field that we want to filter the result set with
491 * @param string $condition the condition that gets passed to the final query as the WHERE clause
492 *
493 * @return void
494 * @access public
495 * @static
496 */
497 public static function populate(
498 &$var,
499 $name,
500 $all = FALSE,
501 $retrieve = 'name',
502 $filter = 'is_active',
503 $condition = NULL,
504 $orderby = NULL,
505 $key = 'id',
506 $force = NULL
507 ) {
508 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
509 $cache = CRM_Utils_Cache::singleton();
510 $var = $cache->get($cacheKey);
511 if ($var && empty($force)) {
512 return $var;
513 }
514
515 $object = new $name ( );
516
517 $object->selectAdd();
518 $object->selectAdd("$key, $retrieve");
519 if ($condition) {
520 $object->whereAdd($condition);
521 }
522
523 if (!$orderby) {
524 $object->orderBy($retrieve);
525 }
526 else {
527 $object->orderBy($orderby);
528 }
529
530 if (!$all) {
531 $object->$filter = 1;
532 }
533
534 $object->find();
535 $var = array();
536 while ($object->fetch()) {
537 $var[$object->$key] = $object->$retrieve;
538 }
539
540 $cache->set($cacheKey, $var);
541 }
542
543 /**
544 * Flush given pseudoconstant so it can be reread from db
545 * nex time it's requested.
546 *
547 * @access public
548 * @static
549 *
550 * @param boolean $name pseudoconstant to be flushed
551 *
552 */
553 public static function flush($name) {
554 if (isset(self::$$name)) {
555 self::$$name = NULL;
556 }
557 }
558
559 /**
560 * Get all location types.
561 *
562 * The static array locationType is returned
563 *
564 * @access public
565 * @static
566 *
567 * @param boolean $all - get All location types - default is to get only active ones.
568 *
569 * @return array - array reference of all location types.
570 *
571 */
572 public static function &locationType($all = FALSE) {
573 if (!self::$locationType) {
574 self::populate(self::$locationType, 'CRM_Core_DAO_LocationType', $all);
575 }
576 return self::$locationType;
577 }
578
579 /**
580 * Get all location vCard names.
581 *
582 * The static array locationVcardName is returned
583 *
584 * @access public
585 * @static
586 *
587 * @param boolean $all - get All location vCard names - default is to get only active ones.
588 *
589 * @return array - array reference of all location vCard names.
590 *
591 */
592 public static function &locationVcardName($all = FALSE) {
593 if (!self::$locationVcardName) {
594 self::populate(self::$locationVcardName, 'CRM_Core_DAO_LocationType', $all, 'vcard_name');
595 }
596 return self::$locationVcardName;
597 }
598
599 /**
600 * Get all location Display names.
601 *
602 * The static array locationDisplayName is returned
603 *
604 * @access public
605 * @static
606 *
607 * @param boolean $all - get All location display names - default is to get only active ones.
608 *
609 * @return array - array reference of all location display names.
610 *
611 */
612 public static function &locationDisplayName($all = FALSE) {
613 if (!self::$locationDisplayName) {
614 self::populate(self::$locationDisplayName, 'CRM_Core_DAO_LocationType', $all, 'display_name');
615 }
616 return self::$locationDisplayName;
617 }
618
619 /**
620 * Get all Activty types.
621 *
622 * The static array activityType is returned
623 *
624 * @param boolean $all - get All Activity types - default is to get only active ones.
625 *
626 * @access public
627 * @static
628 *
629 * @return array - array reference of all activity types.
630 */
631 public static function &activityType() {
632 $args = func_get_args();
633 $all = CRM_Utils_Array::value(0, $args, TRUE);
634 $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
635 $reset = CRM_Utils_Array::value(2, $args, FALSE);
636 $returnColumn = CRM_Utils_Array::value(3, $args, 'label');
637 $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
638 $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
639 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
640 $index .= '_' . (int) $includeCampaignActivities;
641 $index .= '_' . (int) $onlyComponentActivities;
642
643 if (NULL === self::$activityType) {
644 self::$activityType = array();
645 }
646
647 if (!isset(self::$activityType[$index]) || $reset) {
648 $condition = NULL;
649 if (!$all) {
650 $condition = 'AND filter = 0';
651 }
652 $componentClause = " v.component_id IS NULL";
653 if ($onlyComponentActivities) {
654 $componentClause = " v.component_id IS NOT NULL";
655 }
656
657 $componentIds = array();
658 $compInfo = CRM_Core_Component::getEnabledComponents();
659
660 // build filter for listing activity types only if their
661 // respective components are enabled
662 foreach ($compInfo as $compName => $compObj) {
663 if ($compName == 'CiviCase') {
664 if ($includeCaseActivities) {
665 $componentIds[] = $compObj->componentID;
666 }
667 }
668 elseif ($compName == 'CiviCampaign') {
669 if ($includeCampaignActivities) {
670 $componentIds[] = $compObj->componentID;
671 }
672 }
673 else {
674 $componentIds[] = $compObj->componentID;
675 }
676 }
677
678 if (count($componentIds)) {
679 $componentIds = implode(',', $componentIds);
680 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
681 if ($onlyComponentActivities) {
682 $componentClause = " ( v.component_id IN ($componentIds ) )";
683 }
684 }
685 $condition = $condition . ' AND ' . $componentClause;
686
687 self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
688 }
689 return self::$activityType[$index];
690 }
691
692 /**
693 * Get all payment-processor billing modes
694 *
695 * @access public
696 * @static
697 *
698 * @return array ($id => $name)
699 */
700 public static function billingMode() {
701 if (!self::$billingMode) {
702 self::$billingMode = array(
703 CRM_Core_Payment::BILLING_MODE_FORM => 'form',
704 CRM_Core_Payment::BILLING_MODE_BUTTON => 'button',
705 CRM_Core_Payment::BILLING_MODE_NOTIFY => 'notify',
706 );
707 }
708 return self::$billingMode;
709 }
710
711 /**
712 * Get all component names
713 *
714 * @access public
715 * @static
716 *
717 * @return array - array reference of all location display names.
718 *
719 */
720 public static function &component() {
721 if (!self::$component) {
722 self::populate(self::$component, 'CRM_Core_DAO_Component', TRUE, 'name');
723 }
724 return self::$component;
725 }
726
727
728 /**
729 * Get all Individual Prefix.
730 *
731 * The static array individualPrefix is returned
732 *
733 * @access public
734 * @static
735 *
736 * @param boolean $all - get All Individual Prefix - default is to get only active ones.
737 *
738 * @return array - array reference of all individual prefix.
739 *
740 */
741 public static function &individualPrefix() {
742 if (!self::$individualPrefix) {
743 self::$individualPrefix = CRM_Core_OptionGroup::values('individual_prefix');
744 }
745 return self::$individualPrefix;
746 }
747
748 /**
749 * Get all phone type
750 * The static array phoneType is returned
751 *
752 * @access public
753 * @static
754 *
755 * @param boolean $all - get All phone type - default is to get
756 * only active ones.
757 *
758 * @return array - array reference of all phone types.
759 *
760 */
761 public static function &phoneType() {
762 if (!self::$phoneType) {
763 self::$phoneType = CRM_Core_OptionGroup::values('phone_type');
764 }
765 return self::$phoneType;
766 }
767
768 /**
769 * Get all Individual Suffix.
770 *
771 * The static array individualSuffix is returned
772 *
773 * @access public
774 * @static
775 *
776 * @param boolean $all - get All Individual Suffix - default is to get only active ones.
777 *
778 * @return array - array reference of all individual suffix.
779 *
780 */
781 public static function &individualSuffix() {
782 if (!self::$individualSuffix) {
783 self::$individualSuffix = CRM_Core_OptionGroup::values('individual_suffix');
784 }
785 return self::$individualSuffix;
786 }
787
788 /**
789 * Get all Gender.
790 *
791 * The static array gender is returned
792 *
793 * @access public
794 * @static
795 *
796 * @param boolean $all - get All Gender - default is to get only active ones.
797 *
798 * @return array - array reference of all gender.
799 *
800 */
801 public static function &gender($localize = FALSE) {
802 if (!self::$gender) {
803 self::$gender = CRM_Core_OptionGroup::values('gender', FALSE, FALSE, $localize);
804 }
805 return self::$gender;
806 }
807
808 /**
809 * Get all the IM Providers from database.
810 *
811 * The static array imProvider is returned, and if it's
812 * called the first time, the <b>IM DAO</b> is used
813 * to get all the IM Providers.
814 *
815 * Note: any database errors will be trapped by the DAO.
816 *
817 * @access public
818 * @static
819 *
820 * @return array - array reference of all IM providers.
821 *
822 */
823 public static function &IMProvider($localize = FALSE) {
824 if (!self::$imProvider) {
825 self::$imProvider = CRM_Core_OptionGroup::values('instant_messenger_service', FALSE, FALSE, $localize);
826 }
827 return self::$imProvider;
828 }
829
830 /**
831 * Get all the website types from database.
832 *
833 * The static array websiteType is returned, and if it's
834 * called the first time, the <b>Website DAO</b> is used
835 * to get all the Website Types.
836 *
837 * Note: any database errors will be trapped by the DAO.
838 *
839 * @access public
840 * @static
841 *
842 * @return array - array reference of all Website types.
843 *
844 */
845 public static function &websiteType() {
846 if (!self::$websiteType) {
847 self::$websiteType = CRM_Core_OptionGroup::values('website_type');
848 }
849 return self::$websiteType;
850 }
851
852 /**
853 * Get the all From Email Address from database.
854 *
855 * The static array $fromEmailAddress is returned, and if it's
856 * called the first time, DAO is used
857 * to get all the From Email Address
858 *
859 * Note: any database errors will be trapped by the DAO.
860 *
861 * @access public
862 * @static
863 *
864 * @return array - array reference of all From Email Address.
865 */
866 public static function &fromEmailAddress() {
867 if (!self::$fromEmailAddress) {
868 self::$fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address');
869 }
870 return self::$fromEmailAddress;
871 }
872
873 /**
874 * Get the all Mail Protocols from database.
875 *
876 * The static array mailProtocol is returned, and if it's
877 * called the first time, the DAO is used
878 * to get all the Mail Protocol.
879 *
880 * Note: any database errors will be trapped by the DAO.
881 *
882 * @access public
883 * @static
884 *
885 * @return array - array reference of all Mail Protocols.
886 */
887 public static function &mailProtocol() {
888 if (!self::$mailProtocol) {
889 self::$mailProtocol = CRM_Core_OptionGroup::values('mail_protocol');
890 }
891 return self::$mailProtocol;
892 }
893
894 /**
895 * Get all the State/Province from database.
896 *
897 * The static array stateProvince is returned, and if it's
898 * called the first time, the <b>State Province DAO</b> is used
899 * to get all the States.
900 *
901 * Note: any database errors will be trapped by the DAO.
902 *
903 * @access public
904 * @static
905 *
906 * @param int $id - Optional id to return
907 *
908 * @return array - array reference of all State/Provinces.
909 *
910 */
911 public static function &stateProvince($id = FALSE, $limit = TRUE) {
912 if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) {
913 $whereClause = FALSE;
914 $config = CRM_Core_Config::singleton();
915 if ($limit) {
916 $countryIsoCodes = self::countryIsoCode();
917 $limitCodes = $config->provinceLimit();
918 $limitIds = array();
919 foreach ($limitCodes as $code) {
920 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
921 }
922 if (!empty($limitIds)) {
923 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
924 }
925 else {
926 $whereClause = FALSE;
927 }
928 }
929 self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
930
931 // localise the province names if in an non-en_US locale
932 global $tsLocale;
933 if ($tsLocale != '' and $tsLocale != 'en_US') {
934 $i18n = CRM_Core_I18n::singleton();
935 $i18n->localizeArray(self::$stateProvince, array(
936 'context' => 'province',
937 ));
938 self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
939 }
940 }
941 if ($id) {
942 if (array_key_exists($id, self::$stateProvince)) {
943 return self::$stateProvince[$id];
944 }
945 else {
946 $result = NULL;
947 return $result;
948 }
949 }
950 return self::$stateProvince;
951 }
952
953 /**
954 * Get all the State/Province abbreviations from the database.
955 *
956 * Same as above, except gets the abbreviations instead of the names.
957 *
958 * @access public
959 * @static
960 *
961 * @param int $id - Optional id to return
962 *
963 * @return array - array reference of all State/Province abbreviations.
964 */
965 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
966 if ($id > 1) {
967 $query = "
968 SELECT abbreviation
969 FROM civicrm_state_province
970 WHERE id = %1";
971 $params = array(
972 1 => array(
973 $id,
974 'Integer',
975 ),
976 );
977 return CRM_Core_DAO::singleValueQuery($query, $params);
978 }
979
980 if (!self::$stateProvinceAbbreviation || !$id) {
981
982 $whereClause = FALSE;
983
984 if ($limit) {
985 $config = CRM_Core_Config::singleton();
986 $countryIsoCodes = self::countryIsoCode();
987 $limitCodes = $config->provinceLimit();
988 $limitIds = array();
989 foreach ($limitCodes as $code) {
990 $tmpArray = array_keys($countryIsoCodes, $code);
991
992 if (!empty($tmpArray)) {
993 $limitIds[] = array_shift($tmpArray);
994 }
995 }
996 if (!empty($limitIds)) {
997 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
998 }
999 }
1000 self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
1001 }
1002
1003 if ($id) {
1004 if (array_key_exists($id, self::$stateProvinceAbbreviation)) {
1005 return self::$stateProvinceAbbreviation[$id];
1006 }
1007 else {
1008 $result = NULL;
1009 return $result;
1010 }
1011 }
1012 return self::$stateProvinceAbbreviation;
1013 }
1014
1015 /**
1016 * Get all the countries from database.
1017 *
1018 * The static array country is returned, and if it's
1019 * called the first time, the <b>Country DAO</b> is used
1020 * to get all the countries.
1021 *
1022 * Note: any database errors will be trapped by the DAO.
1023 *
1024 * @access public
1025 * @static
1026 *
1027 * @param int $id - Optional id to return
1028 *
1029 * @return array - array reference of all countries.
1030 *
1031 */
1032 public static function country($id = FALSE, $applyLimit = TRUE) {
1033 if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) {
1034
1035 $config = CRM_Core_Config::singleton();
1036 $limitCodes = array();
1037
1038 if ($applyLimit) {
1039 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
1040 // (ensuring it's a subset of the legal values)
1041 // K/P: We need to fix this, i dont think it works with new setting files
1042 $limitCodes = $config->countryLimit();
1043 if (!is_array($limitCodes)) {
1044 $limitCodes = array(
1045 $config->countryLimit => 1,
1046 );
1047 }
1048
1049 $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
1050 }
1051
1052 if (count($limitCodes)) {
1053 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
1054 }
1055 else {
1056 $whereClause = NULL;
1057 }
1058
1059 self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
1060
1061 // if default country is set, percolate it to the top
1062 if ($config->defaultContactCountry()) {
1063 $countryIsoCodes = self::countryIsoCode();
1064 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
1065 if ($defaultID !== FALSE) {
1066 $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country);
1067 self::$country = $default + self::$country;
1068 }
1069 }
1070
1071 // localise the country names if in an non-en_US locale
1072 global $tsLocale;
1073 if ($tsLocale != '' and $tsLocale != 'en_US') {
1074 $i18n = CRM_Core_I18n::singleton();
1075 $i18n->localizeArray(self::$country, array(
1076 'context' => 'country',
1077 ));
1078 self::$country = CRM_Utils_Array::asort(self::$country);
1079 }
1080 }
1081 if ($id) {
1082 if (array_key_exists($id, self::$country)) {
1083 return self::$country[$id];
1084 }
1085 else {
1086 return CRM_Core_DAO::$_nullObject;
1087 }
1088 }
1089 return self::$country;
1090 }
1091
1092 /**
1093 * Get all the country ISO Code abbreviations from the database.
1094 *
1095 * The static array countryIsoCode is returned, and if it's
1096 * called the first time, the <b>Country DAO</b> is used
1097 * to get all the countries' ISO codes.
1098 *
1099 * Note: any database errors will be trapped by the DAO.
1100 *
1101 * @access public
1102 * @static
1103 *
1104 * @return array - array reference of all country ISO codes.
1105 *
1106 */
1107 public static function &countryIsoCode($id = FALSE) {
1108 if (!self::$countryIsoCode) {
1109 self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
1110 }
1111 if ($id) {
1112 if (array_key_exists($id, self::$countryIsoCode)) {
1113 return self::$countryIsoCode[$id];
1114 }
1115 else {
1116 return CRM_Core_DAO::$_nullObject;
1117 }
1118 }
1119 return self::$countryIsoCode;
1120 }
1121
1122 /**
1123 * Get all the categories from database.
1124 *
1125 * The static array tag is returned, and if it's
1126 * called the first time, the <b>Tag DAO</b> is used
1127 * to get all the categories.
1128 *
1129 * Note: any database errors will be trapped by the DAO.
1130 *
1131 * @access public
1132 * @static
1133 *
1134 * @return array - array reference of all categories.
1135 *
1136 */
1137 public static function &tag() {
1138 if (!self::$tag) {
1139 self::populate(self::$tag, 'CRM_Core_DAO_Tag', TRUE);
1140 }
1141 return self::$tag;
1142 }
1143
1144 /**
1145 * Get all groups from database
1146 *
1147 * The static array group is returned, and if it's
1148 * called the first time, the <b>Group DAO</b> is used
1149 * to get all the groups.
1150 *
1151 * Note: any database errors will be trapped by the DAO.
1152 *
1153 * @param string $groupType type of group(Access/Mailing)
1154 * @param boolen $excludeHidden exclude hidden groups.
1155 *
1156 * @access public
1157 * @static
1158 *
1159 * @return array - array reference of all groups.
1160 *
1161 */
1162 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
1163 $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
1164
1165 if (!self::$group) {
1166 self::$group = array();
1167 }
1168
1169 $groupKey = $groupType ? $groupType : 'null';
1170
1171 if (!isset(self::$group[$groupKey])) {
1172 self::$group[$groupKey] = NULL;
1173 self::populate(self::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
1174 }
1175 return self::$group[$groupKey];
1176 }
1177
1178 /**
1179 * Create or get groups iterator (iterates over nested groups in a
1180 * logical fashion)
1181 *
1182 * The GroupNesting instance is returned; it's created if this is being
1183 * called for the first time
1184 *
1185 *
1186 * @access public
1187 * @static
1188 *
1189 * @return mixed - instance of CRM_Contact_BAO_GroupNesting
1190 *
1191 */
1192 public static function &groupIterator($styledLabels = FALSE) {
1193 if (!self::$groupIterator) {
1194 /*
1195 When used as an object, GroupNesting implements Iterator
1196 and iterates nested groups in a logical manner for us
1197 */
1198 self::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
1199 }
1200 return self::$groupIterator;
1201 }
1202
1203 /**
1204 * Get all permissioned groups from database
1205 *
1206 * The static array group is returned, and if it's
1207 * called the first time, the <b>Group DAO</b> is used
1208 * to get all the groups.
1209 *
1210 * Note: any database errors will be trapped by the DAO.
1211 *
1212 * @param string $groupType type of group(Access/Mailing)
1213 * @param boolen $excludeHidden exclude hidden groups.
1214
1215 * @access public
1216 * @static
1217 *
1218 * @return array - array reference of all groups.
1219 *
1220 */
1221 public static function group($groupType = NULL, $excludeHidden = TRUE) {
1222 return CRM_Core_Permission::group($groupType, $excludeHidden);
1223 }
1224
1225 /**
1226 * Get all permissioned groups from database
1227 *
1228 * The static array group is returned, and if it's
1229 * called the first time, the <b>Group DAO</b> is used
1230 * to get all the groups.
1231 *
1232 * Note: any database errors will be trapped by the DAO.
1233 *
1234 * @access public
1235 * @static
1236 *
1237 * @return array - array reference of all groups.
1238 *
1239 */
1240 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1241 if (!self::$staticGroup) {
1242 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1243 if ($onlyPublic) {
1244 $condition .= " AND visibility != 'User and User Admin Only'";
1245 }
1246
1247 if ($groupType) {
1248 $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
1249 }
1250
1251 if ($excludeHidden) {
1252 $condition .= ' AND is_hidden != 1 ';
1253 }
1254
1255 self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1256 }
1257
1258 return self::$staticGroup;
1259 }
1260
1261 /**
1262 * Get all the custom groups
1263 *
1264 * @access public
1265 *
1266 * @return array - array reference of all groups.
1267 * @static
1268 */
1269 public static function &customGroup($reset = FALSE) {
1270 if (!self::$customGroup || $reset) {
1271 self::populate(self::$customGroup, 'CRM_Core_DAO_CustomGroup', FALSE, 'title', 'is_active', NULL, 'title');
1272 }
1273 return self::$customGroup;
1274 }
1275
1276 /**
1277 * Get all the user framework groups
1278 *
1279 * @access public
1280 *
1281 * @return array - array reference of all groups.
1282 * @static
1283 */
1284 public static function &ufGroup() {
1285 if (!self::$ufGroup) {
1286 self::populate(self::$ufGroup, 'CRM_Core_DAO_UFGroup', FALSE, 'title', 'is_active', NULL, 'title');
1287 }
1288 return self::$ufGroup;
1289 }
1290
1291 /**
1292 * Get all Relationship Types from database.
1293 *
1294 * The static array group is returned, and if it's
1295 * called the first time, the <b>RelationshipType DAO</b> is used
1296 * to get all the relationship types.
1297 *
1298 * Note: any database errors will be trapped by the DAO.
1299 *
1300 * @param string $valueColumnName db column name/label.
1301 * @param boolean $reset reset relationship types if true
1302 *
1303 * @access public
1304 * @static
1305 *
1306 * @return array - array reference of all relationship types.
1307 */
1308 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1309 if (!CRM_Utils_Array::value($valueColumnName, self::$relationshipType) || $reset) {
1310 self::$relationshipType[$valueColumnName] = array();
1311
1312 //now we have name/label columns CRM-3336
1313 $column_a_b = "{$valueColumnName}_a_b";
1314 $column_b_a = "{$valueColumnName}_b_a";
1315
1316 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1317 $relationshipTypeDAO->selectAdd();
1318 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1319 $relationshipTypeDAO->is_active = 1;
1320 $relationshipTypeDAO->find();
1321 while ($relationshipTypeDAO->fetch()) {
1322
1323 self::$relationshipType[$valueColumnName][$relationshipTypeDAO->id] = array(
1324 $column_a_b => $relationshipTypeDAO->$column_a_b,
1325 $column_b_a => $relationshipTypeDAO->$column_b_a,
1326 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1327 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1328 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1329 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1330 );
1331 }
1332 }
1333
1334 return self::$relationshipType[$valueColumnName];
1335 }
1336
1337 /**
1338 * Get all the Currency Symbols from Database
1339 *
1340 * @access public
1341 *
1342 * @return array - array reference of all Currency Symbols
1343 * @static
1344 */
1345 public static function &currencySymbols($name = 'symbol', $key = 'id') {
1346 $cacheKey = "{$name}_{$key}";
1347 if (!isset(self::$currencySymbols[$cacheKey])) {
1348 self::populate(self::$currencySymbols[$cacheKey], 'CRM_Financial_DAO_Currency', TRUE, $name, NULL, NULL, 'name', $key);
1349 }
1350
1351 return self::$currencySymbols[$cacheKey];
1352 }
1353
1354 /**
1355 * get all the ISO 4217 currency codes
1356 *
1357 * so far, we use this for validation only, so there's no point of putting this into the database
1358 *
1359 * @access public
1360 *
1361 * @return array - array reference of all currency codes
1362 * @static
1363 */
1364 public static function &currencyCode() {
1365 if (!self::$currencyCode) {
1366 self::$currencyCode = array(
1367 'AFN',
1368 'ALL',
1369 'DZD',
1370 'USD',
1371 'EUR',
1372 'AOA',
1373 'XCD',
1374 'XCD',
1375 'ARS',
1376 'AMD',
1377 'AWG',
1378 'AUD',
1379 'EUR',
1380 'AZM',
1381 'BSD',
1382 'BHD',
1383 'BDT',
1384 'BBD',
1385 'BYR',
1386 'EUR',
1387 'BZD',
1388 'XOF',
1389 'BMD',
1390 'INR',
1391 'BTN',
1392 'BOB',
1393 'BOV',
1394 'BAM',
1395 'BWP',
1396 'NOK',
1397 'BRL',
1398 'USD',
1399 'BND',
1400 'BGN',
1401 'XOF',
1402 'BIF',
1403 'KHR',
1404 'XAF',
1405 'CAD',
1406 'CVE',
1407 'KYD',
1408 'XAF',
1409 'XAF',
1410 'CLP',
1411 'CLF',
1412 'CNY',
1413 'AUD',
1414 'AUD',
1415 'COP',
1416 'COU',
1417 'KMF',
1418 'XAF',
1419 'CDF',
1420 'NZD',
1421 'CRC',
1422 'XOF',
1423 'HRK',
1424 'CUP',
1425 'CYP',
1426 'CZK',
1427 'DKK',
1428 'DJF',
1429 'XCD',
1430 'DOP',
1431 'USD',
1432 'EGP',
1433 'SVC',
1434 'USD',
1435 'XAF',
1436 'ERN',
1437 'EEK',
1438 'ETB',
1439 'FKP',
1440 'DKK',
1441 'FJD',
1442 'EUR',
1443 'EUR',
1444 'EUR',
1445 'XPF',
1446 'EUR',
1447 'XAF',
1448 'GMD',
1449 'GEL',
1450 'EUR',
1451 'GHC',
1452 'GIP',
1453 'EUR',
1454 'DKK',
1455 'XCD',
1456 'EUR',
1457 'USD',
1458 'GTQ',
1459 'GNF',
1460 'GWP',
1461 'XOF',
1462 'GYD',
1463 'HTG',
1464 'USD',
1465 'AUD',
1466 'EUR',
1467 'HNL',
1468 'HKD',
1469 'HUF',
1470 'ISK',
1471 'INR',
1472 'IDR',
1473 'XDR',
1474 'IRR',
1475 'IQD',
1476 'EUR',
1477 'ILS',
1478 'EUR',
1479 'JMD',
1480 'JPY',
1481 'JOD',
1482 'KZT',
1483 'KES',
1484 'AUD',
1485 'KPW',
1486 'KRW',
1487 'KWD',
1488 'KGS',
1489 'LAK',
1490 'LVL',
1491 'LBP',
1492 'ZAR',
1493 'LSL',
1494 'LRD',
1495 'LYD',
1496 'CHF',
1497 'LTL',
1498 'EUR',
1499 'MOP',
1500 'MKD',
1501 'MGA',
1502 'MWK',
1503 'MYR',
1504 'MVR',
1505 'XOF',
1506 'MTL',
1507 'USD',
1508 'EUR',
1509 'MRO',
1510 'MUR',
1511 'EUR',
1512 'MXN',
1513 'MXV',
1514 'USD',
1515 'MDL',
1516 'EUR',
1517 'MNT',
1518 'XCD',
1519 'MAD',
1520 'MZM',
1521 'MMK',
1522 'ZAR',
1523 'NAD',
1524 'AUD',
1525 'NPR',
1526 'EUR',
1527 'ANG',
1528 'XPF',
1529 'NZD',
1530 'NIO',
1531 'XOF',
1532 'NGN',
1533 'NZD',
1534 'AUD',
1535 'USD',
1536 'NOK',
1537 'OMR',
1538 'PKR',
1539 'USD',
1540 'PAB',
1541 'USD',
1542 'PGK',
1543 'PYG',
1544 'PEN',
1545 'PHP',
1546 'NZD',
1547 'PLN',
1548 'EUR',
1549 'USD',
1550 'QAR',
1551 'EUR',
1552 'ROL',
1553 'RON',
1554 'RUB',
1555 'RWF',
1556 'SHP',
1557 'XCD',
1558 'XCD',
1559 'EUR',
1560 'XCD',
1561 'WST',
1562 'EUR',
1563 'STD',
1564 'SAR',
1565 'XOF',
1566 'CSD',
1567 'EUR',
1568 'SCR',
1569 'SLL',
1570 'SGD',
1571 'SKK',
1572 'SIT',
1573 'SBD',
1574 'SOS',
1575 'ZAR',
1576 'EUR',
1577 'LKR',
1578 'SDD',
1579 'SRD',
1580 'NOK',
1581 'SZL',
1582 'SEK',
1583 'CHF',
1584 'CHW',
1585 'CHE',
1586 'SYP',
1587 'TWD',
1588 'TJS',
1589 'TZS',
1590 'THB',
1591 'USD',
1592 'XOF',
1593 'NZD',
1594 'TOP',
1595 'TTD',
1596 'TND',
1597 'TRY',
1598 'TRL',
1599 'TMM',
1600 'USD',
1601 'AUD',
1602 'UGX',
1603 'UAH',
1604 'AED',
1605 'GBP',
1606 'USD',
1607 'USS',
1608 'USN',
1609 'USD',
1610 'UYU',
1611 'UZS',
1612 'VUV',
1613 'VEB',
1614 'VND',
1615 'USD',
1616 'USD',
1617 'XPF',
1618 'MAD',
1619 'YER',
1620 'ZMK',
1621 'ZWD',
1622 'XAU',
1623 'XBA',
1624 'XBB',
1625 'XBC',
1626 'XBD',
1627 'XPD',
1628 'XPT',
1629 'XAG',
1630 'XFU',
1631 'XFO',
1632 'XTS',
1633 'XXX',
1634 );
1635 }
1636 return self::$currencyCode;
1637 }
1638
1639 /**
1640 * Get all the County from database.
1641 *
1642 * The static array county is returned, and if it's
1643 * called the first time, the <b>County DAO</b> is used
1644 * to get all the Counties.
1645 *
1646 * Note: any database errors will be trapped by the DAO.
1647 *
1648 * @access public
1649 * @static
1650 *
1651 * @param int $id - Optional id to return
1652 *
1653 * @return array - array reference of all Counties
1654 *
1655 */
1656 public static function &county($id = FALSE) {
1657 if (!self::$county) {
1658
1659 $config = CRM_Core_Config::singleton();
1660 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1661 self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1662 }
1663 if ($id) {
1664 if (array_key_exists($id, self::$county)) {
1665 return self::$county[$id];
1666 }
1667 else {
1668 return CRM_Core_DAO::$_nullObject;
1669 }
1670 }
1671 return self::$county;
1672 }
1673
1674 /**
1675 * Get all the Preferred Communication Methods from database.
1676 *
1677 * @access public
1678 * @static
1679 *
1680 * @return array self::pcm - array reference of all preferred communication methods.
1681 *
1682 */
1683 public static function &pcm($localize = FALSE) {
1684 if (!self::$pcm) {
1685 self::$pcm = CRM_Core_OptionGroup::values('preferred_communication_method', FALSE, FALSE, $localize);
1686 }
1687 return self::$pcm;
1688 }
1689
1690 /**
1691 * Alias of pcm
1692 */
1693 public static function preferredCommunicationMethod($localize = FALSE) {
1694 return self::pcm($localize);
1695 }
1696
1697 /**
1698 * Get all active payment processors
1699 *
1700 * The static array paymentProcessor is returned
1701 *
1702 * @access public
1703 * @static
1704 *
1705 * @param boolean $all - get payment processors - default is to get only active ones.
1706 * @param boolean $test - get test payment processors
1707 *
1708 * @return array - array of all payment processors
1709 *
1710 */
1711 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1712 $condition = "is_test = ";
1713 $condition .= ($test) ? '1' : '0';
1714
1715 if ($additionalCond) {
1716 $condition .= " AND ( $additionalCond ) ";
1717 }
1718
1719 // CRM-7178. Make sure we only include payment processors valid in ths
1720 // domain
1721 $condition .= " AND domain_id = " . CRM_Core_Config::domainID();
1722
1723 $cacheKey = $condition . '_' . (int) $all;
1724 if (!isset(self::$paymentProcessor[$cacheKey])) {
1725 self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1726 }
1727
1728 return self::$paymentProcessor[$cacheKey];
1729 }
1730
1731 /**
1732 * Get all active payment processors
1733 *
1734 * The static array paymentProcessorType is returned
1735 *
1736 * @access public
1737 * @static
1738 *
1739 * @param boolean $all - get payment processors - default is to get only active ones.
1740 *
1741 * @return array - array of all payment processor types
1742 *
1743 */
1744 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1745 $cacheKey = $id . '_' .$return;
1746 if (empty(self::$paymentProcessorType[$cacheKey])) {
1747 self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1748 }
1749 if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) {
1750 return self::$paymentProcessorType[$cacheKey][$id];
1751 }
1752 return self::$paymentProcessorType[$cacheKey];
1753 }
1754
1755 /**
1756 * Get all the World Regions from Database
1757 *
1758 * @access public
1759 *
1760 * @return array - array reference of all World Regions
1761 * @static
1762 */
1763 public static function &worldRegion($id = FALSE) {
1764 if (!self::$worldRegions) {
1765 self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1766 }
1767
1768 if ($id) {
1769 if (array_key_exists($id, self::$worldRegions)) {
1770 return self::$worldRegions[$id];
1771 }
1772 else {
1773 return CRM_Core_DAO::$_nullObject;
1774 }
1775 }
1776
1777 return self::$worldRegions;
1778 }
1779
1780 /**
1781 * Get all Honor Type.
1782 *
1783 * The static array honorType is returned
1784 *
1785 * @access public
1786 * @static
1787 *
1788 * @param boolean $all - get All Honor Type.
1789 *
1790 * @return array - array reference of all Honor Types.
1791 *
1792 */
1793 public static function &honor() {
1794 if (!self::$honorType) {
1795 self::$honorType = CRM_Core_OptionGroup::values('honor_type');
1796 }
1797 return self::$honorType;
1798 }
1799
1800 /**
1801 * Get all Activity Statuses.
1802 *
1803 * The static array activityStatus is returned
1804 *
1805 * @access public
1806 * @static
1807 *
1808 * @return array - array reference of all activity statuses
1809 */
1810 public static function &activityStatus($column = 'label') {
1811 if (NULL === self::$activityStatus) {
1812 self::$activityStatus = array();
1813 }
1814 if (!array_key_exists($column, self::$activityStatus)) {
1815 self::$activityStatus[$column] = array();
1816
1817 self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1818 }
1819
1820 return self::$activityStatus[$column];
1821 }
1822
1823 /**
1824 * Get all Priorities
1825 *
1826 * The static array Priority is returned
1827 *
1828 * @access public
1829 * @static
1830 *
1831 * @return array - array reference of all Priority
1832 */
1833 public static function &priority() {
1834 if (!self::$priority) {
1835 self::$priority = CRM_Core_OptionGroup::values('priority');
1836 }
1837
1838 return self::$priority;
1839 }
1840
1841 /**
1842 * Get all WYSIWYG Editors.
1843 *
1844 * The static array wysiwygEditor is returned
1845 *
1846 * @access public
1847 * @static
1848 *
1849 * @return array - array reference of all wysiwygEditors
1850 */
1851 public static function &wysiwygEditor() {
1852 if (!self::$wysiwygEditor) {
1853 self::$wysiwygEditor = CRM_Core_OptionGroup::values('wysiwyg_editor');
1854 }
1855 return self::$wysiwygEditor;
1856 }
1857
1858 /**
1859 * Get all Visibility levels.
1860 *
1861 * The static array visibility is returned
1862 *
1863 * @access public
1864 * @static
1865 *
1866 * @return array - array reference of all Visibility levels.
1867 *
1868 */
1869 public static function &visibility($column = 'label') {
1870 if (!isset(self::$visibility)) {
1871 self::$visibility = array( );
1872 }
1873
1874 if (!isset(self::$visibility[$column])) {
1875 self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1876 }
1877
1878 return self::$visibility[$column];
1879 }
1880
1881 /**
1882 * Get all mapping types
1883 *
1884 * @return array - array reference of all mapping types
1885 * @access public
1886 * @static
1887 */
1888 public static function &mappingTypes() {
1889 if (!self::$mappingType) {
1890 self::$mappingType = CRM_Core_OptionGroup::values('mapping_type');
1891 }
1892 return self::$mappingType;
1893 }
1894
1895 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1896 static $_cache = NULL;
1897
1898 $cacheKey = "{$countryID}_{$field}";
1899 if (!$_cache) {
1900 $_cache = array();
1901 }
1902
1903 if (!empty($_cache[$cacheKey])) {
1904 return $_cache[$cacheKey];
1905 }
1906
1907 $query = "
1908 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1909 FROM civicrm_state_province
1910 WHERE country_id = %1
1911 ORDER BY name";
1912 $params = array(
1913 1 => array(
1914 $countryID,
1915 'Integer',
1916 ),
1917 );
1918
1919 $dao = CRM_Core_DAO::executeQuery($query, $params);
1920
1921 $result = array();
1922 while ($dao->fetch()) {
1923 $result[$dao->id] = $dao->name;
1924 }
1925
1926 // localise the stateProvince names if in an non-en_US locale
1927 $config = CRM_Core_Config::singleton();
1928 global $tsLocale;
1929 if ($tsLocale != '' and $tsLocale != 'en_US') {
1930 $i18n = CRM_Core_I18n::singleton();
1931 $i18n->localizeArray($result, array(
1932 'context' => 'province',
1933 ));
1934 $result = CRM_Utils_Array::asort($result);
1935 }
1936
1937 $_cache[$cacheKey] = $result;
1938
1939 CRM_Utils_Hook::buildStateProvinceForCountry($countryID, $result);
1940
1941 return $result;
1942 }
1943
1944 public static function &countyForState($stateID) {
1945 if (is_array($stateID)) {
1946 $states = implode(", ", $stateID);
1947 $query = "
1948 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1949 FROM civicrm_county
1950 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1951 WHERE civicrm_county.state_province_id in ( $states )
1952 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1953
1954 $dao = CRM_Core_DAO::executeQuery($query);
1955
1956 $result = array();
1957 while ($dao->fetch()) {
1958 $result[$dao->id] = $dao->abbreviation . ': ' . $dao->name;
1959 }
1960 }
1961 else {
1962
1963 static $_cache = NULL;
1964
1965 $cacheKey = "{$stateID}_name";
1966 if (!$_cache) {
1967 $_cache = array();
1968 }
1969
1970 if (!empty($_cache[$cacheKey])) {
1971 return $_cache[$cacheKey];
1972 }
1973
1974 $query = "
1975 SELECT civicrm_county.name name, civicrm_county.id id
1976 FROM civicrm_county
1977 WHERE state_province_id = %1
1978 ORDER BY name";
1979 $params = array(
1980 1 => array(
1981 $stateID,
1982 'Integer',
1983 ),
1984 );
1985
1986 $dao = CRM_Core_DAO::executeQuery($query, $params);
1987
1988 $result = array();
1989 while ($dao->fetch()) {
1990 $result[$dao->id] = $dao->name;
1991 }
1992 }
1993
1994 return $result;
1995 }
1996
1997 /**
1998 * Given a state ID return the country ID, this allows
1999 * us to populate forms and values for downstream code
2000 *
2001 * @param $stateID int
2002 *
2003 * @return int the country id that the state belongs to
2004 * @static
2005 * @public
2006 */
2007 static function countryIDForStateID($stateID) {
2008 if (empty($stateID)) {
2009 return CRM_Core_DAO::$_nullObject;
2010 }
2011
2012 $query = "
2013 SELECT country_id
2014 FROM civicrm_state_province
2015 WHERE id = %1
2016 ";
2017 $params = array(1 => array($stateID, 'Integer'));
2018
2019 return CRM_Core_DAO::singleValueQuery($query, $params);
2020 }
2021
2022 /**
2023 * Get all types of Greetings.
2024 *
2025 * The static array of greeting is returned
2026 *
2027 * @access public
2028 * @static
2029 *
2030 * @param $filter - get All Email Greetings - default is to get only active ones.
2031 *
2032 * @return array - array reference of all greetings.
2033 *
2034 */
2035 public static function greeting($filter, $columnName = 'label') {
2036 $index = $filter['greeting_type'] . '_' . $columnName;
2037
2038 // also add contactType to the array
2039 $contactType = CRM_Utils_Array::value('contact_type', $filter);
2040 if ($contactType) {
2041 $index .= '_' . $contactType;
2042 }
2043
2044 if (NULL === self::$greeting) {
2045 self::$greeting = array();
2046 }
2047
2048 if (!CRM_Utils_Array::value($index, self::$greeting)) {
2049 $filterCondition = NULL;
2050 if ($contactType) {
2051 $filterVal = 'v.filter =';
2052 switch ($contactType) {
2053 case 'Individual':
2054 $filterVal .= "1";
2055 break;
2056
2057 case 'Household':
2058 $filterVal .= "2";
2059 break;
2060
2061 case 'Organization':
2062 $filterVal .= "3";
2063 break;
2064 }
2065 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
2066 }
2067
2068 self::$greeting[$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
2069 }
2070
2071 return self::$greeting[$index];
2072 }
2073
2074 /**
2075 * Construct array of default greeting values for contact type
2076 *
2077 * @access public
2078 * @static
2079 *
2080 * @return array - array reference of default greetings.
2081 *
2082 */
2083 public static function &greetingDefaults() {
2084 if (!self::$greetingDefaults) {
2085 $defaultGreetings = array();
2086 $contactTypes = array(
2087 'Individual' => 1,
2088 'Household' => 2,
2089 'Organization' => 3,
2090 );
2091
2092 foreach ($contactTypes as $contactType => $filter) {
2093 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
2094
2095 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
2096 $tokenVal = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
2097 $defaultGreetings[$contactType][$greeting] = $tokenVal;
2098 }
2099 }
2100
2101 self::$greetingDefaults = $defaultGreetings;
2102 }
2103
2104 return self::$greetingDefaults;
2105 }
2106
2107 /**
2108 * Get all the Languages from database.
2109 *
2110 * @access public
2111 * @static
2112 *
2113 * @return array self::languages - array reference of all languages
2114 *
2115 */
2116 public static function &languages() {
2117 return CRM_Core_I18n_PseudoConstant::languages();
2118 }
2119
2120 /**
2121 * Alias of above
2122 */
2123 public static function &preferredLanguage() {
2124 return CRM_Core_I18n_PseudoConstant::languages();
2125 }
2126
2127 /**
2128 * Get all extensions
2129 *
2130 * The static array extensions
2131 *
2132 * FIXME: This is called by civix but not by any core code. We
2133 * should provide an API call which civix can use instead.
2134 *
2135 * @access public
2136 * @static
2137 *
2138 * @return array - array($fullyQualifiedName => $label) list of extensions
2139 */
2140 public static function &getExtensions() {
2141 if (!self::$extensions) {
2142 self::$extensions = array();
2143 $sql = '
2144 SELECT full_name, label
2145 FROM civicrm_extension
2146 WHERE is_active = 1
2147 ';
2148 $dao = CRM_Core_DAO::executeQuery($sql);
2149 while ($dao->fetch()) {
2150 self::$extensions[$dao->full_name] = $dao->label;
2151 }
2152 }
2153
2154 return self::$extensions;
2155 }
2156
2157 /**
2158 * Fetch the list of active extensions of type 'module'
2159 *
2160 * @param $fresh bool whether to forcibly reload extensions list from canonical store
2161 * @access public
2162 * @static
2163 *
2164 * @return array - array(array('prefix' => $, 'file' => $))
2165 */
2166 public static function getModuleExtensions($fresh = FALSE) {
2167 return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh);
2168 }
2169
2170 /**
2171 * Get all Activity Contacts
2172 *
2173 * The static array activityContacts is returned
2174 *
2175 * @access public
2176 * @static
2177 *
2178 * @param string $column db column name/label.
2179 *
2180 * @return array - array reference of all activity Contacts
2181 *
2182 */
2183 public static function &activityContacts($column = 'label') {
2184 if (!self::$activityContacts) {
2185 self::$activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, $column);
2186 }
2187 return self::$activityContacts;
2188 }
2189
2190 /**
2191 * Get all Event Contacts
2192 *
2193 * The static array eventContacts is returned
2194 *
2195 * @access public
2196 * @static
2197 *
2198 * @param string $column db column name/label.
2199 *
2200 * @return array - array reference of all event Contacts
2201 *
2202 */
2203 public static function &eventContacts($column = 'label') {
2204 if (!self::$eventContacts) {
2205 self::$eventContacts = CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, $column);
2206 }
2207 return self::$eventContacts;
2208 }
2209
2210 /**
2211 * Get all options values
2212 *
2213 * The static array option values is returned
2214 *
2215 * @access public
2216 * @static
2217 *
2218 * @param boolean $optionGroupName - get All Option Group values- default is to get only active ones.
2219 *
2220 * @return array - array reference of all Option Group Name
2221 *
2222 */
2223 public static function accountOptionValues($optionGroupName, $id = null, $condition = null) {
2224 $cacheKey = $optionGroupName . '_' . $condition;
2225 if (empty(self::$accountOptionValues[$cacheKey])) {
2226 self::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup::values($optionGroupName, false, false, false, $condition);
2227 }
2228 if ($id) {
2229 return CRM_Utils_Array::value($id, self::$accountOptionValues[$cacheKey]);
2230 }
2231
2232 return self::$accountOptionValues[$cacheKey];
2233 }
2234
2235 /**
2236 * Get all batch modes
2237 *
2238 * The static array batchModes
2239 *
2240 * @access public
2241 * @static
2242 *
2243 * @return array - array reference of all batch modes
2244 */
2245 public static function &getBatchMode($columnName = 'label') {
2246 if (!self::$batchModes) {
2247 self::$batchModes = CRM_Core_OptionGroup::values('batch_mode', false, false, false, null, $columnName);
2248 }
2249
2250 return self::$batchModes;
2251 }
2252
2253 /**
2254 * Get all batch types
2255 *
2256 * The static array batchTypes
2257 *
2258 * @access public
2259 * @static
2260 *
2261 * @return array - array reference of all batch types
2262 */
2263 public static function &getBatchType() {
2264 if (!self::$batchTypes) {
2265 self::$batchTypes = CRM_Core_OptionGroup::values('batch_type');
2266 }
2267
2268 return self::$batchTypes;
2269 }
2270
2271 /**
2272 * Get all batch statuses
2273 *
2274 * The static array batchStatues
2275 *
2276 * @access public
2277 * @static
2278 *
2279 * @return array - array reference of all batch statuses
2280 */
2281 public static function &getBatchStatus() {
2282 if (!self::$batchStatues) {
2283 self::$batchStatues = CRM_Core_OptionGroup::values('batch_status');
2284 }
2285
2286 return self::$batchStatues;
2287 }
2288
2289 /*
2290 * The static array contactType is returned
2291 *
2292 * @access public
2293 * @static
2294 * @param string $column db column name/label.
2295 *
2296 * @return array - array reference of all Types
2297 *
2298 */
2299
2300 public static function &contactType($column = 'label') {
2301 if (!self::$contactType) {
2302 self::$contactType = CRM_Contact_BAO_ContactType::basicTypePairs(TRUE);
2303 }
2304 return self::$contactType;
2305 }
2306
2307 /**
2308 * Get all the auto renew options
2309 *
2310 * @access public
2311 * @static
2312 *
2313 * @return array self::autoRenew - array reference of all autoRenew
2314 *
2315 */
2316 public static function &autoRenew() {
2317 if (!self::$autoRenew) {
2318 self::$autoRenew = CRM_Core_OptionGroup::values('auto_renew_options', FALSE, FALSE);
2319 }
2320 return self::$autoRenew;
2321 }
2322 }
2323