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