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()
208 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
210 * @param String $daoName
211 * @param String $fieldName
212 * @param Array $params
213 * - name string name of the option group
214 * - flip boolean results are return in id => label format if false
215 * if true, the results are reversed
216 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
217 * - localize boolean if true, localize the results before returning
218 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
219 * - keyColumn string the column to use for 'id'
220 * - labelColumn string the column to use for 'label'
221 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
222 * - onlyActive boolean return only the action option values
223 * - fresh boolean ignore cache entries and go back to DB
224 * @param String $context: Context string
226 * @return Array|bool - array on success, FALSE on error.
230 public static function get($daoName, $fieldName, $params = array(), $context = NULL) {
231 CRM_Core_DAO
::buildOptionsContext($context);
232 $flip = !empty($params['flip']);
233 // Merge params with defaults
237 'onlyActive' => ($context == 'validate' ||
$context == 'get') ?
FALSE : TRUE,
241 // Custom fields are not in the schema
242 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
243 $customField = new CRM_Core_DAO_CustomField();
244 $customField->id
= (int) substr($fieldName, 7);
245 $customField->find(TRUE);
248 if (!empty($customField->option_group_id
)) {
249 $options = CRM_Core_OptionGroup
::valuesByID($customField->option_group_id
,
253 // Note: for custom fields the 'name' column is NULL
254 CRM_Utils_Array
::value('labelColumn', $params, 'label'),
255 $params['onlyActive'],
260 if ($customField->data_type
=== 'StateProvince') {
261 $options = self
::stateProvince();
263 elseif ($customField->data_type
=== 'Country') {
264 $options = $context == 'validate' ? self
::countryIsoCode() : self
::country();
266 elseif ($customField->data_type
=== 'Boolean') {
267 $options = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
269 $options = $options && $flip ?
array_flip($options) : $options;
271 if ($options !== FALSE) {
272 CRM_Utils_Hook
::customFieldOptions($customField->id
, $options, FALSE);
274 $customField->free();
278 // Core field: load schema
280 $fieldSpec = $dao->getFieldSpec($fieldName);
282 // If neither worked then this field doesn't exist. Return false.
283 if (empty($fieldSpec)) {
287 // If the field is an enum, explode the enum definition and return the array.
288 if (isset($fieldSpec['enumValues'])) {
289 // use of a space after the comma is inconsistent in xml
290 $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
291 $output = explode(',', $enumStr);
292 return array_combine($output, $output);
295 elseif (!empty($fieldSpec['pseudoconstant'])) {
296 $pseudoconstant = $fieldSpec['pseudoconstant'];
297 // Merge params with schema defaults
299 'condition' => CRM_Utils_Array
::value('condition', $pseudoconstant, array()),
300 'keyColumn' => CRM_Utils_Array
::value('keyColumn', $pseudoconstant),
301 'labelColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoconstant),
304 // Fetch option group from option_value table
305 if(!empty($pseudoconstant['optionGroupName'])) {
306 if ($context == 'validate') {
307 $params['labelColumn'] = 'name';
309 // Call our generic fn for retrieving from the option_value table
310 return CRM_Core_OptionGroup
::values(
311 $pseudoconstant['optionGroupName'],
315 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
316 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
317 $params['onlyActive'],
319 $params['keyColumn'] ?
$params['keyColumn'] : 'value'
323 // Fetch options from other tables
324 if (!empty($pseudoconstant['table'])) {
325 // Normalize params so the serialized cache string will be consistent.
326 CRM_Utils_Array
::remove($params, 'flip', 'fresh');
328 $cacheKey = $daoName . $fieldName . serialize($params);
330 // Retrieve cached options
331 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
332 $output = self
::$cache[$cacheKey];
335 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
336 if (!class_exists($daoName)) {
339 // Get list of fields for the option table
341 $availableFields = array_keys($dao->fieldKeys());
344 $select = "SELECT %1 AS id, %2 AS label";
347 $order = "ORDER BY %2";
349 // Use machine name instead of label in validate context
350 if ($context == 'validate') {
351 if (!empty($pseudoconstant['nameColumn'])) {
352 $params['labelColumn'] = $pseudoconstant['nameColumn'];
354 elseif (in_array('name', $availableFields)) {
355 $params['labelColumn'] = 'name';
358 // Condition param can be passed as an sql clause string or an array of clauses
359 if (!empty($params['condition'])) {
360 $wheres[] = implode(' AND ', (array) $params['condition']);
362 // onlyActive param will automatically filter on common flags
363 if (!empty($params['onlyActive'])) {
364 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0) as $flag => $val) {
365 if (in_array($flag, $availableFields)) {
366 $wheres[] = "$flag = $val";
370 // Filter domain specific options
371 if (in_array('domain_id', $availableFields)) {
372 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
374 $queryParams = array(
375 1 => array($params['keyColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
376 2 => array($params['labelColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
377 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
379 // Add orderColumn param
380 if (!empty($params['orderColumn'])) {
381 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
);
382 $order = "ORDER BY %4";
384 // Support no sorting if $params[orderColumn] is FALSE
385 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
388 // Default to 'weight' if that column exists
389 elseif (in_array('weight', $availableFields)) {
390 $order = "ORDER BY weight";
394 $query = "$select $from";
396 $query .= " WHERE " . implode($wheres, ' AND ');
398 $query .= ' ' . $order;
399 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
400 while ($dao->fetch()) {
401 $output[$dao->id
] = $dao->label
;
405 if (!empty($params['localize']) ||
$pseudoconstant['table'] == 'civicrm_country' ||
$pseudoconstant['table'] == 'civicrm_state_province') {
406 $I18nParams = array();
407 if ($pseudoconstant['table'] == 'civicrm_country') {
408 $I18nParams['context'] = 'country';
410 if ($pseudoconstant['table'] == 'civicrm_state_province') {
411 $I18nParams['context'] = 'province';
413 $i18n = CRM_Core_I18n
::singleton();
414 $i18n->localizeArray($output, $I18nParams);
415 // Maintain sort by label
416 if ($order == "ORDER BY %2") {
417 CRM_Utils_Array
::asort($output);
420 self
::$cache[$cacheKey] = $output;
422 return $flip ?
array_flip($output) : $output;
426 // Return "Yes" and "No" for boolean fields
427 elseif (CRM_Utils_Array
::value('type', $fieldSpec) === CRM_Utils_Type
::T_BOOLEAN
) {
428 $output = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
429 return $flip ?
array_flip($output) : $output;
431 // If we're still here, it's an error. Return FALSE.
436 * Fetch the translated label for a field given its key
438 * @param String $baoName
439 * @param String $fieldName
440 * @param String|Int $key
442 * TODO: Accept multivalued input?
444 * @return bool|null|string
445 * FALSE if the given field has no associated option list
446 * NULL if the given key has no corresponding option
447 * String if label is found
449 static function getLabel($baoName, $fieldName, $key) {
450 $values = $baoName::buildOptions($fieldName, 'get');
451 if ($values === FALSE) {
454 return CRM_Utils_Array
::value($key, $values);
458 * Fetch the machine name for a field given its key
460 * @param String $baoName
461 * @param String $fieldName
462 * @param String|Int $key
464 * @return bool|null|string
465 * FALSE if the given field has no associated option list
466 * NULL if the given key has no corresponding option
467 * String if label is found
469 static function getName($baoName, $fieldName, $key) {
470 $values = $baoName::buildOptions($fieldName, 'validate');
471 if ($values === FALSE) {
474 return CRM_Utils_Array
::value($key, $values);
478 * Fetch the key for a field option given its name
480 * @param String $baoName
481 * @param String $fieldName
482 * @param String|Int $value
484 * @return bool|null|string|number
485 * FALSE if the given field has no associated option list
486 * NULL if the given key has no corresponding option
487 * String|Number if key is found
489 static function getKey($baoName, $fieldName, $value) {
490 $values = $baoName::buildOptions($fieldName, 'validate');
491 if ($values === FALSE) {
494 return CRM_Utils_Array
::key($value, $values);
498 * Lookup the admin page at which a field's option list can be edited
500 * @return string|null
502 static function getOptionEditUrl($fieldSpec) {
503 // If it's an option group, that's easy
504 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
505 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
507 // For everything else...
508 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
509 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($fieldSpec['pseudoconstant']['table']);
513 // We don't have good mapping so have to do a bit of guesswork from the menu
514 list(, $parent, , $child) = explode('_', $daoName);
515 $sql = "SELECT path FROM civicrm_menu
516 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
517 ORDER BY page_callback
519 return CRM_Core_Dao
::singleValueQuery($sql);
525 * DEPRECATED generic populate method
526 * All pseudoconstant functions that use this method are also deprecated.
528 * The static array $var is populated from the db
529 * using the <b>$name DAO</b>.
531 * Note: any database errors will be trapped by the DAO.
533 * @param array $var the associative array we will fill
534 * @param string $name the name of the DAO
535 * @param boolean $all get all objects. default is to get only active ones.
536 * @param string $retrieve the field that we are interested in (normally name, differs in some objects)
537 * @param string $filter the field that we want to filter the result set with
538 * @param string $condition the condition that gets passed to the final query as the WHERE clause
544 public static function populate(
549 $filter = 'is_active',
555 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
556 $cache = CRM_Utils_Cache
::singleton();
557 $var = $cache->get($cacheKey);
558 if ($var && empty($force)) {
562 $object = new $name ( );
564 $object->selectAdd();
565 $object->selectAdd("$key, $retrieve");
567 $object->whereAdd($condition);
571 $object->orderBy($retrieve);
574 $object->orderBy($orderby);
578 $object->$filter = 1;
583 while ($object->fetch()) {
584 $var[$object->$key] = $object->$retrieve;
587 $cache->set($cacheKey, $var);
591 * Flush given pseudoconstant so it can be reread from db
592 * nex time it's requested.
597 * @param boolean $name pseudoconstant to be flushed
600 public static function flush($name = 'cache') {
601 if (isset(self
::$
$name)) {
604 if ($name == 'cache') {
605 CRM_Core_OptionGroup
::flushAll();
610 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
612 * Get all Activty types.
614 * The static array activityType is returned
616 * @param boolean $all - get All Activity types - default is to get only active ones.
621 * @return array - array reference of all activity types.
623 public static function &activityType() {
624 $args = func_get_args();
625 $all = CRM_Utils_Array
::value(0, $args, TRUE);
626 $includeCaseActivities = CRM_Utils_Array
::value(1, $args, FALSE);
627 $reset = CRM_Utils_Array
::value(2, $args, FALSE);
628 $returnColumn = CRM_Utils_Array
::value(3, $args, 'label');
629 $includeCampaignActivities = CRM_Utils_Array
::value(4, $args, FALSE);
630 $onlyComponentActivities = CRM_Utils_Array
::value(5, $args, FALSE);
631 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
632 $index .= '_' . (int) $includeCampaignActivities;
633 $index .= '_' . (int) $onlyComponentActivities;
635 if (NULL === self
::$activityType) {
636 self
::$activityType = array();
639 if (!isset(self
::$activityType[$index]) ||
$reset) {
642 $condition = 'AND filter = 0';
644 $componentClause = " v.component_id IS NULL";
645 if ($onlyComponentActivities) {
646 $componentClause = " v.component_id IS NOT NULL";
649 $componentIds = array();
650 $compInfo = CRM_Core_Component
::getEnabledComponents();
652 // build filter for listing activity types only if their
653 // respective components are enabled
654 foreach ($compInfo as $compName => $compObj) {
655 if ($compName == 'CiviCase') {
656 if ($includeCaseActivities) {
657 $componentIds[] = $compObj->componentID
;
660 elseif ($compName == 'CiviCampaign') {
661 if ($includeCampaignActivities) {
662 $componentIds[] = $compObj->componentID
;
666 $componentIds[] = $compObj->componentID
;
670 if (count($componentIds)) {
671 $componentIds = implode(',', $componentIds);
672 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
673 if ($onlyComponentActivities) {
674 $componentClause = " ( v.component_id IN ($componentIds ) )";
677 $condition = $condition . ' AND ' . $componentClause;
679 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
681 return self
::$activityType[$index];
685 * Get all the State/Province from database.
687 * The static array stateProvince is returned, and if it's
688 * called the first time, the <b>State Province DAO</b> is used
689 * to get all the States.
691 * Note: any database errors will be trapped by the DAO.
696 * @param int $id - Optional id to return
698 * @return array - array reference of all State/Provinces.
701 public static function &stateProvince($id = FALSE, $limit = TRUE) {
702 if (($id && !CRM_Utils_Array
::value($id, self
::$stateProvince)) ||
!self
::$stateProvince ||
!$id) {
703 $whereClause = FALSE;
704 $config = CRM_Core_Config
::singleton();
706 $countryIsoCodes = self
::countryIsoCode();
707 $limitCodes = $config->provinceLimit();
709 foreach ($limitCodes as $code) {
710 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
712 if (!empty($limitIds)) {
713 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
716 $whereClause = FALSE;
719 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
721 // localise the province names if in an non-en_US locale
723 if ($tsLocale != '' and $tsLocale != 'en_US') {
724 $i18n = CRM_Core_I18n
::singleton();
725 $i18n->localizeArray(self
::$stateProvince, array(
726 'context' => 'province',
728 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
732 if (array_key_exists($id, self
::$stateProvince)) {
733 return self
::$stateProvince[$id];
740 return self
::$stateProvince;
744 * Get all the State/Province abbreviations from the database.
746 * Same as above, except gets the abbreviations instead of the names.
751 * @param int $id - Optional id to return
753 * @return array - array reference of all State/Province abbreviations.
755 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
759 FROM civicrm_state_province
767 return CRM_Core_DAO
::singleValueQuery($query, $params);
770 if (!self
::$stateProvinceAbbreviation ||
!$id) {
772 $whereClause = FALSE;
775 $config = CRM_Core_Config
::singleton();
776 $countryIsoCodes = self
::countryIsoCode();
777 $limitCodes = $config->provinceLimit();
779 foreach ($limitCodes as $code) {
780 $tmpArray = array_keys($countryIsoCodes, $code);
782 if (!empty($tmpArray)) {
783 $limitIds[] = array_shift($tmpArray);
786 if (!empty($limitIds)) {
787 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
790 self
::populate(self
::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
794 if (array_key_exists($id, self
::$stateProvinceAbbreviation)) {
795 return self
::$stateProvinceAbbreviation[$id];
802 return self
::$stateProvinceAbbreviation;
806 * Get all the countries from database.
808 * The static array country is returned, and if it's
809 * called the first time, the <b>Country DAO</b> is used
810 * to get all the countries.
812 * Note: any database errors will be trapped by the DAO.
817 * @param int $id - Optional id to return
819 * @return array - array reference of all countries.
822 public static function country($id = FALSE, $applyLimit = TRUE) {
823 if (($id && !CRM_Utils_Array
::value($id, self
::$country)) ||
!self
::$country ||
!$id) {
825 $config = CRM_Core_Config
::singleton();
826 $limitCodes = array();
829 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
830 // (ensuring it's a subset of the legal values)
831 // K/P: We need to fix this, i dont think it works with new setting files
832 $limitCodes = $config->countryLimit();
833 if (!is_array($limitCodes)) {
835 $config->countryLimit
=> 1,
839 $limitCodes = array_intersect(self
::countryIsoCode(), $limitCodes);
842 if (count($limitCodes)) {
843 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
849 self
::populate(self
::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
851 // if default country is set, percolate it to the top
852 if ($config->defaultContactCountry()) {
853 $countryIsoCodes = self
::countryIsoCode();
854 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
855 if ($defaultID !== FALSE) {
856 $default[$defaultID] = CRM_Utils_Array
::value($defaultID, self
::$country);
857 self
::$country = $default + self
::$country;
861 // localise the country names if in an non-en_US locale
863 if ($tsLocale != '' and $tsLocale != 'en_US') {
864 $i18n = CRM_Core_I18n
::singleton();
865 $i18n->localizeArray(self
::$country, array(
866 'context' => 'country',
868 self
::$country = CRM_Utils_Array
::asort(self
::$country);
872 if (array_key_exists($id, self
::$country)) {
873 return self
::$country[$id];
876 return CRM_Core_DAO
::$_nullObject;
879 return self
::$country;
883 * Get all the country ISO Code abbreviations from the database.
885 * The static array countryIsoCode is returned, and if it's
886 * called the first time, the <b>Country DAO</b> is used
887 * to get all the countries' ISO codes.
889 * Note: any database errors will be trapped by the DAO.
894 * @return array - array reference of all country ISO codes.
897 public static function &countryIsoCode($id = FALSE) {
898 if (!self
::$countryIsoCode) {
899 self
::populate(self
::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
902 if (array_key_exists($id, self
::$countryIsoCode)) {
903 return self
::$countryIsoCode[$id];
906 return CRM_Core_DAO
::$_nullObject;
909 return self
::$countryIsoCode;
913 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
915 * Get all groups from database
917 * The static array group is returned, and if it's
918 * called the first time, the <b>Group DAO</b> is used
919 * to get all the groups.
921 * Note: any database errors will be trapped by the DAO.
923 * @param string $groupType type of group(Access/Mailing)
924 * @param boolen $excludeHidden exclude hidden groups.
929 * @return array - array reference of all groups.
932 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
933 $condition = CRM_Contact_BAO_Group
::groupTypeCondition($groupType, $excludeHidden);
936 self
::$group = array();
939 $groupKey = $groupType ?
$groupType : 'null';
941 if (!isset(self
::$group[$groupKey])) {
942 self
::$group[$groupKey] = NULL;
943 self
::populate(self
::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
945 return self
::$group[$groupKey];
949 * Create or get groups iterator (iterates over nested groups in a
952 * The GroupNesting instance is returned; it's created if this is being
953 * called for the first time
959 * @return mixed - instance of CRM_Contact_BAO_GroupNesting
962 public static function &groupIterator($styledLabels = FALSE) {
963 if (!self
::$groupIterator) {
965 When used as an object, GroupNesting implements Iterator
966 and iterates nested groups in a logical manner for us
968 self
::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
970 return self
::$groupIterator;
974 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
976 * Get all permissioned groups from database
978 * The static array group is returned, and if it's
979 * called the first time, the <b>Group DAO</b> is used
980 * to get all the groups.
982 * Note: any database errors will be trapped by the DAO.
984 * @param string $groupType type of group(Access/Mailing)
985 * @param boolen $excludeHidden exclude hidden groups.
990 * @return array - array reference of all groups.
993 public static function group($groupType = NULL, $excludeHidden = TRUE) {
994 return CRM_Core_Permission
::group($groupType, $excludeHidden);
998 * Get all permissioned groups from database
1000 * The static array group is returned, and if it's
1001 * called the first time, the <b>Group DAO</b> is used
1002 * to get all the groups.
1004 * Note: any database errors will be trapped by the DAO.
1009 * @return array - array reference of all groups.
1012 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1013 if (!self
::$staticGroup) {
1014 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1016 $condition .= " AND visibility != 'User and User Admin Only'";
1020 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1023 if ($excludeHidden) {
1024 $condition .= ' AND is_hidden != 1 ';
1027 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1030 return self
::$staticGroup;
1034 * Get all Relationship Types from database.
1036 * The static array group is returned, and if it's
1037 * called the first time, the <b>RelationshipType DAO</b> is used
1038 * to get all the relationship types.
1040 * Note: any database errors will be trapped by the DAO.
1042 * @param string $valueColumnName db column name/label.
1043 * @param boolean $reset reset relationship types if true
1048 * @return array - array reference of all relationship types.
1050 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1051 if (!CRM_Utils_Array
::value($valueColumnName, self
::$relationshipType) ||
$reset) {
1052 self
::$relationshipType[$valueColumnName] = array();
1054 //now we have name/label columns CRM-3336
1055 $column_a_b = "{$valueColumnName}_a_b";
1056 $column_b_a = "{$valueColumnName}_b_a";
1058 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1059 $relationshipTypeDAO->selectAdd();
1060 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1061 $relationshipTypeDAO->is_active
= 1;
1062 $relationshipTypeDAO->find();
1063 while ($relationshipTypeDAO->fetch()) {
1065 self
::$relationshipType[$valueColumnName][$relationshipTypeDAO->id
] = array(
1066 'id' => $relationshipTypeDAO->id
,
1067 $column_a_b => $relationshipTypeDAO->$column_a_b,
1068 $column_b_a => $relationshipTypeDAO->$column_b_a,
1069 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1070 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1071 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1072 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1077 return self
::$relationshipType[$valueColumnName];
1081 * get all the ISO 4217 currency codes
1083 * so far, we use this for validation only, so there's no point of putting this into the database
1087 * @return array - array reference of all currency codes
1090 public static function ¤cyCode() {
1091 if (!self
::$currencyCode) {
1092 self
::$currencyCode = array(
1362 return self
::$currencyCode;
1366 * Get all the County from database.
1368 * The static array county is returned, and if it's
1369 * called the first time, the <b>County DAO</b> is used
1370 * to get all the Counties.
1372 * Note: any database errors will be trapped by the DAO.
1377 * @param int $id - Optional id to return
1379 * @return array - array reference of all Counties
1382 public static function &county($id = FALSE) {
1383 if (!self
::$county) {
1385 $config = CRM_Core_Config
::singleton();
1386 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1387 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1390 if (array_key_exists($id, self
::$county)) {
1391 return self
::$county[$id];
1394 return CRM_Core_DAO
::$_nullObject;
1397 return self
::$county;
1401 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1402 * Get all active payment processors
1404 * The static array paymentProcessor is returned
1409 * @param boolean $all - get payment processors - default is to get only active ones.
1410 * @param boolean $test - get test payment processors
1412 * @return array - array of all payment processors
1415 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1416 $condition = "is_test = ";
1417 $condition .= ($test) ?
'1' : '0';
1419 if ($additionalCond) {
1420 $condition .= " AND ( $additionalCond ) ";
1423 // CRM-7178. Make sure we only include payment processors valid in ths
1425 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1427 $cacheKey = $condition . '_' . (int) $all;
1428 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1429 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1432 return self
::$paymentProcessor[$cacheKey];
1436 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1438 * The static array paymentProcessorType is returned
1443 * @param boolean $all - get payment processors - default is to get only active ones.
1445 * @return array - array of all payment processor types
1448 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1449 $cacheKey = $id . '_' .$return;
1450 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1451 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1453 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1454 return self
::$paymentProcessorType[$cacheKey][$id];
1456 return self
::$paymentProcessorType[$cacheKey];
1460 * Get all the World Regions from Database
1464 * @return array - array reference of all World Regions
1467 public static function &worldRegion($id = FALSE) {
1468 if (!self
::$worldRegions) {
1469 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1473 if (array_key_exists($id, self
::$worldRegions)) {
1474 return self
::$worldRegions[$id];
1477 return CRM_Core_DAO
::$_nullObject;
1481 return self
::$worldRegions;
1485 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1487 * Get all Activity Statuses.
1489 * The static array activityStatus is returned
1494 * @return array - array reference of all activity statuses
1496 public static function &activityStatus($column = 'label') {
1497 if (NULL === self
::$activityStatus) {
1498 self
::$activityStatus = array();
1500 if (!array_key_exists($column, self
::$activityStatus)) {
1501 self
::$activityStatus[$column] = array();
1503 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1506 return self
::$activityStatus[$column];
1510 * DEPRECATED. Please use the buildOptions() method in the appropriate BAO object.
1512 * Get all Visibility levels.
1514 * The static array visibility is returned
1519 * @return array - array reference of all Visibility levels.
1522 public static function &visibility($column = 'label') {
1523 if (!isset(self
::$visibility)) {
1524 self
::$visibility = array( );
1527 if (!isset(self
::$visibility[$column])) {
1528 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1531 return self
::$visibility[$column];
1534 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1535 static $_cache = NULL;
1537 $cacheKey = "{$countryID}_{$field}";
1542 if (!empty($_cache[$cacheKey])) {
1543 return $_cache[$cacheKey];
1547 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1548 FROM civicrm_state_province
1549 WHERE country_id = %1
1558 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1561 while ($dao->fetch()) {
1562 $result[$dao->id
] = $dao->name
;
1565 // localise the stateProvince names if in an non-en_US locale
1566 $config = CRM_Core_Config
::singleton();
1568 if ($tsLocale != '' and $tsLocale != 'en_US') {
1569 $i18n = CRM_Core_I18n
::singleton();
1570 $i18n->localizeArray($result, array(
1571 'context' => 'province',
1573 $result = CRM_Utils_Array
::asort($result);
1576 $_cache[$cacheKey] = $result;
1578 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1583 public static function &countyForState($stateID) {
1584 if (is_array($stateID)) {
1585 $states = implode(", ", $stateID);
1587 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1589 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1590 WHERE civicrm_county.state_province_id in ( $states )
1591 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1593 $dao = CRM_Core_DAO
::executeQuery($query);
1596 while ($dao->fetch()) {
1597 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1602 static $_cache = NULL;
1604 $cacheKey = "{$stateID}_name";
1609 if (!empty($_cache[$cacheKey])) {
1610 return $_cache[$cacheKey];
1614 SELECT civicrm_county.name name, civicrm_county.id id
1616 WHERE state_province_id = %1
1625 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1628 while ($dao->fetch()) {
1629 $result[$dao->id
] = $dao->name
;
1637 * Given a state ID return the country ID, this allows
1638 * us to populate forms and values for downstream code
1640 * @param $stateID int
1642 * @return int the country id that the state belongs to
1646 static function countryIDForStateID($stateID) {
1647 if (empty($stateID)) {
1648 return CRM_Core_DAO
::$_nullObject;
1653 FROM civicrm_state_province
1656 $params = array(1 => array($stateID, 'Integer'));
1658 return CRM_Core_DAO
::singleValueQuery($query, $params);
1662 * Get all types of Greetings.
1664 * The static array of greeting is returned
1669 * @param $filter - get All Email Greetings - default is to get only active ones.
1671 * @return array - array reference of all greetings.
1674 public static function greeting($filter, $columnName = 'label') {
1675 $index = $filter['greeting_type'] . '_' . $columnName;
1677 // also add contactType to the array
1678 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1680 $index .= '_' . $contactType;
1683 if (NULL === self
::$greeting) {
1684 self
::$greeting = array();
1687 if (!CRM_Utils_Array
::value($index, self
::$greeting)) {
1688 $filterCondition = NULL;
1690 $filterVal = 'v.filter =';
1691 switch ($contactType) {
1700 case 'Organization':
1704 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1707 self
::$greeting[$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1710 return self
::$greeting[$index];
1714 * Construct array of default greeting values for contact type
1719 * @return array - array reference of default greetings.
1722 public static function &greetingDefaults() {
1723 if (!self
::$greetingDefaults) {
1724 $defaultGreetings = array();
1725 $contactTypes = self
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
1727 foreach ($contactTypes as $filter => $contactType) {
1728 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1730 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1731 $tokenVal = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1732 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1736 self
::$greetingDefaults = $defaultGreetings;
1739 return self
::$greetingDefaults;
1743 * Get all extensions
1745 * The static array extensions
1747 * FIXME: This is called by civix but not by any core code. We
1748 * should provide an API call which civix can use instead.
1753 * @return array - array($fullyQualifiedName => $label) list of extensions
1755 public static function &getExtensions() {
1756 if (!self
::$extensions) {
1757 self
::$extensions = array();
1759 SELECT full_name, label
1760 FROM civicrm_extension
1763 $dao = CRM_Core_DAO
::executeQuery($sql);
1764 while ($dao->fetch()) {
1765 self
::$extensions[$dao->full_name
] = $dao->label
;
1769 return self
::$extensions;
1773 * Get all options values
1775 * The static array option values is returned
1780 * @param boolean $optionGroupName - get All Option Group values- default is to get only active ones.
1782 * @return array - array reference of all Option Group Name
1785 public static function accountOptionValues($optionGroupName, $id = null, $condition = null) {
1786 $cacheKey = $optionGroupName . '_' . $condition;
1787 if (empty(self
::$accountOptionValues[$cacheKey])) {
1788 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, false, false, false, $condition);
1791 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1794 return self
::$accountOptionValues[$cacheKey];
1798 * Fetch the list of active extensions of type 'module'
1800 * @param $fresh bool whether to forcibly reload extensions list from canonical store
1804 * @return array - array(array('prefix' => $, 'file' => $))
1806 public static function getModuleExtensions($fresh = FALSE) {
1807 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);