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