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