PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Catalog/controllers/Product/CompareController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 182 lines | 102 code | 22 blank | 58 comment | 14 complexity | 798207c8ad555a5b5315ced5294b3614 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog comapare controller
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Product_CompareController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * Action list where need check enabled cookie
  37. *
  38. * @var array
  39. */
  40. protected $_cookieCheckActions = array('add');
  41. /**
  42. * Customer id
  43. *
  44. * @var null|int
  45. */
  46. protected $_customerId = null;
  47. public function indexAction()
  48. {
  49. $items = $this->getRequest()->getParam('items');
  50. if ($beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
  51. Mage::getSingleton('catalog/session')
  52. ->setBeforeCompareUrl(Mage::helper('core')->urlDecode($beforeUrl));
  53. }
  54. if ($items) {
  55. $items = explode(',', $items);
  56. $list = Mage::getSingleton('catalog/product_compare_list');
  57. $list->addProducts($items);
  58. $this->_redirect('*/*/*');
  59. return;
  60. }
  61. $this->loadLayout();
  62. $this->renderLayout();
  63. }
  64. /**
  65. * Add item to compare list
  66. */
  67. public function addAction()
  68. {
  69. $productId = (int) $this->getRequest()->getParam('product');
  70. if ($productId
  71. && (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())
  72. ) {
  73. $product = Mage::getModel('catalog/product')
  74. ->setStoreId(Mage::app()->getStore()->getId())
  75. ->load($productId);
  76. if ($product->getId()/* && !$product->isSuper()*/) {
  77. Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
  78. Mage::getSingleton('catalog/session')->addSuccess(
  79. $this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()))
  80. );
  81. Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
  82. }
  83. Mage::helper('catalog/product_compare')->calculate();
  84. }
  85. $this->_redirectReferer();
  86. }
  87. /**
  88. * Remove item from compare list
  89. */
  90. public function removeAction()
  91. {
  92. if ($productId = (int) $this->getRequest()->getParam('product')) {
  93. $product = Mage::getModel('catalog/product')
  94. ->setStoreId(Mage::app()->getStore()->getId())
  95. ->load($productId);
  96. if($product->getId()) {
  97. /** @var $item Mage_Catalog_Model_Product_Compare_Item */
  98. $item = Mage::getModel('catalog/product_compare_item');
  99. if(Mage::getSingleton('customer/session')->isLoggedIn()) {
  100. $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
  101. } elseif ($this->_customerId) {
  102. $item->addCustomerData(
  103. Mage::getModel('customer/customer')->load($this->_customerId)
  104. );
  105. } else {
  106. $item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
  107. }
  108. $item->loadByProduct($product);
  109. if($item->getId()) {
  110. $item->delete();
  111. Mage::getSingleton('catalog/session')->addSuccess(
  112. $this->__('The product %s has been removed from comparison list.', $product->getName())
  113. );
  114. Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item));
  115. Mage::helper('catalog/product_compare')->calculate();
  116. }
  117. }
  118. }
  119. if (!$this->getRequest()->getParam('isAjax', false)) {
  120. $this->_redirectReferer();
  121. }
  122. }
  123. /**
  124. * Remove all items from comparison list
  125. */
  126. public function clearAction()
  127. {
  128. $items = Mage::getResourceModel('catalog/product_compare_item_collection');
  129. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  130. $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
  131. } elseif ($this->_customerId) {
  132. $items->setCustomerId($this->_customerId);
  133. } else {
  134. $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
  135. }
  136. /** @var $session Mage_Catalog_Model_Session */
  137. $session = Mage::getSingleton('catalog/session');
  138. try {
  139. $items->clear();
  140. $session->addSuccess($this->__('The comparison list was cleared.'));
  141. Mage::helper('catalog/product_compare')->calculate();
  142. } catch (Mage_Core_Exception $e) {
  143. $session->addError($e->getMessage());
  144. } catch (Exception $e) {
  145. $session->addException($e, $this->__('An error occurred while clearing comparison list.'));
  146. }
  147. $this->_redirectReferer();
  148. }
  149. /**
  150. * Setter for customer id
  151. *
  152. * @param int $id
  153. * @return Mage_Catalog_Product_CompareController
  154. */
  155. public function setCustomerId($id)
  156. {
  157. $this->_customerId = $id;
  158. return $this;
  159. }
  160. }