PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/content/checkout/shipping_address.php

https://github.com/subhabrata/oscommerce
PHP | 253 lines | 184 code | 49 blank | 20 comment | 63 complexity | 0286fb5858532dca57e176ac46ef7c74 MD5 | raw file
  1. <?php
  2. /*
  3. $Id:shipping_address.php 188 2005-09-15 02:25:52 +0200 (Do, 15 Sep 2005) hpdl $
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2006 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. require('includes/classes/address_book.php');
  12. class osC_Checkout_Shipping_address extends osC_Template {
  13. /* Private variables */
  14. var $_module = 'shipping_address',
  15. $_group = 'checkout',
  16. $_page_title,
  17. $_page_contents = 'checkout_shipping_address.php',
  18. $_page_image = 'table_background_delivery.gif';
  19. /* Class constructor */
  20. function osC_Checkout_Shipping_address() {
  21. global $osC_Session, $osC_ShoppingCart, $osC_Customer, $osC_Services, $osC_Language, $osC_NavigationHistory, $osC_Breadcrumb;
  22. if ($osC_Customer->isLoggedOn() === false) {
  23. $osC_NavigationHistory->setSnapshot();
  24. osc_redirect(osc_href_link(FILENAME_ACCOUNT, 'login', 'SSL'));
  25. }
  26. if ($osC_ShoppingCart->hasContents() === false) {
  27. osc_redirect(osc_href_link(FILENAME_CHECKOUT, null, 'SSL'));
  28. }
  29. $this->_page_title = $osC_Language->get('shipping_address_heading');
  30. $this->addJavascriptFilename('templates/' . $this->getCode() . '/javascript/checkout_shipping_address.js');
  31. $this->addJavascriptPhpFilename('includes/form_check.js.php');
  32. // if the order contains only virtual products, forward the customer to the billing page as
  33. // a shipping address is not needed
  34. if ($osC_ShoppingCart->getContentType() == 'virtual') {
  35. $osC_ShoppingCart->resetShippingAddress();
  36. $osC_ShoppingCart->resetShippingMethod();
  37. osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'payment', 'SSL'));
  38. }
  39. // if no shipping destination address was selected, use their own address as default
  40. if ($osC_ShoppingCart->hasShippingAddress() === false) {
  41. $osC_ShoppingCart->setShippingAddress($osC_Customer->getDefaultAddressID());
  42. }
  43. if ($osC_Services->isStarted('breadcrumb')) {
  44. $osC_Breadcrumb->add($osC_Language->get('breadcrumb_checkout_shipping'), osc_href_link(FILENAME_CHECKOUT, 'shipping', 'SSL'));
  45. $osC_Breadcrumb->add($osC_Language->get('breadcrumb_checkout_shipping_address'), osc_href_link(FILENAME_CHECKOUT, $this->_module, 'SSL'));
  46. }
  47. if (($_GET[$this->_module] == 'process')) {
  48. $this->_process();
  49. }
  50. }
  51. function &getListing() {
  52. global $osC_Database, $osC_Customer;
  53. $Qaddresses = $osC_Database->query('select ab.address_book_id, 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.customers_id = :customers_id and ab.entry_country_id = c.countries_id');
  54. $Qaddresses->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
  55. $Qaddresses->bindTable(':table_zones', TABLE_ZONES);
  56. $Qaddresses->bindTable(':table_countries', TABLE_COUNTRIES);
  57. $Qaddresses->bindInt(':customers_id', $osC_Customer->getID());
  58. $Qaddresses->execute();
  59. return $Qaddresses;
  60. }
  61. /* Private methods */
  62. function _process() {
  63. global $osC_Database, $osC_Session, $osC_Language, $osC_Customer, $osC_ShoppingCart, $osC_MessageStack, $entry_state_has_zones;
  64. // process a new shipping address
  65. if (($osC_Customer->hasDefaultAddress() === false) || (!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['street_address'])) ) {
  66. if (ACCOUNT_GENDER > 0) {
  67. if (!isset($_POST['gender']) || (($_POST['gender'] != 'm') && ($_POST['gender'] != 'f'))) {
  68. $osC_MessageStack->add('checkout_address', $osC_Language->get('field_customer_gender_error'));
  69. }
  70. }
  71. if (!isset($_POST['firstname']) || (strlen(trim($_POST['firstname'])) < ACCOUNT_FIRST_NAME)) {
  72. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_first_name_error'), ACCOUNT_FIRST_NAME));
  73. }
  74. if (!isset($_POST['lastname']) || (strlen(trim($_POST['lastname'])) < ACCOUNT_LAST_NAME)) {
  75. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_last_name_error'), ACCOUNT_LAST_NAME));
  76. }
  77. if (ACCOUNT_COMPANY > 0) {
  78. if (!isset($_POST['company']) || (strlen(trim($_POST['company'])) < ACCOUNT_COMPANY)) {
  79. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_company_error'), ACCOUNT_COMPANY));
  80. }
  81. }
  82. if (!isset($_POST['street_address']) || (strlen(trim($_POST['street_address'])) < ACCOUNT_STREET_ADDRESS)) {
  83. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_street_address_error'), ACCOUNT_STREET_ADDRESS));
  84. }
  85. if (ACCOUNT_SUBURB > 0) {
  86. if (!isset($_POST['suburb']) || (strlen(trim($_POST['suburb'])) < ACCOUNT_SUBURB)) {
  87. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_suburb_error'), ACCOUNT_SUBURB));
  88. }
  89. }
  90. if (ACCOUNT_POST_CODE > 0) {
  91. if (!isset($_POST['postcode']) || (strlen(trim($_POST['postcode'])) < ACCOUNT_POST_CODE)) {
  92. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_post_code_error'), ACCOUNT_POST_CODE));
  93. }
  94. }
  95. if (!isset($_POST['city']) || (strlen(trim($_POST['city'])) < ACCOUNT_CITY)) {
  96. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_city_error'), ACCOUNT_CITY));
  97. }
  98. if (ACCOUNT_STATE > 0) {
  99. $zone_id = 0;
  100. $Qcheck = $osC_Database->query('select zone_id from :table_zones where zone_country_id = :zone_country_id limit 1');
  101. $Qcheck->bindTable(':table_zones', TABLE_ZONES);
  102. $Qcheck->bindInt(':zone_country_id', $_POST['country']);
  103. $Qcheck->execute();
  104. $entry_state_has_zones = ($Qcheck->numberOfRows() > 0);
  105. $Qcheck->freeResult();
  106. if ($entry_state_has_zones === true) {
  107. $Qzone = $osC_Database->query('select zone_id from :table_zones where zone_country_id = :zone_country_id and zone_code like :zone_code');
  108. $Qzone->bindTable(':table_zones', TABLE_ZONES);
  109. $Qzone->bindInt(':zone_country_id', $_POST['country']);
  110. $Qzone->bindValue(':zone_code', $_POST['state']);
  111. $Qzone->execute();
  112. if ($Qzone->numberOfRows() === 1) {
  113. $zone_id = $Qzone->valueInt('zone_id');
  114. } else {
  115. $Qzone = $osC_Database->query('select zone_id from :table_zones where zone_country_id = :zone_country_id and zone_name like :zone_name');
  116. $Qzone->bindTable(':table_zones', TABLE_ZONES);
  117. $Qzone->bindInt(':zone_country_id', $_POST['country']);
  118. $Qzone->bindValue(':zone_name', $_POST['state'] . '%');
  119. $Qzone->execute();
  120. if ($Qzone->numberOfRows() === 1) {
  121. $zone_id = $Qzone->valueInt('zone_id');
  122. } else {
  123. $osC_MessageStack->add('checkout_address', $osC_Language->get('field_customer_state_select_pull_down_error'));
  124. }
  125. }
  126. $Qzone->freeResult();
  127. } else {
  128. if (strlen(trim($_POST['state'])) < ACCOUNT_STATE) {
  129. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_state_error'), ACCOUNT_STATE));
  130. }
  131. }
  132. }
  133. if ( (is_numeric($_POST['country']) === false) || ($_POST['country'] < 1) ) {
  134. $osC_MessageStack->add('checkout_address', $osC_Language->get('field_customer_country_error'));
  135. }
  136. if (ACCOUNT_TELEPHONE > 0) {
  137. if (!isset($_POST['telephone']) || (strlen(trim($_POST['telephone'])) < ACCOUNT_TELEPHONE)) {
  138. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_telephone_number_error'), ACCOUNT_TELEPHONE));
  139. }
  140. }
  141. if (ACCOUNT_FAX > 0) {
  142. if (!isset($_POST['fax']) || (strlen(trim($_POST['fax'])) < ACCOUNT_FAX)) {
  143. $osC_MessageStack->add('checkout_address', sprintf($osC_Language->get('field_customer_fax_number_error'), ACCOUNT_FAX));
  144. }
  145. }
  146. if ($osC_MessageStack->size('checkout_address') === 0) {
  147. $Qab = $osC_Database->query('insert into :table_address_book (customers_id, entry_gender, entry_company, entry_firstname, entry_lastname, entry_street_address, entry_suburb, entry_postcode, entry_city, entry_state, entry_country_id, entry_zone_id, entry_telephone, entry_fax) values (:customers_id, :entry_gender, :entry_company, :entry_firstname, :entry_lastname, :entry_street_address, :entry_suburb, :entry_postcode, :entry_city, :entry_state, :entry_country_id, :entry_zone_id, :entry_telephone, :entry_fax)');
  148. $Qab->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
  149. $Qab->bindInt(':customers_id', $osC_Customer->getID());
  150. $Qab->bindValue(':entry_gender', (((ACCOUNT_GENDER > -1) && isset($_POST['gender']) && (($_POST['gender'] == 'm') || ($_POST['gender'] == 'f'))) ? $_POST['gender'] : ''));
  151. $Qab->bindValue(':entry_company', ((ACCOUNT_COMPANY > -1) ? trim($_POST['company']) : ''));
  152. $Qab->bindValue(':entry_firstname', trim($_POST['firstname']));
  153. $Qab->bindValue(':entry_lastname', trim($_POST['lastname']));
  154. $Qab->bindValue(':entry_street_address', trim($_POST['street_address']));
  155. $Qab->bindValue(':entry_suburb', ((ACCOUNT_SUBURB > -1) ? trim($_POST['suburb']) : ''));
  156. $Qab->bindValue(':entry_postcode', ((ACCOUNT_POST_CODE > -1) ? trim($_POST['postcode']) : ''));
  157. $Qab->bindValue(':entry_city', trim($_POST['city']));
  158. $Qab->bindValue(':entry_state', ((ACCOUNT_STATE > -1) ? (($zone_id > 0) ? '' : trim($_POST['state'])) : ''));
  159. $Qab->bindInt(':entry_country_id', $_POST['country']);
  160. $Qab->bindInt(':entry_zone_id', ((ACCOUNT_STATE > -1) ? (($zone_id > 0) ? $zone_id : 0) : ''));
  161. $Qab->bindValue(':entry_telephone', ((ACCOUNT_TELEPHONE > -1) ? trim($_POST['telephone']) : ''));
  162. $Qab->bindValue(':entry_fax', ((ACCOUNT_FAX > -1) ? trim($_POST['fax']) : ''));
  163. $Qab->execute();
  164. if ($Qab->affectedRows() === 1) {
  165. $address_book_id = $osC_Database->nextID();
  166. if ($osC_Customer->hasDefaultAddress() === false) {
  167. $Qcustomer = $osC_Database->query('update :table_customers set customers_default_address_id = :customers_default_address_id where customers_id = :customers_id');
  168. $Qcustomer->bindTable(':table_customers', TABLE_CUSTOMERS);
  169. $Qcustomer->bindInt(':customers_default_address_id', $address_book_id);
  170. $Qcustomer->bindInt(':customers_id', $osC_Customer->getID());
  171. $Qcustomer->execute();
  172. $osC_Customer->setCountryID($_POST['country']);
  173. $osC_Customer->setZoneID($zone_id);
  174. $osC_Customer->setDefaultAddressID($address_book_id);
  175. }
  176. $osC_ShoppingCart->setShippingAddress($address_book_id);
  177. osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'shipping', 'SSL'));
  178. } else {
  179. $osC_MessageStack->add('checkout_address', 'Error inserting into address book table.');
  180. }
  181. }
  182. // process the selected shipping destination
  183. } elseif (isset($_POST['address'])) {
  184. $osC_ShoppingCart->setShippingAddress($_POST['address']);
  185. $Qcheck = $osC_Database->query('select address_book_id from :table_address_book where address_book_id = :address_book_id and customers_id = :customers_id limit 1');
  186. $Qcheck->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
  187. $Qcheck->bindInt(':address_book_id', $osC_ShoppingCart->getShippingAddress('id'));
  188. $Qcheck->bindInt(':customers_id', $osC_Customer->getID());
  189. $Qcheck->execute();
  190. if ($Qcheck->numberOfRows() === 1) {
  191. osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'shipping', 'SSL'));
  192. } else {
  193. $osC_ShoppingCart->resetShippingAddress();
  194. }
  195. } else {
  196. $osC_ShoppingCart->setShippingAddress($osC_Customer->getDefaultAddressID());
  197. osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'shipping', 'SSL'));
  198. }
  199. }
  200. }
  201. ?>