/includes/classes/address.php

https://github.com/subhabrata/oscommerce · PHP · 292 lines · 153 code · 61 blank · 78 comment · 22 complexity · 24d53942838d234b11c4ac08715e1f7b MD5 · raw file

  1. <?php
  2. /*
  3. $Id: $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2007 osCommerce
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License v2 (1991)
  9. as published by the Free Software Foundation.
  10. */
  11. /**
  12. * The osC_Address class handles address related functions such as the format and country and zone information
  13. */
  14. class osC_Address {
  15. /**
  16. * Correctly format an address to the address format rule assigned to its country
  17. *
  18. * @param array $address An array (or address_book ID) containing the address information
  19. * @param string $new_line The string to break new lines with
  20. * @access public
  21. * @return string
  22. */
  23. public static function format($address, $new_line = null) {
  24. global $osC_Database;
  25. $address_format = '';
  26. if ( is_numeric($address) ) {
  27. $Qaddress = $osC_Database->query('select ab.entry_firstname as firstname, ab.entry_lastname as lastname, ab.entry_company as company, ab.entry_street_address as street_address, ab.entry_suburb as suburb, ab.entry_city as city, ab.entry_postcode as postcode, ab.entry_state as state, ab.entry_zone_id as zone_id, ab.entry_country_id as country_id, z.zone_code as zone_code, c.countries_name as country_title from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id), :table_countries c where ab.address_book_id = :address_book_id and ab.entry_country_id = c.countries_id');
  28. $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
  29. $Qaddress->bindTable(':table_zones', TABLE_ZONES);
  30. $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
  31. $Qaddress->bindInt(':address_book_id', $address);
  32. $Qaddress->execute();
  33. $address = $Qaddress->toArray();
  34. }
  35. $firstname = $lastname = '';
  36. if ( isset($address['firstname']) && !empty($address['firstname']) ) {
  37. $firstname = $address['firstname'];
  38. $lastname = $address['lastname'];
  39. } elseif ( isset($address['name']) && !empty($address['name']) ) {
  40. $firstname = $address['name'];
  41. }
  42. $state = $address['state'];
  43. $state_code = $address['zone_code'];
  44. if ( isset($address['zone_id']) && is_numeric($address['zone_id']) && ($address['zone_id'] > 0) ) {
  45. $state = osC_Address::getZoneName($address['zone_id']);
  46. $state_code = osC_Address::getZoneCode($address['zone_id']);
  47. }
  48. $country = $address['country_title'];
  49. if ( empty($country) && isset($address['country_id']) && is_numeric($address['country_id']) && ($address['country_id'] > 0) ) {
  50. $country = osC_Address::getCountryName($address['country_id']);
  51. }
  52. if ( isset($address['format']) ) {
  53. $address_format = $address['format'];
  54. } elseif ( isset($address['country_id']) && is_numeric($address['country_id']) && ($address['country_id'] > 0) ) {
  55. $address_format = osC_Address::getFormat($address['country_id']);
  56. }
  57. if ( empty($address_format) ) {
  58. $address_format = ":name\n:street_address\n:postcode :city\n:country";
  59. }
  60. $find_array = array('/\:name\b/',
  61. '/\:street_address\b/',
  62. '/\:suburb\b/',
  63. '/\:city\b/',
  64. '/\:postcode\b/',
  65. '/\:state\b/',
  66. '/\:state_code\b/',
  67. '/\:country\b/');
  68. $replace_array = array(osc_output_string_protected($firstname . ' ' . $lastname),
  69. osc_output_string_protected($address['street_address']),
  70. osc_output_string_protected($address['suburb']),
  71. osc_output_string_protected($address['city']),
  72. osc_output_string_protected($address['postcode']),
  73. osc_output_string_protected($state),
  74. osc_output_string_protected($state_code),
  75. osc_output_string_protected($country));
  76. $formated = preg_replace($find_array, $replace_array, $address_format);
  77. if ( (ACCOUNT_COMPANY > -1) && !empty($address['company']) ) {
  78. $formated = osc_output_string_protected($address['company']) . "\n" . $formated;
  79. }
  80. if ( !empty($new_line) ) {
  81. $formated = str_replace("\n", $new_line, $formated);
  82. }
  83. return $formated;
  84. }
  85. /**
  86. * Return all countries in an array
  87. *
  88. * @access public
  89. * @return array
  90. */
  91. public static function getCountries() {
  92. global $osC_Database;
  93. static $countries;
  94. if ( !isset($countries) ) {
  95. $countries = array();
  96. $Qcountries = $osC_Database->query('select * from :table_countries order by countries_name');
  97. $Qcountries->bindTable(':table_countries', TABLE_COUNTRIES);
  98. $Qcountries->execute();
  99. while ( $Qcountries->next() ) {
  100. $countries[] = array('id' => $Qcountries->valueInt('countries_id'),
  101. 'name' => $Qcountries->value('countries_name'),
  102. 'iso_2' => $Qcountries->value('countries_iso_code_2'),
  103. 'iso_3' => $Qcountries->value('countries_iso_code_3'),
  104. 'format' => $Qcountries->value('address_format'));
  105. }
  106. $Qcountries->freeResult();
  107. }
  108. return $countries;
  109. }
  110. /**
  111. * Return the country name
  112. *
  113. * @param int $id The ID of the country
  114. * @access public
  115. * @return string
  116. */
  117. public static function getCountryName($id) {
  118. global $osC_Database;
  119. $Qcountry = $osC_Database->query('select countries_name from :table_countries where countries_id = :countries_id');
  120. $Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
  121. $Qcountry->bindInt(':countries_id', $id);
  122. $Qcountry->execute();
  123. return $Qcountry->value('countries_name');
  124. }
  125. /**
  126. * Return the country 2 character ISO code
  127. *
  128. * @param int $id The ID of the country
  129. * @access public
  130. * @return string
  131. */
  132. public static function getCountryIsoCode2($id) {
  133. global $osC_Database;
  134. $Qcountry = $osC_Database->query('select countries_iso_code_2 from :table_countries where countries_id = :countries_id');
  135. $Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
  136. $Qcountry->bindInt(':countries_id', $id);
  137. $Qcountry->execute();
  138. return $Qcountry->value('countries_iso_code_2');
  139. }
  140. /**
  141. * Return the country 3 character ISO code
  142. *
  143. * @param int $id The ID of the country
  144. * @access public
  145. * @return string
  146. */
  147. public static function getCountryIsoCode3($id) {
  148. global $osC_Database;
  149. $Qcountry = $osC_Database->query('select countries_iso_code_3 from :table_countries where countries_id = :countries_id');
  150. $Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
  151. $Qcountry->bindInt(':countries_id', $id);
  152. $Qcountry->execute();
  153. return $Qcountry->value('countries_iso_code_3');
  154. }
  155. /**
  156. * Return the address format rule for the country
  157. *
  158. * @param int $id The ID of the country
  159. * @access public
  160. * @return string
  161. */
  162. public static function getFormat($id) {
  163. global $osC_Database;
  164. $Qcountry = $osC_Database->query('select address_format from :table_countries where countries_id = :countries_id');
  165. $Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
  166. $Qcountry->bindInt(':countries_id', $id);
  167. $Qcountry->execute();
  168. return $Qcountry->value('address_format');
  169. }
  170. /**
  171. * Return the zone name
  172. *
  173. * @param int $id The ID of the zone
  174. * @access public
  175. * @return string
  176. */
  177. public static function getZoneName($id) {
  178. global $osC_Database;
  179. $Qzone = $osC_Database->query('select zone_name from :table_zones where zone_id = :zone_id');
  180. $Qzone->bindTable(':table_zones', TABLE_ZONES);
  181. $Qzone->bindInt(':zone_id', $id);
  182. $Qzone->execute();
  183. return $Qzone->value('zone_name');
  184. }
  185. /**
  186. * Return the zone code
  187. *
  188. * @param int $id The ID of the zone
  189. * @access public
  190. * @return string
  191. */
  192. public static function getZoneCode($id) {
  193. global $osC_Database;
  194. $Qzone = $osC_Database->query('select zone_code from :table_zones where zone_id = :zone_id');
  195. $Qzone->bindTable(':table_zones', TABLE_ZONES);
  196. $Qzone->bindInt(':zone_id', $id);
  197. $Qzone->execute();
  198. return $Qzone->value('zone_code');
  199. }
  200. /**
  201. * Return the zones belonging to a country, or all zones
  202. *
  203. * @param int $id The ID of the country
  204. * @access public
  205. * @return array
  206. */
  207. public static function getZones($id = null) {
  208. global $osC_Database;
  209. $zones_array = array();
  210. $Qzones = $osC_Database->query('select z.zone_id, z.zone_country_id, z.zone_name, c.countries_name from :table_zones z, :table_countries c where');
  211. if ( !empty($id) ) {
  212. $Qzones->appendQuery('z.zone_country_id = :zone_country_id and');
  213. $Qzones->bindInt(':zone_country_id', $id);
  214. }
  215. $Qzones->appendQuery('z.zone_country_id = c.countries_id order by c.countries_name, z.zone_name');
  216. $Qzones->bindTable(':table_countries', TABLE_COUNTRIES);
  217. $Qzones->bindTable(':table_zones', TABLE_ZONES);
  218. $Qzones->execute();
  219. while ( $Qzones->next() ) {
  220. $zones_array[] = array('id' => $Qzones->valueInt('zone_id'),
  221. 'name' => $Qzones->value('zone_name'),
  222. 'country_id' => $Qzones->valueInt('zone_country_id'),
  223. 'country_name' => $Qzones->value('countries_name'));
  224. }
  225. return $zones_array;
  226. }
  227. }
  228. ?>