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

https://github.com/gryzz/crystal_magento · PHP · 153 lines · 89 code · 19 blank · 45 comment · 11 complexity · 493f03f1e756b6b50db86fb87a9c00fc 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) 2010 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. public function indexAction()
  42. {
  43. $items = $this->getRequest()->getParam('items');
  44. if ($beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
  45. Mage::getSingleton('catalog/session')
  46. ->setBeforeCompareUrl(Mage::helper('core')->urlDecode($beforeUrl));
  47. }
  48. if ($items) {
  49. $items = explode(',', $items);
  50. $list = Mage::getSingleton('catalog/product_compare_list');
  51. $list->addProducts($items);
  52. $this->_redirect('*/*/*');
  53. return;
  54. }
  55. $this->loadLayout();
  56. $this->renderLayout();
  57. }
  58. /**
  59. * Add item to compare list
  60. */
  61. public function addAction()
  62. {
  63. if ($productId = (int) $this->getRequest()->getParam('product')) {
  64. $product = Mage::getModel('catalog/product')
  65. ->setStoreId(Mage::app()->getStore()->getId())
  66. ->load($productId);
  67. if ($product->getId()/* && !$product->isSuper()*/) {
  68. Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
  69. Mage::getSingleton('catalog/session')->addSuccess(
  70. $this->__('The product %s has been added to comparison list.', Mage::helper('core')->htmlEscape($product->getName()))
  71. );
  72. Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
  73. }
  74. Mage::helper('catalog/product_compare')->calculate();
  75. }
  76. $this->_redirectReferer();
  77. }
  78. /**
  79. * Remove item from compare list
  80. */
  81. public function removeAction()
  82. {
  83. if ($productId = (int) $this->getRequest()->getParam('product')) {
  84. $product = Mage::getModel('catalog/product')
  85. ->setStoreId(Mage::app()->getStore()->getId())
  86. ->load($productId);
  87. if($product->getId()) {
  88. $item = Mage::getModel('catalog/product_compare_item');
  89. if(Mage::getSingleton('customer/session')->isLoggedIn()) {
  90. $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
  91. } else {
  92. $item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
  93. }
  94. $item->loadByProduct($product);
  95. if($item->getId()) {
  96. $item->delete();
  97. Mage::getSingleton('catalog/session')->addSuccess(
  98. $this->__('The product %s has been removed from comparison list.', $product->getName())
  99. );
  100. Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item));
  101. Mage::helper('catalog/product_compare')->calculate();
  102. }
  103. }
  104. }
  105. $this->_redirectReferer();
  106. }
  107. public function clearAction()
  108. {
  109. $items = Mage::getResourceModel('catalog/product_compare_item_collection')
  110. //->useProductItem(true)
  111. //->setStoreId(Mage::app()->getStore()->getId())
  112. ;
  113. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  114. $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
  115. }
  116. else {
  117. $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
  118. }
  119. $session = Mage::getSingleton('catalog/session');
  120. /* @var $session Mage_Catalog_Model_Session */
  121. try {
  122. $items->clear();
  123. $session->addSuccess($this->__('The comparison list was cleared.'));
  124. Mage::helper('catalog/product_compare')->calculate();
  125. }
  126. catch (Mage_Core_Exception $e) {
  127. $session->addError($e->getMessage());
  128. }
  129. catch (Exception $e) {
  130. $session->addException($e, $this->__('An error occurred while clearing comparison list.'));
  131. }
  132. $this->_redirectReferer();
  133. }
  134. }