address.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @file
  4. * UcAddress utility class definition.
  5. */
  6. /**
  7. * Defines an object to hold Ubercart mailing address information.
  8. */
  9. class UcAddress {
  10. /** Given name. */
  11. public $first_name = '';
  12. /** Surname. */
  13. public $last_name = '';
  14. /** Company or organization. */
  15. public $company = '';
  16. /** First line of street address. */
  17. public $street1 = '';
  18. /** Second line of street address. */
  19. public $street2 = '';
  20. /** City name. */
  21. public $city = '';
  22. /** State, provence, or region id. */
  23. public $zone = 0;
  24. /** ISO 3166-1 3-digit numeric country code. */
  25. public $country = 0;
  26. /** Postal code. */
  27. public $postal_code = '';
  28. /** Telephone number. */
  29. public $phone = '';
  30. /** Email address. */
  31. public $email = '';
  32. /**
  33. * Constructor.
  34. *
  35. * @param $country
  36. * ISO 3166-1 3-digit numeric country code. Defaults to the value of the
  37. * uc_store_country system variable if that variable is set, or 840
  38. * (United States of America) if it is not set.
  39. */
  40. public function __construct($country = NULL) {
  41. if (!$this->country) {
  42. $this->country = isset($country) ? $country : variable_get('uc_store_country', 840);
  43. }
  44. }
  45. /**
  46. * Compares two UcAddress objects to determine if they represent the same
  47. * physical address.
  48. *
  49. * Address properties such as first_name, phone, and email aren't considered
  50. * in this comparison because they don't contain information about the
  51. * physical location.
  52. *
  53. * @param $address
  54. * An object of type UcAddress.
  55. *
  56. * @return
  57. * TRUE if the two addresses are the same physical location, else FALSE.
  58. */
  59. public function isSamePhysicalLocation(UcAddress $address) {
  60. $physicalProperty = array(
  61. 'street1', 'street2', 'city', 'zone', 'country', 'postal_code'
  62. );
  63. foreach ($physicalProperty as $property) {
  64. // Canonicalize properties before comparing.
  65. if (UcAddress::makeCanonical($this->$property) !=
  66. UcAddress::makeCanonical($address->$property) ) {
  67. return FALSE;
  68. }
  69. }
  70. return TRUE;
  71. }
  72. /**
  73. * Utility function to simplify comparison of address properties.
  74. *
  75. * For the purpose of this function, the canonical form is stripped of all
  76. * whitespace and has been converted to upper case. This ensures that we
  77. * don't get false inequalities when comparing address properties that a
  78. * human would consider identical, but may be capitalized differently or
  79. * have different whitespace.
  80. *
  81. * @param $string
  82. * String to make canonical.
  83. *
  84. * @return
  85. * Canonical form of input string.
  86. */
  87. public static function makeCanonical($string = '') {
  88. // Remove all whitespace.
  89. $string = preg_replace('/\s+/', '', $string);
  90. // Make all characters upper case.
  91. $string = drupal_strtoupper($string);
  92. return $string;
  93. }
  94. /**
  95. * Formats the address for display based on the country's address format.
  96. *
  97. * @return
  98. * A formatted string containing the address.
  99. */
  100. public function __toString() {
  101. $result = db_query('SELECT * FROM {uc_zones} WHERE zone_id = :id', array(':id' => $this->zone));
  102. if (!($zone_data = $result->fetchAssoc())) {
  103. $zone_data = array('zone_code' => t('N/A'), 'zone_name' => t('Unknown'));
  104. }
  105. $result = db_query('SELECT * FROM {uc_countries} WHERE country_id = :id', array(':id' => $this->country));
  106. if (!($country_data = $result->fetchAssoc())) {
  107. $country_data = array(
  108. 'country_name' => t('Unknown'),
  109. 'country_iso_code_2' => t('N/A'),
  110. 'country_iso_code_3' => t('N/A'),
  111. );
  112. }
  113. $variables = array(
  114. "\r\n" => '<br />',
  115. '!company' => check_plain($this->company),
  116. '!first_name' => check_plain($this->first_name),
  117. '!last_name' => check_plain($this->last_name),
  118. '!street1' => check_plain($this->street1),
  119. '!street2' => check_plain($this->street2),
  120. '!city' => check_plain($this->city),
  121. '!zone_code' => $zone_data['zone_code'],
  122. '!zone_name' => $zone_data['zone_name'],
  123. '!postal_code' => check_plain($this->postal_code),
  124. '!country_name' => t($country_data['country_name']),
  125. '!country_code2' => $country_data['country_iso_code_2'],
  126. '!country_code3' => $country_data['country_iso_code_3'],
  127. );
  128. if (uc_store_default_country() != $this->country) {
  129. $variables['!country_name_if'] = t($country_data['country_name']);
  130. $variables['!country_code2_if'] = $country_data['country_iso_code_2'];
  131. $variables['!country_code3_if'] = $country_data['country_iso_code_3'];
  132. }
  133. else {
  134. $variables['!country_name_if'] = '';
  135. $variables['!country_code2_if'] = '';
  136. $variables['!country_code3_if'] = '';
  137. }
  138. $format = variable_get('uc_address_format_' . $this->country, '');
  139. if (empty($format)) {
  140. $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";
  141. }
  142. $address = strtr($format, $variables);
  143. $address = strtr($address, array("\n" => '<br />'));
  144. $match = array('`^<br( /)?>`', '`<br( /)?>$`', '`<br( /)?>(\s*|[\s*<br( /)?>\s*]+)<br( /)?>`', '`<br( /)?><br( /)?>`', '`<br( /)?>, N/A`');
  145. $replace = array('', '', '<br />', '<br />', '', '');
  146. $address = preg_replace($match, $replace, $address);
  147. return $address;
  148. }
  149. /**
  150. * PHP magic method to use in relation with var_export().
  151. *
  152. * Created for strongarm compatibility.
  153. *
  154. * @param array $data
  155. * Data to import
  156. */
  157. public static function __set_state($data) {
  158. $obj = new self;
  159. foreach ($data as $key => $val) {
  160. $obj->$key = $val;
  161. }
  162. return $obj;
  163. }
  164. }