/includes/quinta/core/controllers/ShoppingCartItemController.class.php

https://github.com/quinta/quintacms · PHP · 130 lines · 88 code · 20 blank · 22 comment · 3 complexity · b41451ab72c040643298dd274a87b245 MD5 · raw file

  1. <?php
  2. /**
  3. * ShoppingCartItemController - provides a panel for the display of a single item in the cart
  4. *
  5. * This class displays the shopping cart item with a field for adjusting the quantity
  6. *
  7. *@author Erik Winn <sidewalksoftware@gmail.com>
  8. *
  9. *@version 0.3
  10. *
  11. * @package Quinta
  12. * @subpackage Classes
  13. */
  14. class ShoppingCartItemController extends QPanel{
  15. protected $objControlBlock;
  16. // Local instance of the ShoppingCartItem
  17. protected $objShoppingCartItem;
  18. protected $objProduct;
  19. protected $fltItemTotal;
  20. public $ctlProductImage;
  21. public $lblProductName;
  22. public $lblDimensions;
  23. public $lblItemPrice;
  24. public $lblTotalPrice;
  25. public $btnRemove;
  26. public $txtQuantity;
  27. public function __construct($objControlBlock, ShoppingCartItem $objShoppingCartItem){
  28. $this->objControlBlock = $objControlBlock;
  29. $this->objShoppingCartItem = $objShoppingCartItem;
  30. try {
  31. parent::__construct($objControlBlock);
  32. } catch (QCallerException $objExc) {
  33. $objExc->IncrementOffset();
  34. throw $objExc;
  35. }
  36. $this->strTemplate = __QUINTA_CORE_VIEWS__ . '/ShoppingCartItemView.tpl.php';
  37. $this->objProduct = Product::Load($this->objShoppingCartItem->ProductId);
  38. $this->lblProductName = new QLabel($this);
  39. $this->lblProductName->HtmlEntities = false;
  40. $this->lblProductName->Text = '<a href="'. __QUINTA_SUBDIRECTORY__ . '/index.php/Products/'
  41. . $this->objProduct->Id . '">' . $this->objProduct->Model . '</a>';
  42. $this->ctlProductImage = new ProductImageLabel($this, $this->objProduct->Id, ImageSizeType::Thumb, 48, 48);
  43. $strHeight = number_format( $this->objProduct->Height , 2 );
  44. $strWidth = number_format( $this->objProduct->Width , 2 );
  45. $this->lblDimensions = new QLabel($this);
  46. $this->lblDimensions->Text = $strHeight . ' x ' . $strWidth;
  47. $this->btnRemove = new QImageButton($this);
  48. $this->btnRemove->ImageUrl = __QUINTA_CORE_IMAGES__ . '/square_button_small_grey.gif';
  49. $this->btnRemove->ActionParameter = $this->objProduct->Id;
  50. $strWarning = Quinta::Translate('Are you SURE you want to remove this item') . ' ?';
  51. $this->btnRemove->AddAction(new QClickEvent(), new QConfirmAction($strWarning));
  52. if(IndexPage::$blnAjaxOk)
  53. $this->btnRemove->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnRemoveItem_Click'));
  54. else
  55. $this->btnRemove->AddAction(new QClickEvent(), new QServerControlAction($this, 'btnRemoveItem_Click'));
  56. $this->lblItemPrice = new QLabel($this);
  57. $this->lblItemPrice->CssClass = 'ItemPrice';
  58. $this->lblItemPrice->Text = money_format('%n',$this->objProduct->RetailPrice);
  59. $this->txtQuantity = new QIntegerTextBox($this);
  60. $this->txtQuantity->CssClass = 'ProductQtyBox';
  61. $this->txtQuantity->Text = $this->objShoppingCartItem->Quantity;
  62. $this->txtQuantity->Minimum = 0;
  63. $this->txtQuantity->Maximum = MAX_PRODUCT_QUANTITY;
  64. $this->txtQuantity->CausesValidation = $this->txtQuantity;
  65. if(IndexPage::$blnAjaxOk)
  66. $this->txtQuantity->AddAction(new QChangeEvent(), new QAjaxControlAction($this, 'InitTotal') );
  67. else
  68. $this->txtQuantity->AddAction(new QChangeEvent(), new QServerControlAction($this, 'InitTotal') );
  69. $this->fltItemTotal = $this->objProduct->RetailPrice * $this->objShoppingCartItem->Quantity;
  70. $this->lblTotalPrice = new QLabel($this);
  71. $this->lblTotalPrice->CssClass = 'ItemTotal';
  72. $this->lblTotalPrice->Text = money_format('%n', $this->fltItemTotal );
  73. }
  74. /**
  75. *@param string strFormId - the main QForm's identifier
  76. *@param string strControlId - the calling Control's id
  77. *@param string strParameters - ingored, as are the above ..
  78. */
  79. public function InitTotal( $strFormId, $strControlId, $strParameters){
  80. $this->objShoppingCartItem->Quantity = $this->txtQuantity->Text;
  81. $this->fltItemTotal = $this->objProduct->RetailPrice * $this->objShoppingCartItem->Quantity;
  82. $this->lblTotalPrice->Text = money_format('%n', $this->fltItemTotal );
  83. $this->objControlBlock->RefreshTotals();
  84. }
  85. /**
  86. * This removes the item when the user clicks the remove button - Note that the
  87. * ShoppingCart function also refreshes the page (a quick fix to redraw the items ..)
  88. */
  89. public function btnRemoveItem_Click($strFormId, $strControlId, $intProductId){
  90. IndexPage::$objShoppingCart->RemoveItem($intProductId);
  91. }
  92. public function __get($strName){
  93. switch ($strName){
  94. case 'ShoppingCartItem':
  95. return $this->objShoppingCartItem ;
  96. case 'Quantity':
  97. return $this->txtQuantity->Text ;
  98. case 'ItemTotal':
  99. return $this->fltItemTotal ;
  100. default:
  101. try {
  102. return parent::__get($strName);
  103. } catch (QCallerException $objExc) {
  104. $objExc->IncrementOffset();
  105. throw $objExc;
  106. }
  107. }
  108. }
  109. }
  110. ?>