PageRenderTime 126ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/controllers/basket/controller.php

https://bitbucket.org/pooshonk/esw
PHP | 257 lines | 190 code | 39 blank | 28 comment | 26 complexity | f1bb309a858da03f342ac5846cdc285d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Basketcontroller {
  3. /**
  4. * Registry object reference
  5. */
  6. private $registry;
  7. private $contents;
  8. private $embedded = false;
  9. private $basket;
  10. public function __construct( PeacockCarterFrameworkRegistry $registry, $directCall )
  11. {
  12. $this->registry = $registry;
  13. require_once( FRAMEWORK_PATH . 'models/store/basket.php');
  14. $this->basket = new Basket( $this->registry );
  15. $this->basket->checkBasket();
  16. if( $directCall == true )
  17. {
  18. // temp
  19. $sql = "SELECT c.ID FROM content c, content_types t, content_versions v, content_versions_pages p WHERE c.type=t.ID AND t.reference='page' AND p.version_id=v.ID AND v.ID=c.current_revision AND c.`order` >= 0 ORDER BY c.`order` ASC LIMIT 1";
  20. $this->registry->getObject('db')->executeQuery( $sql );
  21. $p = $this->registry->getObject('db')->getRows();
  22. $this->registry->getObject('menubuilder')->buildMenu( $p['ID'] );
  23. $urlBits = $this->registry->getURLBits();
  24. if( !isset( $urlBits[1] ) )
  25. {
  26. $this->viewBasket();
  27. }
  28. else
  29. {
  30. switch( $urlBits[1] )
  31. {
  32. case 'view':
  33. $this->viewBasket();
  34. break;
  35. case 'add-product':
  36. echo $this->addProduct( $urlBits[2], 1);
  37. break;
  38. case 'update':
  39. $this->updateBasket();
  40. break;
  41. case 'remove-product':
  42. $this->removeProduct( intval( $urlBits[2] ) );
  43. break;
  44. default:
  45. $this->viewBasket();
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. /**
  52. * Add product to the basket
  53. * @param String productPath the product reference
  54. * @param int $quantity the quantity of the product
  55. * @return String a message for the controller
  56. */
  57. public function addProduct( $productPath, $quantity=1 )
  58. {
  59. // have we run the checkBasket method yet?
  60. if( ! $this->basket->isChecked() == true ) { $this->basket->checkBasket(); }
  61. $response = $this->basket->addProduct( $productPath, $quantity );
  62. //echo $response;
  63. if( $response == 'success' )
  64. {
  65. $this->registry->redirectUser( array( 'products', 'view', $productPath), 'Product added', 'The product has been added to your basket', false );
  66. }
  67. elseif( $response == 'stock' )
  68. {
  69. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php','footer.tpl.php');
  70. $this->registry->getObject('template')->getPage()->addTag('heading', 'Out of stock' );
  71. $this->registry->getObject('template')->getPage()->addTag('message', 'Sorry, that product is out of stock, and could not be added to your basket.' );
  72. }
  73. elseif( $response == 'noproduct' )
  74. {
  75. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php','footer.tpl.php');
  76. $this->registry->getObject('template')->getPage()->addTag('heading', 'Product not found' );
  77. $this->registry->getObject('template')->getPage()->addTag('message', 'Sorry, that product was not found.' );
  78. }
  79. }
  80. public function viewBasket()
  81. {
  82. if( $this->basket->isEmpty() )
  83. {
  84. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/basket/empty.tpl.php','footer.tpl.php');
  85. }
  86. else
  87. {
  88. $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/basket/view.tpl.php','footer.tpl.php');
  89. $contents = $this->basket->getContents();
  90. $products = array();
  91. //echo '<pre>' . print_r( $contents, true ) . '</pre>';
  92. foreach( $contents as $reference => $data )
  93. {
  94. //echo '<pre>' . print_r( $contents, true ) . '</pre>';
  95. $data['basket_id'] = $data['basket'];
  96. $data['basket'] = '';
  97. $data['uniqueproductreference'] = $reference;
  98. $products[] = $data;
  99. }
  100. $basketCache = $this->registry->getObject('db')->cacheData( $products );
  101. $shippingMethodsSQL = "SELECT ID as shipping_method_id, name as shipping_method_name FROM store_shipping_methods WHERE active=1 ORDER BY is_default DESC, `order` ASC";
  102. $methods = array();
  103. $this->registry->getObject('db')->executeQuery( $shippingMethodsSQL );
  104. while( $m = $this->registry->getObject('db')->getRows() )
  105. {
  106. if( $m['shipping_method_id'] == $this->basket->getShippingMethod() )
  107. {
  108. $m['shipping_method_selected'] = "selected='selected'";
  109. }
  110. else
  111. {
  112. $m['shipping_method_selected'] = '';
  113. }
  114. $methods[] = $m;
  115. }
  116. $methodsCache = $this->registry->GetObject('db')->cacheData( $methods );
  117. $this->registry->getObject('template')->getPage()->addTag( 'voucher_code', $this->basket->getVoucherCode() );
  118. //echo $this->basket->getVoucherCode();
  119. $notice = $this->basket->getVoucherNotice();
  120. if( $notice == '' )
  121. {
  122. $this->registry->getObject('template')->getPage()->addTag( 'voucher_notice', '' );
  123. }
  124. else
  125. {
  126. $this->registry->getObject('template')->addTemplateBit( 'voucher_notice', 'store/basket/voucher_notice.tpl.php' );
  127. $this->registry->getObject('template')->getPage()->addPPTag( 'the_voucher_notice', $notice );
  128. }
  129. $paymentMethodsSQL = "SELECT ID as payment_method_id, name as payment_method_name FROM store_payment_methods WHERE active=1 ORDER BY `order`";
  130. $this->registry->getObject('db')->executeQuery( $paymentMethodsSQL );
  131. $methods = array();
  132. while( $m = $this->registry->getObject('db')->getRows() )
  133. {
  134. if( $m['payment_method_id'] == $this->basket->getPaymentMethod() )
  135. {
  136. $m['payment_method_selected'] = "selected='selected'";
  137. }
  138. else
  139. {
  140. $m['payment_method_selected'] = '';
  141. }
  142. $methods[] = $m;
  143. }
  144. $cache = $this->registry->getObject('db')->cacheData( $methods );
  145. $this->registry->getObject('template')->getPage()->addTag( 'payment_methods', array( 'DATA', $cache ) );
  146. $this->registry->getObject('template')->getPage()->addTag( 'shipping_methods', array( 'DATA', $methodsCache ) );
  147. $this->registry->getObject('template')->getPage()->addTag( 'basket_items', array( 'DATA', $basketCache ) );
  148. $this->registry->getObject('template')->getPage()->addTag( 'basket_subtotal', $this->basket->getCost() );
  149. $this->registry->getObject('template')->getPage()->addTag( 'shipping_costs', $this->basket->getShippingCost() );
  150. $this->registry->getObject('template')->getPage()->addTag( 'basket_total', $this->basket->getTotal() );
  151. }
  152. }
  153. /**
  154. * Small basket - prepare small embedded basket
  155. * @return void
  156. */
  157. public function smallBasket()
  158. {
  159. if( $this->basket->isChecked() == false ) { $this->basket->checkBasket(); }
  160. // set our embedded property
  161. $this->embedded = true;
  162. // check that the basket is not empty
  163. if( $this->basket->isEmpty() == false )
  164. {
  165. // basket isn't empty so use the basket template, and set the numBasketItems and basketCost template variables
  166. $this->registry->getObject('template')->addTemplateBit('basket', 'basket.tpl.php');
  167. $this->registry->getObject('template')->getPage()->addPPTag('numBasketItems', $this->basket->getNumProducts() );
  168. $this->registry->getObject('template')->getPage()->addPPTag('basketCost', $this->basket->getTotal());
  169. $this->registry->getObject('template')->getPage()->addPPTag('shippingCost', $this->basket->getShippingCost());
  170. }
  171. else
  172. {
  173. // basket is empty - so use the empty basket template
  174. $this->registry->getObject('template')->addTemplateBit('basket', 'basket-empty.tpl.php');
  175. }
  176. }
  177. /**
  178. * Update the shopping basket
  179. */
  180. private function updateBasket()
  181. {
  182. if( isset( $_POST['shipping_method'] ) ) { $this->basket->setShippingMethod( intval( $_POST['shipping_method'] ) ); }
  183. if( ! $this->basket->isChecked() == true ) { $this->basket->checkBasket(); }
  184. foreach( $this->basket->getContents() as $pid => $data )
  185. {
  186. // get the product rows basket ID
  187. $bid = $data['basket'];
  188. if( intval( $_POST['qty_' . $bid ] ) == 0 )
  189. {
  190. $this->basket->removeProduct( $bid );
  191. }
  192. else
  193. {
  194. $this->basket->updateProductQuantity( $bid, intval( $_POST['qty_' . $bid] ) );
  195. }
  196. }
  197. $this->basket->setPaymentMethod( intval( $_POST['payment_method'] ) );
  198. $this->basket->setShippingMethod( intval( $_POST['shipping_method'] ) );
  199. $this->basket->setVoucherCode( $this->registry->getObject('db')->sanitizeData( $_POST['voucher_code'] ) );
  200. // save the extra processing by marking embedded as false
  201. $this->embedded = false;
  202. if( isset( $_POST['checkout'] ) )
  203. {
  204. header('Location: ' . $this->registry->buildURL(array('checkout'), '', false ) );
  205. exit();
  206. }
  207. else
  208. {
  209. $this->registry->redirectUser(array('basket'), 'Basket updated', 'Your shopping basket has been updated', false );
  210. }
  211. }
  212. public function removeProduct( $bid )
  213. {
  214. $this->basket->removeProduct( $bid );
  215. $this->registry->redirectUser(array('basket') , 'Product removed', 'The product has been removed from your basket', false );
  216. }
  217. }
  218. ?>