Merge pull request #4973 from PalanteJon/cleanup-master
[civicrm-core.git] / CRM / Utils / Address.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * Address utilties
30 *
31 * @package CRM
32 */
33class CRM_Utils_Address {
34
35 /**
100fef9d 36 * Format an address string from address fields and a format string
6a488035
TO
37 *
38 * Format an address basing on the address fields provided.
39 * Use Setting's address_format if there's no format specified.
40 *
77855840
TO
41 * @param array $fields
42 * The address fields.
43 * @param string $format
44 * The desired address format.
45 * @param bool $microformat
46 * If true indicates, the address to be built in hcard-microformat standard.
47 * @param bool $mailing
48 * If true indicates, the call has been made from mailing label.
49 * @param bool $individualFormat
50 * If true indicates, the call has been made for the contact of type 'individual'.
f4aaa82a
EM
51 *
52 * @param null $tokenFields
6a488035 53 *
a6c01b45
CW
54 * @return string
55 * formatted address string
6a488035 56 *
6a488035
TO
57 */
58 static function format(
59 $fields,
e7292422
TO
60 $format = NULL,
61 $microformat = FALSE,
62 $mailing = FALSE,
6a488035 63 $individualFormat = FALSE,
e7292422 64 $tokenFields = NULL
6a488035
TO
65 ) {
66 static $config = NULL;
67
68 if (!$format) {
69 $format =
70 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_format');
71 }
72
73 if ($mailing) {
74 $format =
75 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'mailing_format');
76 }
77
78 $formatted = $format;
79
80 $fullPostalCode = CRM_Utils_Array::value('postal_code', $fields);
81 if (!empty($fields['postal_code_suffix'])) {
82 $fullPostalCode .= "-$fields[postal_code_suffix]";
83 }
84
85 // make sure that some of the fields do have values
86 $emptyFields = array(
87 'supplemental_address_1',
88 'supplemental_address_2',
89 'state_province_name',
90 'county',
91 );
92 foreach ($emptyFields as $f) {
93 if (!isset($fields[$f])) {
94 $fields[$f] = NULL;
95 }
96 }
97
98 $contactName = CRM_Utils_Array::value('display_name', $fields);
99 if (!$individualFormat) {
100 if (isset($fields['id'])) {
101 $type = CRM_Contact_BAO_Contact::getContactType($fields['id']);
102 }
103 else {
104 $type = 'Individual';
105 }
106
107 if ($type == 'Individual') {
108 $contactName = CRM_Utils_Array::value('addressee_display', $fields);
109 }
110 }
111
112 if (!$microformat) {
e7292422 113 // replacements in case of Individual Name Format
6a488035
TO
114 $replacements = array(
115 'contact.display_name' => CRM_Utils_Array::value('display_name', $fields),
116 'contact.individual_prefix' => CRM_Utils_Array::value('individual_prefix', $fields),
e171748b 117 'contact.formal_title' => CRM_Utils_Array::value('formal_title', $fields),
6a488035
TO
118 'contact.first_name' => CRM_Utils_Array::value('first_name', $fields),
119 'contact.middle_name' => CRM_Utils_Array::value('middle_name', $fields),
120 'contact.last_name' => CRM_Utils_Array::value('last_name', $fields),
121 'contact.individual_suffix' => CRM_Utils_Array::value('individual_suffix', $fields),
122 'contact.address_name' => CRM_Utils_Array::value('address_name', $fields),
123 'contact.street_address' => CRM_Utils_Array::value('street_address', $fields),
124 'contact.supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $fields),
125 'contact.supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $fields),
126 'contact.city' => CRM_Utils_Array::value('city', $fields),
127 'contact.state_province_name' => CRM_Utils_Array::value('state_province_name', $fields),
128 'contact.county' => CRM_Utils_Array::value('county', $fields),
129 'contact.state_province' => CRM_Utils_Array::value('state_province', $fields),
130 'contact.postal_code' => $fullPostalCode,
131 'contact.country' => CRM_Utils_Array::value('country', $fields),
132 'contact.world_region' => CRM_Utils_Array::value('world_region', $fields),
133 'contact.geo_code_1' => CRM_Utils_Array::value('geo_code_1', $fields),
134 'contact.geo_code_2' => CRM_Utils_Array::value('geo_code_2', $fields),
135 'contact.current_employer' => CRM_Utils_Array::value('current_employer', $fields),
136 'contact.nick_name' => CRM_Utils_Array::value('nick_name', $fields),
137 'contact.email' => CRM_Utils_Array::value('email', $fields),
138 'contact.im' => CRM_Utils_Array::value('im', $fields),
139 'contact.do_not_email' => CRM_Utils_Array::value('do_not_email', $fields),
140 'contact.do_not_phone' => CRM_Utils_Array::value('do_not_phone', $fields),
141 'contact.do_not_mail' => CRM_Utils_Array::value('do_not_mail', $fields),
142 'contact.do_not_sms' => CRM_Utils_Array::value('do_not_sms', $fields),
143 'contact.do_not_trade' => CRM_Utils_Array::value('do_not_trade', $fields),
144 'contact.job_title' => CRM_Utils_Array::value('job_title', $fields),
145 'contact.birth_date' => CRM_Utils_Array::value('birth_date', $fields),
146 'contact.gender' => CRM_Utils_Array::value('gender', $fields),
147 'contact.is_opt_out' => CRM_Utils_Array::value('is_opt_out', $fields),
6a488035
TO
148 'contact.preferred_mail_format' => CRM_Utils_Array::value('preferred_mail_format', $fields),
149 'contact.phone' => CRM_Utils_Array::value('phone', $fields),
150 'contact.home_URL' => CRM_Utils_Array::value('home_URL', $fields),
151 'contact.contact_source' => CRM_Utils_Array::value('contact_source', $fields),
152 'contact.external_identifier' => CRM_Utils_Array::value('external_identifier', $fields),
153 'contact.contact_id' => CRM_Utils_Array::value('id', $fields),
154 'contact.household_name' => CRM_Utils_Array::value('display_name', $fields),
155 'contact.organization_name' => CRM_Utils_Array::value('display_name', $fields),
156 'contact.legal_name' => CRM_Utils_Array::value('legal_name', $fields),
157 'contact.preferred_communication_method' => CRM_Utils_Array::value('preferred_communication_method', $fields),
aa62b355 158 'contact.communication_style' => CRM_Utils_Array::value('communication_style', $fields),
6a488035
TO
159 'contact.addressee' => CRM_Utils_Array::value('addressee_display', $fields),
160 'contact.email_greeting' => CRM_Utils_Array::value('email_greeting_display', $fields),
161 'contact.postal_greeting' => CRM_Utils_Array::value('postal_greeting_display', $fields),
162 );
163 }
164 else {
165 $replacements = array(
166 'contact.address_name' => "<span class=\"address-name\">" . $fields['address_name'] . "</span>",
167 'contact.street_address' => "<span class=\"street-address\">" . $fields['street_address'] . "</span>",
168 'contact.supplemental_address_1' => "<span class=\"extended-address\">" . $fields['supplemental_address_1'] . "</span>",
169 'contact.supplemental_address_2' => $fields['supplemental_address_2'],
170 'contact.city' => "<span class=\"locality\">" . $fields['city'] . "</span>",
171 'contact.state_province_name' => "<span class=\"region\">" . $fields['state_province_name'] . "</span>",
172 'contact.county' => "<span class=\"region\">" . $fields['county'],
173 'contact.state_province' => "<span class=\"region\">" . $fields['state_province'] . "</span>",
174 'contact.postal_code' => "<span class=\"postal-code\">" . $fullPostalCode . "</span>",
175 'contact.country' => "<span class=\"country-name\">" . $fields['country'] . "</span>",
176 'contact.world_region' => "<span class=\"region\">" . $fields['world_region'] . "</span>",
177 );
178
179 // erase all empty ones, so we dont get blank lines
180 foreach (array_keys($replacements) as $key) {
181 $exactKey = substr($key, 0, 8) == 'contact.' ? substr($key, 8) : $key;
182 if ($key != 'contact.postal_code' &&
183 CRM_Utils_Array::value($exactKey, $fields) == NULL
184 ) {
185 $replacements[$key] = '';
186 }
187 }
188 if (empty($fullPostalCode)) {
189 $replacements['contact.postal_code'] = '';
190 }
191 }
192
193 // replacements in case of Custom Token
194 if (stristr($formatted, 'custom_')) {
195 $customToken = array_keys($fields);
196 foreach ($customToken as $value) {
197 if (substr($value, 0, 7) == 'custom_') {
198 $replacements["contact.{$value}"] = $fields["{$value}"];
199 }
200 }
201 }
202
203 // also sub all token fields
204 if ($tokenFields) {
205 foreach ($tokenFields as $token) {
206 $replacements["{$token}"] = CRM_Utils_Array::value("{$token}", $fields);
207 }
208 }
209
210 // for every token, replace {fooTOKENbar} with fooVALUEbar if
211 // the value is not empty, otherwise drop the whole {fooTOKENbar}
212 foreach ($replacements as $token => $value) {
785374a6 213 if ($value && is_string($value) || is_numeric($value)) {
6a488035
TO
214 $formatted = preg_replace("/{([^{}]*)\b{$token}\b([^{}]*)}/u", "\${1}{$value}\${2}", $formatted);
215 }
216 else {
217 $formatted = preg_replace("/{[^{}]*\b{$token}\b[^{}]*}/u", '', $formatted);
218 }
219 }
220
221 // drop any {...} constructs from lines' ends
222 if (!$microformat) {
223 $formatted = "\n$formatted\n";
224 }
225 else {
226 if ($microformat == 1) {
227 $formatted = "\n<div class=\"location vcard\"><span class=\"adr\">\n$formatted</span></div>\n";
228 }
229 else {
230 $formatted = "\n<div class=\"vcard\"><span class=\"adr\">$formatted</span></div>\n";
231 }
232 }
233
234 $formatted = preg_replace('/\n{[^{}]*}/u', "\n", $formatted);
235 $formatted = preg_replace('/{[^{}]*}\n/u', "\n", $formatted);
236
237 // if there are any 'sibling' {...} constructs, replace them with the
238 // contents of the first one; for example, when there's no state_province:
239 // 1. {city}{, }{state_province}{ }{postal_code}
240 // 2. San Francisco{, }{ }12345
241 // 3. San Francisco, 12345
242 $formatted = preg_replace('/{([^{}]*)}({[^{}]*})+/u', '\1', $formatted);
243
244 // drop any remaining curly braces leaving their contents
245 $formatted = str_replace(array('{', '}'), '', $formatted);
246
247 // drop any empty lines left after the replacements
248 $formatted = preg_replace('/^[ \t]*[\r\n]+/m', '', $formatted);
249
250 if (!$microformat) {
251 $finalFormatted = $formatted;
252 }
253 else {
254 // remove \n from each line and only add at the end
255 // this hack solves formatting issue, when we convert nl2br
353ffa53
TO
256 $lines = array();
257 $count = 1;
6a488035
TO
258 $finalFormatted = NULL;
259 $formattedArray = explode("\n", $formatted);
260 $formattedArray = array_filter($formattedArray);
261
262 foreach ($formattedArray as $line) {
263 $line = trim($line);
264 if ($line) {
265 if ($count > 1 && $count < count($formattedArray)) {
266 $line = "$line\n";
267 }
268 $finalFormatted .= $line;
269 $count++;
270 }
271 }
272 }
273 return $finalFormatted;
274 }
275
5bc392e6
EM
276 /**
277 * @param $format
278 *
279 * @return array
280 */
00be9182 281 public static function sequence($format) {
6a488035
TO
282 // also compute and store the address sequence
283 $addressSequence = array(
284 'address_name',
285 'street_address',
286 'supplemental_address_1',
287 'supplemental_address_2',
288 'city',
289 'county',
290 'state_province',
291 'postal_code',
292 'country',
293 );
294
295 // get the field sequence from the format
296 $newSequence = array();
297 foreach ($addressSequence as $field) {
298 if (substr_count($format, $field)) {
299 $newSequence[strpos($format, $field)] = $field;
300 }
301 }
302 ksort($newSequence);
303
304 // add the addressSequence fields that are missing in the addressFormat
305 // to the end of the list, so that (for example) if state_province is not
306 // specified in the addressFormat it's still in the address-editing form
307 $newSequence = array_merge($newSequence, $addressSequence);
308 $newSequence = array_unique($newSequence);
309 return $newSequence;
310 }
311}