Merge pull request #19093 from civicrm/5.32
[civicrm-core.git] / CRM / Core / BAO / Country.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class contains functions for managing Action Logs
20 */
21 class CRM_Core_BAO_Country extends CRM_Core_DAO_Country {
22
23 /**
24 * Get the list of countries for which we offer provinces.
25 *
26 * @return mixed
27 */
28 public static function provinceLimit() {
29 if (!isset(Civi::$statics[__CLASS__]['provinceLimit'])) {
30 $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();
31 $provinceLimit = Civi::settings()->get('provinceLimit');
32 $country = [];
33 if (is_array($provinceLimit)) {
34 foreach ($provinceLimit as $val) {
35 // CRM-12007
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];
40 }
41 }
42 }
43 else {
44 $country[] = $countryIsoCodes[$provinceLimit];
45 }
46 Civi::$statics[__CLASS__]['provinceLimit'] = $country;
47 }
48 return Civi::$statics[__CLASS__]['provinceLimit'];
49 }
50
51 /**
52 * Get the list of countries (with names) which are available to user.
53 *
54 * @return mixed
55 */
56 public static function countryLimit() {
57 if (!isset(Civi::$statics[__CLASS__]['countryLimit'])) {
58 $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();
59 $country = [];
60 $countryLimit = Civi::settings()->get('countryLimit') ?? [];
61 if (is_array($countryLimit)) {
62 foreach ($countryLimit as $val) {
63 // CRM-12007
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];
68 }
69 }
70 }
71 else {
72 $country[] = $countryIsoCodes[$countryLimit];
73 }
74 Civi::$statics[__CLASS__]['countryLimit'] = $country;
75 }
76 return Civi::$statics[__CLASS__]['countryLimit'];
77 }
78
79 /**
80 * Provide cached default contact country.
81 *
82 * @return string
83 */
84 public static function defaultContactCountry() {
85 static $cachedContactCountry = NULL;
86 $defaultContactCountry = Civi::settings()->get('defaultContactCountry');
87
88 if (!empty($defaultContactCountry) && !$cachedContactCountry) {
89 $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();
90 $cachedContactCountry = CRM_Utils_Array::value($defaultContactCountry,
91 $countryIsoCodes
92 );
93 }
94 return $cachedContactCountry;
95 }
96
97 /**
98 * Provide list of Pinned countries.
99 *
100 * @param $availableCountries
101 * @return array
102 */
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];
112 }
113 }
114 }
115 Civi::$statics[__CLASS__]['cachedPinnedContactCountries'] = $pinnedCountries;
116 }
117
118 return Civi::$statics[__CLASS__]['cachedPinnedContactCountries'];
119 }
120
121 /**
122 * Provide sorted list of countries with default country with first position
123 * then Pinned countries then rest of countries.
124 *
125 * @param $availableCountries
126 * @return array
127 */
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',
135 ]);
136 $availableCountries = CRM_Utils_Array::asort($availableCountries);
137 }
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;
143 }
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;
147 }
148
149 return $availableCountries;
150 }
151
152 /**
153 * Provide cached default country name.
154 *
155 * @return string
156 */
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];
163 }
164 return $cachedContactCountryName;
165 }
166
167 /**
168 * Provide cached default currency symbol.
169 *
170 * @param string $defaultCurrency
171 *
172 * @return string
173 */
174 public static function defaultCurrencySymbol($defaultCurrency = NULL) {
175 static $cachedSymbol = NULL;
176 if (!$cachedSymbol || $defaultCurrency) {
177 $currency = $defaultCurrency ? $defaultCurrency : Civi::settings()->get('defaultCurrency');
178 if ($currency) {
179 $currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', [
180 'labelColumn' => 'symbol',
181 'orderColumn' => TRUE,
182 ]);
183 $cachedSymbol = CRM_Utils_Array::value($currency, $currencySymbols, '');
184 }
185 else {
186 $cachedSymbol = '$';
187 }
188 }
189 return $cachedSymbol;
190 }
191
192 /**
193 * Get the default currency symbol.
194 *
195 * @param string $k Unused variable
196 *
197 * @return string
198 */
199 public static function getDefaultCurrencySymbol($k = NULL) {
200 return CRM_Core_BAO_Country::defaultCurrencySymbol(\Civi::settings()->get('defaultCurrency'));
201 }
202
203 }