Feature to provide mostly used countries in top section of Country select list
[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 favourite contries.
99 *
100 * @param $availableCountries
101 * @return array
102 */
103 public static function favouriteContactCountries($availableCountries) {
104 static $cachedFavouriteContactCountries = [];
105 $favouriteContactCountries = Civi::settings()->get('favouriteContactCountries');
106
107 if (!empty($favouriteContactCountries) && !$cachedFavouriteContactCountries) {
108 $favouriteCountries = [];
109 foreach($favouriteContactCountries as $favouriteContactCountry) {
110 if (array_key_exists($favouriteContactCountry, $availableCountries)) {
111 $favouriteCountries[$favouriteContactCountry] = $availableCountries[$favouriteContactCountry];
112 }
113 }
114 $cachedFavouriteContactCountries = $favouriteCountries;
115 }
116 return $cachedFavouriteContactCountries;
117 }
118
119 /**
120 * Provide sorted list of countries with default country with first position
121 * then favourite countries then rest of countries.
122 *
123 * @param $availableCountries
124 * @return array
125 */
126 public static function _defaultContactCountries($availableCountries) {
127 // localise the country names if in an non-en_US locale
128 $tsLocale = CRM_Core_I18n::getLocale();
129 if ($tsLocale != '' and $tsLocale != 'en_US') {
130 $i18n = CRM_Core_I18n::singleton();
131 $i18n->localizeArray($availableCountries, [
132 'context' => 'country',
133 ]);
134 $availableCountries = CRM_Utils_Array::asort($availableCountries);
135 }
136 $favouriteContactCountries = CRM_Core_BAO_Country::favouriteContactCountries($availableCountries);
137 // if default country is set, percolate it to the top
138 if ($defaultContactCountry = CRM_Core_BAO_Country::defaultContactCountry()) {
139 $countryIsoCodes = CRM_Core_PseudoConstant::countryIsoCode();
140 $defaultID = array_search($defaultContactCountry, $countryIsoCodes);
141 if ($defaultID !== FALSE) {
142 $default = [];
143 $default[$defaultID] = $availableCountries[$defaultID] ?? NULL;
144 $availableCountries = $default + $favouriteContactCountries + $availableCountries;
145 }
146 }
147 elseif (!empty($favouriteContactCountries)) {
148 $availableCountries = $favouriteContactCountries + $availableCountries;
149 }
150
151 return $availableCountries;
152 }
153
154 /**
155 * Provide cached default country name.
156 *
157 * @return string
158 */
159 public static function defaultContactCountryName() {
160 static $cachedContactCountryName = NULL;
161 $defaultContactCountry = Civi::settings()->get('defaultContactCountry');
162 if (!$cachedContactCountryName && $defaultContactCountry) {
163 $countryCodes = CRM_Core_PseudoConstant::country();
164 $cachedContactCountryName = $countryCodes[$defaultContactCountry];
165 }
166 return $cachedContactCountryName;
167 }
168
169 /**
170 * Provide cached default currency symbol.
171 *
172 * @param string $defaultCurrency
173 *
174 * @return string
175 */
176 public static function defaultCurrencySymbol($defaultCurrency = NULL) {
177 static $cachedSymbol = NULL;
178 if (!$cachedSymbol || $defaultCurrency) {
179 $currency = $defaultCurrency ? $defaultCurrency : Civi::settings()->get('defaultCurrency');
180 if ($currency) {
181 $currencySymbols = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', [
182 'labelColumn' => 'symbol',
183 'orderColumn' => TRUE,
184 ]);
185 $cachedSymbol = CRM_Utils_Array::value($currency, $currencySymbols, '');
186 }
187 else {
188 $cachedSymbol = '$';
189 }
190 }
191 return $cachedSymbol;
192 }
193
194 /**
195 * Get the default currency symbol.
196 *
197 * @param string $k Unused variable
198 *
199 * @return string
200 */
201 public static function getDefaultCurrencySymbol($k = NULL) {
202 return CRM_Core_BAO_Country::defaultCurrencySymbol(\Civi::settings()->get('defaultCurrency'));
203 }
204
205 }