PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 228 lines | 122 code | 26 blank | 80 comment | 9 complexity | 9337688cf9f6857b41b215409ad6a794 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 Resource Model
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Reports_Model_Resource_Product_Index_Abstract extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Fields List for update in forsedSave
  37. *
  38. * @var array
  39. */
  40. protected $_fieldsForUpdate = array('store_id', 'added_at');
  41. /**
  42. * Update Customer from visitor (Customer logged in)
  43. *
  44. * @param Mage_Reports_Model_Product_Index_Abstract $object
  45. * @return Mage_Reports_Model_Resource_Product_Index_Abstract
  46. */
  47. public function updateCustomerFromVisitor(Mage_Reports_Model_Product_Index_Abstract $object)
  48. {
  49. /**
  50. * Do nothing if customer not logged in
  51. */
  52. if (!$object->getCustomerId() || !$object->getVisitorId()) {
  53. return $this;
  54. }
  55. $adapter = $this->_getWriteAdapter();
  56. $select = $adapter->select()
  57. ->from($this->getMainTable())
  58. ->where('visitor_id = ?', $object->getVisitorId());
  59. $rowSet = $select->query()->fetchAll();
  60. foreach ($rowSet as $row) {
  61. /* We need to determine if there are rows with known
  62. customer for current product.
  63. */
  64. $select = $adapter->select()
  65. ->from($this->getMainTable())
  66. ->where('customer_id = ?', $object->getCustomerId())
  67. ->where('product_id = ?', $row['product_id']);
  68. $idx = $adapter->fetchRow($select);
  69. if ($idx) {
  70. /* If we are here it means that we have two rows: one with known customer, but second just visitor is set
  71. * One row should be updated with customer_id, second should be deleted
  72. *
  73. */
  74. $adapter->delete($this->getMainTable(), array('index_id = ?' => $row['index_id']));
  75. $where = array('index_id = ?' => $idx['index_id']);
  76. $data = array(
  77. 'visitor_id' => $object->getVisitorId(),
  78. 'store_id' => $object->getStoreId(),
  79. 'added_at' => Varien_Date::now(),
  80. );
  81. } else {
  82. $where = array('index_id = ?' => $row['index_id']);
  83. $data = array(
  84. 'customer_id' => $object->getCustomerId(),
  85. 'store_id' => $object->getStoreId(),
  86. 'added_at' => Varien_Date::now()
  87. );
  88. }
  89. $adapter->update($this->getMainTable(), $data, $where);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Purge visitor data by customer (logout)
  95. *
  96. * @param Mage_Reports_Model_Product_Index_Abstract $object
  97. * @return Mage_Reports_Model_Resource_Product_Index_Abstract
  98. */
  99. public function purgeVisitorByCustomer(Mage_Reports_Model_Product_Index_Abstract $object)
  100. {
  101. /**
  102. * Do nothing if customer not logged in
  103. */
  104. if (!$object->getCustomerId()) {
  105. return $this;
  106. }
  107. $bind = array('visitor_id' => null);
  108. $where = array('customer_id = ?' => (int)$object->getCustomerId());
  109. $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
  110. return $this;
  111. }
  112. /**
  113. * Save Product Index data (forced save)
  114. *
  115. * @param Mage_Reports_Model_Product_Index_Abstract $object
  116. * @return Mage_Reports_Model_Resource_Product_Index_Abstract
  117. */
  118. public function save(Mage_Core_Model_Abstract $object)
  119. {
  120. if ($object->isDeleted()) {
  121. return $this->delete($object);
  122. }
  123. $this->_serializeFields($object);
  124. $this->_beforeSave($object);
  125. $this->_checkUnique($object);
  126. $data = $this->_prepareDataForSave($object);
  127. unset($data[$this->getIdFieldName()]);
  128. $matchFields = array('product_id', 'store_id');
  129. Mage::getResourceHelper('reports')->mergeVisitorProductIndex(
  130. $this->getMainTable(),
  131. $data,
  132. $matchFields
  133. );
  134. $this->unserializeFields($object);
  135. $this->_afterSave($object);
  136. return $this;
  137. }
  138. /**
  139. * Clean index (visitor)
  140. *
  141. * @return Mage_Reports_Model_Resource_Product_Index_Abstract
  142. */
  143. public function clean()
  144. {
  145. while (true) {
  146. $select = $this->_getReadAdapter()->select()
  147. ->from(array('main_table' => $this->getMainTable()), array($this->getIdFieldName()))
  148. ->joinLeft(
  149. array('visitor_table' => $this->getTable('log/visitor')),
  150. 'main_table.visitor_id = visitor_table.visitor_id',
  151. array())
  152. ->where('main_table.visitor_id > ?', 0)
  153. ->where('visitor_table.visitor_id IS NULL')
  154. ->limit(100);
  155. $indexIds = $this->_getReadAdapter()->fetchCol($select);
  156. if (!$indexIds) {
  157. break;
  158. }
  159. $this->_getWriteAdapter()->delete(
  160. $this->getMainTable(),
  161. $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $indexIds)
  162. );
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Add information about product ids to visitor/customer
  168. *
  169. *
  170. * @param Mage_Reports_Model_Product_Index_Abstract $object
  171. * @param array $productIds
  172. * @return Mage_Reports_Model_Resource_Product_Index_Abstract
  173. */
  174. public function registerIds(Varien_Object $object, $productIds)
  175. {
  176. $row = array(
  177. 'visitor_id' => $object->getVisitorId(),
  178. 'customer_id' => $object->getCustomerId(),
  179. 'store_id' => $object->getStoreId(),
  180. );
  181. $addedAt = Varien_Date::toTimestamp(true);
  182. $data = array();
  183. foreach ($productIds as $productId) {
  184. $productId = (int) $productId;
  185. if ($productId) {
  186. $row['product_id'] = $productId;
  187. $row['added_at'] = Varien_Date::formatDate($addedAt);
  188. $data[] = $row;
  189. }
  190. $addedAt -= ($addedAt > 0) ? 1 : 0;
  191. }
  192. $matchFields = array('product_id', 'store_id');
  193. foreach ($data as $row) {
  194. Mage::getResourceHelper('reports')->mergeVisitorProductIndex(
  195. $this->getMainTable(),
  196. $row,
  197. $matchFields
  198. );
  199. }
  200. return $this;
  201. }
  202. }