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 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 $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 = CRM_Utils_Array
::value($fieldName, $fieldKeys);
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 // Skip default condition in validate context
307 'condition' => $context == 'validate' ?
array() : CRM_Utils_Array
::value('condition', $pseudoconstant, array()),
308 'keyColumn' => CRM_Utils_Array
::value('keyColumn', $pseudoconstant),
309 'labelColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoconstant),
312 // Fetch option group from option_value table
313 if(!empty($pseudoconstant['optionGroupName'])) {
314 if ($context == 'validate') {
315 $params['labelColumn'] = 'name';
317 // Call our generic fn for retrieving from the option_value table
318 return CRM_Core_OptionGroup
::values(
319 $pseudoconstant['optionGroupName'],
323 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
324 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
325 $params['onlyActive'],
327 $params['keyColumn'] ?
$params['keyColumn'] : 'value'
331 // Fetch options from other tables
332 if (!empty($pseudoconstant['table'])) {
333 // Normalize params so the serialized cache string will be consistent.
334 CRM_Utils_Array
::remove($params, 'flip', 'fresh');
336 $cacheKey = $daoName . $fieldName . serialize($params);
338 // Retrieve cached options
339 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
340 $output = self
::$cache[$cacheKey];
343 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
344 if (!class_exists($daoName)) {
347 // Get list of fields for the option table
349 $availableFields = array_keys($dao->fieldKeys());
352 $select = "SELECT %1 AS id, %2 AS label";
355 $order = "ORDER BY %2";
357 // Use machine name instead of label in validate context
358 if ($context == 'validate') {
359 if (!empty($pseudoconstant['nameColumn'])) {
360 $params['labelColumn'] = $pseudoconstant['nameColumn'];
362 elseif (in_array('name', $availableFields)) {
363 $params['labelColumn'] = 'name';
366 // Condition param can be passed as an sql clause string or an array of clauses
367 if (!empty($params['condition'])) {
368 $wheres[] = implode(' AND ', (array) $params['condition']);
370 // onlyActive param will automatically filter on common flags
371 if (!empty($params['onlyActive'])) {
372 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0) as $flag => $val) {
373 if (in_array($flag, $availableFields)) {
374 $wheres[] = "$flag = $val";
378 // Filter domain specific options
379 if (in_array('domain_id', $availableFields)) {
380 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
382 $queryParams = array(
383 1 => array($params['keyColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
384 2 => array($params['labelColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
385 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
387 // Add orderColumn param
388 if (!empty($params['orderColumn'])) {
389 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
);
390 $order = "ORDER BY %4";
392 // Support no sorting if $params[orderColumn] is FALSE
393 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
396 // Default to 'weight' if that column exists
397 elseif (in_array('weight', $availableFields)) {
398 $order = "ORDER BY weight";
402 $query = "$select $from";
404 $query .= " WHERE " . implode($wheres, ' AND ');
406 $query .= ' ' . $order;
407 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
408 while ($dao->fetch()) {
409 $output[$dao->id
] = $dao->label
;
413 if (!empty($params['localize']) ||
$pseudoconstant['table'] == 'civicrm_country' ||
$pseudoconstant['table'] == 'civicrm_state_province') {
414 $I18nParams = array();
415 if ($pseudoconstant['table'] == 'civicrm_country') {
416 $I18nParams['context'] = 'country';
418 if ($pseudoconstant['table'] == 'civicrm_state_province') {
419 $I18nParams['context'] = 'province';
421 $i18n = CRM_Core_I18n
::singleton();
422 $i18n->localizeArray($output, $I18nParams);
423 // Maintain sort by label
424 if ($order == "ORDER BY %2") {
425 CRM_Utils_Array
::asort($output);
428 self
::$cache[$cacheKey] = $output;
430 return $flip ?
array_flip($output) : $output;
434 // Return "Yes" and "No" for boolean fields
435 elseif (CRM_Utils_Array
::value('type', $fieldSpec) === CRM_Utils_Type
::T_BOOLEAN
) {
436 $output = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
437 return $flip ?
array_flip($output) : $output;
439 // If we're still here, it's an error. Return FALSE.
444 * Fetch the translated label for a field given its key
446 * @param String $baoName
447 * @param String $fieldName
448 * @param String|Int $key
450 * TODO: Accept multivalued input?
452 * @return bool|null|string
453 * FALSE if the given field has no associated option list
454 * NULL if the given key has no corresponding option
455 * String if label is found
457 static function getLabel($baoName, $fieldName, $key) {
458 $values = $baoName::buildOptions($fieldName, 'get');
459 if ($values === FALSE) {
462 return CRM_Utils_Array
::value($key, $values);
466 * Fetch the machine name for a field given its key
468 * @param String $baoName
469 * @param String $fieldName
470 * @param String|Int $key
472 * @return bool|null|string
473 * FALSE if the given field has no associated option list
474 * NULL if the given key has no corresponding option
475 * String if label is found
477 static function getName($baoName, $fieldName, $key) {
478 $values = $baoName::buildOptions($fieldName, 'validate');
479 if ($values === FALSE) {
482 return CRM_Utils_Array
::value($key, $values);
486 * Fetch the key for a field option given its name
488 * @param String $baoName
489 * @param String $fieldName
490 * @param String|Int $value
492 * @return bool|null|string|number
493 * FALSE if the given field has no associated option list
494 * NULL if the given key has no corresponding option
495 * String|Number if key is found
497 static function getKey($baoName, $fieldName, $value) {
498 $values = $baoName::buildOptions($fieldName, 'validate');
499 if ($values === FALSE) {
502 return CRM_Utils_Array
::key($value, $values);
506 * DEPRECATED generic populate method
507 * All pseudoconstant functions that use this method are also deprecated.
509 * The static array $var is populated from the db
510 * using the <b>$name DAO</b>.
512 * Note: any database errors will be trapped by the DAO.
514 * @param array $var the associative array we will fill
515 * @param string $name the name of the DAO
516 * @param boolean $all get all objects. default is to get only active ones.
517 * @param string $retrieve the field that we are interested in (normally name, differs in some objects)
518 * @param string $filter the field that we want to filter the result set with
519 * @param string $condition the condition that gets passed to the final query as the WHERE clause
525 public static function populate(
530 $filter = 'is_active',
536 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
537 $cache = CRM_Utils_Cache
::singleton();
538 $var = $cache->get($cacheKey);
539 if ($var && empty($force)) {
543 $object = new $name ( );
545 $object->selectAdd();
546 $object->selectAdd("$key, $retrieve");
548 $object->whereAdd($condition);
552 $object->orderBy($retrieve);
555 $object->orderBy($orderby);
559 $object->$filter = 1;
564 while ($object->fetch()) {
565 $var[$object->$key] = $object->$retrieve;
568 $cache->set($cacheKey, $var);
572 * Flush given pseudoconstant so it can be reread from db
573 * nex time it's requested.
578 * @param boolean $name pseudoconstant to be flushed
581 public static function flush($name = 'cache') {
582 if (isset(self
::$
$name)) {
585 if ($name == 'cache') {
586 CRM_Core_OptionGroup
::flushAll();
591 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
593 * Get all Activty types.
595 * The static array activityType is returned
597 * @param boolean $all - get All Activity types - default is to get only active ones.
602 * @return array - array reference of all activity types.
604 public static function &activityType() {
605 $args = func_get_args();
606 $all = CRM_Utils_Array
::value(0, $args, TRUE);
607 $includeCaseActivities = CRM_Utils_Array
::value(1, $args, FALSE);
608 $reset = CRM_Utils_Array
::value(2, $args, FALSE);
609 $returnColumn = CRM_Utils_Array
::value(3, $args, 'label');
610 $includeCampaignActivities = CRM_Utils_Array
::value(4, $args, FALSE);
611 $onlyComponentActivities = CRM_Utils_Array
::value(5, $args, FALSE);
612 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
613 $index .= '_' . (int) $includeCampaignActivities;
614 $index .= '_' . (int) $onlyComponentActivities;
616 if (NULL === self
::$activityType) {
617 self
::$activityType = array();
620 if (!isset(self
::$activityType[$index]) ||
$reset) {
623 $condition = 'AND filter = 0';
625 $componentClause = " v.component_id IS NULL";
626 if ($onlyComponentActivities) {
627 $componentClause = " v.component_id IS NOT NULL";
630 $componentIds = array();
631 $compInfo = CRM_Core_Component
::getEnabledComponents();
633 // build filter for listing activity types only if their
634 // respective components are enabled
635 foreach ($compInfo as $compName => $compObj) {
636 if ($compName == 'CiviCase') {
637 if ($includeCaseActivities) {
638 $componentIds[] = $compObj->componentID
;
641 elseif ($compName == 'CiviCampaign') {
642 if ($includeCampaignActivities) {
643 $componentIds[] = $compObj->componentID
;
647 $componentIds[] = $compObj->componentID
;
651 if (count($componentIds)) {
652 $componentIds = implode(',', $componentIds);
653 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
654 if ($onlyComponentActivities) {
655 $componentClause = " ( v.component_id IN ($componentIds ) )";
658 $condition = $condition . ' AND ' . $componentClause;
660 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
662 return self
::$activityType[$index];
666 * Get all the State/Province from database.
668 * The static array stateProvince is returned, and if it's
669 * called the first time, the <b>State Province DAO</b> is used
670 * to get all the States.
672 * Note: any database errors will be trapped by the DAO.
677 * @param int $id - Optional id to return
679 * @return array - array reference of all State/Provinces.
682 public static function &stateProvince($id = FALSE, $limit = TRUE) {
683 if (($id && !CRM_Utils_Array
::value($id, self
::$stateProvince)) ||
!self
::$stateProvince ||
!$id) {
684 $whereClause = FALSE;
685 $config = CRM_Core_Config
::singleton();
687 $countryIsoCodes = self
::countryIsoCode();
688 $limitCodes = $config->provinceLimit();
690 foreach ($limitCodes as $code) {
691 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
693 if (!empty($limitIds)) {
694 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
697 $whereClause = FALSE;
700 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
702 // localise the province names if in an non-en_US locale
704 if ($tsLocale != '' and $tsLocale != 'en_US') {
705 $i18n = CRM_Core_I18n
::singleton();
706 $i18n->localizeArray(self
::$stateProvince, array(
707 'context' => 'province',
709 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
713 if (array_key_exists($id, self
::$stateProvince)) {
714 return self
::$stateProvince[$id];
721 return self
::$stateProvince;
725 * Get all the State/Province abbreviations from the database.
727 * Same as above, except gets the abbreviations instead of the names.
732 * @param int $id - Optional id to return
734 * @return array - array reference of all State/Province abbreviations.
736 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
740 FROM civicrm_state_province
748 return CRM_Core_DAO
::singleValueQuery($query, $params);
751 if (!self
::$stateProvinceAbbreviation ||
!$id) {
753 $whereClause = FALSE;
756 $config = CRM_Core_Config
::singleton();
757 $countryIsoCodes = self
::countryIsoCode();
758 $limitCodes = $config->provinceLimit();
760 foreach ($limitCodes as $code) {
761 $tmpArray = array_keys($countryIsoCodes, $code);
763 if (!empty($tmpArray)) {
764 $limitIds[] = array_shift($tmpArray);
767 if (!empty($limitIds)) {
768 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
771 self
::populate(self
::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
775 if (array_key_exists($id, self
::$stateProvinceAbbreviation)) {
776 return self
::$stateProvinceAbbreviation[$id];
783 return self
::$stateProvinceAbbreviation;
787 * Get all the countries from database.
789 * The static array country is returned, and if it's
790 * called the first time, the <b>Country DAO</b> is used
791 * to get all the countries.
793 * Note: any database errors will be trapped by the DAO.
798 * @param int $id - Optional id to return
800 * @return array - array reference of all countries.
803 public static function country($id = FALSE, $applyLimit = TRUE) {
804 if (($id && !CRM_Utils_Array
::value($id, self
::$country)) ||
!self
::$country ||
!$id) {
806 $config = CRM_Core_Config
::singleton();
807 $limitCodes = array();
810 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
811 // (ensuring it's a subset of the legal values)
812 // K/P: We need to fix this, i dont think it works with new setting files
813 $limitCodes = $config->countryLimit();
814 if (!is_array($limitCodes)) {
816 $config->countryLimit
=> 1,
820 $limitCodes = array_intersect(self
::countryIsoCode(), $limitCodes);
823 if (count($limitCodes)) {
824 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
830 self
::populate(self
::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
832 // if default country is set, percolate it to the top
833 if ($config->defaultContactCountry()) {
834 $countryIsoCodes = self
::countryIsoCode();
835 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
836 if ($defaultID !== FALSE) {
837 $default[$defaultID] = CRM_Utils_Array
::value($defaultID, self
::$country);
838 self
::$country = $default + self
::$country;
842 // localise the country names if in an non-en_US locale
844 if ($tsLocale != '' and $tsLocale != 'en_US') {
845 $i18n = CRM_Core_I18n
::singleton();
846 $i18n->localizeArray(self
::$country, array(
847 'context' => 'country',
849 self
::$country = CRM_Utils_Array
::asort(self
::$country);
853 if (array_key_exists($id, self
::$country)) {
854 return self
::$country[$id];
857 return CRM_Core_DAO
::$_nullObject;
860 return self
::$country;
864 * Get all the country ISO Code abbreviations from the database.
866 * The static array countryIsoCode is returned, and if it's
867 * called the first time, the <b>Country DAO</b> is used
868 * to get all the countries' ISO codes.
870 * Note: any database errors will be trapped by the DAO.
875 * @return array - array reference of all country ISO codes.
878 public static function &countryIsoCode($id = FALSE) {
879 if (!self
::$countryIsoCode) {
880 self
::populate(self
::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
883 if (array_key_exists($id, self
::$countryIsoCode)) {
884 return self
::$countryIsoCode[$id];
887 return CRM_Core_DAO
::$_nullObject;
890 return self
::$countryIsoCode;
894 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
896 * Get all groups from database
898 * The static array group is returned, and if it's
899 * called the first time, the <b>Group DAO</b> is used
900 * to get all the groups.
902 * Note: any database errors will be trapped by the DAO.
904 * @param string $groupType type of group(Access/Mailing)
905 * @param boolen $excludeHidden exclude hidden groups.
910 * @return array - array reference of all groups.
913 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
914 $condition = CRM_Contact_BAO_Group
::groupTypeCondition($groupType, $excludeHidden);
917 self
::$group = array();
920 $groupKey = $groupType ?
$groupType : 'null';
922 if (!isset(self
::$group[$groupKey])) {
923 self
::$group[$groupKey] = NULL;
924 self
::populate(self
::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
926 return self
::$group[$groupKey];
930 * Create or get groups iterator (iterates over nested groups in a
933 * The GroupNesting instance is returned; it's created if this is being
934 * called for the first time
940 * @return mixed - instance of CRM_Contact_BAO_GroupNesting
943 public static function &groupIterator($styledLabels = FALSE) {
944 if (!self
::$groupIterator) {
946 When used as an object, GroupNesting implements Iterator
947 and iterates nested groups in a logical manner for us
949 self
::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
951 return self
::$groupIterator;
955 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
957 * Get all permissioned groups from database
959 * The static array group is returned, and if it's
960 * called the first time, the <b>Group DAO</b> is used
961 * to get all the groups.
963 * Note: any database errors will be trapped by the DAO.
965 * @param string $groupType type of group(Access/Mailing)
966 * @param boolen $excludeHidden exclude hidden groups.
971 * @return array - array reference of all groups.
974 public static function group($groupType = NULL, $excludeHidden = TRUE) {
975 return CRM_Core_Permission
::group($groupType, $excludeHidden);
979 * Get all permissioned groups from database
981 * The static array group is returned, and if it's
982 * called the first time, the <b>Group DAO</b> is used
983 * to get all the groups.
985 * Note: any database errors will be trapped by the DAO.
990 * @return array - array reference of all groups.
993 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
994 if (!self
::$staticGroup) {
995 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
997 $condition .= " AND visibility != 'User and User Admin Only'";
1001 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1004 if ($excludeHidden) {
1005 $condition .= ' AND is_hidden != 1 ';
1008 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1011 return self
::$staticGroup;
1015 * Get all Relationship Types from database.
1017 * The static array group is returned, and if it's
1018 * called the first time, the <b>RelationshipType DAO</b> is used
1019 * to get all the relationship types.
1021 * Note: any database errors will be trapped by the DAO.
1023 * @param string $valueColumnName db column name/label.
1024 * @param boolean $reset reset relationship types if true
1029 * @return array - array reference of all relationship types.
1031 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1032 if (!CRM_Utils_Array
::value($valueColumnName, self
::$relationshipType) ||
$reset) {
1033 self
::$relationshipType[$valueColumnName] = array();
1035 //now we have name/label columns CRM-3336
1036 $column_a_b = "{$valueColumnName}_a_b";
1037 $column_b_a = "{$valueColumnName}_b_a";
1039 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1040 $relationshipTypeDAO->selectAdd();
1041 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1042 $relationshipTypeDAO->is_active
= 1;
1043 $relationshipTypeDAO->find();
1044 while ($relationshipTypeDAO->fetch()) {
1046 self
::$relationshipType[$valueColumnName][$relationshipTypeDAO->id
] = array(
1047 'id' => $relationshipTypeDAO->id
,
1048 $column_a_b => $relationshipTypeDAO->$column_a_b,
1049 $column_b_a => $relationshipTypeDAO->$column_b_a,
1050 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1051 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1052 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1053 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1058 return self
::$relationshipType[$valueColumnName];
1062 * get all the ISO 4217 currency codes
1064 * so far, we use this for validation only, so there's no point of putting this into the database
1068 * @return array - array reference of all currency codes
1071 public static function ¤cyCode() {
1072 if (!self
::$currencyCode) {
1073 self
::$currencyCode = array(
1343 return self
::$currencyCode;
1347 * Get all the County from database.
1349 * The static array county is returned, and if it's
1350 * called the first time, the <b>County DAO</b> is used
1351 * to get all the Counties.
1353 * Note: any database errors will be trapped by the DAO.
1358 * @param int $id - Optional id to return
1360 * @return array - array reference of all Counties
1363 public static function &county($id = FALSE) {
1364 if (!self
::$county) {
1366 $config = CRM_Core_Config
::singleton();
1367 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1368 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1371 if (array_key_exists($id, self
::$county)) {
1372 return self
::$county[$id];
1375 return CRM_Core_DAO
::$_nullObject;
1378 return self
::$county;
1382 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1383 * Get all active payment processors
1385 * The static array paymentProcessor is returned
1390 * @param boolean $all - get payment processors - default is to get only active ones.
1391 * @param boolean $test - get test payment processors
1393 * @return array - array of all payment processors
1396 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1397 $condition = "is_test = ";
1398 $condition .= ($test) ?
'1' : '0';
1400 if ($additionalCond) {
1401 $condition .= " AND ( $additionalCond ) ";
1404 // CRM-7178. Make sure we only include payment processors valid in ths
1406 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1408 $cacheKey = $condition . '_' . (int) $all;
1409 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1410 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1413 return self
::$paymentProcessor[$cacheKey];
1417 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1419 * The static array paymentProcessorType is returned
1424 * @param boolean $all - get payment processors - default is to get only active ones.
1426 * @return array - array of all payment processor types
1429 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1430 $cacheKey = $id . '_' .$return;
1431 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1432 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1434 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1435 return self
::$paymentProcessorType[$cacheKey][$id];
1437 return self
::$paymentProcessorType[$cacheKey];
1441 * Get all the World Regions from Database
1445 * @return array - array reference of all World Regions
1448 public static function &worldRegion($id = FALSE) {
1449 if (!self
::$worldRegions) {
1450 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1454 if (array_key_exists($id, self
::$worldRegions)) {
1455 return self
::$worldRegions[$id];
1458 return CRM_Core_DAO
::$_nullObject;
1462 return self
::$worldRegions;
1466 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1468 * Get all Activity Statuses.
1470 * The static array activityStatus is returned
1475 * @return array - array reference of all activity statuses
1477 public static function &activityStatus($column = 'label') {
1478 if (NULL === self
::$activityStatus) {
1479 self
::$activityStatus = array();
1481 if (!array_key_exists($column, self
::$activityStatus)) {
1482 self
::$activityStatus[$column] = array();
1484 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1487 return self
::$activityStatus[$column];
1491 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1493 * Get all Visibility levels.
1495 * The static array visibility is returned
1500 * @return array - array reference of all Visibility levels.
1503 public static function &visibility($column = 'label') {
1504 if (!isset(self
::$visibility)) {
1505 self
::$visibility = array( );
1508 if (!isset(self
::$visibility[$column])) {
1509 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1512 return self
::$visibility[$column];
1515 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1516 static $_cache = NULL;
1518 $cacheKey = "{$countryID}_{$field}";
1523 if (!empty($_cache[$cacheKey])) {
1524 return $_cache[$cacheKey];
1528 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1529 FROM civicrm_state_province
1530 WHERE country_id = %1
1539 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1542 while ($dao->fetch()) {
1543 $result[$dao->id
] = $dao->name
;
1546 // localise the stateProvince names if in an non-en_US locale
1547 $config = CRM_Core_Config
::singleton();
1549 if ($tsLocale != '' and $tsLocale != 'en_US') {
1550 $i18n = CRM_Core_I18n
::singleton();
1551 $i18n->localizeArray($result, array(
1552 'context' => 'province',
1554 $result = CRM_Utils_Array
::asort($result);
1557 $_cache[$cacheKey] = $result;
1559 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1564 public static function &countyForState($stateID) {
1565 if (is_array($stateID)) {
1566 $states = implode(", ", $stateID);
1568 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1570 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1571 WHERE civicrm_county.state_province_id in ( $states )
1572 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1574 $dao = CRM_Core_DAO
::executeQuery($query);
1577 while ($dao->fetch()) {
1578 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1583 static $_cache = NULL;
1585 $cacheKey = "{$stateID}_name";
1590 if (!empty($_cache[$cacheKey])) {
1591 return $_cache[$cacheKey];
1595 SELECT civicrm_county.name name, civicrm_county.id id
1597 WHERE state_province_id = %1
1606 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1609 while ($dao->fetch()) {
1610 $result[$dao->id
] = $dao->name
;
1618 * Given a state ID return the country ID, this allows
1619 * us to populate forms and values for downstream code
1621 * @param $stateID int
1623 * @return int the country id that the state belongs to
1627 static function countryIDForStateID($stateID) {
1628 if (empty($stateID)) {
1629 return CRM_Core_DAO
::$_nullObject;
1634 FROM civicrm_state_province
1637 $params = array(1 => array($stateID, 'Integer'));
1639 return CRM_Core_DAO
::singleValueQuery($query, $params);
1643 * Get all types of Greetings.
1645 * The static array of greeting is returned
1650 * @param $filter - get All Email Greetings - default is to get only active ones.
1652 * @return array - array reference of all greetings.
1655 public static function greeting($filter, $columnName = 'label') {
1656 $index = $filter['greeting_type'] . '_' . $columnName;
1658 // also add contactType to the array
1659 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1661 $index .= '_' . $contactType;
1664 if (NULL === self
::$greeting) {
1665 self
::$greeting = array();
1668 if (!CRM_Utils_Array
::value($index, self
::$greeting)) {
1669 $filterCondition = NULL;
1671 $filterVal = 'v.filter =';
1672 switch ($contactType) {
1681 case 'Organization':
1685 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1688 self
::$greeting[$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1691 return self
::$greeting[$index];
1695 * Construct array of default greeting values for contact type
1700 * @return array - array reference of default greetings.
1703 public static function &greetingDefaults() {
1704 if (!self
::$greetingDefaults) {
1705 $defaultGreetings = array();
1706 $contactTypes = self
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
1708 foreach ($contactTypes as $filter => $contactType) {
1709 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1711 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1712 $tokenVal = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1713 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1717 self
::$greetingDefaults = $defaultGreetings;
1720 return self
::$greetingDefaults;
1724 * Get all extensions
1726 * The static array extensions
1728 * FIXME: This is called by civix but not by any core code. We
1729 * should provide an API call which civix can use instead.
1734 * @return array - array($fullyQualifiedName => $label) list of extensions
1736 public static function &getExtensions() {
1737 if (!self
::$extensions) {
1738 self
::$extensions = array();
1740 SELECT full_name, label
1741 FROM civicrm_extension
1744 $dao = CRM_Core_DAO
::executeQuery($sql);
1745 while ($dao->fetch()) {
1746 self
::$extensions[$dao->full_name
] = $dao->label
;
1750 return self
::$extensions;
1754 * Get all options values
1756 * The static array option values is returned
1761 * @param boolean $optionGroupName - get All Option Group values- default is to get only active ones.
1763 * @return array - array reference of all Option Group Name
1766 public static function accountOptionValues($optionGroupName, $id = null, $condition = null) {
1767 $cacheKey = $optionGroupName . '_' . $condition;
1768 if (empty(self
::$accountOptionValues[$cacheKey])) {
1769 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, false, false, false, $condition);
1772 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1775 return self
::$accountOptionValues[$cacheKey];
1779 * Fetch the list of active extensions of type 'module'
1781 * @param $fresh bool whether to forcibly reload extensions list from canonical store
1785 * @return array - array(array('prefix' => $, 'file' => $))
1787 public static function getModuleExtensions($fresh = FALSE) {
1788 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);