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:
28: /**
29: * Catalog Compare Item Model
30: *
31: * @method Mage_Catalog_Model_Resource_Product_Compare_Item _getResource()
32: * @method Mage_Catalog_Model_Resource_Product_Compare_Item getResource()
33: * @method Mage_Catalog_Model_Product_Compare_Item setVisitorId(int $value)
34: * @method Mage_Catalog_Model_Product_Compare_Item setCustomerId(int $value)
35: * @method int getProductId()
36: * @method Mage_Catalog_Model_Product_Compare_Item setProductId(int $value)
37: * @method int getStoreId()
38: * @method Mage_Catalog_Model_Product_Compare_Item setStoreId(int $value)
39: *
40: * @category Mage
41: * @package Mage_Catalog
42: * @author Magento Core Team <core@magentocommerce.com>
43: */
44: class Mage_Catalog_Model_Product_Compare_Item extends Mage_Core_Model_Abstract
45: {
46:
47: /**
48: * Prefix of model events names
49: *
50: * @var string
51: */
52: protected $_eventPrefix = 'catalog_compare_item';
53:
54: /**
55: * Parameter name in event
56: *
57: * In observe method you can use $observer->getEvent()->getItem() in this case
58: *
59: * @var string
60: */
61: protected $_eventObject = 'item';
62:
63: /**
64: * Initialize resourse model
65: *
66: */
67: protected function _construct()
68: {
69: $this->_init('catalog/product_compare_item');
70: }
71:
72: /**
73: * Retrieve Resource instance
74: *
75: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item
76: */
77: protected function _getResource()
78: {
79: return parent::_getResource();
80: }
81:
82: /**
83: * Set current store before save
84: *
85: * @return Mage_Catalog_Model_Product_Compare_Item
86: */
87: protected function _beforeSave()
88: {
89: parent::_beforeSave();
90: if (!$this->hasStoreId()) {
91: $this->setStoreId(Mage::app()->getStore()->getId());
92: }
93:
94: return $this;
95: }
96:
97: /**
98: * Add customer data from customer object
99: *
100: * @param Mage_Customer_Model_Customer $customer
101: * @return Mage_Catalog_Model_Product_Compare_Item
102: */
103: public function addCustomerData(Mage_Customer_Model_Customer $customer)
104: {
105: $this->setCustomerId($customer->getId());
106: return $this;
107: }
108:
109: /**
110: * Set visitor
111: *
112: * @param int $visitorId
113: * @return Mage_Catalog_Model_Product_Compare_Item
114: */
115: public function addVisitorId($visitorId)
116: {
117: $this->setVisitorId($visitorId);
118: return $this;
119: }
120:
121: /**
122: * Load compare item by product
123: *
124: * @param mixed $product
125: * @return Mage_Catalog_Model_Product_Compare_Item
126: */
127: public function loadByProduct($product)
128: {
129: $this->_getResource()->loadByProduct($this, $product);
130: return $this;
131: }
132:
133: /**
134: * Set product data
135: *
136: * @param mixed $product
137: * @return Mage_Catalog_Model_Product_Compare_Item
138: */
139: public function addProductData($product)
140: {
141: if ($product instanceof Mage_Catalog_Model_Product) {
142: $this->setProductId($product->getId());
143: }
144: else if(intval($product)) {
145: $this->setProductId(intval($product));
146: }
147:
148: return $this;
149: }
150:
151: /**
152: * Retrieve data for save
153: *
154: * @return array
155: */
156: public function getDataForSave()
157: {
158: $data = array();
159: $data['customer_id'] = $this->getCustomerId();
160: $data['visitor_id'] = $this->getVisitorId();
161: $data['product_id'] = $this->getProductId();
162:
163: return $data;
164: }
165:
166: /**
167: * Customer login bind process
168: *
169: * @return Mage_Catalog_Model_Product_Compare_Item
170: */
171: public function bindCustomerLogin()
172: {
173: $this->_getResource()->updateCustomerFromVisitor($this);
174:
175: Mage::helper('catalog/product_compare')->setCustomerId($this->getCustomerId())->calculate();
176: return $this;
177: }
178:
179: /**
180: * Customer logout bind process
181: *
182: * @param Varien_Event_Observer $observer
183: * @return Mage_Catalog_Model_Product_Compare_Item
184: */
185: public function bindCustomerLogout(Varien_Event_Observer $observer = null)
186: {
187: $this->_getResource()->purgeVisitorByCustomer($this);
188:
189: Mage::helper('catalog/product_compare')->calculate(true);
190: return $this;
191: }
192:
193: /**
194: * Clean compare items
195: *
196: * @return Mage_Catalog_Model_Product_Compare_Item
197: */
198: public function clean()
199: {
200: $this->_getResource()->clean($this);
201: return $this;
202: }
203:
204: /**
205: * Retrieve Customer Id if loggined
206: *
207: * @return int
208: */
209: public function getCustomerId()
210: {
211: if (!$this->hasData('customer_id')) {
212: $customerId = Mage::getSingleton('customer/session')->getCustomerId();
213: $this->setData('customer_id', $customerId);
214: }
215: return $this->getData('customer_id');
216: }
217:
218: /**
219: * Retrieve Visitor Id
220: *
221: * @return int
222: */
223: public function getVisitorId()
224: {
225: if (!$this->hasData('visitor_id')) {
226: $visitorId = Mage::getSingleton('log/visitor')->getId();
227: $this->setData('visitor_id', $visitorId);
228: }
229: return $this->getData('visitor_id');
230: }
231: }
232: