Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Tim

By: a guest on Apr 24th, 2009  |  syntax: PHP  |  size: 2.57 KB  |  views: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. class CartController extends AppController
  4. {
  5.  
  6.   /**
  7.    * Add a product to the shopping cart.
  8.   */
  9.  
  10.   public function add_product($id)
  11.   {
  12.     $cart = new CartModel();
  13.     $cart->addItem($id);              // Updates the PageModel stack (session)
  14.    
  15.     $page = new PageModel();
  16.     $next = $page->popPage();
  17.    
  18.     switch($next->result) {
  19.       case 'redirect':
  20.         $this->redirect($next->url);
  21.         break;
  22.       case 'render':
  23.         $this->render($next->view);
  24.         break;
  25.     }
  26.   }
  27.  
  28.   public function details()
  29.   {
  30.     // ... display cart details ...
  31.   }
  32.  
  33. }
  34.  
  35. class CartModel extends AppModel
  36. {
  37.  
  38.   public function addItem($id)
  39.   {
  40.     $user = new UserModel();
  41.     $page = new PageModel();
  42.    
  43.     if (!$user->loggedIn()) {    
  44.       $page->pushPage('redirect', "cart/add_product/$id");
  45.       $page->pushPage('redirect', "user/login");    
  46.       return;
  47.     }
  48.    
  49.     // ... add item to the database ...
  50.    
  51.     $page->pushPage('render', 'cart_details.tpl');
  52.     $page->message = 'Item has been successfully added to your cart.';
  53.    
  54.   }
  55.  
  56. }
  57.  
  58. class UserController extends AppController
  59. {
  60.        
  61.   /**
  62.    * User login form
  63.   */
  64.        
  65.   public function login()
  66.   {
  67.    
  68.     $page = new PageModel();
  69.     $user = new UserModel();
  70.    
  71.     if ($_POST) {
  72.            
  73.           // ... code goes here to check password
  74.          
  75.           if ($login_ok) {
  76.    
  77.             // ... if login is ok, continue
  78.              
  79.       } else {
  80.      
  81.         // ... if login failed, stack the form on the view again
  82.      
  83.         $page->message = 'Invalid username or password. Try again.';
  84.         $page->pushPage('render', 'login_form.tpl');
  85.        
  86.           }
  87.      
  88.     }
  89.    
  90.     $next = $page->popPage();
  91.    
  92.     switch($next->result) {
  93.       case 'redirect':
  94.         $this->redirect($next->url);
  95.         break;
  96.       case 'render':
  97.         $this->render($next->view);
  98.         break;
  99.     }
  100.    
  101.   }
  102. }
  103.      
  104. class PageModel extends AppModel
  105. {
  106.   public function pushPage($type, $target)
  107.   {
  108.     // ... add page to top of stack
  109.   }
  110.  
  111.   public function popPage()
  112.   {
  113.     // ... get page (and remove) from top of stack
  114.   }
  115.  
  116. }
  117.  
  118.  
  119. /**
  120.  * Summary: When the user attempts to add an item to the shopping cart and they are
  121.  * not logged in, we push a login page onto the page stack. They go to the login
  122.  * page and fill it out. If the login fails, the form is pushed onto the stack. If
  123.  * the login succeeds, we automatically go back to attempting to add the item to the
  124.  * shopping cart. This time, the user is logged-in so it will be successful.
  125. */
clone this paste RAW Paste Data