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