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