A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.8 KiB

12 years ago
  1. <?php
  2. if(!defined('QUASICMS') ) die("No quasi.");
  3. if (!defined("SHOPPINGCARTMODULE.CLASS.PHP")){
  4. define("SHOPPINGCARTMODULE.CLASS.PHP",1);
  5. /**
  6. * Class ShoppingCartModule - mini shopping cart display
  7. *@author Erik Winn <erikwinnmail@yahoo.com>
  8. *
  9. * This module provides the mini cart view for items in the shopping cart.
  10. * It is created by default for any user that signs in. An account has one
  11. * shopping cart, when checkout occurs the items are removed from the
  12. * cart when the order is created.
  13. *
  14. * The mini view is a small box containing a hyperlinked list of the items
  15. * in the cart, clicking on an item redirects the user to the product page, clicking
  16. * on the title header ("Shopping Cart") redirects the user to a full cart view page.
  17. *
  18. *@todo
  19. * - add total price (incl. shipping) estimates
  20. *
  21. * $Id: ShoppingCartModule.class.php 286 2008-10-10 23:33:36Z erikwinn $
  22. *@version 0.1
  23. *
  24. *@copyright (C) 2008 by Erik Winn
  25. *@license GPL v.2
  26. This program is free software; you can redistribute it and/or modify
  27. it under the terms of the GNU General Public License as published by
  28. the Free Software Foundation; either version 2 of the License, or
  29. (at your option) any later version.
  30. This program is distributed in the hope that it will be useful,
  31. but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. GNU General Public License for more details.
  34. You should have received a copy of the GNU General Public License
  35. along with this program; if not, write to the Free Software
  36. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  37. *
  38. *@package Quasi
  39. * @subpackage Modules
  40. */
  41. class ShoppingCartModule extends QPanel
  42. {
  43. /**
  44. *@var ContentBlockView - objControlBlock the controlling block for the module
  45. */
  46. protected $objControlBlock;
  47. /**
  48. * @var Module objModule - local reference or instance of the module ORM object
  49. */
  50. protected $objModule;
  51. /**
  52. * @var Account objAccount - local reference to the current user's account object
  53. */
  54. protected $objAccount;
  55. /**
  56. * @var ShoppingCart objShoppingCart - local reference to the current user's shopping cart object
  57. */
  58. protected $objShoppingCart;
  59. /**
  60. * @var integer intItemCount - number of (line) items in the cart
  61. */
  62. protected $intItemCount = 0;
  63. /**
  64. * @var QPanel pnlHeader - the title box for the cart
  65. */
  66. public $pnlHeader;
  67. /**
  68. * @var QPanel pnlItemList - the panel containing the list of items
  69. */
  70. public $pnlItemList;
  71. /**
  72. * Module constructor
  73. *@param ContentBlockView - objContentBlock parent controller object.
  74. *@param Module objModule - the ORM object of the module displayed
  75. */
  76. public function __construct( ContentBlockView $objControlBlock, $objModule)
  77. {
  78. $this->objControlBlock =& $objControlBlock;
  79. $this->objModule =& $objModule;
  80. try {
  81. parent::__construct($this->objControlBlock, 'CartModule', true);
  82. } catch (QCallerException $objExc) {
  83. $objExc->IncrementOffset();
  84. throw $objExc;
  85. }
  86. $this->strTemplate = __QUASI_CORE_TEMPLATES__ . '/ShoppingCartModule.tpl.php';
  87. $this->objAccount =& IndexPage::$objAccount;
  88. $this->objShoppingCart =& IndexPage::$objShoppingCart;
  89. $this->pnlHeader = new QPanel($this, 'CartBoxHeader');
  90. $this->pnlHeader->Text = Quasi::Translate('Shopping Cart');
  91. if(IndexPage::$blnAjaxOk)
  92. $this->pnlHeader->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'pnlHeader_Click'));
  93. else
  94. $this->pnlHeader->AddAction(new QClickEvent(), new QServerControlAction($this, 'pnlHeader_Click'));
  95. $this->pnlItemList = new QPanel($this, 'CartBoxList');
  96. $this->pnlItemList->AutoRenderChildren = true;
  97. if($this->objAccount instanceof Account)
  98. $this->init();
  99. }
  100. protected function init()
  101. {
  102. $this->intItemCount = ShoppingCartItem::CountByShoppingCartId($this->objShoppingCart->Id);
  103. if($this->intItemCount > 0)
  104. $this->CreateItemList();
  105. else
  106. $this->pnlItemList->Text = Quasi::Translate('There are no items in your cart') . '.';
  107. }
  108. protected function CreateItemList()
  109. {
  110. foreach( $this->objShoppingCart->GetShoppingCartItemArray() as $objItem )
  111. {
  112. $objProduct = Product::Load($objItem->ProductId);
  113. $intQuantity = $objItem->Quantity;
  114. $pnlCartItem = new QPanel($this->pnlItemList);
  115. $href = __QUASI_SUBDIRECTORY__ . '/index.php/Products/' . $objProduct->Id ;
  116. $pnlCartItem->Text = '<a href="' . $href . '">' . $objProduct->Model . " ({$intQuantity}) </a> ";
  117. $pnlCartItem->CssClass = 'ShoppingCartItem';
  118. /* another way to do this
  119. $pnlCartItem->Text = $intQuantity . ' ' . $objProduct->Model;
  120. $pnlCartItem->ActionParameter = $objProduct->Id;
  121. $pnlCartItem->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'CartItem_Click'));
  122. */
  123. }
  124. }
  125. public function RefreshCart()
  126. {
  127. if($this->objShoppingCart instanceof ShoppingCart)
  128. {
  129. $this->MarkAsModified();
  130. $this->RemoveChildControls(true);
  131. $this->init();
  132. }
  133. }
  134. /**
  135. * This function is unused - it responds to the example alternate link shown above, currently disabled.
  136. */
  137. public function CartItem_Click($strFormId, $strControlId, $intProductId)
  138. {
  139. Qapplication::Redirect( __QUASI_SUBDIRECTORY__ . '/index.php/Products/' . $intProductId );
  140. }
  141. /**
  142. * This function is unused - it responds to a hypothetical "Check Out" button .. currently disabled.
  143. */
  144. public function btnCheckOut_Click($strFormId, $strControlId, $strParameter)
  145. {
  146. Quasi::Redirect(__QUASI_SUBDIRECTORY__ . '/index.php/CheckOut');
  147. }
  148. /**
  149. * This function responds to a click on the cart box header, redirecting the user to the
  150. * full shopping cart view page
  151. */
  152. public function pnlHeader_Click($strFormId, $strControlId, $strParameter)
  153. {
  154. Quasi::Redirect(__QUASI_SUBDIRECTORY__ . '/index.php/ShoppingCart');
  155. }
  156. public function __get($strName)
  157. {
  158. switch ($strName)
  159. {
  160. case 'Module':
  161. return $this->objModule ;
  162. case 'Account':
  163. return $this->objAccount ;
  164. case 'ShoppingCart':
  165. return $this->objShoppingCart ;
  166. case 'ItemCount':
  167. return $this->intItemCount ;
  168. default:
  169. try {
  170. return parent::__get($strName);
  171. } catch (QCallerException $objExc) {
  172. $objExc->IncrementOffset();
  173. throw $objExc;
  174. }
  175. }
  176. }
  177. public function __set($strName, $mixValue)
  178. {
  179. switch ($strName)
  180. {
  181. case 'Account':
  182. try {
  183. return ($this->objAccount = QType::Cast($mixValue, 'Account' ));
  184. } catch (QInvalidCastException $objExc) {
  185. $objExc->IncrementOffset();
  186. throw $objExc;
  187. }
  188. case 'ShoppingCart':
  189. try {
  190. return ($this->objShoppingCart = QType::Cast($mixValue, 'ShoppingCart' ));
  191. } catch (QInvalidCastException $objExc) {
  192. $objExc->IncrementOffset();
  193. throw $objExc;
  194. }
  195. case 'ItemCount':
  196. try {
  197. return ($this->intItemCount = QType::Cast($mixValue, Qtype::Integer ));
  198. } catch (QInvalidCastException $objExc) {
  199. $objExc->IncrementOffset();
  200. throw $objExc;
  201. }
  202. default:
  203. try {
  204. return (parent::__set($strName, $mixValue));
  205. } catch (QCallerException $objExc) {
  206. $objExc->IncrementOffset();
  207. throw $objExc;
  208. }
  209. }
  210. }
  211. }//end class
  212. }//end define
  213. ?>