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