PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 225 lines | 97 code | 21 blank | 107 comment | 9 complexity | c9cb90c8affbf50fa2dd41d2d891478c 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_Reports
  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. * 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. ->setCustomerId($this->getCustomerId())
  152. ->addIndexFilter();
  153. Mage::getSingleton('catalog/product_visibility')
  154. ->addVisibleInSiteFilterToCollection($collection);
  155. $count = $collection->getSize();
  156. $this->_getSession()->setData($this->_countCacheKey, $count);
  157. return $this;
  158. }
  159. /**
  160. * Retrieve Exclude Product Ids List for Collection
  161. *
  162. * @return array
  163. */
  164. public function getExcludeProductIds()
  165. {
  166. return array();
  167. }
  168. /**
  169. * Retrieve count of product index items
  170. *
  171. * @return int
  172. */
  173. public function getCount()
  174. {
  175. if (!$this->_countCacheKey) {
  176. return 0;
  177. }
  178. if (!$this->_getSession()->hasData($this->_countCacheKey)) {
  179. $this->calculate();
  180. }
  181. return $this->_getSession()->getData($this->_countCacheKey);
  182. }
  183. /**
  184. * Clean index (visitors)
  185. *
  186. * @return Mage_Reports_Model_Product_Index_Abstract
  187. */
  188. public function clean()
  189. {
  190. $this->_getResource()->clean($this);
  191. return $this;
  192. }
  193. /**
  194. * Add product ids to current visitor/customer log
  195. * @param array $productIds
  196. * @return Mage_Reports_Model_Product_Index_Abstract
  197. */
  198. public function registerIds($productIds)
  199. {
  200. $this->_getResource()->registerIds($this, $productIds);
  201. $this->_getSession()->unsData($this->_countCacheKey);
  202. return $this;
  203. }
  204. }