| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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-2018 |
| 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 | * This function is also used to generate a contact's display_name and |
| 43 | * sort_name. |
| 44 | * |
| 45 | * @param array $fields |
| 46 | * The address fields. |
| 47 | * @param string $format |
| 48 | * The desired address format. |
| 49 | * @param bool $microformat |
| 50 | * If true indicates, the address to be built in hcard-microformat standard. |
| 51 | * @param bool $mailing |
| 52 | * If true indicates, the call has been made from mailing label. |
| 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 | $tokenFields = NULL |
| 65 | ) { |
| 66 | static $config = NULL; |
| 67 | |
| 68 | if (!$format) { |
| 69 | $format = Civi::settings()->get('address_format'); |
| 70 | } |
| 71 | |
| 72 | if ($mailing) { |
| 73 | $format = Civi::settings()->get('mailing_format'); |
| 74 | } |
| 75 | |
| 76 | $formatted = $format; |
| 77 | |
| 78 | $fullPostalCode = CRM_Utils_Array::value('postal_code', $fields); |
| 79 | if (!empty($fields['postal_code_suffix'])) { |
| 80 | $fullPostalCode .= "-$fields[postal_code_suffix]"; |
| 81 | } |
| 82 | |
| 83 | // make sure that some of the fields do have values |
| 84 | $emptyFields = array( |
| 85 | 'supplemental_address_1', |
| 86 | 'supplemental_address_2', |
| 87 | 'supplemental_address_3', |
| 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 | //CRM-16876 Display countries in all caps when in mailing mode. |
| 98 | if ($mailing && !empty($fields['country'])) { |
| 99 | if (Civi::settings()->get('hideCountryMailingLabels')) { |
| 100 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 101 | $domainLocation = CRM_Core_BAO_Location::getValues(array('contact_id' => $domain->contact_id)); |
| 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 | else { |
| 108 | //Capitalization display on uppercase to contries with special characters |
| 109 | $fields['country'] = mb_convert_case($fields['country'], MB_CASE_UPPER, "UTF-8"); |
| 110 | } |
| 111 | } |
| 112 | else { |
| 113 | $fields['country'] = mb_convert_case($fields['country'], MB_CASE_UPPER, "UTF-8"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (!$microformat) { |
| 118 | // replacements in case of Individual Name Format |
| 119 | $replacements = array( |
| 120 | 'contact.display_name' => CRM_Utils_Array::value('display_name', $fields), |
| 121 | 'contact.individual_prefix' => CRM_Utils_Array::value('individual_prefix', $fields), |
| 122 | 'contact.formal_title' => CRM_Utils_Array::value('formal_title', $fields), |
| 123 | 'contact.first_name' => CRM_Utils_Array::value('first_name', $fields), |
| 124 | 'contact.middle_name' => CRM_Utils_Array::value('middle_name', $fields), |
| 125 | 'contact.last_name' => CRM_Utils_Array::value('last_name', $fields), |
| 126 | 'contact.individual_suffix' => CRM_Utils_Array::value('individual_suffix', $fields), |
| 127 | 'contact.address_name' => CRM_Utils_Array::value('address_name', $fields), |
| 128 | 'contact.street_address' => CRM_Utils_Array::value('street_address', $fields), |
| 129 | 'contact.supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $fields), |
| 130 | 'contact.supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $fields), |
| 131 | 'contact.supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', $fields), |
| 132 | 'contact.city' => CRM_Utils_Array::value('city', $fields), |
| 133 | 'contact.state_province_name' => CRM_Utils_Array::value('state_province_name', $fields), |
| 134 | 'contact.county' => CRM_Utils_Array::value('county', $fields), |
| 135 | 'contact.state_province' => CRM_Utils_Array::value('state_province', $fields), |
| 136 | 'contact.postal_code' => $fullPostalCode, |
| 137 | 'contact.country' => CRM_Utils_Array::value('country', $fields), |
| 138 | 'contact.world_region' => CRM_Utils_Array::value('world_region', $fields), |
| 139 | 'contact.geo_code_1' => CRM_Utils_Array::value('geo_code_1', $fields), |
| 140 | 'contact.geo_code_2' => CRM_Utils_Array::value('geo_code_2', $fields), |
| 141 | 'contact.current_employer' => CRM_Utils_Array::value('current_employer', $fields), |
| 142 | 'contact.nick_name' => CRM_Utils_Array::value('nick_name', $fields), |
| 143 | 'contact.email' => CRM_Utils_Array::value('email', $fields), |
| 144 | 'contact.im' => CRM_Utils_Array::value('im', $fields), |
| 145 | 'contact.do_not_email' => CRM_Utils_Array::value('do_not_email', $fields), |
| 146 | 'contact.do_not_phone' => CRM_Utils_Array::value('do_not_phone', $fields), |
| 147 | 'contact.do_not_mail' => CRM_Utils_Array::value('do_not_mail', $fields), |
| 148 | 'contact.do_not_sms' => CRM_Utils_Array::value('do_not_sms', $fields), |
| 149 | 'contact.do_not_trade' => CRM_Utils_Array::value('do_not_trade', $fields), |
| 150 | 'contact.job_title' => CRM_Utils_Array::value('job_title', $fields), |
| 151 | 'contact.birth_date' => CRM_Utils_Array::value('birth_date', $fields), |
| 152 | 'contact.gender' => CRM_Utils_Array::value('gender', $fields), |
| 153 | 'contact.is_opt_out' => CRM_Utils_Array::value('is_opt_out', $fields), |
| 154 | 'contact.preferred_mail_format' => CRM_Utils_Array::value('preferred_mail_format', $fields), |
| 155 | 'contact.phone' => CRM_Utils_Array::value('phone', $fields), |
| 156 | 'contact.home_URL' => CRM_Utils_Array::value('home_URL', $fields), |
| 157 | 'contact.contact_source' => CRM_Utils_Array::value('contact_source', $fields), |
| 158 | 'contact.external_identifier' => CRM_Utils_Array::value('external_identifier', $fields), |
| 159 | 'contact.contact_id' => CRM_Utils_Array::value('id', $fields), |
| 160 | 'contact.household_name' => CRM_Utils_Array::value('display_name', $fields), |
| 161 | 'contact.organization_name' => CRM_Utils_Array::value('display_name', $fields), |
| 162 | 'contact.legal_name' => CRM_Utils_Array::value('legal_name', $fields), |
| 163 | 'contact.preferred_communication_method' => CRM_Utils_Array::value('preferred_communication_method', $fields), |
| 164 | 'contact.communication_style' => CRM_Utils_Array::value('communication_style', $fields), |
| 165 | 'contact.addressee' => CRM_Utils_Array::value('addressee_display', $fields), |
| 166 | 'contact.email_greeting' => CRM_Utils_Array::value('email_greeting_display', $fields), |
| 167 | 'contact.postal_greeting' => CRM_Utils_Array::value('postal_greeting_display', $fields), |
| 168 | ); |
| 169 | } |
| 170 | else { |
| 171 | $replacements = array( |
| 172 | 'contact.address_name' => "<span class=\"address-name\">" . $fields['address_name'] . "</span>", |
| 173 | 'contact.street_address' => "<span class=\"street-address\">" . $fields['street_address'] . "</span>", |
| 174 | 'contact.supplemental_address_1' => "<span class=\"extended-address\">" . $fields['supplemental_address_1'] . "</span>", |
| 175 | 'contact.supplemental_address_2' => $fields['supplemental_address_2'], |
| 176 | 'contact.supplemental_address_3' => $fields['supplemental_address_3'], |
| 177 | 'contact.city' => "<span class=\"locality\">" . $fields['city'] . "</span>", |
| 178 | 'contact.state_province_name' => "<span class=\"region\">" . $fields['state_province_name'] . "</span>", |
| 179 | 'contact.county' => "<span class=\"region\">" . $fields['county'], |
| 180 | 'contact.state_province' => "<span class=\"region\">" . $fields['state_province'] . "</span>", |
| 181 | 'contact.postal_code' => "<span class=\"postal-code\">" . $fullPostalCode . "</span>", |
| 182 | 'contact.country' => "<span class=\"country-name\">" . $fields['country'] . "</span>", |
| 183 | 'contact.world_region' => "<span class=\"region\">" . $fields['world_region'] . "</span>", |
| 184 | ); |
| 185 | |
| 186 | // erase all empty ones, so we dont get blank lines |
| 187 | foreach (array_keys($replacements) as $key) { |
| 188 | $exactKey = substr($key, 0, 8) == 'contact.' ? substr($key, 8) : $key; |
| 189 | if ($key != 'contact.postal_code' && |
| 190 | CRM_Utils_Array::value($exactKey, $fields) == NULL |
| 191 | ) { |
| 192 | $replacements[$key] = ''; |
| 193 | } |
| 194 | } |
| 195 | if (empty($fullPostalCode)) { |
| 196 | $replacements['contact.postal_code'] = ''; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // replacements in case of Custom Token |
| 201 | if (stristr($formatted, 'custom_')) { |
| 202 | $customToken = array_keys($fields); |
| 203 | foreach ($customToken as $value) { |
| 204 | if (substr($value, 0, 7) == 'custom_') { |
| 205 | $replacements["contact.{$value}"] = $fields["{$value}"]; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // also sub all token fields |
| 211 | if ($tokenFields) { |
| 212 | foreach ($tokenFields as $token) { |
| 213 | $replacements["{$token}"] = CRM_Utils_Array::value("{$token}", $fields); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // for every token, replace {fooTOKENbar} with fooVALUEbar if |
| 218 | // the value is not empty, otherwise drop the whole {fooTOKENbar} |
| 219 | foreach ($replacements as $token => $value) { |
| 220 | if ($value && is_string($value) || is_numeric($value)) { |
| 221 | $formatted = preg_replace("/{([^{}]*)\b{$token}\b([^{}]*)}/u", "\${1}{$value}\${2}", $formatted); |
| 222 | } |
| 223 | else { |
| 224 | $formatted = preg_replace("/{[^{}]*\b{$token}\b[^{}]*}/u", '', $formatted); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // drop any {...} constructs from lines' ends |
| 229 | if (!$microformat) { |
| 230 | $formatted = "\n$formatted\n"; |
| 231 | } |
| 232 | else { |
| 233 | if ($microformat == 1) { |
| 234 | $formatted = "\n<div class=\"location vcard\"><span class=\"adr\">\n$formatted</span></div>\n"; |
| 235 | } |
| 236 | else { |
| 237 | $formatted = "\n<div class=\"vcard\"><span class=\"adr\">$formatted</span></div>\n"; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | $formatted = preg_replace('/\n{[^{}]*}/u', "\n", $formatted); |
| 242 | $formatted = preg_replace('/{[^{}]*}\n/u', "\n", $formatted); |
| 243 | |
| 244 | // if there are any 'sibling' {...} constructs, replace them with the |
| 245 | // contents of the first one; for example, when there's no state_province: |
| 246 | // 1. {city}{, }{state_province}{ }{postal_code} |
| 247 | // 2. San Francisco{, }{ }12345 |
| 248 | // 3. San Francisco, 12345 |
| 249 | $formatted = preg_replace('/{([^{}]*)}({[^{}]*})+/u', '\1', $formatted); |
| 250 | |
| 251 | // drop any remaining curly braces leaving their contents |
| 252 | $formatted = str_replace(array('{', '}'), '', $formatted); |
| 253 | |
| 254 | // drop any empty lines left after the replacements |
| 255 | $formatted = preg_replace('/^[ \t]*[\r\n]+/m', '', $formatted); |
| 256 | |
| 257 | if (!$microformat) { |
| 258 | $finalFormatted = $formatted; |
| 259 | } |
| 260 | else { |
| 261 | // remove \n from each line and only add at the end |
| 262 | // this hack solves formatting issue, when we convert nl2br |
| 263 | $lines = array(); |
| 264 | $count = 1; |
| 265 | $finalFormatted = NULL; |
| 266 | $formattedArray = explode("\n", $formatted); |
| 267 | $formattedArray = array_filter($formattedArray); |
| 268 | |
| 269 | foreach ($formattedArray as $line) { |
| 270 | $line = trim($line); |
| 271 | if ($line) { |
| 272 | if ($count > 1 && $count < count($formattedArray)) { |
| 273 | $line = "$line\n"; |
| 274 | } |
| 275 | $finalFormatted .= $line; |
| 276 | $count++; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return $finalFormatted; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @param $format |
| 285 | * |
| 286 | * @return array |
| 287 | */ |
| 288 | public static function sequence($format) { |
| 289 | // also compute and store the address sequence |
| 290 | $addressSequence = array( |
| 291 | 'address_name', |
| 292 | 'street_address', |
| 293 | 'supplemental_address_1', |
| 294 | 'supplemental_address_2', |
| 295 | 'supplemental_address_3', |
| 296 | 'city', |
| 297 | 'county', |
| 298 | 'state_province', |
| 299 | 'postal_code', |
| 300 | 'country', |
| 301 | ); |
| 302 | |
| 303 | // get the field sequence from the format |
| 304 | $newSequence = array(); |
| 305 | foreach ($addressSequence as $field) { |
| 306 | if (substr_count($format, $field)) { |
| 307 | $newSequence[strpos($format, $field)] = $field; |
| 308 | } |
| 309 | } |
| 310 | ksort($newSequence); |
| 311 | |
| 312 | // add the addressSequence fields that are missing in the addressFormat |
| 313 | // to the end of the list, so that (for example) if state_province is not |
| 314 | // specified in the addressFormat it's still in the address-editing form |
| 315 | $newSequence = array_merge($newSequence, $addressSequence); |
| 316 | $newSequence = array_unique($newSequence); |
| 317 | return $newSequence; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Extract the billing fields from the form submission and format them for display. |
| 322 | * |
| 323 | * @param array $params |
| 324 | * @param int $billingLocationTypeID |
| 325 | * |
| 326 | * @return string |
| 327 | */ |
| 328 | public static function getFormattedBillingAddressFieldsFromParameters($params, $billingLocationTypeID) { |
| 329 | $addressParts = array( |
| 330 | "street_address" => "billing_street_address-{$billingLocationTypeID}", |
| 331 | "city" => "billing_city-{$billingLocationTypeID}", |
| 332 | "postal_code" => "billing_postal_code-{$billingLocationTypeID}", |
| 333 | "state_province" => "state_province-{$billingLocationTypeID}", |
| 334 | "country" => "country-{$billingLocationTypeID}", |
| 335 | ); |
| 336 | |
| 337 | $addressFields = array(); |
| 338 | foreach ($addressParts as $name => $field) { |
| 339 | $value = CRM_Utils_Array::value($field, $params); |
| 340 | $alternateName = 'billing_' . $name . '_id-' . $billingLocationTypeID; |
| 341 | $alternate2 = 'billing_' . $name . '-' . $billingLocationTypeID; |
| 342 | if (isset($params[$alternate2]) && !isset($params[$alternateName])) { |
| 343 | $alternateName = $alternate2; |
| 344 | } |
| 345 | //Include values which prepend 'billing_' to country and state_province. |
| 346 | if (CRM_Utils_Array::value($alternateName, $params)) { |
| 347 | if (empty($value) || !is_numeric($value)) { |
| 348 | $value = $params[$alternateName]; |
| 349 | } |
| 350 | } |
| 351 | if (is_numeric($value) && ($name == 'state_province' || $name == 'country')) { |
| 352 | if ($name == 'state_province') { |
| 353 | $addressFields[$name] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value); |
| 354 | $addressFields[$name . '_name'] = CRM_Core_PseudoConstant::stateProvince($value); |
| 355 | } |
| 356 | if ($name == 'country') { |
| 357 | $addressFields[$name] = CRM_Core_PseudoConstant::countryIsoCode($value); |
| 358 | } |
| 359 | } |
| 360 | else { |
| 361 | $addressFields[$name] = $value; |
| 362 | } |
| 363 | } |
| 364 | return CRM_Utils_Address::format($addressFields); |
| 365 | } |
| 366 | |
| 367 | } |