PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Catalog/Helper/Product/Compare.php

https://bitbucket.org/jit_bec/shopifine
PHP | 314 lines | 138 code | 37 blank | 139 comment | 10 complexity | 50a40182591af964e50e588ae05c4086 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 Product Compare Helper
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Helper_Product_Compare extends Mage_Core_Helper_Url
  34. {
  35. /**
  36. * Product Compare Items Collection
  37. *
  38. * @var Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection
  39. */
  40. protected $_itemCollection;
  41. /**
  42. * Product Comapare Items Collection has items flag
  43. *
  44. * @var bool
  45. */
  46. protected $_hasItems;
  47. /**
  48. * Allow used Flat catalog product for product compare items collection
  49. *
  50. * @var bool
  51. */
  52. protected $_allowUsedFlat = true;
  53. /**
  54. * Customer id
  55. *
  56. * @var null|int
  57. */
  58. protected $_customerId = null;
  59. /**
  60. * Retrieve Catalog Session instance
  61. *
  62. * @return Mage_Catalog_Model_Session
  63. */
  64. protected function _getSession()
  65. {
  66. return Mage::getSingleton('catalog/session');
  67. }
  68. /**
  69. * Retrieve compare list url
  70. *
  71. * @return string
  72. */
  73. public function getListUrl()
  74. {
  75. $itemIds = array();
  76. foreach ($this->getItemCollection() as $item) {
  77. $itemIds[] = $item->getId();
  78. }
  79. $params = array(
  80. 'items'=>implode(',', $itemIds),
  81. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
  82. );
  83. return $this->_getUrl('catalog/product_compare', $params);
  84. }
  85. /**
  86. * Get parameters used for build add product to compare list urls
  87. *
  88. * @param Mage_Catalog_Model_Product $product
  89. * @return array
  90. */
  91. protected function _getUrlParams($product)
  92. {
  93. return array(
  94. 'product' => $product->getId(),
  95. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
  96. );
  97. }
  98. /**
  99. * Retrieve url for adding product to conpare list
  100. *
  101. * @param Mage_Catalog_Model_Product $product
  102. * @return string
  103. */
  104. public function getAddUrl($product)
  105. {
  106. return $this->_getUrl('catalog/product_compare/add', $this->_getUrlParams($product));
  107. }
  108. /**
  109. * Retrive add to wishlist url
  110. *
  111. * @param Mage_Catalog_Model_Product $product
  112. * @return string
  113. */
  114. public function getAddToWishlistUrl($product)
  115. {
  116. $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
  117. $params = array(
  118. 'product'=>$product->getId(),
  119. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
  120. );
  121. return $this->_getUrl('wishlist/index/add', $params);
  122. }
  123. /**
  124. * Retrive add to cart url
  125. *
  126. * @param Mage_Catalog_Model_Product $product
  127. * @return string
  128. */
  129. public function getAddToCartUrl($product)
  130. {
  131. $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
  132. $params = array(
  133. 'product'=>$product->getId(),
  134. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
  135. );
  136. return $this->_getUrl('checkout/cart/add', $params);
  137. }
  138. /**
  139. * Retrieve remove item from compare list url
  140. *
  141. * @param $item
  142. * @return string
  143. */
  144. public function getRemoveUrl($item)
  145. {
  146. $params = array(
  147. 'product'=>$item->getId(),
  148. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
  149. );
  150. return $this->_getUrl('catalog/product_compare/remove', $params);
  151. }
  152. /**
  153. * Retrieve clear compare list url
  154. *
  155. * @return string
  156. */
  157. public function getClearListUrl()
  158. {
  159. $params = array(
  160. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
  161. );
  162. return $this->_getUrl('catalog/product_compare/clear', $params);
  163. }
  164. /**
  165. * Retrieve compare list items collection
  166. *
  167. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection
  168. */
  169. public function getItemCollection()
  170. {
  171. if (!$this->_itemCollection) {
  172. $this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
  173. ->useProductItem(true)
  174. ->setStoreId(Mage::app()->getStore()->getId());
  175. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  176. $this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
  177. } elseif ($this->_customerId) {
  178. $this->_itemCollection->setCustomerId($this->_customerId);
  179. } else {
  180. $this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
  181. }
  182. Mage::getSingleton('catalog/product_visibility')
  183. ->addVisibleInSiteFilterToCollection($this->_itemCollection);
  184. /* Price data is added to consider item stock status using price index */
  185. $this->_itemCollection->addPriceData();
  186. $this->_itemCollection->addAttributeToSelect('name')
  187. ->addUrlRewrite()
  188. ->load();
  189. /* update compare items count */
  190. $this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection));
  191. }
  192. return $this->_itemCollection;
  193. }
  194. /**
  195. * Calculate cache product compare collection
  196. *
  197. * @param bool $logout
  198. * @return Mage_Catalog_Helper_Product_Compare
  199. */
  200. public function calculate($logout = false)
  201. {
  202. // first visit
  203. if (!$this->_getSession()->hasCatalogCompareItemsCount() && !$this->_customerId) {
  204. $count = 0;
  205. } else {
  206. /** @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Compare_Item_Collection */
  207. $collection = Mage::getResourceModel('catalog/product_compare_item_collection')
  208. ->useProductItem(true);
  209. if (!$logout && Mage::getSingleton('customer/session')->isLoggedIn()) {
  210. $collection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
  211. } elseif ($this->_customerId) {
  212. $collection->setCustomerId($this->_customerId);
  213. } else {
  214. $collection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
  215. }
  216. /* Price data is added to consider item stock status using price index */
  217. $collection->addPriceData();
  218. Mage::getSingleton('catalog/product_visibility')
  219. ->addVisibleInSiteFilterToCollection($collection);
  220. $count = $collection->getSize();
  221. }
  222. $this->_getSession()->setCatalogCompareItemsCount($count);
  223. return $this;
  224. }
  225. /**
  226. * Retrieve count of items in compare list
  227. *
  228. * @return int
  229. */
  230. public function getItemCount()
  231. {
  232. if (!$this->_getSession()->hasCatalogCompareItemsCount()) {
  233. $this->calculate();
  234. }
  235. return $this->_getSession()->getCatalogCompareItemsCount();
  236. }
  237. /**
  238. * Check has items
  239. *
  240. * @return bool
  241. */
  242. public function hasItems()
  243. {
  244. return $this->getItemCount() > 0;
  245. }
  246. /**
  247. * Set is allow used flat (for collection)
  248. *
  249. * @param bool $flag
  250. * @return Mage_Catalog_Helper_Product_Compare
  251. */
  252. public function setAllowUsedFlat($flag)
  253. {
  254. $this->_allowUsedFlat = (bool)$flag;
  255. return $this;
  256. }
  257. /**
  258. * Retrieve is allow used flat (for collection)
  259. *
  260. * @return bool
  261. */
  262. public function getAllowUsedFlat()
  263. {
  264. return $this->_allowUsedFlat;
  265. }
  266. /**
  267. * Setter for customer id
  268. *
  269. * @param int $id
  270. * @return Mage_Catalog_Helper_Product_Compare
  271. */
  272. public function setCustomerId($id)
  273. {
  274. $this->_customerId = $id;
  275. return $this;
  276. }
  277. }