3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
50 class CRM_Core_PseudoConstant
{
53 * Static cache for pseudoconstant arrays.
56 private static $cache;
59 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
64 private static $activityType;
70 private static $stateProvince;
76 private static $county;
79 * States/provinces abbreviations
82 private static $stateProvinceAbbreviation;
88 private static $country;
94 private static $countryIsoCode;
97 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
102 private static $group;
108 private static $groupIterator;
114 private static $relationshipType;
117 * Civicrm groups that are not smart groups
120 private static $staticGroup;
126 private static $currencyCode;
132 private static $paymentProcessor;
135 * Payment processor types
138 private static $paymentProcessorType;
144 private static $worldRegions;
147 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
152 private static $activityStatus;
158 private static $visibility;
164 private static $greeting;
170 private static $greetingDefaults;
173 * Extensions of type module
176 private static $extensions;
179 * Financial Account Type
182 private static $accountOptionValues;
188 private static $taxRates;
191 * Low-level option getter, rarely accessed directly.
192 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
193 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
195 * @param string $daoName
196 * @param string $fieldName
197 * @param array $params
198 * - name string name of the option group
199 * - flip boolean results are return in id => label format if false
200 * if true, the results are reversed
201 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
202 * - localize boolean if true, localize the results before returning
203 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
204 * - keyColumn string the column to use for 'id'
205 * - labelColumn string the column to use for 'label'
206 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
207 * - onlyActive boolean return only the action option values
208 * - fresh boolean ignore cache entries and go back to DB
209 * @param string $context : Context string
212 * array on success, FALSE on error.
215 public static function get($daoName, $fieldName, $params = array(), $context = NULL) {
216 CRM_Core_DAO
::buildOptionsContext($context);
217 $flip = !empty($params['flip']);
218 // Merge params with defaults
222 'onlyActive' => ($context == 'validate' ||
$context == 'get') ?
FALSE : TRUE,
226 // Custom fields are not in the schema
227 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
228 $customField = new CRM_Core_DAO_CustomField();
229 $customField->id
= (int) substr($fieldName, 7);
230 $customField->find(TRUE);
233 if (!empty($customField->option_group_id
)) {
234 $options = CRM_Core_OptionGroup
::valuesByID($customField->option_group_id
,
238 // Note: for custom fields the 'name' column is NULL
239 CRM_Utils_Array
::value('labelColumn', $params, 'label'),
240 $params['onlyActive'],
245 if ($customField->data_type
=== 'StateProvince') {
246 $options = self
::stateProvince();
248 elseif ($customField->data_type
=== 'Country') {
249 $options = $context == 'validate' ? self
::countryIsoCode() : self
::country();
251 elseif ($customField->data_type
=== 'Boolean') {
252 $options = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
255 CRM_Utils_Hook
::customFieldOptions($customField->id
, $options, FALSE);
256 if ($options && $flip) {
257 $options = array_flip($options);
259 $customField->free();
263 // Core field: load schema
264 $dao = new $daoName();
265 $fieldSpec = $dao->getFieldSpec($fieldName);
267 // If neither worked then this field doesn't exist. Return false.
268 if (empty($fieldSpec)) {
272 elseif (!empty($fieldSpec['pseudoconstant'])) {
273 $pseudoconstant = $fieldSpec['pseudoconstant'];
275 // if callback is specified..
276 if (!empty($pseudoconstant['callback'])) {
277 return call_user_func(Civi\Core\Resolver
::singleton()->get($pseudoconstant['callback']));
280 // Merge params with schema defaults
282 'condition' => CRM_Utils_Array
::value('condition', $pseudoconstant, array()),
283 'keyColumn' => CRM_Utils_Array
::value('keyColumn', $pseudoconstant),
284 'labelColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoconstant),
287 if ($context == 'abbreviate') {
288 switch ($fieldName) {
289 case 'state_province_id':
290 $params['labelColumn'] = 'abbreviation';
294 $params['labelColumn'] = 'iso_code';
301 // Fetch option group from option_value table
302 if (!empty($pseudoconstant['optionGroupName'])) {
303 if ($context == 'validate') {
304 $params['labelColumn'] = 'name';
306 // Call our generic fn for retrieving from the option_value table
307 return CRM_Core_OptionGroup
::values(
308 $pseudoconstant['optionGroupName'],
312 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
313 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
314 $params['onlyActive'],
316 $params['keyColumn'] ?
$params['keyColumn'] : 'value'
320 // Fetch options from other tables
321 if (!empty($pseudoconstant['table'])) {
322 // Normalize params so the serialized cache string will be consistent.
323 CRM_Utils_Array
::remove($params, 'flip', 'fresh');
325 $cacheKey = $daoName . $fieldName . serialize($params);
327 // Retrieve cached options
328 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
329 $output = self
::$cache[$cacheKey];
332 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
333 if (!class_exists($daoName)) {
336 // Get list of fields for the option table
337 $dao = new $daoName();
338 $availableFields = array_keys($dao->fieldKeys());
341 $select = "SELECT %1 AS id, %2 AS label";
344 $order = "ORDER BY %2";
346 // Use machine name instead of label in validate context
347 if ($context == 'validate') {
348 if (!empty($pseudoconstant['nameColumn'])) {
349 $params['labelColumn'] = $pseudoconstant['nameColumn'];
351 elseif (in_array('name', $availableFields)) {
352 $params['labelColumn'] = 'name';
355 // Condition param can be passed as an sql clause string or an array of clauses
356 if (!empty($params['condition'])) {
357 $wheres[] = implode(' AND ', (array) $params['condition']);
359 // onlyActive param will automatically filter on common flags
360 if (!empty($params['onlyActive'])) {
361 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0) as $flag => $val) {
362 if (in_array($flag, $availableFields)) {
363 $wheres[] = "$flag = $val";
367 // Filter domain specific options
368 if (in_array('domain_id', $availableFields)) {
369 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
371 $queryParams = array(
372 1 => array($params['keyColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
373 2 => array($params['labelColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
374 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
376 // Add orderColumn param
377 if (!empty($params['orderColumn'])) {
378 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
);
379 $order = "ORDER BY %4";
381 // Support no sorting if $params[orderColumn] is FALSE
382 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
385 // Default to 'weight' if that column exists
386 elseif (in_array('weight', $availableFields)) {
387 $order = "ORDER BY weight";
391 $query = "$select $from";
393 $query .= " WHERE " . implode($wheres, ' AND ');
395 $query .= ' ' . $order;
396 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
397 while ($dao->fetch()) {
398 $output[$dao->id
] = $dao->label
;
402 if (!empty($params['localize']) ||
$pseudoconstant['table'] == 'civicrm_country' ||
$pseudoconstant['table'] == 'civicrm_state_province') {
403 $I18nParams = array();
404 if ($pseudoconstant['table'] == 'civicrm_country') {
405 $I18nParams['context'] = 'country';
407 if ($pseudoconstant['table'] == 'civicrm_state_province') {
408 $I18nParams['context'] = 'province';
410 $i18n = CRM_Core_I18n
::singleton();
411 $i18n->localizeArray($output, $I18nParams);
412 // Maintain sort by label
413 if ($order == "ORDER BY %2") {
414 CRM_Utils_Array
::asort($output);
417 self
::$cache[$cacheKey] = $output;
419 return $flip ?
array_flip($output) : $output;
423 // Return "Yes" and "No" for boolean fields
424 elseif (CRM_Utils_Array
::value('type', $fieldSpec) === CRM_Utils_Type
::T_BOOLEAN
) {
425 $output = $context == 'validate' ?
array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
426 return $flip ?
array_flip($output) : $output;
428 // If we're still here, it's an error. Return FALSE.
433 * Fetch the translated label for a field given its key.
435 * @param string $baoName
436 * @param string $fieldName
437 * @param string|Int $key
439 * TODO: Accept multivalued input?
441 * @return bool|null|string
442 * FALSE if the given field has no associated option list
443 * NULL if the given key has no corresponding option
444 * String if label is found
446 public static function getLabel($baoName, $fieldName, $key) {
447 $values = $baoName::buildOptions($fieldName, 'get');
448 if ($values === FALSE) {
451 return CRM_Utils_Array
::value($key, $values);
455 * Fetch the machine name for a field given its key.
457 * @param string $baoName
458 * @param string $fieldName
459 * @param string|Int $key
461 * @return bool|null|string
462 * FALSE if the given field has no associated option list
463 * NULL if the given key has no corresponding option
464 * String if label is found
466 public static function getName($baoName, $fieldName, $key) {
467 $values = $baoName::buildOptions($fieldName, 'validate');
468 if ($values === FALSE) {
471 return CRM_Utils_Array
::value($key, $values);
475 * Fetch the key for a field option given its name.
477 * @param string $baoName
478 * @param string $fieldName
479 * @param string|Int $value
481 * @return bool|null|string|int
482 * FALSE if the given field has no associated option list
483 * NULL if the given key has no corresponding option
484 * String|Number if key is found
486 public static function getKey($baoName, $fieldName, $value) {
487 $values = $baoName::buildOptions($fieldName, 'validate');
488 if ($values === FALSE) {
491 return CRM_Utils_Array
::key($value, $values);
495 * Lookup the admin page at which a field's option list can be edited
497 * @return string|null
499 public static function getOptionEditUrl($fieldSpec) {
500 // If it's an option group, that's easy
501 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
502 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
504 // For everything else...
505 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
506 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($fieldSpec['pseudoconstant']['table']);
510 // We don't have good mapping so have to do a bit of guesswork from the menu
511 list(, $parent, , $child) = explode('_', $daoName);
512 $sql = "SELECT path FROM civicrm_menu
513 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
514 ORDER BY page_callback
516 return CRM_Core_Dao
::singleValueQuery($sql);
522 * DEPRECATED generic populate method.
523 * All pseudoconstant functions that use this method are also @deprecated
525 * The static array $var is populated from the db
526 * using the <b>$name DAO</b>.
528 * Note: any database errors will be trapped by the DAO.
531 * The associative array we will fill.
532 * @param string $name
533 * The name of the DAO.
535 * Get all objects. default is to get only active ones.
536 * @param string $retrieve
537 * The field that we are interested in (normally name, differs in some objects).
538 * @param string $filter
539 * The field that we want to filter the result set with.
540 * @param string $condition
541 * The condition that gets passed to the final query as the WHERE clause.
543 * @param null $orderby
549 public static function populate(
554 $filter = 'is_active',
560 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
561 $cache = CRM_Utils_Cache
::singleton();
562 $var = $cache->get($cacheKey);
563 if ($var && empty($force)) {
567 $object = new $name();
569 $object->selectAdd();
570 $object->selectAdd("$key, $retrieve");
572 $object->whereAdd($condition);
576 $object->orderBy($retrieve);
579 $object->orderBy($orderby);
583 $object->$filter = 1;
588 while ($object->fetch()) {
589 $var[$object->$key] = $object->$retrieve;
592 $cache->set($cacheKey, $var);
596 * Flush given pseudoconstant so it can be reread from db.
597 * nex time it's requested.
600 * @param bool|string $name pseudoconstant to be flushed
602 public static function flush($name = 'cache') {
603 if (isset(self
::$
$name)) {
606 if ($name == 'cache') {
607 CRM_Core_OptionGroup
::flushAll();
612 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
614 * Get all Activty types.
616 * The static array activityType is returned
620 * array reference of all activity types.
622 public static function &activityType() {
623 $args = func_get_args();
624 $all = CRM_Utils_Array
::value(0, $args, TRUE);
625 $includeCaseActivities = CRM_Utils_Array
::value(1, $args, FALSE);
626 $reset = CRM_Utils_Array
::value(2, $args, FALSE);
627 $returnColumn = CRM_Utils_Array
::value(3, $args, 'label');
628 $includeCampaignActivities = CRM_Utils_Array
::value(4, $args, FALSE);
629 $onlyComponentActivities = CRM_Utils_Array
::value(5, $args, FALSE);
630 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
631 $index .= '_' . (int) $includeCampaignActivities;
632 $index .= '_' . (int) $onlyComponentActivities;
634 if (NULL === self
::$activityType) {
635 self
::$activityType = array();
638 if (!isset(self
::$activityType[$index]) ||
$reset) {
641 $condition = 'AND filter = 0';
643 $componentClause = " v.component_id IS NULL";
644 if ($onlyComponentActivities) {
645 $componentClause = " v.component_id IS NOT NULL";
648 $componentIds = array();
649 $compInfo = CRM_Core_Component
::getEnabledComponents();
651 // build filter for listing activity types only if their
652 // respective components are enabled
653 foreach ($compInfo as $compName => $compObj) {
654 if ($compName == 'CiviCase') {
655 if ($includeCaseActivities) {
656 $componentIds[] = $compObj->componentID
;
659 elseif ($compName == 'CiviCampaign') {
660 if ($includeCampaignActivities) {
661 $componentIds[] = $compObj->componentID
;
665 $componentIds[] = $compObj->componentID
;
669 if (count($componentIds)) {
670 $componentIds = implode(',', $componentIds);
671 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
672 if ($onlyComponentActivities) {
673 $componentClause = " ( v.component_id IN ($componentIds ) )";
676 $condition = $condition . ' AND ' . $componentClause;
678 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
680 return self
::$activityType[$index];
684 * Get all the State/Province from database.
686 * The static array stateProvince is returned, and if it's
687 * called the first time, the <b>State Province DAO</b> is used
688 * to get all the States.
690 * Note: any database errors will be trapped by the DAO.
693 * @param bool|int $id - Optional id to return
698 * array reference of all State/Provinces.
700 public static function &stateProvince($id = FALSE, $limit = TRUE) {
701 if (($id && !CRM_Utils_Array
::value($id, self
::$stateProvince)) ||
!self
::$stateProvince ||
!$id) {
702 $whereClause = FALSE;
703 $config = CRM_Core_Config
::singleton();
705 $countryIsoCodes = self
::countryIsoCode();
706 $limitCodes = $config->provinceLimit();
708 foreach ($limitCodes as $code) {
709 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
711 if (!empty($limitIds)) {
712 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
715 $whereClause = FALSE;
718 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
720 // localise the province names if in an non-en_US locale
722 if ($tsLocale != '' and $tsLocale != 'en_US') {
723 $i18n = CRM_Core_I18n
::singleton();
724 $i18n->localizeArray(self
::$stateProvince, array(
725 'context' => 'province',
727 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
731 if (array_key_exists($id, self
::$stateProvince)) {
732 return self
::$stateProvince[$id];
739 return self
::$stateProvince;
743 * Get all the State/Province abbreviations from the database.
745 * Same as above, except gets the abbreviations instead of the names.
748 * @param bool|int $id - Optional id to return
753 * 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.
815 * @param bool|int $id - Optional id to return
817 * @param bool $applyLimit
820 * 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.
895 * 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
924 * Type of group(Access/Mailing).
925 * @param bool $excludeHidden
926 * Exclude hidden groups.
930 * 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
957 * @param bool $styledLabels
959 * @return CRM_Contact_BAO_GroupNesting
961 public static function &groupIterator($styledLabels = FALSE) {
962 if (!self
::$groupIterator) {
963 // When used as an object, GroupNesting implements Iterator
964 // and iterates nested groups in a logical manner for us
965 self
::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
967 return self
::$groupIterator;
971 * Get all permissioned groups from database.
973 * The static array group is returned, and if it's
974 * called the first time, the <b>Group DAO</b> is used
975 * to get all the groups.
977 * Note: any database errors will be trapped by the DAO.
979 * @param string $groupType
980 * Type of group(Access/Mailing).
981 * @param bool $excludeHidden
982 * Exclude hidden groups.
986 * array reference of all groups.
988 public static function group($groupType = NULL, $excludeHidden = TRUE) {
989 return CRM_Core_Permission
::group($groupType, $excludeHidden);
993 * Fetch groups in a nested format suitable for use in select form element.
994 * @param bool $checkPermissions
995 * @param string|null $groupType
996 * @param bool $excludeHidden
999 public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
1000 $groups = $checkPermissions ? self
::group($groupType, $excludeHidden) : self
::allGroup($groupType, $excludeHidden);
1001 return CRM_Contact_BAO_Group
::getGroupsHierarchy($groups, NULL, ' ', TRUE);
1005 * Get all permissioned groups from database.
1007 * The static array group is returned, and if it's
1008 * called the first time, the <b>Group DAO</b> is used
1009 * to get all the groups.
1011 * Note: any database errors will be trapped by the DAO.
1014 * @param bool $onlyPublic
1015 * @param null $groupType
1016 * @param bool $excludeHidden
1019 * array reference of all groups.
1021 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1022 if (!self
::$staticGroup) {
1023 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1025 $condition .= " AND visibility != 'User and User Admin Only'";
1029 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1032 if ($excludeHidden) {
1033 $condition .= ' AND is_hidden != 1 ';
1036 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1039 return self
::$staticGroup;
1043 * Get all Relationship Types from database.
1045 * The static array group is returned, and if it's
1046 * called the first time, the <b>RelationshipType DAO</b> is used
1047 * to get all the relationship types.
1049 * Note: any database errors will be trapped by the DAO.
1051 * @param string $valueColumnName
1052 * Db column name/label.
1053 * @param bool $reset
1054 * Reset relationship types if true.
1058 * array reference of all relationship types.
1060 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1061 if (!CRM_Utils_Array
::value($valueColumnName, self
::$relationshipType) ||
$reset) {
1062 self
::$relationshipType[$valueColumnName] = array();
1064 //now we have name/label columns CRM-3336
1065 $column_a_b = "{$valueColumnName}_a_b";
1066 $column_b_a = "{$valueColumnName}_b_a";
1068 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1069 $relationshipTypeDAO->selectAdd();
1070 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1071 $relationshipTypeDAO->is_active
= 1;
1072 $relationshipTypeDAO->find();
1073 while ($relationshipTypeDAO->fetch()) {
1075 self
::$relationshipType[$valueColumnName][$relationshipTypeDAO->id
] = array(
1076 'id' => $relationshipTypeDAO->id
,
1077 $column_a_b => $relationshipTypeDAO->$column_a_b,
1078 $column_b_a => $relationshipTypeDAO->$column_b_a,
1079 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1080 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1081 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1082 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1087 return self
::$relationshipType[$valueColumnName];
1091 * Get all the ISO 4217 currency codes
1093 * so far, we use this for validation only, so there's no point of putting this into the database
1097 * array reference of all currency codes
1099 public static function ¤cyCode() {
1100 if (!self
::$currencyCode) {
1101 self
::$currencyCode = array(
1371 return self
::$currencyCode;
1375 * Get all the County from database.
1377 * The static array county is returned, and if it's
1378 * called the first time, the <b>County DAO</b> is used
1379 * to get all the Counties.
1381 * Note: any database errors will be trapped by the DAO.
1384 * @param bool|int $id - Optional id to return
1387 * array reference of all Counties
1389 public static function &county($id = FALSE) {
1390 if (!self
::$county) {
1392 $config = CRM_Core_Config
::singleton();
1393 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1394 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1397 if (array_key_exists($id, self
::$county)) {
1398 return self
::$county[$id];
1401 return CRM_Core_DAO
::$_nullObject;
1404 return self
::$county;
1408 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1409 * Get all active payment processors
1411 * The static array paymentProcessor is returned
1415 * Get payment processors - default is to get only active ones.
1417 * Get test payment processors.
1419 * @param null $additionalCond
1422 * array of all payment processors
1424 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1425 $condition = "is_test = ";
1426 $condition .= ($test) ?
'1' : '0';
1428 if ($additionalCond) {
1429 $condition .= " AND ( $additionalCond ) ";
1432 // CRM-7178. Make sure we only include payment processors valid in ths
1434 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1436 $cacheKey = $condition . '_' . (int) $all;
1437 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1438 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1441 return self
::$paymentProcessor[$cacheKey];
1445 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1447 * The static array paymentProcessorType is returned
1451 * Get payment processors - default is to get only active ones.
1454 * @param string $return
1457 * array of all payment processor types
1459 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1460 $cacheKey = $id . '_' . $return;
1461 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1462 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1464 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1465 return self
::$paymentProcessorType[$cacheKey][$id];
1467 return self
::$paymentProcessorType[$cacheKey];
1471 * Get all the World Regions from Database.
1477 * array reference of all World Regions
1479 public static function &worldRegion($id = FALSE) {
1480 if (!self
::$worldRegions) {
1481 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1485 if (array_key_exists($id, self
::$worldRegions)) {
1486 return self
::$worldRegions[$id];
1489 return CRM_Core_DAO
::$_nullObject;
1493 return self
::$worldRegions;
1497 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1499 * Get all Activity Statuses.
1501 * The static array activityStatus is returned
1504 * @param string $column
1507 * array reference of all activity statuses
1509 public static function &activityStatus($column = 'label') {
1510 if (NULL === self
::$activityStatus) {
1511 self
::$activityStatus = array();
1513 if (!array_key_exists($column, self
::$activityStatus)) {
1514 self
::$activityStatus[$column] = array();
1516 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1519 return self
::$activityStatus[$column];
1523 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1525 * Get all Visibility levels.
1527 * The static array visibility is returned
1530 * @param string $column
1533 * array reference of all Visibility levels.
1535 public static function &visibility($column = 'label') {
1536 if (!isset(self
::$visibility)) {
1537 self
::$visibility = array();
1540 if (!isset(self
::$visibility[$column])) {
1541 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1544 return self
::$visibility[$column];
1548 * @param int $countryID
1549 * @param string $field
1553 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1554 static $_cache = NULL;
1556 $cacheKey = "{$countryID}_{$field}";
1561 if (!empty($_cache[$cacheKey])) {
1562 return $_cache[$cacheKey];
1566 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1567 FROM civicrm_state_province
1568 WHERE country_id = %1
1577 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1580 while ($dao->fetch()) {
1581 $result[$dao->id
] = $dao->name
;
1584 // localise the stateProvince names if in an non-en_US locale
1585 $config = CRM_Core_Config
::singleton();
1587 if ($tsLocale != '' and $tsLocale != 'en_US') {
1588 $i18n = CRM_Core_I18n
::singleton();
1589 $i18n->localizeArray($result, array(
1590 'context' => 'province',
1592 $result = CRM_Utils_Array
::asort($result);
1595 $_cache[$cacheKey] = $result;
1597 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1603 * @param int $stateID
1607 public static function &countyForState($stateID) {
1608 if (is_array($stateID)) {
1609 $states = implode(", ", $stateID);
1611 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1613 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1614 WHERE civicrm_county.state_province_id in ( $states )
1615 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1617 $dao = CRM_Core_DAO
::executeQuery($query);
1620 while ($dao->fetch()) {
1621 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1626 static $_cache = NULL;
1628 $cacheKey = "{$stateID}_name";
1633 if (!empty($_cache[$cacheKey])) {
1634 return $_cache[$cacheKey];
1638 SELECT civicrm_county.name name, civicrm_county.id id
1640 WHERE state_province_id = %1
1649 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1652 while ($dao->fetch()) {
1653 $result[$dao->id
] = $dao->name
;
1661 * Given a state ID return the country ID, this allows
1662 * us to populate forms and values for downstream code
1664 * @param int $stateID
1667 * the country id that the state belongs to
1669 public static function countryIDForStateID($stateID) {
1670 if (empty($stateID)) {
1671 return CRM_Core_DAO
::$_nullObject;
1676 FROM civicrm_state_province
1679 $params = array(1 => array($stateID, 'Integer'));
1681 return CRM_Core_DAO
::singleValueQuery($query, $params);
1685 * Get all types of Greetings.
1687 * The static array of greeting is returned
1691 * Get All Email Greetings - default is to get only active ones.
1693 * @param string $columnName
1696 * array reference of all greetings.
1698 public static function greeting($filter, $columnName = 'label') {
1699 $index = $filter['greeting_type'] . '_' . $columnName;
1701 // also add contactType to the array
1702 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1704 $index .= '_' . $contactType;
1707 if (NULL === self
::$greeting) {
1708 self
::$greeting = array();
1711 if (!CRM_Utils_Array
::value($index, self
::$greeting)) {
1712 $filterCondition = NULL;
1714 $filterVal = 'v.filter =';
1715 switch ($contactType) {
1724 case 'Organization':
1728 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1731 self
::$greeting[$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1734 return self
::$greeting[$index];
1738 * Construct array of default greeting values for contact type.
1742 * array reference of default greetings.
1744 public static function &greetingDefaults() {
1745 if (!self
::$greetingDefaults) {
1746 $defaultGreetings = array();
1747 $contactTypes = self
::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1748 'keyColumn' => 'id',
1749 'labelColumn' => 'name',
1752 foreach ($contactTypes as $filter => $contactType) {
1753 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1755 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
1756 $tokenVal = CRM_Core_OptionGroup
::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1757 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1761 self
::$greetingDefaults = $defaultGreetings;
1764 return self
::$greetingDefaults;
1768 * Get all extensions.
1770 * The static array extensions
1772 * FIXME: This is called by civix but not by any core code. We
1773 * should provide an API call which civix can use instead.
1777 * array($fullyQualifiedName => $label) list of extensions
1779 public static function &getExtensions() {
1780 if (!self
::$extensions) {
1781 self
::$extensions = array();
1783 SELECT full_name, label
1784 FROM civicrm_extension
1787 $dao = CRM_Core_DAO
::executeQuery($sql);
1788 while ($dao->fetch()) {
1789 self
::$extensions[$dao->full_name
] = $dao->label
;
1793 return self
::$extensions;
1797 * Get all options values.
1799 * The static array option values is returned
1802 * @param bool $optionGroupName
1803 * Get All Option Group values- default is to get only active ones.
1806 * @param null $condition
1809 * array reference of all Option Group Name
1811 public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) {
1812 $cacheKey = $optionGroupName . '_' . $condition;
1813 if (empty(self
::$accountOptionValues[$cacheKey])) {
1814 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, FALSE, FALSE, FALSE, $condition);
1817 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1820 return self
::$accountOptionValues[$cacheKey];
1824 * Fetch the list of active extensions of type 'module'
1826 * @param bool $fresh
1827 * Whether to forcibly reload extensions list from canonical store.
1830 * array(array('prefix' => $, 'file' => $))
1832 public static function getModuleExtensions($fresh = FALSE) {
1833 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);
1838 * Get all tax rates.
1840 * The static array tax rates is returned
1843 * array list of tax rates with the financial type
1845 public static function getTaxRates() {
1846 if (!self
::$taxRates) {
1847 self
::$taxRates = array();
1849 SELECT fa.tax_rate, efa.entity_id
1850 FROM civicrm_entity_financial_account efa
1851 INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
1852 INNER JOIN civicrm_option_value cov ON cov.value = efa.account_relationship
1853 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
1854 WHERE efa.entity_table = 'civicrm_financial_type'
1855 AND cov.name = 'Sales Tax Account is'
1856 AND cog.name = 'account_relationship'
1857 AND fa.is_active = 1";
1858 $dao = CRM_Core_DAO
::executeQuery($sql);
1859 while ($dao->fetch()) {
1860 self
::$taxRates[$dao->entity_id
] = $dao->tax_rate
;
1864 return self
::$taxRates;