Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
50bfb460 | 29 | * Address Utilities |
6a488035 TO |
30 | * |
31 | * @package CRM | |
6b83d5bd | 32 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
33 | */ |
34 | class CRM_Utils_Address { | |
35 | ||
36 | /** | |
fe482240 | 37 | * Format an address string from address fields and a format string. |
6a488035 TO |
38 | * |
39 | * Format an address basing on the address fields provided. | |
40 | * Use Setting's address_format if there's no format specified. | |
41 | * | |
4c49535e J |
42 | * This function is also used to generate a contact's display_name and |
43 | * sort_name. | |
44 | * | |
77855840 TO |
45 | * @param array $fields |
46 | * The address fields. | |
47 | * @param string $format | |
48 | * The desired address format. | |
49 | * @param bool $microformat | |
50 | * If true indicates, the address to be built in hcard-microformat standard. | |
51 | * @param bool $mailing | |
52 | * If true indicates, the call has been made from mailing label. | |
f4aaa82a | 53 | * @param null $tokenFields |
6a488035 | 54 | * |
a6c01b45 CW |
55 | * @return string |
56 | * formatted address string | |
6a488035 | 57 | * |
6a488035 | 58 | */ |
389bcebf | 59 | public static function format( |
6a488035 | 60 | $fields, |
e7292422 TO |
61 | $format = NULL, |
62 | $microformat = FALSE, | |
63 | $mailing = FALSE, | |
e7292422 | 64 | $tokenFields = NULL |
6a488035 TO |
65 | ) { |
66 | static $config = NULL; | |
67 | ||
68 | if (!$format) { | |
aaffa79f | 69 | $format = Civi::settings()->get('address_format'); |
6a488035 TO |
70 | } |
71 | ||
72 | if ($mailing) { | |
aaffa79f | 73 | $format = Civi::settings()->get('mailing_format'); |
6a488035 TO |
74 | } |
75 | ||
76 | $formatted = $format; | |
77 | ||
78 | $fullPostalCode = CRM_Utils_Array::value('postal_code', $fields); | |
79 | if (!empty($fields['postal_code_suffix'])) { | |
80 | $fullPostalCode .= "-$fields[postal_code_suffix]"; | |
81 | } | |
82 | ||
83 | // make sure that some of the fields do have values | |
be2fb01f | 84 | $emptyFields = [ |
6a488035 TO |
85 | 'supplemental_address_1', |
86 | 'supplemental_address_2', | |
207f62c6 | 87 | 'supplemental_address_3', |
6a488035 TO |
88 | 'state_province_name', |
89 | 'county', | |
be2fb01f | 90 | ]; |
6a488035 TO |
91 | foreach ($emptyFields as $f) { |
92 | if (!isset($fields[$f])) { | |
93 | $fields[$f] = NULL; | |
94 | } | |
95 | } | |
96 | ||
61ca7d1f | 97 | //CRM-16876 Display countries in all caps when in mailing mode. |
04c56532 SL |
98 | if ($mailing && !empty($fields['country'])) { |
99 | if (Civi::settings()->get('hideCountryMailingLabels')) { | |
100 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
be2fb01f | 101 | $domainLocation = CRM_Core_BAO_Location::getValues(['contact_id' => $domain->contact_id]); |
04c56532 SL |
102 | $domainAddress = $domainLocation['address'][1]; |
103 | $domainCountryId = $domainAddress['country_id']; | |
104 | if ($fields['country'] == CRM_Core_PseudoConstant::country($domainCountryId)) { | |
105 | $fields['country'] = NULL; | |
106 | } | |
e22e10f5 | 107 | else { |
e8bbf756 | 108 | //Capitalization display on uppercase to contries with special characters |
ed3d77d5 | 109 | $fields['country'] = mb_convert_case($fields['country'], MB_CASE_UPPER, "UTF-8"); |
e22e10f5 | 110 | } |
04c56532 SL |
111 | } |
112 | else { | |
4c733767 | 113 | $fields['country'] = mb_convert_case($fields['country'], MB_CASE_UPPER, "UTF-8"); |
04c56532 SL |
114 | } |
115 | } | |
116 | ||
6a488035 | 117 | if (!$microformat) { |
e7292422 | 118 | // replacements in case of Individual Name Format |
be2fb01f | 119 | $replacements = [ |
6a488035 TO |
120 | 'contact.display_name' => CRM_Utils_Array::value('display_name', $fields), |
121 | 'contact.individual_prefix' => CRM_Utils_Array::value('individual_prefix', $fields), | |
e171748b | 122 | 'contact.formal_title' => CRM_Utils_Array::value('formal_title', $fields), |
6a488035 TO |
123 | 'contact.first_name' => CRM_Utils_Array::value('first_name', $fields), |
124 | 'contact.middle_name' => CRM_Utils_Array::value('middle_name', $fields), | |
125 | 'contact.last_name' => CRM_Utils_Array::value('last_name', $fields), | |
126 | 'contact.individual_suffix' => CRM_Utils_Array::value('individual_suffix', $fields), | |
127 | 'contact.address_name' => CRM_Utils_Array::value('address_name', $fields), | |
128 | 'contact.street_address' => CRM_Utils_Array::value('street_address', $fields), | |
129 | 'contact.supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $fields), | |
130 | 'contact.supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $fields), | |
207f62c6 | 131 | 'contact.supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', $fields), |
6a488035 TO |
132 | 'contact.city' => CRM_Utils_Array::value('city', $fields), |
133 | 'contact.state_province_name' => CRM_Utils_Array::value('state_province_name', $fields), | |
134 | 'contact.county' => CRM_Utils_Array::value('county', $fields), | |
135 | 'contact.state_province' => CRM_Utils_Array::value('state_province', $fields), | |
136 | 'contact.postal_code' => $fullPostalCode, | |
137 | 'contact.country' => CRM_Utils_Array::value('country', $fields), | |
138 | 'contact.world_region' => CRM_Utils_Array::value('world_region', $fields), | |
139 | 'contact.geo_code_1' => CRM_Utils_Array::value('geo_code_1', $fields), | |
140 | 'contact.geo_code_2' => CRM_Utils_Array::value('geo_code_2', $fields), | |
141 | 'contact.current_employer' => CRM_Utils_Array::value('current_employer', $fields), | |
142 | 'contact.nick_name' => CRM_Utils_Array::value('nick_name', $fields), | |
143 | 'contact.email' => CRM_Utils_Array::value('email', $fields), | |
144 | 'contact.im' => CRM_Utils_Array::value('im', $fields), | |
145 | 'contact.do_not_email' => CRM_Utils_Array::value('do_not_email', $fields), | |
146 | 'contact.do_not_phone' => CRM_Utils_Array::value('do_not_phone', $fields), | |
147 | 'contact.do_not_mail' => CRM_Utils_Array::value('do_not_mail', $fields), | |
148 | 'contact.do_not_sms' => CRM_Utils_Array::value('do_not_sms', $fields), | |
149 | 'contact.do_not_trade' => CRM_Utils_Array::value('do_not_trade', $fields), | |
150 | 'contact.job_title' => CRM_Utils_Array::value('job_title', $fields), | |
151 | 'contact.birth_date' => CRM_Utils_Array::value('birth_date', $fields), | |
152 | 'contact.gender' => CRM_Utils_Array::value('gender', $fields), | |
153 | 'contact.is_opt_out' => CRM_Utils_Array::value('is_opt_out', $fields), | |
6a488035 TO |
154 | 'contact.preferred_mail_format' => CRM_Utils_Array::value('preferred_mail_format', $fields), |
155 | 'contact.phone' => CRM_Utils_Array::value('phone', $fields), | |
156 | 'contact.home_URL' => CRM_Utils_Array::value('home_URL', $fields), | |
157 | 'contact.contact_source' => CRM_Utils_Array::value('contact_source', $fields), | |
158 | 'contact.external_identifier' => CRM_Utils_Array::value('external_identifier', $fields), | |
159 | 'contact.contact_id' => CRM_Utils_Array::value('id', $fields), | |
160 | 'contact.household_name' => CRM_Utils_Array::value('display_name', $fields), | |
161 | 'contact.organization_name' => CRM_Utils_Array::value('display_name', $fields), | |
162 | 'contact.legal_name' => CRM_Utils_Array::value('legal_name', $fields), | |
163 | 'contact.preferred_communication_method' => CRM_Utils_Array::value('preferred_communication_method', $fields), | |
aa62b355 | 164 | 'contact.communication_style' => CRM_Utils_Array::value('communication_style', $fields), |
6a488035 TO |
165 | 'contact.addressee' => CRM_Utils_Array::value('addressee_display', $fields), |
166 | 'contact.email_greeting' => CRM_Utils_Array::value('email_greeting_display', $fields), | |
167 | 'contact.postal_greeting' => CRM_Utils_Array::value('postal_greeting_display', $fields), | |
be2fb01f | 168 | ]; |
6a488035 TO |
169 | } |
170 | else { | |
be2fb01f | 171 | $replacements = [ |
6a488035 TO |
172 | 'contact.address_name' => "<span class=\"address-name\">" . $fields['address_name'] . "</span>", |
173 | 'contact.street_address' => "<span class=\"street-address\">" . $fields['street_address'] . "</span>", | |
174 | 'contact.supplemental_address_1' => "<span class=\"extended-address\">" . $fields['supplemental_address_1'] . "</span>", | |
175 | 'contact.supplemental_address_2' => $fields['supplemental_address_2'], | |
207f62c6 | 176 | 'contact.supplemental_address_3' => $fields['supplemental_address_3'], |
6a488035 TO |
177 | 'contact.city' => "<span class=\"locality\">" . $fields['city'] . "</span>", |
178 | 'contact.state_province_name' => "<span class=\"region\">" . $fields['state_province_name'] . "</span>", | |
179 | 'contact.county' => "<span class=\"region\">" . $fields['county'], | |
180 | 'contact.state_province' => "<span class=\"region\">" . $fields['state_province'] . "</span>", | |
181 | 'contact.postal_code' => "<span class=\"postal-code\">" . $fullPostalCode . "</span>", | |
182 | 'contact.country' => "<span class=\"country-name\">" . $fields['country'] . "</span>", | |
183 | 'contact.world_region' => "<span class=\"region\">" . $fields['world_region'] . "</span>", | |
be2fb01f | 184 | ]; |
6a488035 TO |
185 | |
186 | // erase all empty ones, so we dont get blank lines | |
187 | foreach (array_keys($replacements) as $key) { | |
188 | $exactKey = substr($key, 0, 8) == 'contact.' ? substr($key, 8) : $key; | |
189 | if ($key != 'contact.postal_code' && | |
190 | CRM_Utils_Array::value($exactKey, $fields) == NULL | |
191 | ) { | |
192 | $replacements[$key] = ''; | |
193 | } | |
194 | } | |
195 | if (empty($fullPostalCode)) { | |
196 | $replacements['contact.postal_code'] = ''; | |
197 | } | |
198 | } | |
199 | ||
200 | // replacements in case of Custom Token | |
201 | if (stristr($formatted, 'custom_')) { | |
202 | $customToken = array_keys($fields); | |
203 | foreach ($customToken as $value) { | |
204 | if (substr($value, 0, 7) == 'custom_') { | |
205 | $replacements["contact.{$value}"] = $fields["{$value}"]; | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
210 | // also sub all token fields | |
211 | if ($tokenFields) { | |
212 | foreach ($tokenFields as $token) { | |
213 | $replacements["{$token}"] = CRM_Utils_Array::value("{$token}", $fields); | |
214 | } | |
215 | } | |
216 | ||
217 | // for every token, replace {fooTOKENbar} with fooVALUEbar if | |
218 | // the value is not empty, otherwise drop the whole {fooTOKENbar} | |
219 | foreach ($replacements as $token => $value) { | |
785374a6 | 220 | if ($value && is_string($value) || is_numeric($value)) { |
6a488035 TO |
221 | $formatted = preg_replace("/{([^{}]*)\b{$token}\b([^{}]*)}/u", "\${1}{$value}\${2}", $formatted); |
222 | } | |
223 | else { | |
224 | $formatted = preg_replace("/{[^{}]*\b{$token}\b[^{}]*}/u", '', $formatted); | |
225 | } | |
226 | } | |
227 | ||
228 | // drop any {...} constructs from lines' ends | |
229 | if (!$microformat) { | |
230 | $formatted = "\n$formatted\n"; | |
231 | } | |
232 | else { | |
233 | if ($microformat == 1) { | |
234 | $formatted = "\n<div class=\"location vcard\"><span class=\"adr\">\n$formatted</span></div>\n"; | |
235 | } | |
236 | else { | |
237 | $formatted = "\n<div class=\"vcard\"><span class=\"adr\">$formatted</span></div>\n"; | |
238 | } | |
239 | } | |
240 | ||
241 | $formatted = preg_replace('/\n{[^{}]*}/u', "\n", $formatted); | |
242 | $formatted = preg_replace('/{[^{}]*}\n/u', "\n", $formatted); | |
243 | ||
244 | // if there are any 'sibling' {...} constructs, replace them with the | |
245 | // contents of the first one; for example, when there's no state_province: | |
246 | // 1. {city}{, }{state_province}{ }{postal_code} | |
247 | // 2. San Francisco{, }{ }12345 | |
248 | // 3. San Francisco, 12345 | |
249 | $formatted = preg_replace('/{([^{}]*)}({[^{}]*})+/u', '\1', $formatted); | |
250 | ||
251 | // drop any remaining curly braces leaving their contents | |
be2fb01f | 252 | $formatted = str_replace(['{', '}'], '', $formatted); |
6a488035 TO |
253 | |
254 | // drop any empty lines left after the replacements | |
255 | $formatted = preg_replace('/^[ \t]*[\r\n]+/m', '', $formatted); | |
256 | ||
257 | if (!$microformat) { | |
258 | $finalFormatted = $formatted; | |
259 | } | |
260 | else { | |
261 | // remove \n from each line and only add at the end | |
262 | // this hack solves formatting issue, when we convert nl2br | |
be2fb01f | 263 | $lines = []; |
353ffa53 | 264 | $count = 1; |
6a488035 TO |
265 | $finalFormatted = NULL; |
266 | $formattedArray = explode("\n", $formatted); | |
267 | $formattedArray = array_filter($formattedArray); | |
268 | ||
269 | foreach ($formattedArray as $line) { | |
270 | $line = trim($line); | |
271 | if ($line) { | |
272 | if ($count > 1 && $count < count($formattedArray)) { | |
273 | $line = "$line\n"; | |
274 | } | |
275 | $finalFormatted .= $line; | |
276 | $count++; | |
277 | } | |
278 | } | |
279 | } | |
280 | return $finalFormatted; | |
281 | } | |
282 | ||
5bc392e6 EM |
283 | /** |
284 | * @param $format | |
285 | * | |
286 | * @return array | |
287 | */ | |
00be9182 | 288 | public static function sequence($format) { |
6a488035 | 289 | // also compute and store the address sequence |
be2fb01f | 290 | $addressSequence = [ |
6a488035 TO |
291 | 'address_name', |
292 | 'street_address', | |
293 | 'supplemental_address_1', | |
294 | 'supplemental_address_2', | |
207f62c6 | 295 | 'supplemental_address_3', |
6a488035 TO |
296 | 'city', |
297 | 'county', | |
298 | 'state_province', | |
299 | 'postal_code', | |
300 | 'country', | |
be2fb01f | 301 | ]; |
6a488035 TO |
302 | |
303 | // get the field sequence from the format | |
be2fb01f | 304 | $newSequence = []; |
6a488035 TO |
305 | foreach ($addressSequence as $field) { |
306 | if (substr_count($format, $field)) { | |
307 | $newSequence[strpos($format, $field)] = $field; | |
308 | } | |
309 | } | |
310 | ksort($newSequence); | |
311 | ||
312 | // add the addressSequence fields that are missing in the addressFormat | |
313 | // to the end of the list, so that (for example) if state_province is not | |
314 | // specified in the addressFormat it's still in the address-editing form | |
315 | $newSequence = array_merge($newSequence, $addressSequence); | |
316 | $newSequence = array_unique($newSequence); | |
317 | return $newSequence; | |
318 | } | |
96025800 | 319 | |
d90ab525 SL |
320 | /** |
321 | * Extract the billing fields from the form submission and format them for display. | |
322 | * | |
323 | * @param array $params | |
324 | * @param int $billingLocationTypeID | |
325 | * | |
326 | * @return string | |
327 | */ | |
328 | public static function getFormattedBillingAddressFieldsFromParameters($params, $billingLocationTypeID) { | |
be2fb01f | 329 | $addressParts = [ |
d90ab525 SL |
330 | "street_address" => "billing_street_address-{$billingLocationTypeID}", |
331 | "city" => "billing_city-{$billingLocationTypeID}", | |
332 | "postal_code" => "billing_postal_code-{$billingLocationTypeID}", | |
333 | "state_province" => "state_province-{$billingLocationTypeID}", | |
334 | "country" => "country-{$billingLocationTypeID}", | |
be2fb01f | 335 | ]; |
d90ab525 | 336 | |
be2fb01f | 337 | $addressFields = []; |
d90ab525 | 338 | foreach ($addressParts as $name => $field) { |
9c86599c | 339 | $value = CRM_Utils_Array::value($field, $params); |
340 | $alternateName = 'billing_' . $name . '_id-' . $billingLocationTypeID; | |
341 | $alternate2 = 'billing_' . $name . '-' . $billingLocationTypeID; | |
342 | if (isset($params[$alternate2]) && !isset($params[$alternateName])) { | |
343 | $alternateName = $alternate2; | |
344 | } | |
dd22a911 | 345 | //Include values which prepend 'billing_' to country and state_province. |
9c86599c | 346 | if (CRM_Utils_Array::value($alternateName, $params)) { |
347 | if (empty($value) || !is_numeric($value)) { | |
348 | $value = $params[$alternateName]; | |
349 | } | |
350 | } | |
8568b05c | 351 | if (is_numeric($value) && ($name == 'state_province' || $name == 'country')) { |
9c86599c | 352 | if ($name == 'state_province') { |
353 | $addressFields[$name] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); | |
8568b05c | 354 | $addressFields[$name . '_name'] = CRM_Core_PseudoConstant::stateProvince($value); |
b6fe7f06 | 355 | } |
9c86599c | 356 | if ($name == 'country') { |
357 | $addressFields[$name] = CRM_Core_PseudoConstant::countryIsoCode($value); | |
358 | } | |
359 | } | |
360 | else { | |
361 | $addressFields[$name] = $value; | |
dd22a911 | 362 | } |
d90ab525 SL |
363 | } |
364 | return CRM_Utils_Address::format($addressFields); | |
365 | } | |
366 | ||
6a488035 | 367 | } |