PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Reports/Model/Product/Index/Abstract.php

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 224 lines | 96 code | 21 blank | 107 comment | 9 complexity | 9b30ead9f84850d51815c7de3296e924 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Reports
  23. * @copyright Copyright (c) 2010 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. * Reports Product Index Abstract Model
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Reports_Model_Product_Index_Abstract extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Cache key name for Count of product index
  37. *
  38. * @var string
  39. */
  40. protected $_countCacheKey;
  41. /**
  42. * Prepare customer/visitor, store data before save
  43. *
  44. * @return Mage_Reports_Model_Product_Index_Abstract
  45. */
  46. protected function _beforeSave()
  47. {
  48. parent::_beforeSave();
  49. if (!$this->hasVisitorId()) {
  50. $this->setVisitorId($this->getVisitorId());
  51. }
  52. if (!$this->hasCustomerId()) {
  53. $this->setCustomerId($this->getCustomerId());
  54. }
  55. if (!$this->hasStoreId()) {
  56. $this->setStoreId($this->getStoreId());
  57. }
  58. if (!$this->hasAddedAt()) {
  59. $this->setAddedAt(now());
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Retrieve visitor id
  65. *
  66. * if don't exists return current visitor id
  67. *
  68. * @return int
  69. */
  70. public function getVisitorId()
  71. {
  72. if ($this->hasData('visitor_id')) {
  73. return $this->getData('visitor_id');
  74. }
  75. return Mage::getSingleton('log/visitor')->getId();
  76. }
  77. /**
  78. * Retrieve customer id
  79. *
  80. * if customer don't logged in return null
  81. *
  82. * @return int
  83. */
  84. public function getCustomerId()
  85. {
  86. if ($this->hasData('customer_id')) {
  87. return $this->getData('customer_id');
  88. }
  89. return Mage::getSingleton('customer/session')->getCustomerId();
  90. }
  91. /**
  92. * Retrieve store id
  93. *
  94. * default return current store id
  95. *
  96. * @return int
  97. */
  98. public function getStoreId()
  99. {
  100. if ($this->hasData('store_id')) {
  101. return $this->getData('store_id');
  102. }
  103. return Mage::app()->getStore()->getId();
  104. }
  105. /**
  106. * Retrieve resource instance wrapper
  107. *
  108. * @return Mage_Reports_Model_Mysql4_Product_Index_Abstract
  109. */
  110. protected function _getResource()
  111. {
  112. return parent::_getResource();
  113. }
  114. /**
  115. * On customer loggin merge visitor/customer index
  116. *
  117. * @return Mage_Reports_Model_Product_Index_Abstract
  118. */
  119. public function updateCustomerFromVisitor()
  120. {
  121. $this->_getResource()->updateCustomerFromVisitor($this);
  122. return $this;
  123. }
  124. /**
  125. * Purge visitor data by customer (logout)
  126. *
  127. * @return Mage_Reports_Model_Product_Index_Abstract
  128. */
  129. public function purgeVisitorByCustomer()
  130. {
  131. $this->_getResource()->purgeVisitorByCustomer($this);
  132. return $this;
  133. }
  134. /**
  135. * Retrieve Reports Session instance
  136. *
  137. * @return Mage_Reports_Model_Session
  138. */
  139. protected function _getSession()
  140. {
  141. return Mage::getSingleton('reports/session');
  142. }
  143. /**
  144. * Calculate count of product index items cache
  145. *
  146. * @return Mage_Reports_Model_Product_Index_Abstract
  147. */
  148. public function calculate()
  149. {
  150. $collection = $this->getCollection()
  151. ->addIndexFilter();
  152. Mage::getSingleton('catalog/product_visibility')
  153. ->addVisibleInSiteFilterToCollection($collection);
  154. $count = $collection->getSize();
  155. $this->_getSession()->setData($this->_countCacheKey, $count);
  156. return $this;
  157. }
  158. /**
  159. * Retrieve Exclude Product Ids List for Collection
  160. *
  161. * @return array
  162. */
  163. public function getExcludeProductIds()
  164. {
  165. return array();
  166. }
  167. /**
  168. * Retrieve count of product index items
  169. *
  170. * @return int
  171. */
  172. public function getCount()
  173. {
  174. if (!$this->_countCacheKey) {
  175. return 0;
  176. }
  177. if (!$this->_getSession()->hasData($this->_countCacheKey)) {
  178. $this->calculate();
  179. }
  180. return $this->_getSession()->getData($this->_countCacheKey);
  181. }
  182. /**
  183. * Clean index (visitors)
  184. *
  185. * @return Mage_Reports_Model_Product_Index_Abstract
  186. */
  187. public function clean()
  188. {
  189. $this->_getResource()->clean($this);
  190. return $this;
  191. }
  192. /**
  193. * Add product ids to current visitor/customer log
  194. * @param array $productIds
  195. * @return Mage_Reports_Model_Product_Index_Abstract
  196. */
  197. public function registerIds($productIds)
  198. {
  199. $this->_getResource()->registerIds($this, $productIds);
  200. $this->_getSession()->unsData($this->_countCacheKey);
  201. return $this;
  202. }
  203. }