3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains functions for managing Action Logs
21 class CRM_Core_BAO_Country
extends CRM_Core_DAO_Country
{
24 * Get the list of countries for which we offer provinces.
28 public static function provinceLimit() {
29 if (!isset(Civi
::$statics[__CLASS__
]['provinceLimit'])) {
30 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
31 $provinceLimit = Civi
::settings()->get('provinceLimit');
33 if (is_array($provinceLimit)) {
34 foreach ($provinceLimit as $val) {
36 // some countries have disappeared and hence they might be in country limit
37 // but not in the country table
38 if (isset($countryIsoCodes[$val])) {
39 $country[] = $countryIsoCodes[$val];
44 $country[] = $countryIsoCodes[$provinceLimit];
46 Civi
::$statics[__CLASS__
]['provinceLimit'] = $country;
48 return Civi
::$statics[__CLASS__
]['provinceLimit'];
52 * Get the list of countries (with names) which are available to user.
56 public static function countryLimit() {
57 if (!isset(Civi
::$statics[__CLASS__
]['countryLimit'])) {
58 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
60 $countryLimit = Civi
::settings()->get('countryLimit') ??
[];
61 if (is_array($countryLimit)) {
62 foreach ($countryLimit as $val) {
64 // some countries have disappeared and hence they might be in country limit
65 // but not in the country table
66 if (isset($countryIsoCodes[$val])) {
67 $country[] = $countryIsoCodes[$val];
72 $country[] = $countryIsoCodes[$countryLimit];
74 Civi
::$statics[__CLASS__
]['countryLimit'] = $country;
76 return Civi
::$statics[__CLASS__
]['countryLimit'];
80 * Provide cached default contact country.
84 public static function defaultContactCountry() {
85 static $cachedContactCountry = NULL;
86 $defaultContactCountry = Civi
::settings()->get('defaultContactCountry');
88 if (!empty($defaultContactCountry) && !$cachedContactCountry) {
89 $countryIsoCodes = CRM_Core_PseudoConstant
::countryIsoCode();
90 $cachedContactCountry = CRM_Utils_Array
::value($defaultContactCountry,
94 return $cachedContactCountry;
98 * Provide list of Pinned countries.
100 * @param $availableCountries
103 public static function pinnedContactCountries($availableCountries) {
104 if (!isset(Civi
::$statics[__CLASS__
]['cachedPinnedContactCountries'])) {
105 $pinnedContactCountries = Civi
::settings()->get('pinnedContactCountries');
106 $pinnedCountries = [];
107 if (!empty($pinnedContactCountries)) {
108 foreach ($pinnedContactCountries as $pinnedContactCountry) {
109 // pinned country must exist in available country list.
110 if (array_key_exists($pinnedContactCountry, $availableCountries)) {
111 $pinnedCountries[$pinnedContactCountry] = $availableCountries[$pinnedContactCountry];
115 Civi
::$statics[__CLASS__
]['cachedPinnedContactCountries'] = $pinnedCountries;
118 return Civi
::$statics[__CLASS__
]['cachedPinnedContactCountries'];
122 * Provide sorted list of countries with default country with first position
123 * then Pinned countries then rest of countries.
125 * @param $availableCountries
128 public static function _defaultContactCountries($availableCountries) {
129 // localise the country names if in an non-en_US locale
130 $tsLocale = CRM_Core_I18n
::getLocale();
131 if ($tsLocale != '' and $tsLocale != 'en_US') {
132 $i18n = CRM_Core_I18n
::singleton();
133 $i18n->localizeArray($availableCountries, [
134 'context' => 'country',
136 $availableCountries = CRM_Utils_Array
::asort($availableCountries);
138 $pinnedContactCountries = CRM_Core_BAO_Country
::pinnedContactCountries($availableCountries);
139 // if default country is set, percolate it to the top, then pinned countries and then remaining available countries.
140 if ($defaultContactCountry = Civi
::settings()->get('defaultContactCountry')) {
141 $default = [$defaultContactCountry => $availableCountries[$defaultContactCountry] ??
NULL];
142 $availableCountries = $default +
$pinnedContactCountries +
$availableCountries;
144 elseif (!empty($pinnedContactCountries)) {
145 // if default country is missing then use only pinned countries at the top then rest of the countries.
146 $availableCountries = $pinnedContactCountries +
$availableCountries;
149 return $availableCountries;
153 * Provide cached default country name.
157 public static function defaultContactCountryName() {
158 static $cachedContactCountryName = NULL;
159 $defaultContactCountry = Civi
::settings()->get('defaultContactCountry');
160 if (!$cachedContactCountryName && $defaultContactCountry) {
161 $countryCodes = CRM_Core_PseudoConstant
::country();
162 $cachedContactCountryName = $countryCodes[$defaultContactCountry];
164 return $cachedContactCountryName;
168 * Provide cached default currency symbol.
170 * @param string $defaultCurrency
174 public static function defaultCurrencySymbol($defaultCurrency = NULL) {
175 static $cachedSymbol = NULL;
176 if (!$cachedSymbol ||
$defaultCurrency) {
177 $currency = $defaultCurrency ?
$defaultCurrency : Civi
::settings()->get('defaultCurrency');
179 $currencySymbols = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'currency', [
180 'labelColumn' => 'symbol',
181 'orderColumn' => TRUE,
183 $cachedSymbol = CRM_Utils_Array
::value($currency, $currencySymbols, '');
189 return $cachedSymbol;
193 * Get the default currency symbol.
195 * @param string $k Unused variable
199 public static function getDefaultCurrencySymbol($k = NULL) {
200 return CRM_Core_BAO_Country
::defaultCurrencySymbol(\Civi
::settings()->get('defaultCurrency'));