00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Catalog 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 00028 /** 00029 * Catalog comapare controller 00030 * 00031 * @category Mage 00032 * @package Mage_Catalog 00033 * @author Magento Core Team <core@magentocommerce.com> 00034 */ 00035 class Mage_Catalog_Product_CompareController extends Mage_Core_Controller_Front_Action 00036 { 00037 /** 00038 * Action list where need check enabled cookie 00039 * 00040 * @var array 00041 */ 00042 protected $_cookieCheckActions = array('add'); 00043 00044 public function indexAction() 00045 { 00046 $items = $this->getRequest()->getParam('items'); 00047 00048 if ($beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) { 00049 Mage::getSingleton('catalog/session') 00050 ->setBeforeCompareUrl(Mage::helper('core')->urlDecode($beforeUrl)); 00051 } 00052 00053 if ($items) { 00054 $items = explode(',', $items); 00055 $list = Mage::getSingleton('catalog/product_compare_list'); 00056 $list->addProducts($items); 00057 $this->_redirect('*/*/*'); 00058 return; 00059 } 00060 00061 $this->loadLayout(); 00062 $this->renderLayout(); 00063 } 00064 00065 /** 00066 * Add item to compare list 00067 */ 00068 public function addAction() 00069 { 00070 if ($productId = (int) $this->getRequest()->getParam('product')) { 00071 $product = Mage::getModel('catalog/product') 00072 ->setStoreId(Mage::app()->getStore()->getId()) 00073 ->load($productId); 00074 00075 if ($product->getId()/* && !$product->isSuper()*/) { 00076 Mage::getSingleton('catalog/product_compare_list')->addProduct($product); 00077 Mage::getSingleton('catalog/session')->addSuccess( 00078 $this->__('Product %s successfully added to compare list', $product->getName()) 00079 ); 00080 Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product)); 00081 } 00082 00083 Mage::helper('catalog/product_compare')->calculate(); 00084 } 00085 00086 $this->_redirectReferer(); 00087 } 00088 00089 /** 00090 * Remove item from compare list 00091 */ 00092 public function removeAction() 00093 { 00094 if ($productId = (int) $this->getRequest()->getParam('product')) { 00095 $product = Mage::getModel('catalog/product') 00096 ->setStoreId(Mage::app()->getStore()->getId()) 00097 ->load($productId); 00098 00099 if($product->getId()) { 00100 $item = Mage::getModel('catalog/product_compare_item'); 00101 if(Mage::getSingleton('customer/session')->isLoggedIn()) { 00102 $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer()); 00103 } else { 00104 $item->addVisitorId(Mage::getSingleton('log/visitor')->getId()); 00105 } 00106 00107 $item->loadByProduct($product); 00108 00109 if($item->getId()) { 00110 $item->delete(); 00111 Mage::getSingleton('catalog/session')->addSuccess( 00112 $this->__('Product %s successfully removed from compare list', $product->getName()) 00113 ); 00114 Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item)); 00115 Mage::helper('catalog/product_compare')->calculate(); 00116 } 00117 } 00118 } 00119 $this->_redirectReferer(); 00120 } 00121 00122 public function clearAction() 00123 { 00124 $items = Mage::getResourceModel('catalog/product_compare_item_collection') 00125 //->useProductItem(true) 00126 //->setStoreId(Mage::app()->getStore()->getId()) 00127 ; 00128 00129 if (Mage::getSingleton('customer/session')->isLoggedIn()) { 00130 $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId()); 00131 } 00132 else { 00133 $items->setVisitorId(Mage::getSingleton('log/visitor')->getId()); 00134 } 00135 00136 $session = Mage::getSingleton('catalog/session'); 00137 /* @var $session Mage_Catalog_Model_Session */ 00138 00139 try { 00140 $items->clear(); 00141 $session->addSuccess($this->__('Compare list successfully cleared')); 00142 Mage::helper('catalog/product_compare')->calculate(); 00143 } 00144 catch (Mage_Core_Exception $e) { 00145 $session->addError($e->getMessage()); 00146 } 00147 catch (Exception $e) { 00148 $session->addException($e, $this->__('There was an error while cleared compare list')); 00149 } 00150 00151 $this->_redirectReferer(); 00152 } 00153 }