Advertisement
Guest User

cart controller example

a guest
Nov 25th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Eshop\Controller;
  4.  
  5. class Cart extends \Controller\Base
  6. {
  7.     public function action_index()
  8.     {
  9.         // check if cart is empty
  10.         if(\Eshop\Cart::total_qty() == 0)
  11.         {
  12.             $view = \View::forge('modules/eshop/cart-empty');
  13.            
  14.             // breadcrumb
  15.             $view->breadcrumb = array(
  16.                 array(
  17.                     'url'   => \Uri::current(),
  18.                     'title' => __('eshop.cart.title_empty'),
  19.                 ),
  20.             );
  21.            
  22.             return $view;
  23.         }
  24.    
  25.         $view = \View::forge('modules/eshop/cart');
  26.        
  27.         // breadcrumb
  28.         $view->breadcrumb = array(
  29.             array(
  30.                 'url'   => \Uri::current(),
  31.                 'title' => __('eshop.cart.title').' - '.__('eshop.checkout.title'),
  32.             ),
  33.         );
  34.        
  35.         // load assets
  36.         // forms
  37.         \Asset::css('compiled/page-forms.css', array(), 'styles');
  38.         \Asset::css('page-forms.less', array('rel' => 'stylesheet/less'), 'dev_styles');
  39.         \Asset::js('pureSelect/pureSelect.jquery.js', array(), 'scripts');
  40.         \Asset::js('forms.js', array(), 'scripts');
  41.         // page
  42.         \Asset::css('compiled/page-cart-checkout.css', array(), 'styles');
  43.         \Asset::css('page-cart-checkout.less', array('rel' => 'stylesheet/less'), 'dev_styles');
  44.         \Asset::js('eshop/cart/js/cart.js', array(), 'scripts');
  45.        
  46.         $view->items = \Eshop\Cart::items();
  47.        
  48.         return $view;
  49.     }
  50.    
  51.     public function action_insert()
  52.     {
  53.         // reset messages
  54.         \Messages::reset();
  55.        
  56.         if (\Input::method() == 'POST')
  57.         {
  58.             \Eshop\Cart::add(array(
  59.                 'name' => \Input::post('name'),
  60.                 'id'   => \Input::post('id'),
  61.                 'qty'  => \Input::post('qty', 1),
  62.                 'url'  => \Input::post('url'),
  63.             ));
  64.            
  65.             \Messages::success(__('eshop.cart.item_was_inserted'));
  66.         }
  67.        
  68.         \Response::redirect_back(\Router::get('eshop[cart]'));
  69.     }
  70.    
  71.     public function action_remove($id)
  72.     {
  73.         // reset messages
  74.         \Messages::reset();
  75.    
  76.         \Eshop\Cart::remove($id);
  77.        
  78.         \Messages::success(__('eshop.cart.item_was_removed'));
  79.        
  80.         if(\Input::is_ajax())
  81.         {
  82.             \Messages::reset();
  83.             return $this->get_cart_contents();
  84.         }
  85.        
  86.         \Response::redirect_back(\Router::get('eshop[cart]'));
  87.     }
  88.    
  89.     public function action_update()
  90.     {
  91.         // reset messages
  92.         \Messages::reset();
  93.        
  94.         if (\Input::method() == 'POST')
  95.         {
  96.             $qtys = \Input::post('qty');
  97.            
  98.             foreach($qtys as $id => $qty)
  99.             {
  100.                 $item = \Eshop\Cart::item($id);
  101.                 $item->update('qty', $qty);
  102.             }
  103.            
  104.             \Messages::success(__('eshop.cart.cart_was_updated'));
  105.         }
  106.        
  107.         if(\Input::is_ajax())
  108.         {
  109.             \Messages::reset();
  110.             return $this->get_cart_contents();
  111.         }
  112.        
  113.         \Response::redirect_back(\Router::get('eshop[cart]'));
  114.     }
  115.    
  116.     public function action_get_cart_header()
  117.     {
  118.         return \View::forge('modules/eshop/cart/header');
  119.     }
  120.    
  121.     protected function get_cart_contents()
  122.     {
  123.         $view = \View::forge('modules/eshop/cart/contents');
  124.        
  125.         $view->items = \Eshop\Cart::items();
  126.        
  127.         return $view;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement