You are here

public function UcAddress::__toString in Ubercart 7.3

Formats the address for display based on the country's address format.

Return value

A formatted string containing the address.

File

uc_store/classes/address.inc, line 119
UcAddress utility class definition.

Class

UcAddress
Defines an object to hold Ubercart mailing address information.

Code

public function __toString() {
  $result = db_query('SELECT * FROM {uc_zones} WHERE zone_id = :id', array(
    ':id' => $this->zone,
  ));
  if (!($zone_data = $result
    ->fetchAssoc())) {
    $zone_data = array(
      'zone_code' => t('N/A'),
      'zone_name' => t('Unknown'),
    );
  }
  $result = db_query('SELECT * FROM {uc_countries} WHERE country_id = :id', array(
    ':id' => $this->country,
  ));
  if (!($country_data = $result
    ->fetchAssoc())) {
    $country_data = array(
      'country_name' => t('Unknown'),
      'country_iso_code_2' => t('N/A'),
      'country_iso_code_3' => t('N/A'),
    );
  }
  $variables = array(
    "\r\n" => '<br />',
    '!company' => check_plain($this->company),
    '!first_name' => check_plain($this->first_name),
    '!last_name' => check_plain($this->last_name),
    '!street1' => check_plain($this->street1),
    '!street2' => check_plain($this->street2),
    '!city' => check_plain($this->city),
    '!zone_code' => $zone_data['zone_code'],
    '!zone_name' => $zone_data['zone_name'],
    '!postal_code' => check_plain($this->postal_code),
    '!country_name' => t($country_data['country_name']),
    '!country_code2' => $country_data['country_iso_code_2'],
    '!country_code3' => $country_data['country_iso_code_3'],
  );
  if (uc_store_default_country() != $this->country) {
    $variables['!country_name_if'] = t($country_data['country_name']);
    $variables['!country_code2_if'] = $country_data['country_iso_code_2'];
    $variables['!country_code3_if'] = $country_data['country_iso_code_3'];
  }
  else {
    $variables['!country_name_if'] = '';
    $variables['!country_code2_if'] = '';
    $variables['!country_code3_if'] = '';
  }
  $format = variable_get('uc_address_format_' . $this->country, '');
  if (empty($format)) {
    $format = "!company\r\n!first_name !last_name\r\n!street1\r\n!street2\r\n!city, !zone_code !postal_code\r\n!country_name_if";
  }
  $address = strtr($format, $variables);
  $address = strtr($address, array(
    "\n" => '<br />',
  ));
  $match = array(
    '`^<br( /)?>`',
    '`<br( /)?>$`',
    '`<br( /)?>(\\s*|[\\s*<br( /)?>\\s*]+)<br( /)?>`',
    '`<br( /)?><br( /)?>`',
    '`<br( /)?>, N/A`',
  );
  $replace = array(
    '',
    '',
    '<br />',
    '<br />',
    '',
    '',
  );
  $address = preg_replace($match, $replace, $address);
  return $address;
}