PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Tag/Model/Resource/Indexer/Summary.php

https://bitbucket.org/acidel/buykoala
PHP | 265 lines | 163 code | 24 blank | 78 comment | 9 complexity | 9e67701e164f4f24c2ab88b1ce1ca579 MD5 | raw file
  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_Tag
  23. * @copyright Copyright (c) 2011 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. * Tag Indexer Model
  28. *
  29. * @category Mage
  30. * @package Mage_Tag
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Tag_Model_Resource_Indexer_Summary extends Mage_Catalog_Model_Resource_Product_Indexer_Abstract
  34. {
  35. /**
  36. * Define main table
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('tag/summary', 'tag_id');
  42. }
  43. /**
  44. * Process tag save
  45. *
  46. * @param Mage_Index_Model_Event $event
  47. * @return Mage_Tag_Model_Resource_Indexer_Summary
  48. */
  49. public function tagSave(Mage_Index_Model_Event $event)
  50. {
  51. $data = $event->getNewData();
  52. if (empty($data['tag_reindex_tag_id'])) {
  53. return $this;
  54. }
  55. return $this->aggregate($data['tag_reindex_tag_id']);
  56. }
  57. /**
  58. * Process tag relation save
  59. *
  60. * @param Mage_Index_Model_Event $event
  61. * @return Mage_Tag_Model_Resource_Indexer_Summary
  62. */
  63. public function tagRelationSave(Mage_Index_Model_Event $event)
  64. {
  65. $data = $event->getNewData();
  66. if (empty($data['tag_reindex_tag_id'])) {
  67. return $this;
  68. }
  69. return $this->aggregate($data['tag_reindex_tag_id']);
  70. }
  71. /**
  72. * Process product save.
  73. * Method is responsible for index support when product was saved.
  74. *
  75. * @param Mage_Index_Model_Event $event
  76. * @return Mage_Tag_Model_Resource_Indexer_Summary
  77. */
  78. public function catalogProductSave(Mage_Index_Model_Event $event)
  79. {
  80. $data = $event->getNewData();
  81. if (empty($data['tag_reindex_required'])) {
  82. return $this;
  83. }
  84. $tagIds = Mage::getModel('tag/tag_relation')
  85. ->setProductId($event->getEntityPk())
  86. ->getRelatedTagIds();
  87. return $this->aggregate($tagIds);
  88. }
  89. /**
  90. * Process product delete.
  91. * Method is responsible for index support when product was deleted
  92. *
  93. * @param Mage_Index_Model_Event $event
  94. * @return Mage_Tag_Model_Resource_Indexer_Summary
  95. */
  96. public function catalogProductDelete(Mage_Index_Model_Event $event)
  97. {
  98. $data = $event->getNewData();
  99. if (empty($data['tag_reindex_tag_ids'])) {
  100. return $this;
  101. }
  102. return $this->aggregate($data['tag_reindex_tag_ids']);
  103. }
  104. /**
  105. * Process product massaction
  106. *
  107. * @param Mage_Index_Model_Event $event
  108. * @return Mage_Tag_Model_Resource_Indexer_Summary
  109. */
  110. public function catalogProductMassAction(Mage_Index_Model_Event $event)
  111. {
  112. $data = $event->getNewData();
  113. if (empty($data['tag_reindex_tag_ids'])) {
  114. return $this;
  115. }
  116. return $this->aggregate($data['tag_reindex_tag_ids']);
  117. }
  118. /**
  119. * Reindex all tags
  120. *
  121. * @return Mage_Tag_Model_Resource_Indexer_Summary
  122. */
  123. public function reindexAll()
  124. {
  125. return $this->aggregate();
  126. }
  127. /**
  128. * Aggregate tags by specified ids
  129. *
  130. * @param null|int|array $tagIds
  131. * @return Mage_Tag_Model_Resource_Indexer_Summary
  132. */
  133. public function aggregate($tagIds = null)
  134. {
  135. $writeAdapter = $this->_getWriteAdapter();
  136. $this->beginTransaction();
  137. try {
  138. if (!empty($tagIds)) {
  139. $writeAdapter->delete(
  140. $this->getTable('tag/summary'), array('tag_id IN(?)' => $tagIds)
  141. );
  142. } else {
  143. $writeAdapter->delete($this->getTable('tag/summary'));
  144. }
  145. $select = $writeAdapter->select()
  146. ->from(
  147. array('tr' => $this->getTable('tag/relation')),
  148. array(
  149. 'tr.tag_id',
  150. 'tr.store_id',
  151. 'customers' => 'COUNT(DISTINCT tr.customer_id)',
  152. 'products' => 'COUNT(DISTINCT tr.product_id)',
  153. 'popularity' => 'COUNT(tr.customer_id) + MIN('
  154. . $writeAdapter->getCheckSql(
  155. 'tp.base_popularity IS NOT NULL',
  156. 'tp.base_popularity',
  157. '0'
  158. )
  159. . ')',
  160. 'uses' => new Zend_Db_Expr(0), // deprecated since 1.4.0.1
  161. 'historical_uses' => new Zend_Db_Expr(0), // deprecated since 1.4.0.1
  162. 'base_popularity' => new Zend_Db_Expr(0) // deprecated since 1.4.0.1
  163. )
  164. )
  165. ->joinInner(
  166. array('cs' => $this->getTable('core/store')),
  167. 'cs.store_id = tr.store_id',
  168. array()
  169. )
  170. ->joinInner(
  171. array('pw' => $this->getTable('catalog/product_website')),
  172. 'cs.website_id = pw.website_id AND tr.product_id = pw.product_id',
  173. array()
  174. )
  175. ->joinInner(
  176. array('e' => $this->getTable('catalog/product')),
  177. 'tr.product_id = e.entity_id',
  178. array()
  179. )
  180. ->joinLeft(
  181. array('tp' => $this->getTable('tag/properties')),
  182. 'tp.tag_id = tr.tag_id AND tp.store_id = tr.store_id',
  183. array()
  184. )
  185. ->group(array(
  186. 'tr.tag_id',
  187. 'tr.store_id'
  188. ));
  189. $statusCond = $writeAdapter->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
  190. $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $statusCond);
  191. $visibilityCond = $writeAdapter
  192. ->quoteInto('!=?', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
  193. $this->_addAttributeToSelect($select, 'visibility', 'e.entity_id', 'cs.store_id', $visibilityCond);
  194. if (!empty($tagIds)) {
  195. $select->where('tr.tag_id IN(?)', $tagIds);
  196. }
  197. Mage::dispatchEvent('prepare_catalog_product_index_select', array(
  198. 'select' => $select,
  199. 'entity_field' => new Zend_Db_Expr('e.entity_id'),
  200. 'website_field' => new Zend_Db_Expr('cs.website_id'),
  201. 'store_field' => new Zend_Db_Expr('cs.store_id')
  202. ));
  203. $writeAdapter->query(
  204. $select->insertFromSelect($this->getTable('tag/summary'), array(
  205. 'tag_id',
  206. 'store_id',
  207. 'customers',
  208. 'products',
  209. 'popularity',
  210. 'uses', // deprecated since 1.4.0.1
  211. 'historical_uses', // deprecated since 1.4.0.1
  212. 'base_popularity' // deprecated since 1.4.0.1
  213. ))
  214. );
  215. $selectedFields = array(
  216. 'tag_id' => 'tag_id',
  217. 'store_id' => new Zend_Db_Expr(0),
  218. 'customers' => 'COUNT(DISTINCT customer_id)',
  219. 'products' => 'COUNT(DISTINCT product_id)',
  220. 'popularity' => 'COUNT(customer_id)',
  221. 'uses' => new Zend_Db_Expr(0), // deprecated since 1.4.0.1
  222. 'historical_uses' => new Zend_Db_Expr(0), // deprecated since 1.4.0.1
  223. 'base_popularity' => new Zend_Db_Expr(0) // deprecated since 1.4.0.1
  224. );
  225. $agregateSelect = $writeAdapter->select();
  226. $agregateSelect->from($this->getTable('tag/relation'), $selectedFields)
  227. ->group('tag_id');
  228. if (!empty($tagIds)) {
  229. $agregateSelect->where('tag_id IN(?)', $tagIds);
  230. }
  231. $writeAdapter->query(
  232. $agregateSelect->insertFromSelect($this->getTable('tag/summary'), array_keys($selectedFields))
  233. );
  234. $this->commit();
  235. } catch (Exception $e) {
  236. $this->rollBack();
  237. throw $e;
  238. }
  239. return $this;
  240. }
  241. }