-
Notifications
You must be signed in to change notification settings - Fork 10
/
uc_store.countries.inc
465 lines (421 loc) · 14.8 KB
/
uc_store.countries.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* @file
* Store administration forms for country and address handling.
*/
/**
* Imports settings from a country file.
*
* @see uc_country_import_form_submit()
* @ingroup forms
*/
function uc_country_import_form($form, &$form_state) {
$countries = array();
$result = db_query("SELECT * FROM {uc_countries}");
foreach ($result as $country) {
$countries[t($country->country_name)] = $country;
}
uksort($countries, 'strnatcasecmp');
$files = _uc_country_import_list();
$header = array(t('Country'), t('Code'), t('Version'), t('Operations'));
$rows = array();
if (is_array($countries)) {
$query = array('token' => backdrop_get_token('uc_country_op_link'));
foreach ($countries as $country) {
$row = array(
t($country->country_name),
$country->country_iso_code_3,
array(
'data' => abs($country->version),
'align' => 'center',
),
);
$links = array();
if ($country->version < $files[$country->country_id]['version'] && $country->version > 0) {
$links[] = array(
'title' => t('update'),
'href' => 'admin/store/settings/countries/' . $country->country_id . '/update/' . $files[$country->country_id]['version'],
'query' => $query,
);
// Also add an icon to the country name for emphasis.
$row[0] .= ' <span class = "country-needs-update">' . "\u{26A0}" . '</span>';
}
if ($country->version < 0) {
$links[] = array(
'title' => t('enable'),
'href' => 'admin/store/settings/countries/' . $country->country_id . '/enable',
'query' => $query,
);
}
else {
$links[] = array(
'title' => t('disable'),
'href' => 'admin/store/settings/countries/' . $country->country_id . '/disable',
'query' => $query,
);
}
$links[] = array(
'title' => t('remove'),
'href' => 'admin/store/settings/countries/' . $country->country_id . '/remove',
);
$operations = array(
'#type' => 'operations',
'#links' => $links,
);
$row[] = backdrop_render($operations);
$rows[] = $row;
unset($files[$country->country_id]);
}
}
$import_list = array();
foreach ($files as $file) {
$import_list[$file['file']] = $file['file'];
}
if (!empty($import_list)) {
ksort($import_list);
$form['country_import'] = array(
'#title' => t('Import countries'),
'#type' => 'fieldset',
'#collapsed' => TRUE,
'#collapsible' => TRUE,
);
$form['country_import']['text'] = array(
'#type' => 'help',
'#markup' => t('To import new country data, select it in the list and click the import button. If you are using a custom or contributed import file, it must be placed in the Ubercart folder uc_store/countries.'),
);
$form['country_import']['import_file'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#options' => $import_list,
'#multiple' => TRUE,
'#size' => min(10, count($import_list)),
);
$form['country_import']['actions'] = array('#type' => 'actions');
$form['country_import']['actions']['import_button'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
}
$form['country_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $form;
}
/**
* Form submission handler for uc_country_import_form().
*
* @see uc_country_import_form()
*/
function uc_country_import_form_submit($form, &$form_state) {
$files = $form_state['values']['import_file'];
foreach ($files as $file) {
if (uc_country_import($file)) {
backdrop_set_message(t('Country file @file imported.', array('@file' => $file)));
}
else {
backdrop_set_message(t('Country file @file could not import or had no install function.', array('@file' => $file)), 'error');
}
}
}
/**
* Form builder to set country address formats.
*
* @see uc_country_formats_form_submit()
* @ingroup forms
*/
function uc_country_formats_form($form, &$form_state) {
$form['instructions'] = array(
'#type' => 'fieldset',
'#title' => t('Address format variables'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$header = array(t('Variable'), t('Description'));
$rows = array(
array('!first_name', t("Customer's first name")),
array('!last_name', t("Customer's last name")),
array('!company', t('Company name')),
array('!street1', t('First street address field')),
array('!street2', t('Second street address field')),
array('!city', t('City name')),
array('!zone_name', t('Full name of the zone')),
array('!zone_code', t('Abbreviation of the zone')),
array('!postal_code', t('Postal code')),
array('!country_name', t('Name of the country')),
array('!country_code2', t('2 digit country abbreviation')),
array('!country_code3', t('3 digit country abbreviation')),
);
$form['instructions']['text'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#prefix' => '<div><p>' . t('The following variables should be used in configuring addresses for the countries you ship to:') . '</p>',
'#suffix' => '<p>' . t('Adding _if to any country variable will make it display only for addresses whose country is different than the default store country.') . '</p></div>',
);
$countries = array();
$result = db_query("SELECT * FROM {uc_countries}");
foreach ($result as $country) {
$countries[t($country->country_name)] = $country;
}
uksort($countries, 'strnatcasecmp');
if (is_array($countries)) {
$form['countries'] = array(
'#type' => 'vertical_tabs',
'#tree' => TRUE,
);
foreach ($countries as $country) {
$form['countries']['country'][$country->country_id] = array(
'#type' => 'fieldset',
'#title' => check_plain(t($country->country_name)),
'#group' => 'country',
);
$form['countries']['country'][$country->country_id]['address_format'] = array(
'#type' => 'textarea',
'#title' => t('@country address format', array('@country' => t($country->country_name))),
'#default_value' => config_get('uc_store.settings', 'uc_address_format_' . $country->country_id),
'#description' => t('Use the variables mentioned in the instructions to format an address for this country.'),
'#rows' => 7,
);
}
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit changes'),
);
return $form;
}
/**
* Form submission handler for uc_country_formats_form().
*
* @see uc_country_formats_form()
*/
function uc_country_formats_form_submit($form, &$form_state) {
foreach ($form_state['values']['countries']['country'] as $country_id => $data) {
config_set('uc_store.settings', 'uc_address_format_' . $country_id, $data['address_format']);
}
backdrop_set_message(t('Country settings saved.'));
}
/**
* Disables a country so it remains installed but is no longer selectable.
*
* @param $country_id
* The ISO 3166-1 numeric country code.
*/
function uc_country_disable($country_id) {
$result = db_query("SELECT * FROM {uc_countries} WHERE country_id = :id", array(':id' => $country_id));
if ($country = $result->fetchObject()) {
if ($country->version > 0) {
db_update('uc_countries')
->fields(array(
'version' => -$country->version,
))
->condition('country_id', $country_id)
->execute();
backdrop_set_message(t('@country disabled.', array('@country' => t($country->country_name))));
}
else {
backdrop_set_message(t('@country is already disabled.', array('@country' => t($country->country_name))), 'error');
}
}
else {
backdrop_set_message(t('Attempted to disable an invalid country.'), 'error');
}
backdrop_goto('admin/store/settings/countries');
}
/**
* Enables a disabled country.
*
* @param $country_id
* The ISO 3166-1 numeric country code.
*/
function uc_country_enable($country_id) {
$result = db_query("SELECT * FROM {uc_countries} WHERE country_id = :id", array(':id' => $country_id));
if ($country = $result->fetchObject()) {
if ($country->version < 0) {
db_update('uc_countries')
->fields(array(
'version' => abs($country->version),
))
->condition('country_id', $country_id)
->execute();
backdrop_set_message(t('@country enabled.', array('@country' => t($country->country_name))));
}
else {
backdrop_set_message(t('@country is already enabled.', array('@country' => t($country->country_name))), 'error');
}
}
else {
backdrop_set_message(t('Attempted to enable an invalid country.'), 'error');
}
backdrop_goto('admin/store/settings/countries');
}
/**
* Form to completely remove a country.
*
* @param $country_id
* The ISO 3166-1 numeric country code.
*
* @see uc_country_remove_form_submit()
* @ingroup forms
*/
function uc_country_remove_form($form, &$form_state, $country_id) {
// Fetch the country name from the database.
$country = t(db_query("SELECT country_name FROM {uc_countries} WHERE country_id = :id", array(':id' => $country_id))->fetchField());
// If orders exist for this country, show a warning message prior to removal.
if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] != t('Remove') && module_exists('uc_order')) {
$count = db_query("SELECT COUNT(order_id) FROM {uc_orders} WHERE delivery_country = :delivery_country OR billing_country = :billing_country", array(':delivery_country' => $country_id, ':billing_country' => $country_id))->fetchField();
if ($count > 0) {
backdrop_set_message(t('Warning: @count orders were found with addresses in this country. Removing this country now will cause errors to show on those order pages. You might consider simply disabling this country instead.', array('@count' => $count)), 'error');
}
}
// Store the country ID in the form array for processing.
$form['country_id'] = array(
'#type' => 'value',
'#value' => $country_id,
);
return confirm_form($form, t('Are you sure you want to remove @country from the system?', array('@country' => $country)), 'admin/store/settings/countries', NULL, t('Remove'));
}
/**
* Form submission handler for uc_country_remove_form().
*
* @see uc_country_remove_form()
*/
function uc_country_remove_form_submit($form, &$form_state) {
$country_id = $form_state['values']['country_id'];
$result = db_query("SELECT * FROM {uc_countries} WHERE country_id = :id", array(':id' => $country_id));
if (!($country = $result->fetchObject())) {
backdrop_set_message(t('Attempted to remove an invalid country.'), 'error');
backdrop_goto('admin/store/settings/countries');
}
db_delete('uc_countries')
->condition('country_id', $country_id)
->execute();
db_delete('uc_zones')
->condition('zone_country_id', $country_id)
->execute();
config_clear('uc_store.settings', 'uc_address_format_' . $country_id);
$func_base = _uc_country_import_include($country_id, $country->version);
if ($func_base !== FALSE) {
$func = $func_base . '_uninstall';
if (function_exists($func)) {
$func();
}
}
backdrop_set_message(t('@country removed.', array('@country' => t($country->country_name))));
backdrop_goto('admin/store/settings/countries');
}
/**
* Updates a country definition to a specific CIF file version.
*
* @param $country_id
* The ISO 3166-1 numeric country code.
* @param $version
* Version number of CIF file.
*/
function uc_country_update($country_id, $version) {
$result = db_query("SELECT * FROM {uc_countries} WHERE country_id = :id", array(':id' => $country_id));
if (!($country = $result->fetchObject())) {
backdrop_set_message(t('Attempted to update an invalid country.'));
backdrop_goto('admin/store/settings/countries');
}
if ($version < $country->version) {
backdrop_set_message(t('You cannot update to a previous version.'));
backdrop_goto('admin/store/settings/countries');
}
$func_base = _uc_country_import_include($country_id, $version);
if ($func_base !== FALSE) {
$func = $func_base . '_update';
if (function_exists($func)) {
for ($i = $country->version + 1; $i <= $version; $i++) {
$func($i);
}
}
db_update('uc_countries')
->fields(array(
'version' => $version,
))
->condition('country_id', $country_id)
->execute();
backdrop_set_message(t('Country update complete.'));
}
else {
backdrop_set_message(t('Attempted to update an invalid country.'));
}
backdrop_goto('admin/store/settings/countries');
}
/**
* Imports an Ubercart country file by filename.
*
* @param $file
* The filename of the country to import.
*
* @return
* TRUE or FALSE indicating whether or not the country was imported.
*/
function uc_country_import($file) {
require_once backdrop_get_path('module', 'uc_store') . '/countries/' . $file;
$pieces = explode('_', substr($file, 0, strlen($file) - 4));
$country_id = $pieces[count($pieces) - 2];
$version = $pieces[count($pieces) - 1];
$country = substr($file, 0, strlen($file) - strlen($country_id) - strlen($version) - 6);
$func = $country . '_install';
if (function_exists($func)) {
$func();
return TRUE;
}
return FALSE;
}
/**
* Includes the appropriate country file and return the base for hooks.
*
* @param $country_id
* ISO 3166-1 numeric country code for the CIF file to import.
* @param $version
* Version number of the CIF to import.
*
* @return
* A string containing the portion of the filename holding the country name.
*/
function _uc_country_import_include($country_id, $version) {
$dir = backdrop_get_path('module', 'uc_store') . '/countries/';
$match = '_' . $country_id . '_' . $version . '.cif';
$matchlen = strlen($match);
$countries = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== FALSE) {
switch (filetype($dir . $file)) {
case 'file':
if (substr($file, -$matchlen) == $match) {
require_once $dir . $file;
return substr($file, 0, strlen($file) - $matchlen);
}
break;
}
}
closedir($dh);
}
}
return FALSE;
}
/**
* Performs country actions after validating the CSRF-prevention token.
*
* @param $function
* The function name to run.
* @param $param
* Parameter to pass to the function specified.
* @param $param2
* (optional) Parameter to pass to the function specified.
*/
function _uc_country_perform_country_action($function, $param, $param2 = NULL) {
if (isset($_GET['token']) && backdrop_valid_token($_GET['token'], 'uc_country_op_link')) {
call_user_func($function, $param, $param2);
}
else {
return MENU_ACCESS_DENIED;
}
}