Commit | Line | Data |
---|---|---|
0acb7f15 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
0acb7f15 | 5 | | | |
bc77d7c0 TO |
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 | | |
0acb7f15 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
0acb7f15 TO |
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'); | |
be2fb01f | 32 | $country = []; |
0acb7f15 TO |
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(); | |
be2fb01f | 59 | $country = []; |
2f3d0664 | 60 | $countryLimit = Civi::settings()->get('countryLimit') ?? []; |
0acb7f15 TO |
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 | ||
3c4a4657 | 97 | /** |
3a260ed6 | 98 | * Provide list of Pinned countries. |
3c4a4657 SP |
99 | * |
100 | * @param $availableCountries | |
101 | * @return array | |
102 | */ | |
3a260ed6 | 103 | public static function pinnedContactCountries($availableCountries) { |
7b11bd27 SP |
104 | if (!isset(Civi::$statics[__CLASS__]['cachedPinnedContactCountries'])) { |
105 | $pinnedContactCountries = Civi::settings()->get('pinnedContactCountries'); | |
3a260ed6 | 106 | $pinnedCountries = []; |
7b11bd27 SP |
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 | } | |
3c4a4657 SP |
113 | } |
114 | } | |
7b11bd27 | 115 | Civi::$statics[__CLASS__]['cachedPinnedContactCountries'] = $pinnedCountries; |
3c4a4657 | 116 | } |
7b11bd27 SP |
117 | |
118 | return Civi::$statics[__CLASS__]['cachedPinnedContactCountries']; | |
3c4a4657 SP |
119 | } |
120 | ||
121 | /** | |
122 | * Provide sorted list of countries with default country with first position | |
3a260ed6 | 123 | * then Pinned countries then rest of countries. |
3c4a4657 SP |
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 | } | |
3a260ed6 | 138 | $pinnedContactCountries = CRM_Core_BAO_Country::pinnedContactCountries($availableCountries); |
7b11bd27 SP |
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; | |
3c4a4657 | 143 | } |
3a260ed6 | 144 | elseif (!empty($pinnedContactCountries)) { |
7b11bd27 | 145 | // if default country is missing then use only pinned countries at the top then rest of the countries. |
3a260ed6 | 146 | $availableCountries = $pinnedContactCountries + $availableCountries; |
3c4a4657 SP |
147 | } |
148 | ||
149 | return $availableCountries; | |
150 | } | |
151 | ||
0acb7f15 TO |
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 | * | |
ad37ac8e | 170 | * @param string $defaultCurrency |
171 | * | |
0acb7f15 TO |
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) { | |
be2fb01f | 179 | $currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', [ |
0acb7f15 TO |
180 | 'labelColumn' => 'symbol', |
181 | 'orderColumn' => TRUE, | |
be2fb01f | 182 | ]); |
0acb7f15 TO |
183 | $cachedSymbol = CRM_Utils_Array::value($currency, $currencySymbols, ''); |
184 | } | |
185 | else { | |
186 | $cachedSymbol = '$'; | |
187 | } | |
188 | } | |
189 | return $cachedSymbol; | |
190 | } | |
191 | ||
f2ac86d1 | 192 | /** |
193 | * Get the default currency symbol. | |
194 | * | |
195 | * @param string $k Unused variable | |
196 | * | |
197 | * @return string | |
198 | */ | |
d7c217ae | 199 | public static function getDefaultCurrencySymbol($k = NULL) { |
a5dfa653 | 200 | return CRM_Core_BAO_Country::defaultCurrencySymbol(\Civi::settings()->get('defaultCurrency')); |
d7c217ae TO |
201 | } |
202 | ||
0acb7f15 | 203 | } |