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.

232 lines
8.8 KiB

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