3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Stores all constants and pseudo constants for CRM application.
32 * examples of constants are "Contact Type" which will always be either
33 * 'Individual', 'Household', 'Organization'.
35 * pseudo constants are entities from the database whose values rarely
36 * change. examples are list of countries, states, location types,
39 * currently we're getting the data from the underlying database. this
40 * will be reworked to use caching.
42 * Note: All pseudoconstants should be uninitialized or default to NULL.
43 * This provides greater consistency/predictability after flushing.
46 * @copyright CiviCRM LLC (c) 2004-2013
50 class CRM_Core_PseudoConstant
{
53 * static cache for pseudoconstant arrays
57 private static $cache;
60 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
66 private static $activityType;
73 private static $stateProvince;
80 private static $county;
83 * states/provinces abbreviations
87 private static $stateProvinceAbbreviation;
94 private static $country;
101 private static $countryIsoCode;
104 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
110 private static $group;
117 private static $groupIterator;
124 private static $relationshipType;
127 * civicrm groups that are not smart groups
131 private static $staticGroup;
138 private static $currencyCode;
145 private static $paymentProcessor;
148 * payment processor types
152 private static $paymentProcessorType;
159 private static $worldRegions;
162 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
168 private static $activityStatus;
175 private static $visibility;
182 private static $greeting;
189 private static $greetingDefaults;
192 * Extensions of type module
196 private static $extensions;
199 * Financial Account Type
203 private static $accountOptionValues;
206 * Low-level option getter, rarely accessed directly.
207 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
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
225 * @return Array on success, FALSE on error.
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
236 'onlyActive' => ($context == 'validate' ||
$context == 'create') ?
FALSE : TRUE,
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);
247 if (!empty($customField->option_group_id
)) {
248 $options = CRM_Core_OptionGroup
::valuesByID($customField->option_group_id
,
252 // Note: for custom fields the 'name' column is NULL
253 CRM_Utils_Array
::value('labelColumn', $params, 'label'),
254 $params['onlyActive'],
259 if ($customField->data_type
=== 'StateProvince') {
260 $options = self
::stateProvince();
262 elseif ($customField->data_type
=== 'Country') {
263 $options = $context == 'validate' ? self
::countryIsoCode() : self
::country();
265 elseif ($customField->data_type
=== 'Boolean') {
266 $options = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
268 $options = $options && $flip ?
array_flip($options) : $options;
270 if ($options !== FALSE) {
271 CRM_Utils_Hook
::customFieldOptions($customField->id
, $options, FALSE);
273 $customField->free();
277 // Core field: load schema
279 $fields = $dao->fields();
280 $fieldKeys = $dao->fieldKeys();
283 // Support "unique names" as well as sql names
284 $fieldKey = $fieldName;
285 if (empty($fields[$fieldKey])) {
286 $fieldKey = $fieldKeys[$fieldName];
288 // If neither worked then this field doesn't exist. Return false.
289 if (empty($fields[$fieldKey])) {
292 $fieldSpec = $fields[$fieldKey];
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);
302 elseif (!empty($fieldSpec['pseudoconstant'])) {
303 $pseudoconstant = $fieldSpec['pseudoconstant'];
304 // Merge params with schema defaults
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),
311 // Fetch option group from option_value table
312 if(!empty($pseudoconstant['optionGroupName'])) {
313 if ($context == 'validate') {
314 $params['labelColumn'] = 'name';
316 // Call our generic fn for retrieving from the option_value table
317 return CRM_Core_OptionGroup
::values(
318 $pseudoconstant['optionGroupName'],
322 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
323 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
324 $params['onlyActive'],
326 $params['keyColumn'] ?
$params['keyColumn'] : 'value'
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');
335 $cacheKey = $daoName . $fieldName . serialize($params);
337 // Retrieve cached options
338 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
339 $output = self
::$cache[$cacheKey];
342 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
343 if (!class_exists($daoName)) {
346 // Get list of fields for the option table
348 $availableFields = array_keys($dao->fieldKeys());
351 $select = "SELECT %1 AS id, %2 AS label";
354 $order = "ORDER BY %2";
356 // Use machine name instead of label in validate context
357 if ($context == 'validate') {
358 if (!empty($pseudoconstant['nameColumn'])) {
359 $params['labelColumn'] = $pseudoconstant['nameColumn'];
361 elseif (in_array('name', $availableFields)) {
362 $params['labelColumn'] = 'name';
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']);
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";
377 // Filter domain specific options
378 if (in_array('domain_id', $availableFields)) {
379 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
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
),
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";
391 // Support no sorting if $params[orderColumn] is FALSE
392 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
395 // Default to 'weight' if that column exists
396 elseif (in_array('weight', $availableFields)) {
397 $order = "ORDER BY weight";
401 $query = "$select $from";
403 $query .= " WHERE " . implode($wheres, ' AND ');
405 $query .= ' ' . $order;
406 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
407 while ($dao->fetch()) {
408 $output[$dao->id
] = $dao->label
;
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';
417 if ($pseudoconstant['table'] == 'civicrm_state_province') {
418 $I18nParams['context'] = 'province';
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);
427 self
::$cache[$cacheKey] = $output;
429 return $flip ?
array_flip($output) : $output;
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;
438 // If we're still here, it's an error. Return FALSE.
443 * Fetch the translated label for a field given its key
445 * @param String $baoName
446 * @param String $fieldName
447 * @param String|Int $key
449 * TODO: Accept multivalued input?
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
456 static function getLabel($baoName, $fieldName, $key) {
457 $values = $baoName::buildOptions($fieldName, 'get');
458 if ($values === FALSE) {
461 return CRM_Utils_Array
::value($key, $values);
465 * Fetch the machine name for a field given its key
467 * @param String $baoName
468 * @param String $fieldName
469 * @param String|Int $key
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
476 static function getName($baoName, $fieldName, $key) {
477 $values = $baoName::buildOptions($fieldName, 'validate');
478 if ($values === FALSE) {
481 return CRM_Utils_Array
::value($key, $values);
485 * Fetch the key for a field option given its name
487 * @param String $baoName
488 * @param String $fieldName
489 * @param String|Int $value
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
496 static function getKey($baoName, $fieldName, $value) {
497 $values = $baoName::buildOptions($fieldName, 'validate');
498 if ($values === FALSE) {
501 return CRM_Utils_Array
::key($value, $values);
505 * DEPRECATED generic populate method
506 * All pseudoconstant functions that use this method are also deprecated.
508 * The static array $var is populated from the db
509 * using the <b>$name DAO</b>.
511 * Note: any database errors will be trapped by the DAO.
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
524 public static function populate(
529 $filter = 'is_active',
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)) {
542 $object = new $name ( );
544 $object->selectAdd();
545 $object->selectAdd("$key, $retrieve");
547 $object->whereAdd($condition);
551 $object->orderBy($retrieve);
554 $object->orderBy($orderby);
558 $object->$filter = 1;
563 while ($object->fetch()) {
564 $var[$object->$key] = $object->$retrieve;
567 $cache->set($cacheKey, $var);
571 * Flush given pseudoconstant so it can be reread from db
572 * nex time it's requested.
577 * @param boolean $name pseudoconstant to be flushed
580 public static function flush($name = 'cache') {
581 if (isset(self
::$
$name)) {
584 if ($name == 'cache') {
585 CRM_Core_OptionGroup
::flushAll();
590 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
592 * Get all Activty types.
594 * The static array activityType is returned
596 * @param boolean $all - get All Activity types - default is to get only active ones.
601 * @return array - array reference of all activity types.
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;
615 if (NULL === self
::$activityType) {
616 self
::$activityType = array();
619 if (!isset(self
::$activityType[$index]) ||
$reset) {
622 $condition = 'AND filter = 0';
624 $componentClause = " v.component_id IS NULL";
625 if ($onlyComponentActivities) {
626 $componentClause = " v.component_id IS NOT NULL";
629 $componentIds = array();
630 $compInfo = CRM_Core_Component
::getEnabledComponents();
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
;
640 elseif ($compName == 'CiviCampaign') {
641 if ($includeCampaignActivities) {
642 $componentIds[] = $compObj->componentID
;
646 $componentIds[] = $compObj->componentID
;
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 ) )";
657 $condition = $condition . ' AND ' . $componentClause;
659 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
661 return self
::$activityType[$index];
665 * Get all the State/Province from database.
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.
671 * Note: any database errors will be trapped by the DAO.
676 * @param int $id - Optional id to return
678 * @return array - array reference of all State/Provinces.
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();
686 $countryIsoCodes = self
::countryIsoCode();
687 $limitCodes = $config->provinceLimit();
689 foreach ($limitCodes as $code) {
690 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
692 if (!empty($limitIds)) {
693 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
696 $whereClause = FALSE;
699 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
701 // localise the province names if in an non-en_US locale
703 if ($tsLocale != '' and $tsLocale != 'en_US') {
704 $i18n = CRM_Core_I18n
::singleton();
705 $i18n->localizeArray(self
::$stateProvince, array(
706 'context' => 'province',
708 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
712 if (array_key_exists($id, self
::$stateProvince)) {
713 return self
::$stateProvince[$id];
720 return self
::$stateProvince;
724 * Get all the State/Province abbreviations from the database.
726 * Same as above, except gets the abbreviations instead of the names.
731 * @param int $id - Optional id to return
733 * @return array - array reference of all State/Province abbreviations.
735 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
739 FROM civicrm_state_province
747 return CRM_Core_DAO
::singleValueQuery($query, $params);
750 if (!self
::$stateProvinceAbbreviation ||
!$id) {
752 $whereClause = FALSE;
755 $config = CRM_Core_Config
::singleton();
756 $countryIsoCodes = self
::countryIsoCode();
757 $limitCodes = $config->provinceLimit();
759 foreach ($limitCodes as $code) {
760 $tmpArray = array_keys($countryIsoCodes, $code);
762 if (!empty($tmpArray)) {
763 $limitIds[] = array_shift($tmpArray);
766 if (!empty($limitIds)) {
767 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
770 self
::populate(self
::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
774 if (array_key_exists($id, self
::$stateProvinceAbbreviation)) {
775 return self
::$stateProvinceAbbreviation[$id];
782 return self
::$stateProvinceAbbreviation;
786 * Get all the countries from database.
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.
792 * Note: any database errors will be trapped by the DAO.
797 * @param int $id - Optional id to return
799 * @return array - array reference of all countries.
802 public static function country($id = FALSE, $applyLimit = TRUE) {
803 if (($id && !CRM_Utils_Array
::value($id, self
::$country)) ||
!self
::$country ||
!$id) {
805 $config = CRM_Core_Config
::singleton();
806 $limitCodes = array();
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)) {
815 $config->countryLimit
=> 1,
819 $limitCodes = array_intersect(self
::countryIsoCode(), $limitCodes);
822 if (count($limitCodes)) {
823 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
829 self
::populate(self
::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
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;
841 // localise the country names if in an non-en_US locale
843 if ($tsLocale != '' and $tsLocale != 'en_US') {
844 $i18n = CRM_Core_I18n
::singleton();
845 $i18n->localizeArray(self
::$country, array(
846 'context' => 'country',
848 self
::$country = CRM_Utils_Array
::asort(self
::$country);
852 if (array_key_exists($id, self
::$country)) {
853 return self
::$country[$id];
856 return CRM_Core_DAO
::$_nullObject;
859 return self
::$country;
863 * Get all the country ISO Code abbreviations from the database.
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.
869 * Note: any database errors will be trapped by the DAO.
874 * @return array - array reference of all country ISO codes.
877 public static function &countryIsoCode($id = FALSE) {
878 if (!self
::$countryIsoCode) {
879 self
::populate(self
::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
882 if (array_key_exists($id, self
::$countryIsoCode)) {
883 return self
::$countryIsoCode[$id];
886 return CRM_Core_DAO
::$_nullObject;
889 return self
::$countryIsoCode;
893 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
895 * Get all groups from database
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.
901 * Note: any database errors will be trapped by the DAO.
903 * @param string $groupType type of group(Access/Mailing)
904 * @param boolen $excludeHidden exclude hidden groups.
909 * @return array - array reference of all groups.
912 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
913 $condition = CRM_Contact_BAO_Group
::groupTypeCondition($groupType, $excludeHidden);
916 self
::$group = array();
919 $groupKey = $groupType ?
$groupType : 'null';
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);
925 return self
::$group[$groupKey];
929 * Create or get groups iterator (iterates over nested groups in a
932 * The GroupNesting instance is returned; it's created if this is being
933 * called for the first time
939 * @return mixed - instance of CRM_Contact_BAO_GroupNesting
942 public static function &groupIterator($styledLabels = FALSE) {
943 if (!self
::$groupIterator) {
945 When used as an object, GroupNesting implements Iterator
946 and iterates nested groups in a logical manner for us
948 self
::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
950 return self
::$groupIterator;
954 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
956 * Get all permissioned groups from database
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.
962 * Note: any database errors will be trapped by the DAO.
964 * @param string $groupType type of group(Access/Mailing)
965 * @param boolen $excludeHidden exclude hidden groups.
970 * @return array - array reference of all groups.
973 public static function group($groupType = NULL, $excludeHidden = TRUE) {
974 return CRM_Core_Permission
::group($groupType, $excludeHidden);
978 * Get all permissioned groups from database
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.
984 * Note: any database errors will be trapped by the DAO.
989 * @return array - array reference of all groups.
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';
996 $condition .= " AND visibility != 'User and User Admin Only'";
1000 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1003 if ($excludeHidden) {
1004 $condition .= ' AND is_hidden != 1 ';
1007 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1010 return self
::$staticGroup;
1014 * Get all Relationship Types from database.
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.
1020 * Note: any database errors will be trapped by the DAO.
1022 * @param string $valueColumnName db column name/label.
1023 * @param boolean $reset reset relationship types if true
1028 * @return array - array reference of all relationship types.
1030 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1031 if (!CRM_Utils_Array
::value($valueColumnName, self
::$relationshipType) ||
$reset) {
1032 self
::$relationshipType[$valueColumnName] = array();
1034 //now we have name/label columns CRM-3336
1035 $column_a_b = "{$valueColumnName}_a_b";
1036 $column_b_a = "{$valueColumnName}_b_a";
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()) {
1045 self
::$relationshipType[$valueColumnName][$relationshipTypeDAO->id
] = array(
1046 $column_a_b => $relationshipTypeDAO->$column_a_b,
1047 $column_b_a => $relationshipTypeDAO->$column_b_a,
1048 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1049 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1050 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1051 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1056 return self
::$relationshipType[$valueColumnName];
1060 * get all the ISO 4217 currency codes
1062 * so far, we use this for validation only, so there's no point of putting this into the database
1066 * @return array - array reference of all currency codes
1069 public static function ¤cyCode() {
1070 if (!self
::$currencyCode) {
1071 self
::$currencyCode = array(
1341 return self
::$currencyCode;
1345 * Get all the County from database.
1347 * The static array county is returned, and if it's
1348 * called the first time, the <b>County DAO</b> is used
1349 * to get all the Counties.
1351 * Note: any database errors will be trapped by the DAO.
1356 * @param int $id - Optional id to return
1358 * @return array - array reference of all Counties
1361 public static function &county($id = FALSE) {
1362 if (!self
::$county) {
1364 $config = CRM_Core_Config
::singleton();
1365 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1366 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1369 if (array_key_exists($id, self
::$county)) {
1370 return self
::$county[$id];
1373 return CRM_Core_DAO
::$_nullObject;
1376 return self
::$county;
1380 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1381 * Get all active payment processors
1383 * The static array paymentProcessor is returned
1388 * @param boolean $all - get payment processors - default is to get only active ones.
1389 * @param boolean $test - get test payment processors
1391 * @return array - array of all payment processors
1394 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1395 $condition = "is_test = ";
1396 $condition .= ($test) ?
'1' : '0';
1398 if ($additionalCond) {
1399 $condition .= " AND ( $additionalCond ) ";
1402 // CRM-7178. Make sure we only include payment processors valid in ths
1404 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1406 $cacheKey = $condition . '_' . (int) $all;
1407 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1408 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1411 return self
::$paymentProcessor[$cacheKey];
1415 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1417 * The static array paymentProcessorType is returned
1422 * @param boolean $all - get payment processors - default is to get only active ones.
1424 * @return array - array of all payment processor types
1427 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1428 $cacheKey = $id . '_' .$return;
1429 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1430 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1432 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1433 return self
::$paymentProcessorType[$cacheKey][$id];
1435 return self
::$paymentProcessorType[$cacheKey];
1439 * Get all the World Regions from Database
1443 * @return array - array reference of all World Regions
1446 public static function &worldRegion($id = FALSE) {
1447 if (!self
::$worldRegions) {
1448 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1452 if (array_key_exists($id, self
::$worldRegions)) {
1453 return self
::$worldRegions[$id];
1456 return CRM_Core_DAO
::$_nullObject;
1460 return self
::$worldRegions;
1464 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1466 * Get all Activity Statuses.
1468 * The static array activityStatus is returned
1473 * @return array - array reference of all activity statuses
1475 public static function &activityStatus($column = 'label') {
1476 if (NULL === self
::$activityStatus) {
1477 self
::$activityStatus = array();
1479 if (!array_key_exists($column, self
::$activityStatus)) {
1480 self
::$activityStatus[$column] = array();
1482 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1485 return self
::$activityStatus[$column];
1489 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1491 * Get all Visibility levels.
1493 * The static array visibility is returned
1498 * @return array - array reference of all Visibility levels.
1501 public static function &visibility($column = 'label') {
1502 if (!isset(self
::$visibility)) {
1503 self
::$visibility = array( );
1506 if (!isset(self
::$visibility[$column])) {
1507 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1510 return self
::$visibility[$column];
1513 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1514 static $_cache = NULL;
1516 $cacheKey = "{$countryID}_{$field}";
1521 if (!empty($_cache[$cacheKey])) {
1522 return $_cache[$cacheKey];
1526 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1527 FROM civicrm_state_province
1528 WHERE country_id = %1
1537 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1540 while ($dao->fetch()) {
1541 $result[$dao->id
] = $dao->name
;
1544 // localise the stateProvince names if in an non-en_US locale
1545 $config = CRM_Core_Config
::singleton();
1547 if ($tsLocale != '' and $tsLocale != 'en_US') {
1548 $i18n = CRM_Core_I18n
::singleton();
1549 $i18n->localizeArray($result, array(
1550 'context' => 'province',
1552 $result = CRM_Utils_Array
::asort($result);
1555 $_cache[$cacheKey] = $result;
1557 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1562 public static function &countyForState($stateID) {
1563 if (is_array($stateID)) {
1564 $states = implode(", ", $stateID);
1566 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1568 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1569 WHERE civicrm_county.state_province_id in ( $states )
1570 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1572 $dao = CRM_Core_DAO
::executeQuery($query);
1575 while ($dao->fetch()) {
1576 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1581 static $_cache = NULL;
1583 $cacheKey = "{$stateID}_name";
1588 if (!empty($_cache[$cacheKey])) {
1589 return $_cache[$cacheKey];
1593 SELECT civicrm_county.name name, civicrm_county.id id
1595 WHERE state_province_id = %1
1604 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1607 while ($dao->fetch()) {
1608 $result[$dao->id
] = $dao->name
;
1616 * Given a state ID return the country ID, this allows
1617 * us to populate forms and values for downstream code
1619 * @param $stateID int
1621 * @return int the country id that the state belongs to
1625 static function countryIDForStateID($stateID) {
1626 if (empty($stateID)) {
1627 return CRM_Core_DAO
::$_nullObject;
1632 FROM civicrm_state_province
1635 $params = array(1 => array($stateID, 'Integer'));
1637 return CRM_Core_DAO
::singleValueQuery($query, $params);
1641 * Get all types of Greetings.
1643 * The static array of greeting is returned
1648 * @param $filter - get All Email Greetings - default is to get only active ones.
1650 * @return array - array reference of all greetings.
1653 public static function greeting($filter, $columnName = 'label') {
1654 $index = $filter['greeting_type'] . '_' . $columnName;
1656 // also add contactType to the array
1657 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1659 $index .= '_' . $contactType;
1662 if (NULL === self
::$greeting) {
1663 self
::$greeting = array();
1666 if (!CRM_Utils_Array
::value($index, self
::$greeting)) {
1667 $filterCondition = NULL;
1669 $filterVal = 'v.filter =';
1670 switch ($contactType) {
1679 case 'Organization':
1683 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1686 self
::$greeting[$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1689 return self
::$greeting[$index];
1693 * Construct array of default greeting values for contact type
1698 * @return array - array reference of default greetings.
1701 public static function &greetingDefaults() {
1702 if (!self
::$greetingDefaults) {
1703 $defaultGreetings = array();
1704 $contactTypes = self
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
1706 foreach ($contactTypes as $filter => $contactType) {
1707 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1709 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1710 $tokenVal = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1711 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1715 self
::$greetingDefaults = $defaultGreetings;
1718 return self
::$greetingDefaults;
1722 * Get all extensions
1724 * The static array extensions
1726 * FIXME: This is called by civix but not by any core code. We
1727 * should provide an API call which civix can use instead.
1732 * @return array - array($fullyQualifiedName => $label) list of extensions
1734 public static function &getExtensions() {
1735 if (!self
::$extensions) {
1736 self
::$extensions = array();
1738 SELECT full_name, label
1739 FROM civicrm_extension
1742 $dao = CRM_Core_DAO
::executeQuery($sql);
1743 while ($dao->fetch()) {
1744 self
::$extensions[$dao->full_name
] = $dao->label
;
1748 return self
::$extensions;
1752 * Get all options values
1754 * The static array option values is returned
1759 * @param boolean $optionGroupName - get All Option Group values- default is to get only active ones.
1761 * @return array - array reference of all Option Group Name
1764 public static function accountOptionValues($optionGroupName, $id = null, $condition = null) {
1765 $cacheKey = $optionGroupName . '_' . $condition;
1766 if (empty(self
::$accountOptionValues[$cacheKey])) {
1767 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, false, false, false, $condition);
1770 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1773 return self
::$accountOptionValues[$cacheKey];
1777 * Fetch the list of active extensions of type 'module'
1779 * @param $fresh bool whether to forcibly reload extensions list from canonical store
1783 * @return array - array(array('prefix' => $, 'file' => $))
1785 public static function getModuleExtensions($fresh = FALSE) {
1786 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);