1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Product_CompareController extends Mage_Core_Controller_Front_Action
36: {
37: 38: 39: 40: 41:
42: protected $_cookieCheckActions = array('add');
43:
44: 45: 46: 47: 48:
49: protected $_customerId = null;
50:
51: public function indexAction()
52: {
53: $items = $this->getRequest()->getParam('items');
54:
55: if ($beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
56: Mage::getSingleton('catalog/session')
57: ->setBeforeCompareUrl(Mage::helper('core')->urlDecode($beforeUrl));
58: }
59:
60: if ($items) {
61: $items = explode(',', $items);
62: $list = Mage::getSingleton('catalog/product_compare_list');
63: $list->addProducts($items);
64: $this->_redirect('*/*/*');
65: return;
66: }
67:
68: $this->loadLayout();
69: $this->renderLayout();
70: }
71:
72: 73: 74:
75: public function addAction()
76: {
77: $productId = (int) $this->getRequest()->getParam('product');
78: if ($productId
79: && (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())
80: ) {
81: $product = Mage::getModel('catalog/product')
82: ->setStoreId(Mage::app()->getStore()->getId())
83: ->load($productId);
84:
85: if ($product->getId()) {
86: Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
87: Mage::getSingleton('catalog/session')->addSuccess(
88: $this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()))
89: );
90: Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
91: }
92:
93: Mage::helper('catalog/product_compare')->calculate();
94: }
95:
96: $this->_redirectReferer();
97: }
98:
99: 100: 101:
102: public function removeAction()
103: {
104: if ($productId = (int) $this->getRequest()->getParam('product')) {
105: $product = Mage::getModel('catalog/product')
106: ->setStoreId(Mage::app()->getStore()->getId())
107: ->load($productId);
108:
109: if($product->getId()) {
110:
111: $item = Mage::getModel('catalog/product_compare_item');
112: if(Mage::getSingleton('customer/session')->isLoggedIn()) {
113: $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
114: } elseif ($this->_customerId) {
115: $item->addCustomerData(
116: Mage::getModel('customer/customer')->load($this->_customerId)
117: );
118: } else {
119: $item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
120: }
121:
122: $item->loadByProduct($product);
123:
124: if($item->getId()) {
125: $item->delete();
126: Mage::getSingleton('catalog/session')->addSuccess(
127: $this->__('The product %s has been removed from comparison list.', $product->getName())
128: );
129: Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item));
130: Mage::helper('catalog/product_compare')->calculate();
131: }
132: }
133: }
134:
135: if (!$this->getRequest()->getParam('isAjax', false)) {
136: $this->_redirectReferer();
137: }
138: }
139:
140: 141: 142:
143: public function clearAction()
144: {
145: $items = Mage::getResourceModel('catalog/product_compare_item_collection');
146:
147: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
148: $items->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
149: } elseif ($this->_customerId) {
150: $items->setCustomerId($this->_customerId);
151: } else {
152: $items->setVisitorId(Mage::getSingleton('log/visitor')->getId());
153: }
154:
155:
156: $session = Mage::getSingleton('catalog/session');
157:
158: try {
159: $items->clear();
160: $session->addSuccess($this->__('The comparison list was cleared.'));
161: Mage::helper('catalog/product_compare')->calculate();
162: } catch (Mage_Core_Exception $e) {
163: $session->addError($e->getMessage());
164: } catch (Exception $e) {
165: $session->addException($e, $this->__('An error occurred while clearing comparison list.'));
166: }
167:
168: $this->_redirectReferer();
169: }
170:
171: 172: 173: 174: 175: 176:
177: public function setCustomerId($id)
178: {
179: $this->_customerId = $id;
180: return $this;
181: }
182: }
183: