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