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

https://github.com/rgranadino/magento-mirror · PHP · 179 lines · 99 code · 22 blank · 58 comment · 12 complexity · b656d7862cb2aa95b74df8691f44ba95 MD5 · raw file

  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) 2011 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. if ($productId = (int) $this->getRequest()->getParam('product')) {
  70. $product = Mage::getModel('catalog/product')
  71. ->setStoreId(Mage::app()->getStore()->getId())
  72. ->load($productId);
  73. if ($product->getId()/* && !$product->isSuper()*/) {
  74. Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
  75. Mage::getSingleton('catalog/session')->addSuccess(
  76. $this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()))
  77. );
  78. Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
  79. }
  80. Mage::helper('catalog/product_compare')->calculate();
  81. }
  82. $this->_redirectReferer();
  83. }
  84. /**
  85. * Remove item from compare list
  86. */
  87. public function removeAction()
  88. {
  89. if ($productId = (int) $this->getRequest()->getParam('product')) {
  90. $product = Mage::getModel('catalog/product')
  91. ->setStoreId(Mage::app()->getStore()->getId())
  92. ->load($productId);
  93. if($product->getId()) {
  94. /** @var $item Mage_Catalog_Model_Product_Compare_Item */
  95. $item = Mage::getModel('catalog/product_compare_item');
  96. if(Mage::getSingleton('customer/session')->isLoggedIn()) {
  97. $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
  98. } elseif ($this->_customerId) {
  99. $item->addCustomerData(
  100. Mage::getModel('customer/customer')->load($this->_customerId)
  101. );
  102. } else {
  103. $item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
  104. }
  105. $item->loadByProduct($product);
  106. if($item->getId()) {
  107. $item->delete();
  108. Mage::getSingleton('catalog/session')->addSuccess(
  109. $this->__('The product %s has been removed from comparison list.', $product->getName())
  110. );
  111. Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item));
  112. Mage::helper('catalog/product_compare')->calculate();
  113. }
  114. }
  115. }
  116. if (!$this->getRequest()->getParam('isAjax', false)) {
  117. $this->_redirectReferer();
  118. }
  119. }
  120. /**
  121. * Remove all items from comparison list
  122. */
  123. public function clearAction()
  124. {
  125. $items = Mage::getResourceModel('catalog/product_compare_item_collection');
  126. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  127. $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
  128. } elseif ($this->_customerId) {
  129. $items->setCustomerId($this->_customerId);
  130. } else {
  131. $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
  132. }
  133. /** @var $session Mage_Catalog_Model_Session */
  134. $session = Mage::getSingleton('catalog/session');
  135. try {
  136. $items->clear();
  137. $session->addSuccess($this->__('The comparison list was cleared.'));
  138. Mage::helper('catalog/product_compare')->calculate();
  139. } catch (Mage_Core_Exception $e) {
  140. $session->addError($e->getMessage());
  141. } catch (Exception $e) {
  142. $session->addException($e, $this->__('An error occurred while clearing comparison list.'));
  143. }
  144. $this->_redirectReferer();
  145. }
  146. /**
  147. * Setter for customer id
  148. *
  149. * @param int $id
  150. * @return Mage_Catalog_Product_CompareController
  151. */
  152. public function setCustomerId($id)
  153. {
  154. $this->_customerId = $id;
  155. return $this;
  156. }
  157. }