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