3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
48 class CRM_Core_PseudoConstant
{
51 * Static cache for pseudoconstant arrays.
54 private static $cache;
57 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
62 private static $activityType;
68 private static $stateProvince;
74 private static $county;
77 * States/provinces abbreviations
80 private static $stateProvinceAbbreviation = array();
86 private static $country;
92 private static $countryIsoCode;
95 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
100 private static $group;
106 private static $relationshipType;
109 * Civicrm groups that are not smart groups
112 private static $staticGroup;
118 private static $currencyCode;
124 private static $paymentProcessor;
127 * Payment processor types
130 private static $paymentProcessorType;
136 private static $worldRegions;
139 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
144 private static $activityStatus;
150 private static $visibility;
156 private static $greeting;
159 * Extensions of type module
162 private static $extensions;
165 * Financial Account Type
168 private static $accountOptionValues;
171 * Low-level option getter, rarely accessed directly.
172 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
173 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
175 * NOTE: If someone undertakes a refactoring of this, please consider the use-case of
176 * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
177 * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
178 * between $field lookup and the $pseudoconstant evaluation.
180 * @param string $daoName
181 * @param string $fieldName
182 * @param array $params
183 * - name string name of the option group
184 * - flip boolean results are return in id => label format if false
185 * if true, the results are reversed
186 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
187 * - localize boolean if true, localize the results before returning
188 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
189 * - keyColumn string the column to use for 'id'
190 * - labelColumn string the column to use for 'label'
191 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
192 * - onlyActive boolean return only the action option values
193 * - fresh boolean ignore cache entries and go back to DB
194 * @param string $context : Context string
197 * array on success, FALSE on error.
200 public static function get($daoName, $fieldName, $params = array(), $context = NULL) {
201 CRM_Core_DAO
::buildOptionsContext($context);
202 $flip = !empty($params['flip']);
203 // Merge params with defaults
207 'onlyActive' => ($context == 'validate' ||
$context == 'get') ?
FALSE : TRUE,
209 'context' => $context,
211 $entity = CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getCanonicalClassName($daoName));
213 // Custom fields are not in the schema
214 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
215 $customField = new CRM_Core_BAO_CustomField();
216 $customField->id
= (int) substr($fieldName, 7);
217 $options = $customField->getOptions($context);
218 if ($options && $flip) {
219 $options = array_flip($options);
221 $customField->free();
225 // Core field: load schema
226 $dao = new $daoName();
227 $fieldSpec = $dao->getFieldSpec($fieldName);
230 // Ensure we have the canonical name for this field
231 $fieldName = CRM_Utils_Array
::value('name', $fieldSpec, $fieldName);
233 // Return false if field doesn't exist.
234 if (empty($fieldSpec)) {
238 elseif (!empty($fieldSpec['pseudoconstant'])) {
239 $pseudoconstant = $fieldSpec['pseudoconstant'];
241 // if callback is specified..
242 if (!empty($pseudoconstant['callback'])) {
243 $fieldOptions = call_user_func(Civi\Core\Resolver
::singleton()->get($pseudoconstant['callback']), $context);
244 //CRM-18223: Allow additions to field options via hook.
245 CRM_Utils_Hook
::fieldOptions($entity, $fieldName, $fieldOptions, $params);
246 return $fieldOptions;
249 // Merge params with schema defaults
251 'condition' => CRM_Utils_Array
::value('condition', $pseudoconstant, array()),
252 'keyColumn' => CRM_Utils_Array
::value('keyColumn', $pseudoconstant),
253 'labelColumn' => CRM_Utils_Array
::value('labelColumn', $pseudoconstant),
256 if ($context == 'abbreviate') {
257 switch ($fieldName) {
258 case 'state_province_id':
259 $params['labelColumn'] = 'abbreviation';
263 $params['labelColumn'] = 'iso_code';
270 // Fetch option group from option_value table
271 if (!empty($pseudoconstant['optionGroupName'])) {
272 if ($context == 'validate') {
273 $params['labelColumn'] = 'name';
275 if ($context == 'match') {
276 $params['keyColumn'] = 'name';
278 // Call our generic fn for retrieving from the option_value table
279 $options = CRM_Core_OptionGroup
::values(
280 $pseudoconstant['optionGroupName'],
284 $params['condition'] ?
' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
285 $params['labelColumn'] ?
$params['labelColumn'] : 'label',
286 $params['onlyActive'],
288 $params['keyColumn'] ?
$params['keyColumn'] : 'value',
289 !empty($params['orderColumn']) ?
$params['orderColumn'] : 'weight'
291 CRM_Utils_Hook
::fieldOptions($entity, $fieldName, $options, $params);
295 // Fetch options from other tables
296 if (!empty($pseudoconstant['table'])) {
297 // Normalize params so the serialized cache string will be consistent.
298 CRM_Utils_Array
::remove($params, 'flip', 'fresh');
300 $cacheKey = $daoName . $fieldName . serialize($params);
302 // Retrieve cached options
303 if (isset(self
::$cache[$cacheKey]) && empty($params['fresh'])) {
304 $output = self
::$cache[$cacheKey];
307 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($pseudoconstant['table']);
308 if (!class_exists($daoName)) {
311 // Get list of fields for the option table
312 $dao = new $daoName();
313 $availableFields = array_keys($dao->fieldKeys());
316 $select = "SELECT %1 AS id, %2 AS label";
319 $order = "ORDER BY %2";
321 // Use machine name in certain contexts
322 if ($context == 'validate' ||
$context == 'match') {
323 $nameField = $context == 'validate' ?
'labelColumn' : 'keyColumn';
324 if (!empty($pseudoconstant['nameColumn'])) {
325 $params[$nameField] = $pseudoconstant['nameColumn'];
327 elseif (in_array('name', $availableFields)) {
328 $params[$nameField] = 'name';
331 // Condition param can be passed as an sql clause string or an array of clauses
332 if (!empty($params['condition'])) {
333 $wheres[] = implode(' AND ', (array) $params['condition']);
335 // onlyActive param will automatically filter on common flags
336 if (!empty($params['onlyActive'])) {
337 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0) as $flag => $val) {
338 if (in_array($flag, $availableFields)) {
339 $wheres[] = "$flag = $val";
343 // Filter domain specific options
344 if (in_array('domain_id', $availableFields)) {
345 $wheres[] = 'domain_id = ' . CRM_Core_Config
::domainID();
347 $queryParams = array(
348 1 => array($params['keyColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
349 2 => array($params['labelColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
350 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
),
352 // Add orderColumn param
353 if (!empty($params['orderColumn'])) {
354 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
);
355 $order = "ORDER BY %4";
357 // Support no sorting if $params[orderColumn] is FALSE
358 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
361 // Default to 'weight' if that column exists
362 elseif (in_array('weight', $availableFields)) {
363 $order = "ORDER BY weight";
367 $query = "$select $from";
369 $query .= " WHERE " . implode($wheres, ' AND ');
371 $query .= ' ' . $order;
372 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
373 while ($dao->fetch()) {
374 $output[$dao->id
] = $dao->label
;
378 if (!empty($params['localize']) ||
$pseudoconstant['table'] == 'civicrm_country' ||
$pseudoconstant['table'] == 'civicrm_state_province') {
379 $I18nParams = array();
380 if ($pseudoconstant['table'] == 'civicrm_country') {
381 $I18nParams['context'] = 'country';
383 if ($pseudoconstant['table'] == 'civicrm_state_province') {
384 $I18nParams['context'] = 'province';
386 $i18n = CRM_Core_I18n
::singleton();
387 $i18n->localizeArray($output, $I18nParams);
388 // Maintain sort by label
389 if ($order == "ORDER BY %2") {
390 CRM_Utils_Array
::asort($output);
393 CRM_Utils_Hook
::fieldOptions($entity, $fieldName, $output, $params);
394 self
::$cache[$cacheKey] = $output;
396 return $flip ?
array_flip($output) : $output;
400 // Return "Yes" and "No" for boolean fields
401 elseif (CRM_Utils_Array
::value('type', $fieldSpec) === CRM_Utils_Type
::T_BOOLEAN
) {
402 $output = $context == 'validate' ?
array(0, 1) : CRM_Core_SelectValues
::boolean();
403 CRM_Utils_Hook
::fieldOptions($entity, $fieldName, $output, $params);
404 return $flip ?
array_flip($output) : $output;
406 // If we're still here, it's an error. Return FALSE.
411 * Fetch the translated label for a field given its key.
413 * @param string $baoName
414 * @param string $fieldName
415 * @param string|Int $key
417 * TODO: Accept multivalued input?
419 * @return bool|null|string
420 * FALSE if the given field has no associated option list
421 * NULL if the given key has no corresponding option
422 * String if label is found
424 public static function getLabel($baoName, $fieldName, $key) {
425 $values = $baoName::buildOptions($fieldName, 'get');
426 if ($values === FALSE) {
429 return CRM_Utils_Array
::value($key, $values);
433 * Fetch the machine name for a field given its key.
435 * @param string $baoName
436 * @param string $fieldName
437 * @param string|Int $key
439 * @return bool|null|string
440 * FALSE if the given field has no associated option list
441 * NULL if the given key has no corresponding option
442 * String if label is found
444 public static function getName($baoName, $fieldName, $key) {
445 $values = $baoName::buildOptions($fieldName, 'validate');
446 if ($values === FALSE) {
449 return CRM_Utils_Array
::value($key, $values);
453 * Fetch the key for a field option given its name.
455 * @param string $baoName
456 * @param string $fieldName
457 * @param string|Int $value
459 * @return bool|null|string|int
460 * FALSE if the given field has no associated option list
461 * NULL if the given key has no corresponding option
462 * String|Number if key is found
464 public static function getKey($baoName, $fieldName, $value) {
465 $values = $baoName::buildOptions($fieldName, 'validate');
466 if ($values === FALSE) {
469 return CRM_Utils_Array
::key($value, $values);
473 * Lookup the admin page at which a field's option list can be edited
475 * @return string|null
477 public static function getOptionEditUrl($fieldSpec) {
478 // If it's an option group, that's easy
479 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
480 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
482 // For everything else...
483 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
484 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($fieldSpec['pseudoconstant']['table']);
488 // We don't have good mapping so have to do a bit of guesswork from the menu
489 list(, $parent, , $child) = explode('_', $daoName);
490 $sql = "SELECT path FROM civicrm_menu
491 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
492 ORDER BY page_callback
494 return CRM_Core_Dao
::singleValueQuery($sql);
500 * DEPRECATED generic populate method.
501 * All pseudoconstant functions that use this method are also @deprecated
503 * The static array $var is populated from the db
504 * using the <b>$name DAO</b>.
506 * Note: any database errors will be trapped by the DAO.
509 * The associative array we will fill.
510 * @param string $name
511 * The name of the DAO.
513 * Get all objects. default is to get only active ones.
514 * @param string $retrieve
515 * The field that we are interested in (normally name, differs in some objects).
516 * @param string $filter
517 * The field that we want to filter the result set with.
518 * @param string $condition
519 * The condition that gets passed to the final query as the WHERE clause.
521 * @param bool $orderby
527 public static function populate(
532 $filter = 'is_active',
538 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
539 $cache = CRM_Utils_Cache
::singleton();
540 $var = $cache->get($cacheKey);
541 if ($var && empty($force)) {
545 /* @var CRM_Core_DAO $object */
546 $object = new $name();
548 $object->selectAdd();
549 $object->selectAdd("$key, $retrieve");
551 $object->whereAdd($condition);
555 $object->orderBy($retrieve);
558 $object->orderBy($orderby);
562 $object->$filter = 1;
563 $aclClauses = array_filter($name::getSelectWhereClause());
564 foreach ($aclClauses as $clause) {
565 $object->whereAdd($clause);
571 while ($object->fetch()) {
572 $var[$object->$key] = $object->$retrieve;
575 $cache->set($cacheKey, $var);
579 * Flush given pseudoconstant so it can be reread from db.
580 * nex time it's requested.
583 * @param bool|string $name pseudoconstant to be flushed
585 public static function flush($name = 'cache') {
586 if (isset(self
::$
$name)) {
589 if ($name == 'cache') {
590 CRM_Core_OptionGroup
::flushAll();
591 if (isset(\Civi
::$statics[__CLASS__
])) {
592 unset(\Civi
::$statics[__CLASS__
]);
598 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
600 * Get all Activty types.
602 * The static array activityType is returned
606 * array reference of all activity types.
608 public static function &activityType() {
609 $args = func_get_args();
610 $all = CRM_Utils_Array
::value(0, $args, TRUE);
611 $includeCaseActivities = CRM_Utils_Array
::value(1, $args, FALSE);
612 $reset = CRM_Utils_Array
::value(2, $args, FALSE);
613 $returnColumn = CRM_Utils_Array
::value(3, $args, 'label');
614 $includeCampaignActivities = CRM_Utils_Array
::value(4, $args, FALSE);
615 $onlyComponentActivities = CRM_Utils_Array
::value(5, $args, FALSE);
616 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
617 $index .= '_' . (int) $includeCampaignActivities;
618 $index .= '_' . (int) $onlyComponentActivities;
620 if (NULL === self
::$activityType) {
621 self
::$activityType = array();
624 if (!isset(self
::$activityType[$index]) ||
$reset) {
627 $condition = 'AND filter = 0';
629 $componentClause = " v.component_id IS NULL";
630 if ($onlyComponentActivities) {
631 $componentClause = " v.component_id IS NOT NULL";
634 $componentIds = array();
635 $compInfo = CRM_Core_Component
::getEnabledComponents();
637 // build filter for listing activity types only if their
638 // respective components are enabled
639 foreach ($compInfo as $compName => $compObj) {
640 if ($compName == 'CiviCase') {
641 if ($includeCaseActivities) {
642 $componentIds[] = $compObj->componentID
;
645 elseif ($compName == 'CiviCampaign') {
646 if ($includeCampaignActivities) {
647 $componentIds[] = $compObj->componentID
;
651 $componentIds[] = $compObj->componentID
;
655 if (count($componentIds)) {
656 $componentIds = implode(',', $componentIds);
657 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
658 if ($onlyComponentActivities) {
659 $componentClause = " ( v.component_id IN ($componentIds ) )";
662 $condition = $condition . ' AND ' . $componentClause;
664 self
::$activityType[$index] = CRM_Core_OptionGroup
::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
666 return self
::$activityType[$index];
670 * Get all the State/Province from database.
672 * The static array stateProvince is returned, and if it's
673 * called the first time, the <b>State Province DAO</b> is used
674 * to get all the States.
676 * Note: any database errors will be trapped by the DAO.
679 * @param bool|int $id - Optional id to return
684 * array reference of all State/Provinces.
686 public static function &stateProvince($id = FALSE, $limit = TRUE) {
687 if (($id && !CRM_Utils_Array
::value($id, self
::$stateProvince)) ||
!self
::$stateProvince ||
!$id) {
688 $whereClause = FALSE;
690 $countryIsoCodes = self
::countryIsoCode();
691 $limitCodes = CRM_Core_BAO_Country
::provinceLimit();
693 foreach ($limitCodes as $code) {
694 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
696 if (!empty($limitIds)) {
697 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
700 $whereClause = FALSE;
703 self
::populate(self
::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
705 // localise the province names if in an non-en_US locale
706 $tsLocale = CRM_Core_I18n
::getLocale();
707 if ($tsLocale != '' and $tsLocale != 'en_US') {
708 $i18n = CRM_Core_I18n
::singleton();
709 $i18n->localizeArray(self
::$stateProvince, array(
710 'context' => 'province',
712 self
::$stateProvince = CRM_Utils_Array
::asort(self
::$stateProvince);
716 if (array_key_exists($id, self
::$stateProvince)) {
717 return self
::$stateProvince[$id];
724 return self
::$stateProvince;
728 * Get all the State/Province abbreviations from the database.
730 * Same as above, except gets the abbreviations instead of the names.
733 * @param bool|int $id - Optional id to return
738 * array reference of all State/Province abbreviations.
740 public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
741 if ($id && is_numeric($id)) {
742 if (!array_key_exists($id, (array) self
::$stateProvinceAbbreviation)) {
743 $query = "SELECT abbreviation
744 FROM civicrm_state_province
752 self
::$stateProvinceAbbreviation[$id] = CRM_Core_DAO
::singleValueQuery($query, $params);
754 return self
::$stateProvinceAbbreviation[$id];
757 $whereClause = FALSE;
760 $countryIsoCodes = self
::countryIsoCode();
761 $limitCodes = CRM_Core_BAO_Country
::provinceLimit();
763 foreach ($limitCodes as $code) {
764 $tmpArray = array_keys($countryIsoCodes, $code);
766 if (!empty($tmpArray)) {
767 $limitIds[] = array_shift($tmpArray);
770 if (!empty($limitIds)) {
771 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
774 self
::populate(self
::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
777 return self
::$stateProvinceAbbreviation;
781 * Get all the State/Province abbreviations from the database for the specified country.
783 * @param int $countryID
786 * array of all State/Province abbreviations for the given country.
788 public static function stateProvinceAbbreviationForCountry($countryID) {
789 if (!isset(\Civi
::$statics[__CLASS__
]['stateProvinceAbbreviationForCountry'][$countryID])) {
790 \Civi
::$statics[__CLASS__
]['stateProvinceAbbreviationForCountry'][$countryID] = array();
792 self
::populate(\Civi
::$statics[__CLASS__
]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation');
793 return \Civi
::$statics[__CLASS__
]['stateProvinceAbbreviationForCountry'][$countryID];
797 * Get all the State/Province abbreviations from the database for the default country.
800 * array of all State/Province abbreviations for the given country.
802 public static function stateProvinceAbbreviationForDefaultCountry() {
803 return CRM_Core_PseudoConstant
::stateProvinceAbbreviationForCountry(Civi
::settings()->get('defaultContactCountry'));
807 * Get all the countries from database.
809 * The static array country is returned, and if it's
810 * called the first time, the <b>Country DAO</b> is used
811 * to get all the countries.
813 * Note: any database errors will be trapped by the DAO.
816 * @param bool|int $id - Optional id to return
818 * @param bool $applyLimit
821 * array reference of all countries.
823 public static function country($id = FALSE, $applyLimit = TRUE) {
824 if (($id && !CRM_Utils_Array
::value($id, self
::$country)) ||
!self
::$country ||
!$id) {
826 $config = CRM_Core_Config
::singleton();
827 $limitCodes = array();
830 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
831 // (ensuring it's a subset of the legal values)
832 // K/P: We need to fix this, i dont think it works with new setting files
833 $limitCodes = CRM_Core_BAO_Country
::countryLimit();
834 if (!is_array($limitCodes)) {
836 $config->countryLimit
=> 1,
840 $limitCodes = array_intersect(self
::countryIsoCode(), $limitCodes);
843 if (count($limitCodes)) {
844 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
850 self
::populate(self
::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
852 // if default country is set, percolate it to the top
853 if ($config->defaultContactCountry()) {
854 $countryIsoCodes = self
::countryIsoCode();
855 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
856 if ($defaultID !== FALSE) {
857 $default[$defaultID] = CRM_Utils_Array
::value($defaultID, self
::$country);
858 self
::$country = $default + self
::$country;
862 // localise the country names if in an non-en_US locale
863 $tsLocale = CRM_Core_I18n
::getLocale();
864 if ($tsLocale != '' and $tsLocale != 'en_US') {
865 $i18n = CRM_Core_I18n
::singleton();
866 $i18n->localizeArray(self
::$country, array(
867 'context' => 'country',
869 self
::$country = CRM_Utils_Array
::asort(self
::$country);
873 if (array_key_exists($id, self
::$country)) {
874 return self
::$country[$id];
880 return self
::$country;
884 * Get all the country ISO Code abbreviations from the database.
886 * The static array countryIsoCode is returned, and if it's
887 * called the first time, the <b>Country DAO</b> is used
888 * to get all the countries' ISO codes.
890 * Note: any database errors will be trapped by the DAO.
896 * array reference of all country ISO codes.
898 public static function &countryIsoCode($id = FALSE) {
899 if (!self
::$countryIsoCode) {
900 self
::populate(self
::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
903 if (array_key_exists($id, self
::$countryIsoCode)) {
904 return self
::$countryIsoCode[$id];
907 return CRM_Core_DAO
::$_nullObject;
910 return self
::$countryIsoCode;
914 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
916 * Get all groups from database
918 * The static array group is returned, and if it's
919 * called the first time, the <b>Group DAO</b> is used
920 * to get all the groups.
922 * Note: any database errors will be trapped by the DAO.
924 * @param string $groupType
925 * Type of group(Access/Mailing).
926 * @param bool $excludeHidden
927 * Exclude hidden groups.
931 * array reference of all groups.
933 public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
934 $condition = CRM_Contact_BAO_Group
::groupTypeCondition($groupType, $excludeHidden);
935 $groupKey = ($groupType ?
$groupType : 'null') . !empty($excludeHidden);
937 if (!isset(Civi
::$statics[__CLASS__
]['groups']['allGroup'][$groupKey])) {
938 self
::populate(Civi
::$statics[__CLASS__
]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
940 return Civi
::$statics[__CLASS__
]['groups']['allGroup'][$groupKey];
944 * Get all permissioned groups from database.
946 * The static array group is returned, and if it's
947 * called the first time, the <b>Group DAO</b> is used
948 * to get all the groups.
950 * Note: any database errors will be trapped by the DAO.
952 * @param string $groupType
953 * Type of group(Access/Mailing).
954 * @param bool $excludeHidden
955 * Exclude hidden groups.
959 * array reference of all groups.
961 public static function group($groupType = NULL, $excludeHidden = TRUE) {
962 return CRM_Core_Permission
::group($groupType, $excludeHidden);
966 * Fetch groups in a nested format suitable for use in select form element.
967 * @param bool $checkPermissions
968 * @param string|null $groupType
969 * @param bool $excludeHidden
972 public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
973 $groups = $checkPermissions ? self
::group($groupType, $excludeHidden) : self
::allGroup($groupType, $excludeHidden);
974 return CRM_Contact_BAO_Group
::getGroupsHierarchy($groups, NULL, ' ', TRUE);
978 * Get all permissioned groups from database.
980 * The static array group is returned, and if it's
981 * called the first time, the <b>Group DAO</b> is used
982 * to get all the groups.
984 * Note: any database errors will be trapped by the DAO.
987 * @param bool $onlyPublic
988 * @param null $groupType
989 * @param bool $excludeHidden
992 * array reference of all groups.
994 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
995 if (!self
::$staticGroup) {
996 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
998 $condition .= " AND visibility != 'User and User Admin Only'";
1002 $condition .= ' AND ' . CRM_Contact_BAO_Group
::groupTypeCondition($groupType);
1005 if ($excludeHidden) {
1006 $condition .= ' AND is_hidden != 1 ';
1009 self
::populate(self
::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1012 return self
::$staticGroup;
1016 * Get all Relationship Types from database.
1018 * The static array group is returned, and if it's
1019 * called the first time, the <b>RelationshipType DAO</b> is used
1020 * to get all the relationship types.
1022 * Note: any database errors will be trapped by the DAO.
1024 * @param string $valueColumnName
1025 * Db column name/label.
1026 * @param bool $reset
1027 * Reset relationship types if true.
1028 * @param bool $isActive
1029 * Filter by is_active. NULL to disable.
1032 * array reference of all relationship types.
1034 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) {
1035 $cacheKey = $valueColumnName . '::' . $isActive;
1036 if (!CRM_Utils_Array
::value($cacheKey, self
::$relationshipType) ||
$reset) {
1037 self
::$relationshipType[$cacheKey] = array();
1039 //now we have name/label columns CRM-3336
1040 $column_a_b = "{$valueColumnName}_a_b";
1041 $column_b_a = "{$valueColumnName}_b_a";
1043 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1044 $relationshipTypeDAO->selectAdd();
1045 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1046 if ($isActive !== NULL) {
1047 $relationshipTypeDAO->is_active
= $isActive;
1049 $relationshipTypeDAO->find();
1050 while ($relationshipTypeDAO->fetch()) {
1052 self
::$relationshipType[$cacheKey][$relationshipTypeDAO->id
] = array(
1053 'id' => $relationshipTypeDAO->id
,
1054 $column_a_b => $relationshipTypeDAO->$column_a_b,
1055 $column_b_a => $relationshipTypeDAO->$column_b_a,
1056 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1057 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1058 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1059 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1064 return self
::$relationshipType[$cacheKey];
1068 * Get all the ISO 4217 currency codes
1070 * so far, we use this for validation only, so there's no point of putting this into the database
1074 * array reference of all currency codes
1076 public static function ¤cyCode() {
1077 if (!self
::$currencyCode) {
1079 $query = "SELECT name FROM civicrm_currency";
1080 $dao = CRM_Core_DAO
::executeQuery($query);
1081 $currencyCode = array();
1082 while ($dao->fetch()) {
1083 self
::$currencyCode[] = $dao->name
;
1086 return self
::$currencyCode;
1090 * Get all the County from database.
1092 * The static array county is returned, and if it's
1093 * called the first time, the <b>County DAO</b> is used
1094 * to get all the Counties.
1096 * Note: any database errors will be trapped by the DAO.
1099 * @param bool|int $id - Optional id to return
1102 * array reference of all Counties
1104 public static function &county($id = FALSE) {
1105 if (!self
::$county) {
1107 $config = CRM_Core_Config
::singleton();
1108 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1109 self
::populate(self
::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1112 if (array_key_exists($id, self
::$county)) {
1113 return self
::$county[$id];
1116 return CRM_Core_DAO
::$_nullObject;
1119 return self
::$county;
1123 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1124 * Get all active payment processors
1126 * The static array paymentProcessor is returned
1130 * Get payment processors - default is to get only active ones.
1132 * Get test payment processors.
1134 * @param null $additionalCond
1137 * array of all payment processors
1139 public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1140 $condition = "is_test = ";
1141 $condition .= ($test) ?
'1' : '0';
1143 if ($additionalCond) {
1144 $condition .= " AND ( $additionalCond ) ";
1147 // CRM-7178. Make sure we only include payment processors valid in this
1149 $condition .= " AND domain_id = " . CRM_Core_Config
::domainID();
1151 $cacheKey = $condition . '_' . (int) $all;
1152 if (!isset(self
::$paymentProcessor[$cacheKey])) {
1153 self
::populate(self
::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1156 return self
::$paymentProcessor[$cacheKey];
1160 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1162 * The static array paymentProcessorType is returned
1166 * Get payment processors - default is to get only active ones.
1169 * @param string $return
1172 * array of all payment processor types
1174 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
1175 $cacheKey = $id . '_' . $return;
1176 if (empty(self
::$paymentProcessorType[$cacheKey])) {
1177 self
::populate(self
::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1179 if ($id && CRM_Utils_Array
::value($id, self
::$paymentProcessorType[$cacheKey])) {
1180 return self
::$paymentProcessorType[$cacheKey][$id];
1182 return self
::$paymentProcessorType[$cacheKey];
1186 * Get all the World Regions from Database.
1192 * array reference of all World Regions
1194 public static function &worldRegion($id = FALSE) {
1195 if (!self
::$worldRegions) {
1196 self
::populate(self
::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1200 if (array_key_exists($id, self
::$worldRegions)) {
1201 return self
::$worldRegions[$id];
1204 return CRM_Core_DAO
::$_nullObject;
1208 return self
::$worldRegions;
1212 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1214 * Get all Activity Statuses.
1216 * The static array activityStatus is returned
1219 * @param string $column
1222 * array reference of all activity statuses
1224 public static function &activityStatus($column = 'label') {
1225 if (NULL === self
::$activityStatus) {
1226 self
::$activityStatus = array();
1228 if (!array_key_exists($column, self
::$activityStatus)) {
1229 self
::$activityStatus[$column] = array();
1231 self
::$activityStatus[$column] = CRM_Core_OptionGroup
::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1234 return self
::$activityStatus[$column];
1238 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
1240 * Get all Visibility levels.
1242 * The static array visibility is returned
1245 * @param string $column
1248 * array reference of all Visibility levels.
1250 public static function &visibility($column = 'label') {
1251 if (!isset(self
::$visibility)) {
1252 self
::$visibility = array();
1255 if (!isset(self
::$visibility[$column])) {
1256 self
::$visibility[$column] = CRM_Core_OptionGroup
::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
1259 return self
::$visibility[$column];
1263 * @param int $countryID
1264 * @param string $field
1268 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1269 static $_cache = NULL;
1271 $cacheKey = "{$countryID}_{$field}";
1276 if (!empty($_cache[$cacheKey])) {
1277 return $_cache[$cacheKey];
1281 SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1282 FROM civicrm_state_province
1283 WHERE country_id = %1
1292 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1295 while ($dao->fetch()) {
1296 $result[$dao->id
] = $dao->name
;
1299 // localise the stateProvince names if in an non-en_US locale
1300 $config = CRM_Core_Config
::singleton();
1301 $tsLocale = CRM_Core_I18n
::getLocale();
1302 if ($tsLocale != '' and $tsLocale != 'en_US') {
1303 $i18n = CRM_Core_I18n
::singleton();
1304 $i18n->localizeArray($result, array(
1305 'context' => 'province',
1307 $result = CRM_Utils_Array
::asort($result);
1310 $_cache[$cacheKey] = $result;
1312 CRM_Utils_Hook
::buildStateProvinceForCountry($countryID, $result);
1318 * @param int $stateID
1322 public static function &countyForState($stateID) {
1323 if (is_array($stateID)) {
1324 $states = implode(", ", $stateID);
1326 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1328 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1329 WHERE civicrm_county.state_province_id in ( $states )
1330 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1332 $dao = CRM_Core_DAO
::executeQuery($query);
1335 while ($dao->fetch()) {
1336 $result[$dao->id
] = $dao->abbreviation
. ': ' . $dao->name
;
1341 static $_cache = NULL;
1343 $cacheKey = "{$stateID}_name";
1348 if (!empty($_cache[$cacheKey])) {
1349 return $_cache[$cacheKey];
1353 SELECT civicrm_county.name name, civicrm_county.id id
1355 WHERE state_province_id = %1
1364 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1367 while ($dao->fetch()) {
1368 $result[$dao->id
] = $dao->name
;
1376 * Given a state ID return the country ID, this allows
1377 * us to populate forms and values for downstream code
1379 * @param int $stateID
1382 * the country id that the state belongs to
1384 public static function countryIDForStateID($stateID) {
1385 if (empty($stateID)) {
1391 FROM civicrm_state_province
1394 $params = array(1 => array($stateID, 'Integer'));
1396 return CRM_Core_DAO
::singleValueQuery($query, $params);
1400 * Get all types of Greetings.
1402 * The static array of greeting is returned
1406 * Get All Email Greetings - default is to get only active ones.
1408 * @param string $columnName
1411 * array reference of all greetings.
1413 public static function greeting($filter, $columnName = 'label') {
1414 if (!isset(Civi
::$statics[__CLASS__
]['greeting'])) {
1415 Civi
::$statics[__CLASS__
]['greeting'] = array();
1418 $index = $filter['greeting_type'] . '_' . $columnName;
1420 // also add contactType to the array
1421 $contactType = CRM_Utils_Array
::value('contact_type', $filter);
1423 $index .= '_' . $contactType;
1426 if (!CRM_Utils_Array
::value($index, Civi
::$statics[__CLASS__
]['greeting'])) {
1427 $filterCondition = NULL;
1429 $filterVal = 'v.filter =';
1430 switch ($contactType) {
1439 case 'Organization':
1443 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1446 Civi
::$statics[__CLASS__
]['greeting'][$index] = CRM_Core_OptionGroup
::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1449 return Civi
::$statics[__CLASS__
]['greeting'][$index];
1453 * Get all extensions.
1455 * The static array extensions
1457 * FIXME: This is called by civix but not by any core code. We
1458 * should provide an API call which civix can use instead.
1462 * array($fullyQualifiedName => $label) list of extensions
1464 public static function &getExtensions() {
1465 if (!self
::$extensions) {
1466 self
::$extensions = array();
1468 SELECT full_name, label
1469 FROM civicrm_extension
1472 $dao = CRM_Core_DAO
::executeQuery($sql);
1473 while ($dao->fetch()) {
1474 self
::$extensions[$dao->full_name
] = $dao->label
;
1478 return self
::$extensions;
1482 * Get all options values.
1484 * The static array option values is returned
1487 * @param bool $optionGroupName
1488 * Get All Option Group values- default is to get only active ones.
1491 * @param null $condition
1494 * array reference of all Option Group Name
1496 public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) {
1497 $cacheKey = $optionGroupName . '_' . $condition;
1498 if (empty(self
::$accountOptionValues[$cacheKey])) {
1499 self
::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup
::values($optionGroupName, FALSE, FALSE, FALSE, $condition);
1502 return CRM_Utils_Array
::value($id, self
::$accountOptionValues[$cacheKey]);
1505 return self
::$accountOptionValues[$cacheKey];
1509 * Fetch the list of active extensions of type 'module'
1511 * @param bool $fresh
1512 * Whether to forcibly reload extensions list from canonical store.
1515 * array(array('prefix' => $, 'file' => $))
1517 public static function getModuleExtensions($fresh = FALSE) {
1518 return CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles($fresh);
1523 * Get all tax rates.
1525 * The static array tax rates is returned
1528 * array list of tax rates with the financial type
1530 public static function getTaxRates() {
1531 if (!isset(Civi
::$statics[__CLASS__
]['taxRates'])) {
1532 Civi
::$statics[__CLASS__
]['taxRates'] = array();
1533 $option = civicrm_api3('option_value', 'get', array(
1535 'option_group_id' => 'account_relationship',
1536 'name' => 'Sales Tax Account is',
1539 if ($option['count'] !== 0) {
1540 if ($option['count'] > 1) {
1541 foreach ($option['values'] as $opt) {
1542 $value[] = $opt['value'];
1546 $value[] = $option['values'][0]['value'];
1548 $where = 'AND efa.account_relationship IN (' . implode(', ', $value) . ' )';
1554 SELECT fa.tax_rate, efa.entity_id
1555 FROM civicrm_entity_financial_account efa
1556 INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
1557 WHERE efa.entity_table = 'civicrm_financial_type'
1559 AND fa.is_active = 1";
1560 $dao = CRM_Core_DAO
::executeQuery($sql);
1561 while ($dao->fetch()) {
1562 Civi
::$statics[__CLASS__
]['taxRates'][$dao->entity_id
] = $dao->tax_rate
;
1566 return Civi
::$statics[__CLASS__
]['taxRates'];