PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/magento/app/code/core/Mage/Catalog/Model/Product/Compare/Item.php

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