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