/trunk/controllers/basket/controller.php
PHP | 257 lines | 190 code | 39 blank | 28 comment | 26 complexity | f1bb309a858da03f342ac5846cdc285d MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
-
- class Basketcontroller {
-
- /**
- * Registry object reference
- */
- private $registry;
- private $contents;
- private $embedded = false;
-
- private $basket;
-
- public function __construct( PeacockCarterFrameworkRegistry $registry, $directCall )
- {
- $this->registry = $registry;
-
- require_once( FRAMEWORK_PATH . 'models/store/basket.php');
- $this->basket = new Basket( $this->registry );
- $this->basket->checkBasket();
- if( $directCall == true )
- {
- // temp
- $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";
- $this->registry->getObject('db')->executeQuery( $sql );
- $p = $this->registry->getObject('db')->getRows();
- $this->registry->getObject('menubuilder')->buildMenu( $p['ID'] );
- $urlBits = $this->registry->getURLBits();
- if( !isset( $urlBits[1] ) )
- {
- $this->viewBasket();
- }
- else
- {
- switch( $urlBits[1] )
- {
- case 'view':
- $this->viewBasket();
- break;
- case 'add-product':
- echo $this->addProduct( $urlBits[2], 1);
- break;
- case 'update':
- $this->updateBasket();
- break;
- case 'remove-product':
- $this->removeProduct( intval( $urlBits[2] ) );
- break;
- default:
- $this->viewBasket();
- break;
- }
- }
-
- }
- }
-
- /**
- * Add product to the basket
- * @param String productPath the product reference
- * @param int $quantity the quantity of the product
- * @return String a message for the controller
- */
- public function addProduct( $productPath, $quantity=1 )
- {
- // have we run the checkBasket method yet?
- if( ! $this->basket->isChecked() == true ) { $this->basket->checkBasket(); }
-
- $response = $this->basket->addProduct( $productPath, $quantity );
- //echo $response;
- if( $response == 'success' )
- {
- $this->registry->redirectUser( array( 'products', 'view', $productPath), 'Product added', 'The product has been added to your basket', false );
- }
- elseif( $response == 'stock' )
- {
- $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php','footer.tpl.php');
- $this->registry->getObject('template')->getPage()->addTag('heading', 'Out of stock' );
- $this->registry->getObject('template')->getPage()->addTag('message', 'Sorry, that product is out of stock, and could not be added to your basket.' );
- }
- elseif( $response == 'noproduct' )
- {
- $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'message.tpl.php','footer.tpl.php');
- $this->registry->getObject('template')->getPage()->addTag('heading', 'Product not found' );
- $this->registry->getObject('template')->getPage()->addTag('message', 'Sorry, that product was not found.' );
- }
-
- }
-
- public function viewBasket()
- {
- if( $this->basket->isEmpty() )
- {
- $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/basket/empty.tpl.php','footer.tpl.php');
-
- }
- else
- {
- $this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'store/basket/view.tpl.php','footer.tpl.php');
-
- $contents = $this->basket->getContents();
-
- $products = array();
- //echo '<pre>' . print_r( $contents, true ) . '</pre>';
- foreach( $contents as $reference => $data )
- {
- //echo '<pre>' . print_r( $contents, true ) . '</pre>';
- $data['basket_id'] = $data['basket'];
- $data['basket'] = '';
- $data['uniqueproductreference'] = $reference;
- $products[] = $data;
-
- }
-
- $basketCache = $this->registry->getObject('db')->cacheData( $products );
- $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";
- $methods = array();
- $this->registry->getObject('db')->executeQuery( $shippingMethodsSQL );
- while( $m = $this->registry->getObject('db')->getRows() )
- {
- if( $m['shipping_method_id'] == $this->basket->getShippingMethod() )
- {
- $m['shipping_method_selected'] = "selected='selected'";
- }
- else
- {
- $m['shipping_method_selected'] = '';
- }
- $methods[] = $m;
- }
- $methodsCache = $this->registry->GetObject('db')->cacheData( $methods );
- $this->registry->getObject('template')->getPage()->addTag( 'voucher_code', $this->basket->getVoucherCode() );
- //echo $this->basket->getVoucherCode();
- $notice = $this->basket->getVoucherNotice();
- if( $notice == '' )
- {
- $this->registry->getObject('template')->getPage()->addTag( 'voucher_notice', '' );
-
- }
- else
- {
- $this->registry->getObject('template')->addTemplateBit( 'voucher_notice', 'store/basket/voucher_notice.tpl.php' );
- $this->registry->getObject('template')->getPage()->addPPTag( 'the_voucher_notice', $notice );
- }
-
- $paymentMethodsSQL = "SELECT ID as payment_method_id, name as payment_method_name FROM store_payment_methods WHERE active=1 ORDER BY `order`";
- $this->registry->getObject('db')->executeQuery( $paymentMethodsSQL );
- $methods = array();
- while( $m = $this->registry->getObject('db')->getRows() )
- {
- if( $m['payment_method_id'] == $this->basket->getPaymentMethod() )
- {
- $m['payment_method_selected'] = "selected='selected'";
- }
- else
- {
- $m['payment_method_selected'] = '';
- }
- $methods[] = $m;
- }
- $cache = $this->registry->getObject('db')->cacheData( $methods );
- $this->registry->getObject('template')->getPage()->addTag( 'payment_methods', array( 'DATA', $cache ) );
-
-
-
- $this->registry->getObject('template')->getPage()->addTag( 'shipping_methods', array( 'DATA', $methodsCache ) );
- $this->registry->getObject('template')->getPage()->addTag( 'basket_items', array( 'DATA', $basketCache ) );
- $this->registry->getObject('template')->getPage()->addTag( 'basket_subtotal', $this->basket->getCost() );
- $this->registry->getObject('template')->getPage()->addTag( 'shipping_costs', $this->basket->getShippingCost() );
- $this->registry->getObject('template')->getPage()->addTag( 'basket_total', $this->basket->getTotal() );
-
- }
-
- }
-
- /**
- * Small basket - prepare small embedded basket
- * @return void
- */
- public function smallBasket()
- {
- if( $this->basket->isChecked() == false ) { $this->basket->checkBasket(); }
- // set our embedded property
- $this->embedded = true;
- // check that the basket is not empty
- if( $this->basket->isEmpty() == false )
- {
- // basket isn't empty so use the basket template, and set the numBasketItems and basketCost template variables
- $this->registry->getObject('template')->addTemplateBit('basket', 'basket.tpl.php');
- $this->registry->getObject('template')->getPage()->addPPTag('numBasketItems', $this->basket->getNumProducts() );
- $this->registry->getObject('template')->getPage()->addPPTag('basketCost', $this->basket->getTotal());
- $this->registry->getObject('template')->getPage()->addPPTag('shippingCost', $this->basket->getShippingCost());
- }
- else
- {
- // basket is empty - so use the empty basket template
- $this->registry->getObject('template')->addTemplateBit('basket', 'basket-empty.tpl.php');
- }
-
- }
-
- /**
- * Update the shopping basket
- */
- private function updateBasket()
- {
- if( isset( $_POST['shipping_method'] ) ) { $this->basket->setShippingMethod( intval( $_POST['shipping_method'] ) ); }
- if( ! $this->basket->isChecked() == true ) { $this->basket->checkBasket(); }
- foreach( $this->basket->getContents() as $pid => $data )
- {
- // get the product rows basket ID
- $bid = $data['basket'];
- if( intval( $_POST['qty_' . $bid ] ) == 0 )
- {
- $this->basket->removeProduct( $bid );
- }
- else
- {
- $this->basket->updateProductQuantity( $bid, intval( $_POST['qty_' . $bid] ) );
- }
- }
- $this->basket->setPaymentMethod( intval( $_POST['payment_method'] ) );
- $this->basket->setShippingMethod( intval( $_POST['shipping_method'] ) );
- $this->basket->setVoucherCode( $this->registry->getObject('db')->sanitizeData( $_POST['voucher_code'] ) );
- // save the extra processing by marking embedded as false
- $this->embedded = false;
- if( isset( $_POST['checkout'] ) )
- {
- header('Location: ' . $this->registry->buildURL(array('checkout'), '', false ) );
- exit();
- }
- else
- {
- $this->registry->redirectUser(array('basket'), 'Basket updated', 'Your shopping basket has been updated', false );
- }
-
-
- }
-
- public function removeProduct( $bid )
- {
- $this->basket->removeProduct( $bid );
- $this->registry->redirectUser(array('basket') , 'Product removed', 'The product has been removed from your basket', false );
-
- }
-
-
-
-
-
-
-
-
-
- }
-
- ?>