uc_store.countries.inc 14 KB

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