Merge pull request #9065 from seamuslee001/hide_downloaded_extensions
[civicrm-core.git] / CRM / Utils / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 32 * @copyright CiviCRM LLC (c) 2004-2016
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 }
107 }
108 else {
109 $fields['country'] = strtoupper($fields['country']);
110 }
111 }
112
6a488035
TO
113 $contactName = CRM_Utils_Array::value('display_name', $fields);
114 if (!$individualFormat) {
115 if (isset($fields['id'])) {
116 $type = CRM_Contact_BAO_Contact::getContactType($fields['id']);
117 }
118 else {
119 $type = 'Individual';
120 }
121
122 if ($type == 'Individual') {
123 $contactName = CRM_Utils_Array::value('addressee_display', $fields);
124 }
125 }
126
127 if (!$microformat) {
e7292422 128 // replacements in case of Individual Name Format
6a488035
TO
129 $replacements = array(
130 'contact.display_name' => CRM_Utils_Array::value('display_name', $fields),
131 'contact.individual_prefix' => CRM_Utils_Array::value('individual_prefix', $fields),
e171748b 132 'contact.formal_title' => CRM_Utils_Array::value('formal_title', $fields),
6a488035
TO
133 'contact.first_name' => CRM_Utils_Array::value('first_name', $fields),
134 'contact.middle_name' => CRM_Utils_Array::value('middle_name', $fields),
135 'contact.last_name' => CRM_Utils_Array::value('last_name', $fields),
136 'contact.individual_suffix' => CRM_Utils_Array::value('individual_suffix', $fields),
137 'contact.address_name' => CRM_Utils_Array::value('address_name', $fields),
138 'contact.street_address' => CRM_Utils_Array::value('street_address', $fields),
139 'contact.supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $fields),
140 'contact.supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $fields),
141 'contact.city' => CRM_Utils_Array::value('city', $fields),
142 'contact.state_province_name' => CRM_Utils_Array::value('state_province_name', $fields),
143 'contact.county' => CRM_Utils_Array::value('county', $fields),
144 'contact.state_province' => CRM_Utils_Array::value('state_province', $fields),
145 'contact.postal_code' => $fullPostalCode,
146 'contact.country' => CRM_Utils_Array::value('country', $fields),
147 'contact.world_region' => CRM_Utils_Array::value('world_region', $fields),
148 'contact.geo_code_1' => CRM_Utils_Array::value('geo_code_1', $fields),
149 'contact.geo_code_2' => CRM_Utils_Array::value('geo_code_2', $fields),
150 'contact.current_employer' => CRM_Utils_Array::value('current_employer', $fields),
151 'contact.nick_name' => CRM_Utils_Array::value('nick_name', $fields),
152 'contact.email' => CRM_Utils_Array::value('email', $fields),
153 'contact.im' => CRM_Utils_Array::value('im', $fields),
154 'contact.do_not_email' => CRM_Utils_Array::value('do_not_email', $fields),
155 'contact.do_not_phone' => CRM_Utils_Array::value('do_not_phone', $fields),
156 'contact.do_not_mail' => CRM_Utils_Array::value('do_not_mail', $fields),
157 'contact.do_not_sms' => CRM_Utils_Array::value('do_not_sms', $fields),
158 'contact.do_not_trade' => CRM_Utils_Array::value('do_not_trade', $fields),
159 'contact.job_title' => CRM_Utils_Array::value('job_title', $fields),
160 'contact.birth_date' => CRM_Utils_Array::value('birth_date', $fields),
161 'contact.gender' => CRM_Utils_Array::value('gender', $fields),
162 'contact.is_opt_out' => CRM_Utils_Array::value('is_opt_out', $fields),
6a488035
TO
163 'contact.preferred_mail_format' => CRM_Utils_Array::value('preferred_mail_format', $fields),
164 'contact.phone' => CRM_Utils_Array::value('phone', $fields),
165 'contact.home_URL' => CRM_Utils_Array::value('home_URL', $fields),
166 'contact.contact_source' => CRM_Utils_Array::value('contact_source', $fields),
167 'contact.external_identifier' => CRM_Utils_Array::value('external_identifier', $fields),
168 'contact.contact_id' => CRM_Utils_Array::value('id', $fields),
169 'contact.household_name' => CRM_Utils_Array::value('display_name', $fields),
170 'contact.organization_name' => CRM_Utils_Array::value('display_name', $fields),
171 'contact.legal_name' => CRM_Utils_Array::value('legal_name', $fields),
172 'contact.preferred_communication_method' => CRM_Utils_Array::value('preferred_communication_method', $fields),
aa62b355 173 'contact.communication_style' => CRM_Utils_Array::value('communication_style', $fields),
6a488035
TO
174 'contact.addressee' => CRM_Utils_Array::value('addressee_display', $fields),
175 'contact.email_greeting' => CRM_Utils_Array::value('email_greeting_display', $fields),
176 'contact.postal_greeting' => CRM_Utils_Array::value('postal_greeting_display', $fields),
177 );
178 }
179 else {
180 $replacements = array(
181 'contact.address_name' => "<span class=\"address-name\">" . $fields['address_name'] . "</span>",
182 'contact.street_address' => "<span class=\"street-address\">" . $fields['street_address'] . "</span>",
183 'contact.supplemental_address_1' => "<span class=\"extended-address\">" . $fields['supplemental_address_1'] . "</span>",
184 'contact.supplemental_address_2' => $fields['supplemental_address_2'],
185 'contact.city' => "<span class=\"locality\">" . $fields['city'] . "</span>",
186 'contact.state_province_name' => "<span class=\"region\">" . $fields['state_province_name'] . "</span>",
187 'contact.county' => "<span class=\"region\">" . $fields['county'],
188 'contact.state_province' => "<span class=\"region\">" . $fields['state_province'] . "</span>",
189 'contact.postal_code' => "<span class=\"postal-code\">" . $fullPostalCode . "</span>",
190 'contact.country' => "<span class=\"country-name\">" . $fields['country'] . "</span>",
191 'contact.world_region' => "<span class=\"region\">" . $fields['world_region'] . "</span>",
192 );
193
194 // erase all empty ones, so we dont get blank lines
195 foreach (array_keys($replacements) as $key) {
196 $exactKey = substr($key, 0, 8) == 'contact.' ? substr($key, 8) : $key;
197 if ($key != 'contact.postal_code' &&
198 CRM_Utils_Array::value($exactKey, $fields) == NULL
199 ) {
200 $replacements[$key] = '';
201 }
202 }
203 if (empty($fullPostalCode)) {
204 $replacements['contact.postal_code'] = '';
205 }
206 }
207
208 // replacements in case of Custom Token
209 if (stristr($formatted, 'custom_')) {
210 $customToken = array_keys($fields);
211 foreach ($customToken as $value) {
212 if (substr($value, 0, 7) == 'custom_') {
213 $replacements["contact.{$value}"] = $fields["{$value}"];
214 }
215 }
216 }
217
218 // also sub all token fields
219 if ($tokenFields) {
220 foreach ($tokenFields as $token) {
221 $replacements["{$token}"] = CRM_Utils_Array::value("{$token}", $fields);
222 }
223 }
224
225 // for every token, replace {fooTOKENbar} with fooVALUEbar if
226 // the value is not empty, otherwise drop the whole {fooTOKENbar}
227 foreach ($replacements as $token => $value) {
785374a6 228 if ($value && is_string($value) || is_numeric($value)) {
6a488035
TO
229 $formatted = preg_replace("/{([^{}]*)\b{$token}\b([^{}]*)}/u", "\${1}{$value}\${2}", $formatted);
230 }
231 else {
232 $formatted = preg_replace("/{[^{}]*\b{$token}\b[^{}]*}/u", '', $formatted);
233 }
234 }
235
236 // drop any {...} constructs from lines' ends
237 if (!$microformat) {
238 $formatted = "\n$formatted\n";
239 }
240 else {
241 if ($microformat == 1) {
242 $formatted = "\n<div class=\"location vcard\"><span class=\"adr\">\n$formatted</span></div>\n";
243 }
244 else {
245 $formatted = "\n<div class=\"vcard\"><span class=\"adr\">$formatted</span></div>\n";
246 }
247 }
248
249 $formatted = preg_replace('/\n{[^{}]*}/u', "\n", $formatted);
250 $formatted = preg_replace('/{[^{}]*}\n/u', "\n", $formatted);
251
252 // if there are any 'sibling' {...} constructs, replace them with the
253 // contents of the first one; for example, when there's no state_province:
254 // 1. {city}{, }{state_province}{ }{postal_code}
255 // 2. San Francisco{, }{ }12345
256 // 3. San Francisco, 12345
257 $formatted = preg_replace('/{([^{}]*)}({[^{}]*})+/u', '\1', $formatted);
258
259 // drop any remaining curly braces leaving their contents
260 $formatted = str_replace(array('{', '}'), '', $formatted);
261
262 // drop any empty lines left after the replacements
263 $formatted = preg_replace('/^[ \t]*[\r\n]+/m', '', $formatted);
264
265 if (!$microformat) {
266 $finalFormatted = $formatted;
267 }
268 else {
269 // remove \n from each line and only add at the end
270 // this hack solves formatting issue, when we convert nl2br
353ffa53
TO
271 $lines = array();
272 $count = 1;
6a488035
TO
273 $finalFormatted = NULL;
274 $formattedArray = explode("\n", $formatted);
275 $formattedArray = array_filter($formattedArray);
276
277 foreach ($formattedArray as $line) {
278 $line = trim($line);
279 if ($line) {
280 if ($count > 1 && $count < count($formattedArray)) {
281 $line = "$line\n";
282 }
283 $finalFormatted .= $line;
284 $count++;
285 }
286 }
287 }
288 return $finalFormatted;
289 }
290
5bc392e6
EM
291 /**
292 * @param $format
293 *
294 * @return array
295 */
00be9182 296 public static function sequence($format) {
6a488035
TO
297 // also compute and store the address sequence
298 $addressSequence = array(
299 'address_name',
300 'street_address',
301 'supplemental_address_1',
302 'supplemental_address_2',
303 'city',
304 'county',
305 'state_province',
306 'postal_code',
307 'country',
308 );
309
310 // get the field sequence from the format
311 $newSequence = array();
312 foreach ($addressSequence as $field) {
313 if (substr_count($format, $field)) {
314 $newSequence[strpos($format, $field)] = $field;
315 }
316 }
317 ksort($newSequence);
318
319 // add the addressSequence fields that are missing in the addressFormat
320 // to the end of the list, so that (for example) if state_province is not
321 // specified in the addressFormat it's still in the address-editing form
322 $newSequence = array_merge($newSequence, $addressSequence);
323 $newSequence = array_unique($newSequence);
324 return $newSequence;
325 }
96025800 326
6a488035 327}