hpdl
|
732
|
1
|
<?php
|
|
2
|
/*
|
|
3
|
$Id: $
|
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
hpdl
|
1860
|
8
|
Copyright (c) 2007 osCommerce
|
hpdl
|
732
|
9
|
|
hpdl
|
1498
|
10
|
This program is free software; you can redistribute it and/or modify
|
|
11
|
it under the terms of the GNU General Public License v2 (1991)
|
|
12
|
as published by the Free Software Foundation.
|
hpdl
|
732
|
13
|
*/
|
|
14
|
|
hpdl
|
1860
|
15
|
/**
|
|
16
|
* The osC_Address class handles address related functions such as the format and country and zone information
|
|
17
|
*/
|
|
18
|
|
hpdl
|
732
|
19
|
class osC_Address {
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
31
|
global $osC_Database;
|
|
32
|
|
|
33
|
$address_format = '';
|
|
34
|
|
hpdl
|
1860
|
35
|
if ( is_numeric($address) ) {
|
hpdl
|
831
|
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');
|
hpdl
|
732
|
37
|
$Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
|
hpdl
|
831
|
38
|
$Qaddress->bindTable(':table_zones', TABLE_ZONES);
|
|
39
|
$Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
|
hpdl
|
732
|
40
|
$Qaddress->bindInt(':address_book_id', $address);
|
|
41
|
$Qaddress->execute();
|
|
42
|
|
|
43
|
$address = $Qaddress->toArray();
|
|
44
|
}
|
|
45
|
|
|
46
|
$firstname = $lastname = '';
|
|
47
|
|
hpdl
|
1860
|
48
|
if ( isset($address['firstname']) && !empty($address['firstname']) ) {
|
hpdl
|
732
|
49
|
$firstname = $address['firstname'];
|
|
50
|
$lastname = $address['lastname'];
|
hpdl
|
1860
|
51
|
} elseif ( isset($address['name']) && !empty($address['name']) ) {
|
hpdl
|
732
|
52
|
$firstname = $address['name'];
|
|
53
|
}
|
|
54
|
|
|
55
|
$state = $address['state'];
|
hpdl
|
831
|
56
|
$state_code = $address['zone_code'];
|
hpdl
|
732
|
57
|
|
hpdl
|
1860
|
58
|
if ( isset($address['zone_id']) && is_numeric($address['zone_id']) && ($address['zone_id'] > 0) ) {
|
hpdl
|
732
|
59
|
$state = osC_Address::getZoneName($address['zone_id']);
|
|
60
|
$state_code = osC_Address::getZoneCode($address['zone_id']);
|
|
61
|
}
|
|
62
|
|
hpdl
|
831
|
63
|
$country = $address['country_title'];
|
hpdl
|
732
|
64
|
|
hpdl
|
1860
|
65
|
if ( empty($country) && isset($address['country_id']) && is_numeric($address['country_id']) && ($address['country_id'] > 0) ) {
|
hpdl
|
732
|
66
|
$country = osC_Address::getCountryName($address['country_id']);
|
|
67
|
}
|
|
68
|
|
hpdl
|
1860
|
69
|
if ( isset($address['format']) ) {
|
hpdl
|
732
|
70
|
$address_format = $address['format'];
|
hpdl
|
1860
|
71
|
} elseif ( isset($address['country_id']) && is_numeric($address['country_id']) && ($address['country_id'] > 0) ) {
|
hpdl
|
732
|
72
|
$address_format = osC_Address::getFormat($address['country_id']);
|
|
73
|
}
|
|
74
|
|
hpdl
|
1860
|
75
|
if ( empty($address_format) ) {
|
hpdl
|
732
|
76
|
$address_format = ":name\n:street_address\n:postcode :city\n:country";
|
|
77
|
}
|
|
78
|
|
|
79
|
$find_array = array('/\:name\b/',
|
|
80
|
'/\:street_address\b/',
|
|
81
|
'/\:suburb\b/',
|
|
82
|
'/\:city\b/',
|
|
83
|
'/\:postcode\b/',
|
|
84
|
'/\:state\b/',
|
|
85
|
'/\:state_code\b/',
|
|
86
|
'/\:country\b/');
|
|
87
|
|
|
88
|
$replace_array = array(osc_output_string_protected($firstname . ' ' . $lastname),
|
|
89
|
osc_output_string_protected($address['street_address']),
|
|
90
|
osc_output_string_protected($address['suburb']),
|
|
91
|
osc_output_string_protected($address['city']),
|
|
92
|
osc_output_string_protected($address['postcode']),
|
|
93
|
osc_output_string_protected($state),
|
|
94
|
osc_output_string_protected($state_code),
|
|
95
|
osc_output_string_protected($country));
|
|
96
|
|
|
97
|
$formated = preg_replace($find_array, $replace_array, $address_format);
|
|
98
|
|
|
99
|
if ( (ACCOUNT_COMPANY > -1) && !empty($address['company']) ) {
|
hpdl
|
1860
|
100
|
$formated = osc_output_string_protected($address['company']) . "\n" . $formated;
|
hpdl
|
732
|
101
|
}
|
|
102
|
|
hpdl
|
1860
|
103
|
if ( !empty($new_line) ) {
|
hpdl
|
732
|
104
|
$formated = str_replace("\n", $new_line, $formated);
|
|
105
|
}
|
|
106
|
|
|
107
|
return $formated;
|
|
108
|
}
|
|
109
|
|
hpdl
|
1860
|
110
|
/**
|
|
111
|
* Return all countries in an array
|
|
112
|
*
|
|
113
|
* @access public
|
|
114
|
* @return array
|
|
115
|
*/
|
|
116
|
|
|
117
|
public static function getCountries() {
|
hpdl
|
732
|
118
|
global $osC_Database;
|
|
119
|
|
hpdl
|
1860
|
120
|
static $countries;
|
hpdl
|
732
|
121
|
|
hpdl
|
1860
|
122
|
if ( !isset($countries) ) {
|
|
123
|
$countries = array();
|
hpdl
|
732
|
124
|
|
|
125
|
$Qcountries = $osC_Database->query('select * from :table_countries order by countries_name');
|
|
126
|
$Qcountries->bindTable(':table_countries', TABLE_COUNTRIES);
|
|
127
|
$Qcountries->execute();
|
|
128
|
|
hpdl
|
1860
|
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'));
|
hpdl
|
732
|
135
|
}
|
|
136
|
|
|
137
|
$Qcountries->freeResult();
|
|
138
|
}
|
|
139
|
|
hpdl
|
1860
|
140
|
return $countries;
|
hpdl
|
732
|
141
|
}
|
|
142
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
152
|
global $osC_Database;
|
|
153
|
|
|
154
|
$Qcountry = $osC_Database->query('select countries_name from :table_countries where countries_id = :countries_id');
|
|
155
|
$Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
|
|
156
|
$Qcountry->bindInt(':countries_id', $id);
|
|
157
|
$Qcountry->execute();
|
|
158
|
|
|
159
|
return $Qcountry->value('countries_name');
|
|
160
|
}
|
|
161
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
171
|
global $osC_Database;
|
|
172
|
|
|
173
|
$Qcountry = $osC_Database->query('select countries_iso_code_2 from :table_countries where countries_id = :countries_id');
|
|
174
|
$Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
|
|
175
|
$Qcountry->bindInt(':countries_id', $id);
|
|
176
|
$Qcountry->execute();
|
|
177
|
|
|
178
|
return $Qcountry->value('countries_iso_code_2');
|
|
179
|
}
|
|
180
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
190
|
global $osC_Database;
|
|
191
|
|
|
192
|
$Qcountry = $osC_Database->query('select countries_iso_code_3 from :table_countries where countries_id = :countries_id');
|
|
193
|
$Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
|
|
194
|
$Qcountry->bindInt(':countries_id', $id);
|
|
195
|
$Qcountry->execute();
|
|
196
|
|
|
197
|
return $Qcountry->value('countries_iso_code_3');
|
|
198
|
}
|
|
199
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
209
|
global $osC_Database;
|
|
210
|
|
|
211
|
$Qcountry = $osC_Database->query('select address_format from :table_countries where countries_id = :countries_id');
|
|
212
|
$Qcountry->bindTable(':table_countries', TABLE_COUNTRIES);
|
|
213
|
$Qcountry->bindInt(':countries_id', $id);
|
|
214
|
$Qcountry->execute();
|
|
215
|
|
|
216
|
return $Qcountry->value('address_format');
|
|
217
|
}
|
|
218
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
228
|
global $osC_Database;
|
|
229
|
|
|
230
|
$Qzone = $osC_Database->query('select zone_name from :table_zones where zone_id = :zone_id');
|
|
231
|
$Qzone->bindTable(':table_zones', TABLE_ZONES);
|
|
232
|
$Qzone->bindInt(':zone_id', $id);
|
|
233
|
$Qzone->execute();
|
|
234
|
|
|
235
|
return $Qzone->value('zone_name');
|
|
236
|
}
|
|
237
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
732
|
247
|
global $osC_Database;
|
|
248
|
|
|
249
|
$Qzone = $osC_Database->query('select zone_code from :table_zones where zone_id = :zone_id');
|
|
250
|
$Qzone->bindTable(':table_zones', TABLE_ZONES);
|
|
251
|
$Qzone->bindInt(':zone_id', $id);
|
|
252
|
$Qzone->execute();
|
|
253
|
|
|
254
|
return $Qzone->value('zone_code');
|
|
255
|
}
|
hpdl
|
758
|
256
|
|
hpdl
|
1860
|
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) {
|
hpdl
|
758
|
266
|
global $osC_Database;
|
|
267
|
|
|
268
|
$zones_array = array();
|
|
269
|
|
hpdl
|
1076
|
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');
|
|
271
|
|
hpdl
|
1860
|
272
|
if ( !empty($id) ) {
|
hpdl
|
1076
|
273
|
$Qzones->appendQuery('z.zone_country_id = :zone_country_id and');
|
|
274
|
$Qzones->bindInt(':zone_country_id', $id);
|
|
275
|
}
|
|
276
|
|
|
277
|
$Qzones->appendQuery('z.zone_country_id = c.countries_id order by c.countries_name, z.zone_name');
|
|
278
|
$Qzones->bindTable(':table_countries', TABLE_COUNTRIES);
|
hpdl
|
758
|
279
|
$Qzones->bindTable(':table_zones', TABLE_ZONES);
|
|
280
|
$Qzones->execute();
|
|
281
|
|
hpdl
|
1860
|
282
|
while ( $Qzones->next() ) {
|
hpdl
|
758
|
283
|
$zones_array[] = array('id' => $Qzones->valueInt('zone_id'),
|
hpdl
|
1076
|
284
|
'name' => $Qzones->value('zone_name'),
|
|
285
|
'country_id' => $Qzones->valueInt('zone_country_id'),
|
|
286
|
'country_name' => $Qzones->value('countries_name'));
|
hpdl
|
758
|
287
|
}
|
|
288
|
|
|
289
|
return $zones_array;
|
|
290
|
}
|
hpdl
|
732
|
291
|
}
|
|
292
|
?>
|