3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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|bool - 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 == 'get') ?
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 $fieldSpec = $dao->getFieldSpec($fieldName);
281 // If neither worked then this field doesn't exist. Return false.
282 if (empty($fieldSpec)) {
286 elseif (!empty($fieldSpec['pseudoconstant'])) {
287 $pseudoconstant = $fieldSpec['pseudoconstant'];
288 // Merge params with schema defaults
290 'condition' => CRM_Utils_Array
::value('condition', $pseudoconstant, array()),
291 'keyColumn' => CRM_Utils_Array
::value('keyColumn', $pseudoconstant),
292 'labelColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoconstant),
295 // Fetch option group from option_value table
296 if(!empty($pseudoconstant['optionGroupName'])) {
297 if ($context == 'validate') {
298 $params['labelColumn'] = 'name';
300 // Call our generic fn for retrieving from the option_value table
301 return CRM_Core_OptionGroup
::values(
302 $pseudoconstant['optionGroupName'],
306 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
307 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
308 $params['onlyActive'],
310 $params['keyColumn'] ?
$params['keyColumn'] : 'value'
314 // Fetch options from other tables
315 if (!empty($pseudoconstant['table'])) {
316 // Normalize params so the serialized cache string will be consistent.
317 CRM_Utils_Array
::remove($params, 'flip', 'fresh');
319 $cacheKey = $daoName . $fieldName . serialize($params);
321 // Retrieve cached options
322 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
323 $output = self
::$cache[$cacheKey];
326 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
327 if (!class_exists($daoName)) {
330 // Get list of fields for the option table
332 $availableFields = array_keys($dao->fieldKeys());
335 $select = "SELECT %1 AS id, %2 AS label";
338 $order = "ORDER BY %2";
340 // Use machine name instead of label in validate context
341 if ($context == 'validate') {
342 if (!empty($pseudoconstant['nameColumn'])) {
343 $params['labelColumn'] = $pseudoconstant['nameColumn'];
345 elseif (in_array('name', $availableFields)) {
346 $params['labelColumn'] = 'name';
349 // Condition param can be passed as an sql clause string or an array of clauses
350 if (!empty($params['condition'])) {
351 $wheres[] = implode(' AND ', (array) $params['condition']);
353 // onlyActive param will automatically filter on common flags
354 if (!empty($params['onlyActive'])) {
355 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0) as $flag => $val) {
356 if (in_array($flag, $availableFields)) {
357 $wheres[] = "$flag = $val";
361 // Filter domain specific options
362 if (in_array('domain_id', $availableFields)) {
363 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
365 $queryParams = array(
366 1 => array($params['keyColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
367 2 => array($params['labelColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
368 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
370 // Add orderColumn param
371 if (!empty($params['orderColumn'])) {
372 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
);
373 $order = "ORDER BY %4";
375 // Support no sorting if $params[orderColumn] is FALSE
376 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
379 // Default to 'weight' if that column exists
380 elseif (in_array('weight', $availableFields)) {
381 $order = "ORDER BY weight";
385 $query = "$select $from";
387 $query .= " WHERE " . implode($wheres, ' AND ');
389 $query .= ' ' . $order;
390 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
391 while ($dao->fetch()) {
392 $output[$dao->id
] = $dao->label
;
396 if (!empty($params['localize']) ||
$pseudoconstant['table'] == 'civicrm_country' ||
$pseudoconstant['table'] == 'civicrm_state_province') {
397 $I18nParams = array();
398 if ($pseudoconstant['table'] == 'civicrm_country') {
399 $I18nParams['context'] = 'country';
401 if ($pseudoconstant['table'] == 'civicrm_state_province') {
402 $I18nParams['context'] = 'province';
404 $i18n = CRM_Core_I18n
::singleton();
405 $i18n->localizeArray($output, $I18nParams);
406 // Maintain sort by label
407 if ($order == "ORDER BY %2") {
408 CRM_Utils_Array
::asort($output);
411 self
::$cache[$cacheKey] = $output;
413 return $flip ?
array_flip($output) : $output;
417 // Return "Yes" and "No" for boolean fields
418 elseif (CRM_Utils_Array
::value('type', $fieldSpec) === CRM_Utils_Type
::T_BOOLEAN
) {
419 $output = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
420 return $flip ?
array_flip($output) : $output;
422 // If we're still here, it's an error. Return FALSE.
427 * Fetch the translated label for a field given its key
429 * @param String $baoName
430 * @param String $fieldName
431 * @param String|Int $key
433 * TODO: Accept multivalued input?
435 * @return bool|null|string
436 * FALSE if the given field has no associated option list
437 * NULL if the given key has no corresponding option
438 * String if label is found
440 static function getLabel($baoName, $fieldName, $key) {
441 $values = $baoName::buildOptions($fieldName, 'get');
442 if ($values === FALSE) {
445 return CRM_Utils_Array
::value($key, $values);
449 * Fetch the machine name for a field given its key
451 * @param String $baoName
452 * @param String $fieldName
453 * @param String|Int $key
455 * @return bool|null|string
456 * FALSE if the given field has no associated option list
457 * NULL if the given key has no corresponding option
458 * String if label is found
460 static function getName($baoName, $fieldName, $key) {
461 $values = $baoName::buildOptions($fieldName, 'validate');
462 if ($values === FALSE) {
465 return CRM_Utils_Array
::value($key, $values);
469 * Fetch the key for a field option given its name
471 * @param String $baoName
472 * @param String $fieldName
473 * @param String|Int $value
475 * @return bool|null|string|number
476 * FALSE if the given field has no associated option list
477 * NULL if the given key has no corresponding option
478 * String|Number if key is found
480 static function getKey($baoName, $fieldName, $value) {
481 $values = $baoName::buildOptions($fieldName, 'validate');
482 if ($values === FALSE) {
485 return CRM_Utils_Array
::key($value, $values);
489 * Lookup the admin page at which a field's option list can be edited
491 * @return string|null
493 static function getOptionEditUrl($fieldSpec) {
494 // If it's an option group, that's easy
495 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
496 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
498 // For everything else...
499 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
500 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($fieldSpec['pseudoconstant']['table']);
504 // We don't have good mapping so have to do a bit of guesswork from the menu
505 list(, , , $ent) = explode('_', $daoName);
506 $sql = "SELECT path FROM civicrm_menu
507 WHERE page_callback LIKE '%CRM_Admin_Page_$ent%'
509 return CRM_Core_Dao
::singleValueQuery($sql);
515 * DEPRECATED generic populate method
516 * All pseudoconstant functions that use this method are also deprecated.
518 * The static array $var is populated from the db
519 * using the <b>$name DAO</b>.
521 * Note: any database errors will be trapped by the DAO.
523 * @param array $var the associative array we will fill
524 * @param string $name the name of the DAO
525 * @param boolean $all get all objects. default is to get only active ones.
526 * @param string $retrieve the field that we are interested in (normally name, differs in some objects)
527 * @param string $filter the field that we want to filter the result set with
528 * @param string $condition the condition that gets passed to the final query as the WHERE clause
534 public static function populate(
539 $filter = 'is_active',
545 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
546 $cache = CRM_Utils_Cache
::singleton();
547 $var = $cache->get($cacheKey);
548 if ($var && empty($force)) {
552 $object = new $name ( );
554 $object->selectAdd();
555 $object->selectAdd("$key, $retrieve");
557 $object->whereAdd($condition);
561 $object->orderBy($retrieve);
564 $object->orderBy($orderby);
568 $object->$filter = 1;
573 while ($object->fetch()) {
574 $var[$object->$key] = $object->$retrieve;
577 $cache->set($cacheKey, $var);
581 * Flush given pseudoconstant so it can be reread from db
582 * nex time it's requested.
587 * @param boolean $name pseudoconstant to be flushed
590 public static function flush($name = 'cache') {
591 if (isset(self
::$
$name)) {
594 if ($name == 'cache') {
595 CRM_Core_OptionGroup
::flushAll();
600 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
602 * Get all Activty types.
604 * The static array activityType is returned
606 * @param boolean $all - get All Activity types - default is to get only active ones.
611 * @return array - array reference of all activity types.
613 public static function &activityType() {
614 $args = func_get_args();
615 $all = CRM_Utils_Array
::value(0, $args, TRUE);
616 $includeCaseActivities = CRM_Utils_Array
::value(1, $args, FALSE);
617 $reset = CRM_Utils_Array
::value(2, $args, FALSE);
618 $returnColumn = CRM_Utils_Array
::value(3, $args, 'label');
619 $includeCampaignActivities = CRM_Utils_Array
::value(4, $args, FALSE);
620 $onlyComponentActivities = CRM_Utils_Array
::value(5, $args, FALSE);
621 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
622 $index .= '_' . (int) $includeCampaignActivities;
623 $index .= '_' . (int) $onlyComponentActivities;
625 if (NULL === self
::$activityType) {
626 self
::$activityType = array();
629 if (!isset(self
::$activityType[$index]) ||
$reset) {
632 $condition = 'AND filter = 0';
634 $componentClause = " v.component_id IS NULL";
635 if ($onlyComponentActivities) {
636 $componentClause = " v.component_id IS NOT NULL";
639 $componentIds = array();
640 $compInfo = CRM_Core_Component
::getEnabledComponents();
642 // build filter for listing activity types only if their
643 // respective components are enabled
644 foreach ($compInfo as $compName => $compObj) {
645 if ($compName == 'CiviCase') {
646 if ($includeCaseActivities) {
647 $componentIds[] = $compObj->componentID
;
650 elseif ($compName == 'CiviCampaign') {
651 if ($includeCampaignActivities) {
652 $componentIds[] = $compObj->componentID
;
656 $componentIds[] = $compObj->componentID
;
660 if (count($componentIds)) {
661 $componentIds = implode(',', $componentIds);
662 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
663 if ($onlyComponentActivities) {
664 $componentClause = " ( v.component_id IN ($componentIds ) )";
667 $condition = $condition . ' AND ' . $componentClause;
669 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
671 return self
::$activityType[$index];
675 * Get all the State/Province from database.
677 * The static array stateProvince is returned, and if it's
678 * called the first time, the <b>State Province DAO</b> is used
679 * to get all the States.
681 * Note: any database errors will be trapped by the DAO.
686 * @param int $id - Optional id to return
688 * @return array - array reference of all State/Provinces.
691 public static function &stateProvince($id = FALSE, $limit = TRUE) {
692 if (($id && !CRM_Utils_Array
::value($id, self
::$stateProvince)) ||
!self
::$stateProvince ||
!$id) {
693 $whereClause = FALSE;
694 $config = CRM_Core_Config
::singleton();
696 $countryIsoCodes = self
::countryIsoCode();
697 $limitCodes = $config->provinceLimit();
699 foreach ($limitCodes as $code) {
700 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
702 if (!empty($limitIds)) {
703 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
706 $whereClause = FALSE;
709 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
711 // localise the province names if in an non-en_US locale
713 if ($tsLocale != '' and $tsLocale != 'en_US') {
714 $i18n = CRM_Core_I18n
::singleton();
715 $i18n->localizeArray(self
::$stateProvince, array(
716 'context' => 'province',
718 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
722 if (array_key_exists($id, self
::$stateProvince)) {
723 return self
::$stateProvince[$id];
730 return self
::$stateProvince;
734 * Get all the State/Province abbreviations from the database.
736 * Same as above, except gets the abbreviations instead of the names.
741 * @param int $id - Optional id to return
743 * @return array - array reference of all State/Province abbreviations.
745 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
749 FROM civicrm_state_province
757 return CRM_Core_DAO
::singleValueQuery($query, $params);
760 if (!self
::$stateProvinceAbbreviation ||
!$id) {
762 $whereClause = FALSE;
765 $config = CRM_Core_Config
::singleton();
766 $countryIsoCodes = self
::countryIsoCode();
767 $limitCodes = $config->provinceLimit();
769 foreach ($limitCodes as $code) {
770 $tmpArray = array_keys($countryIsoCodes, $code);
772 if (!empty($tmpArray)) {
773 $limitIds[] = array_shift($tmpArray);
776 if (!empty($limitIds)) {
777 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
780 self
::populate(self
::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
784 if (array_key_exists($id, self
::$stateProvinceAbbreviation)) {
785 return self
::$stateProvinceAbbreviation[$id];
792 return self
::$stateProvinceAbbreviation;
796 * Get all the countries from database.
798 * The static array country is returned, and if it's
799 * called the first time, the <b>Country DAO</b> is used
800 * to get all the countries.
802 * Note: any database errors will be trapped by the DAO.
807 * @param int $id - Optional id to return
809 * @return array - array reference of all countries.
812 public static function country($id = FALSE, $applyLimit = TRUE) {
813 if (($id && !CRM_Utils_Array
::value($id, self
::$country)) ||
!self
::$country ||
!$id) {
815 $config = CRM_Core_Config
::singleton();
816 $limitCodes = array();
819 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
820 // (ensuring it's a subset of the legal values)
821 // K/P: We need to fix this, i dont think it works with new setting files
822 $limitCodes = $config->countryLimit();
823 if (!is_array($limitCodes)) {
825 $config->countryLimit
=> 1,
829 $limitCodes = array_intersect(self
::countryIsoCode(), $limitCodes);
832 if (count($limitCodes)) {
833 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
839 self
::populate(self
::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
841 // if default country is set, percolate it to the top
842 if ($config->defaultContactCountry()) {
843 $countryIsoCodes = self
::countryIsoCode();
844 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
845 if ($defaultID !== FALSE) {
846 $default[$defaultID] = CRM_Utils_Array
::value($defaultID, self
::$country);
847 self
::$country = $default + self
::$country;
851 // localise the country names if in an non-en_US locale
853 if ($tsLocale != '' and $tsLocale != 'en_US') {
854 $i18n = CRM_Core_I18n
::singleton();
855 $i18n->localizeArray(self
::$country, array(
856 'context' => 'country',
858 self
::$country = CRM_Utils_Array
::asort(self
::$country);
862 if (array_key_exists($id, self
::$country)) {
863 return self
::$country[$id];
866 return CRM_Core_DAO
::$_nullObject;
869 return self
::$country;
873 * Get all the country ISO Code abbreviations from the database.
875 * The static array countryIsoCode is returned, and if it's
876 * called the first time, the <b>Country DAO</b> is used
877 * to get all the countries' ISO codes.
879 * Note: any database errors will be trapped by the DAO.
884 * @return array - array reference of all country ISO codes.
887 public static function &countryIsoCode($id = FALSE) {
888 if (!self
::$countryIsoCode) {
889 self
::populate(self
::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
892 if (array_key_exists($id, self
::$countryIsoCode)) {
893 return self
::$countryIsoCode[$id];
896 return CRM_Core_DAO
::$_nullObject;
899 return self
::$countryIsoCode;
903 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
905 * Get all groups from database
907 * The static array group is returned, and if it's
908 * called the first time, the <b>Group DAO</b> is used
909 * to get all the groups.
911 * Note: any database errors will be trapped by the DAO.
913 * @param string $groupType type of group(Access/Mailing)
914 * @param boolen $excludeHidden exclude hidden groups.
919 * @return array - array reference of all groups.
922 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
923 $condition = CRM_Contact_BAO_Group
::groupTypeCondition($groupType, $excludeHidden);
926 self
::$group = array();
929 $groupKey = $groupType ?
$groupType : 'null';
931 if (!isset(self
::$group[$groupKey])) {
932 self
::$group[$groupKey] = NULL;
933 self
::populate(self
::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
935 return self
::$group[$groupKey];
939 * Create or get groups iterator (iterates over nested groups in a
942 * The GroupNesting instance is returned; it's created if this is being
943 * called for the first time
949 * @return mixed - instance of CRM_Contact_BAO_GroupNesting
952 public static function &groupIterator($styledLabels = FALSE) {
953 if (!self
::$groupIterator) {
955 When used as an object, GroupNesting implements Iterator
956 and iterates nested groups in a logical manner for us
958 self
::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
960 return self
::$groupIterator;
964 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
966 * Get all permissioned groups from database
968 * The static array group is returned, and if it's
969 * called the first time, the <b>Group DAO</b> is used
970 * to get all the groups.
972 * Note: any database errors will be trapped by the DAO.
974 * @param string $groupType type of group(Access/Mailing)
975 * @param boolen $excludeHidden exclude hidden groups.
980 * @return array - array reference of all groups.
983 public static function group($groupType = NULL, $excludeHidden = TRUE) {
984 return CRM_Core_Permission
::group($groupType, $excludeHidden);
988 * Get all permissioned groups from database
990 * The static array group is returned, and if it's
991 * called the first time, the <b>Group DAO</b> is used
992 * to get all the groups.
994 * Note: any database errors will be trapped by the DAO.
999 * @return array - array reference of all groups.
1002 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1003 if (!self
::$staticGroup) {
1004 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1006 $condition .= " AND visibility != 'User and User Admin Only'";
1010 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1013 if ($excludeHidden) {
1014 $condition .= ' AND is_hidden != 1 ';
1017 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1020 return self
::$staticGroup;
1024 * Get all Relationship Types from database.
1026 * The static array group is returned, and if it's
1027 * called the first time, the <b>RelationshipType DAO</b> is used
1028 * to get all the relationship types.
1030 * Note: any database errors will be trapped by the DAO.
1032 * @param string $valueColumnName db column name/label.
1033 * @param boolean $reset reset relationship types if true
1038 * @return array - array reference of all relationship types.
1040 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1041 if (!CRM_Utils_Array
::value($valueColumnName, self
::$relationshipType) ||
$reset) {
1042 self
::$relationshipType[$valueColumnName] = array();
1044 //now we have name/label columns CRM-3336
1045 $column_a_b = "{$valueColumnName}_a_b";
1046 $column_b_a = "{$valueColumnName}_b_a";
1048 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1049 $relationshipTypeDAO->selectAdd();
1050 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1051 $relationshipTypeDAO->is_active
= 1;
1052 $relationshipTypeDAO->find();
1053 while ($relationshipTypeDAO->fetch()) {
1055 self
::$relationshipType[$valueColumnName][$relationshipTypeDAO->id
] = array(
1056 'id' => $relationshipTypeDAO->id
,
1057 $column_a_b => $relationshipTypeDAO->$column_a_b,
1058 $column_b_a => $relationshipTypeDAO->$column_b_a,
1059 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1060 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1061 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1062 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1067 return self
::$relationshipType[$valueColumnName];
1071 * get all the ISO 4217 currency codes
1073 * so far, we use this for validation only, so there's no point of putting this into the database
1077 * @return array - array reference of all currency codes
1080 public static function ¤cyCode() {
1081 if (!self
::$currencyCode) {
1082 self
::$currencyCode = array(
1352 return self
::$currencyCode;
1356 * Get all the County from database.
1358 * The static array county is returned, and if it's
1359 * called the first time, the <b>County DAO</b> is used
1360 * to get all the Counties.
1362 * Note: any database errors will be trapped by the DAO.
1367 * @param int $id - Optional id to return
1369 * @return array - array reference of all Counties
1372 public static function &county($id = FALSE) {
1373 if (!self
::$county) {
1375 $config = CRM_Core_Config
::singleton();
1376 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1377 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1380 if (array_key_exists($id, self
::$county)) {
1381 return self
::$county[$id];
1384 return CRM_Core_DAO
::$_nullObject;
1387 return self
::$county;
1391 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1392 * Get all active payment processors
1394 * The static array paymentProcessor is returned
1399 * @param boolean $all - get payment processors - default is to get only active ones.
1400 * @param boolean $test - get test payment processors
1402 * @return array - array of all payment processors
1405 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1406 $condition = "is_test = ";
1407 $condition .= ($test) ?
'1' : '0';
1409 if ($additionalCond) {
1410 $condition .= " AND ( $additionalCond ) ";
1413 // CRM-7178. Make sure we only include payment processors valid in ths
1415 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1417 $cacheKey = $condition . '_' . (int) $all;
1418 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1419 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1422 return self
::$paymentProcessor[$cacheKey];
1426 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1428 * The static array paymentProcessorType is returned
1433 * @param boolean $all - get payment processors - default is to get only active ones.
1435 * @return array - array of all payment processor types
1438 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1439 $cacheKey = $id . '_' .$return;
1440 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1441 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1443 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1444 return self
::$paymentProcessorType[$cacheKey][$id];
1446 return self
::$paymentProcessorType[$cacheKey];
1450 * Get all the World Regions from Database
1454 * @return array - array reference of all World Regions
1457 public static function &worldRegion($id = FALSE) {
1458 if (!self
::$worldRegions) {
1459 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1463 if (array_key_exists($id, self
::$worldRegions)) {
1464 return self
::$worldRegions[$id];
1467 return CRM_Core_DAO
::$_nullObject;
1471 return self
::$worldRegions;
1475 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1477 * Get all Activity Statuses.
1479 * The static array activityStatus is returned
1484 * @return array - array reference of all activity statuses
1486 public static function &activityStatus($column = 'label') {
1487 if (NULL === self
::$activityStatus) {
1488 self
::$activityStatus = array();
1490 if (!array_key_exists($column, self
::$activityStatus)) {
1491 self
::$activityStatus[$column] = array();
1493 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1496 return self
::$activityStatus[$column];
1500 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1502 * Get all Visibility levels.
1504 * The static array visibility is returned
1509 * @return array - array reference of all Visibility levels.
1512 public static function &visibility($column = 'label') {
1513 if (!isset(self
::$visibility)) {
1514 self
::$visibility = array( );
1517 if (!isset(self
::$visibility[$column])) {
1518 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1521 return self
::$visibility[$column];
1524 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1525 static $_cache = NULL;
1527 $cacheKey = "{$countryID}_{$field}";
1532 if (!empty($_cache[$cacheKey])) {
1533 return $_cache[$cacheKey];
1537 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1538 FROM civicrm_state_province
1539 WHERE country_id = %1
1548 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1551 while ($dao->fetch()) {
1552 $result[$dao->id
] = $dao->name
;
1555 // localise the stateProvince names if in an non-en_US locale
1556 $config = CRM_Core_Config
::singleton();
1558 if ($tsLocale != '' and $tsLocale != 'en_US') {
1559 $i18n = CRM_Core_I18n
::singleton();
1560 $i18n->localizeArray($result, array(
1561 'context' => 'province',
1563 $result = CRM_Utils_Array
::asort($result);
1566 $_cache[$cacheKey] = $result;
1568 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1573 public static function &countyForState($stateID) {
1574 if (is_array($stateID)) {
1575 $states = implode(", ", $stateID);
1577 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1579 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1580 WHERE civicrm_county.state_province_id in ( $states )
1581 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1583 $dao = CRM_Core_DAO
::executeQuery($query);
1586 while ($dao->fetch()) {
1587 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1592 static $_cache = NULL;
1594 $cacheKey = "{$stateID}_name";
1599 if (!empty($_cache[$cacheKey])) {
1600 return $_cache[$cacheKey];
1604 SELECT civicrm_county.name name, civicrm_county.id id
1606 WHERE state_province_id = %1
1615 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1618 while ($dao->fetch()) {
1619 $result[$dao->id
] = $dao->name
;
1627 * Given a state ID return the country ID, this allows
1628 * us to populate forms and values for downstream code
1630 * @param $stateID int
1632 * @return int the country id that the state belongs to
1636 static function countryIDForStateID($stateID) {
1637 if (empty($stateID)) {
1638 return CRM_Core_DAO
::$_nullObject;
1643 FROM civicrm_state_province
1646 $params = array(1 => array($stateID, 'Integer'));
1648 return CRM_Core_DAO
::singleValueQuery($query, $params);
1652 * Get all types of Greetings.
1654 * The static array of greeting is returned
1659 * @param $filter - get All Email Greetings - default is to get only active ones.
1661 * @return array - array reference of all greetings.
1664 public static function greeting($filter, $columnName = 'label') {
1665 $index = $filter['greeting_type'] . '_' . $columnName;
1667 // also add contactType to the array
1668 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1670 $index .= '_' . $contactType;
1673 if (NULL === self
::$greeting) {
1674 self
::$greeting = array();
1677 if (!CRM_Utils_Array
::value($index, self
::$greeting)) {
1678 $filterCondition = NULL;
1680 $filterVal = 'v.filter =';
1681 switch ($contactType) {
1690 case 'Organization':
1694 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1697 self
::$greeting[$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1700 return self
::$greeting[$index];
1704 * Construct array of default greeting values for contact type
1709 * @return array - array reference of default greetings.
1712 public static function &greetingDefaults() {
1713 if (!self
::$greetingDefaults) {
1714 $defaultGreetings = array();
1715 $contactTypes = self
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
1717 foreach ($contactTypes as $filter => $contactType) {
1718 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1720 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1721 $tokenVal = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1722 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1726 self
::$greetingDefaults = $defaultGreetings;
1729 return self
::$greetingDefaults;
1733 * Get all extensions
1735 * The static array extensions
1737 * FIXME: This is called by civix but not by any core code. We
1738 * should provide an API call which civix can use instead.
1743 * @return array - array($fullyQualifiedName => $label) list of extensions
1745 public static function &getExtensions() {
1746 if (!self
::$extensions) {
1747 self
::$extensions = array();
1749 SELECT full_name, label
1750 FROM civicrm_extension
1753 $dao = CRM_Core_DAO
::executeQuery($sql);
1754 while ($dao->fetch()) {
1755 self
::$extensions[$dao->full_name
] = $dao->label
;
1759 return self
::$extensions;
1763 * Get all options values
1765 * The static array option values is returned
1770 * @param boolean $optionGroupName - get All Option Group values- default is to get only active ones.
1772 * @return array - array reference of all Option Group Name
1775 public static function accountOptionValues($optionGroupName, $id = null, $condition = null) {
1776 $cacheKey = $optionGroupName . '_' . $condition;
1777 if (empty(self
::$accountOptionValues[$cacheKey])) {
1778 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, false, false, false, $condition);
1781 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1784 return self
::$accountOptionValues[$cacheKey];
1788 * Fetch the list of active extensions of type 'module'
1790 * @param $fresh bool whether to forcibly reload extensions list from canonical store
1794 * @return array - array(array('prefix' => $, 'file' => $))
1796 public static function getModuleExtensions($fresh = FALSE) {
1797 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);