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

/app/code/core/Mage/Catalog/Model/Resource/Product/Indexer/Eav/Abstract.php

https://bitbucket.org/acidel/buykoala
PHP | 287 lines | 154 code | 36 blank | 97 comment | 6 complexity | 2c9cfd18ff491745b14afddaca42ea9c 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_Catalog
  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. * Catalog Product Eav Attributes abstract indexer resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  34. extends Mage_Catalog_Model_Resource_Product_Indexer_Abstract
  35. {
  36. /**
  37. * Rebuild all index data
  38. *
  39. *
  40. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  41. */
  42. public function reindexAll()
  43. {
  44. $this->useIdxTable(true);
  45. $this->beginTransaction();
  46. try {
  47. $this->clearTemporaryIndexTable();
  48. $this->_prepareIndex();
  49. $this->_prepareRelationIndex();
  50. $this->_removeNotVisibleEntityFromIndex();
  51. $this->syncData();
  52. $this->commit();
  53. } catch (Exception $e) {
  54. $this->rollBack();
  55. throw $e;
  56. }
  57. return $this;
  58. }
  59. /**
  60. * Rebuild index data by entities
  61. *
  62. *
  63. * @param int|array $processIds
  64. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  65. * @throws Exception
  66. */
  67. public function reindexEntities($processIds)
  68. {
  69. $adapter = $this->_getWriteAdapter();
  70. $this->clearTemporaryIndexTable();
  71. if (!is_array($processIds)) {
  72. $processIds = array($processIds);
  73. }
  74. $parentIds = $this->getRelationsByChild($processIds);
  75. if ($parentIds) {
  76. $processIds = array_unique(array_merge($processIds, $parentIds));
  77. }
  78. $childIds = $this->getRelationsByParent($parentIds);
  79. if ($childIds) {
  80. $processIds = array_unique(array_merge($processIds, $childIds));
  81. }
  82. $this->_prepareIndex($processIds);
  83. $this->_prepareRelationIndex($processIds);
  84. $this->_removeNotVisibleEntityFromIndex();
  85. $adapter->beginTransaction();
  86. try {
  87. // remove old index
  88. $where = $adapter->quoteInto('entity_id IN(?)', $processIds);
  89. $adapter->delete($this->getMainTable(), $where);
  90. // insert new index
  91. $this->useDisableKeys(false);
  92. $this->insertFromTable($this->getIdxTable(), $this->getMainTable());
  93. $this->useDisableKeys(true);
  94. $adapter->commit();
  95. } catch (Exception $e) {
  96. $adapter->rollBack();
  97. throw $e;
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Rebuild index data by attribute id
  103. * If attribute is not indexable remove data by attribute
  104. *
  105. *
  106. * @param int $attributeId
  107. * @param bool $isIndexable
  108. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  109. */
  110. public function reindexAttribute($attributeId, $isIndexable = true)
  111. {
  112. if (!$isIndexable) {
  113. $this->_removeAttributeIndexData($attributeId);
  114. } else {
  115. $this->clearTemporaryIndexTable();
  116. $this->_prepareIndex(null, $attributeId);
  117. $this->_prepareRelationIndex();
  118. $this->_removeNotVisibleEntityFromIndex();
  119. $this->_synchronizeAttributeIndexData($attributeId);
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Prepare data index for indexable attributes
  125. *
  126. * @param array $entityIds the entity ids limitation
  127. * @param int $attributeId the attribute id limitation
  128. */
  129. abstract protected function _prepareIndex($entityIds = null, $attributeId = null);
  130. /**
  131. * Remove Not Visible products from temporary data index
  132. *
  133. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  134. */
  135. protected function _removeNotVisibleEntityFromIndex()
  136. {
  137. $write = $this->_getWriteAdapter();
  138. $idxTable = $this->getIdxTable();
  139. $select = $write->select()
  140. ->from($idxTable, null);
  141. $condition = $write->quoteInto('=?',Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
  142. $this->_addAttributeToSelect(
  143. $select,
  144. 'visibility',
  145. $idxTable . '.entity_id',
  146. $idxTable . '.store_id',
  147. $condition
  148. );
  149. $query = $select->deleteFromSelect($idxTable);
  150. $write->query($query);
  151. return $this;
  152. }
  153. /**
  154. * Prepare data index for product relations
  155. *
  156. * @param array $parentIds the parent entity ids limitation
  157. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  158. */
  159. protected function _prepareRelationIndex($parentIds = null)
  160. {
  161. $write = $this->_getWriteAdapter();
  162. $idxTable = $this->getIdxTable();
  163. $select = $write->select()
  164. ->from(array('l' => $this->getTable('catalog/product_relation')), 'parent_id')
  165. ->join(
  166. array('cs' => $this->getTable('core/store')),
  167. '',
  168. array())
  169. ->join(
  170. array('i' => $idxTable),
  171. 'l.child_id = i.entity_id AND cs.store_id = i.store_id',
  172. array('attribute_id', 'store_id', 'value'))
  173. ->group(array(
  174. 'l.parent_id', 'i.attribute_id', 'i.store_id', 'i.value'
  175. ));
  176. if (!is_null($parentIds)) {
  177. $select->where('l.parent_id IN(?)', $parentIds);
  178. }
  179. /**
  180. * Add additional external limitation
  181. */
  182. Mage::dispatchEvent('prepare_catalog_product_index_select', array(
  183. 'select' => $select,
  184. 'entity_field' => new Zend_Db_Expr('l.parent_id'),
  185. 'website_field' => new Zend_Db_Expr('cs.website_id'),
  186. 'store_field' => new Zend_Db_Expr('cs.store_id')
  187. ));
  188. $query = $write->insertFromSelect($select, $idxTable, array(), Varien_Db_Adapter_Interface::INSERT_IGNORE);
  189. $write->query($query);
  190. return $this;
  191. }
  192. /**
  193. * Retrieve condition for retrieve indexable attribute select
  194. * the catalog/eav_attribute table must have alias is ca
  195. *
  196. * @return string
  197. */
  198. protected function _getIndexableAttributesCondition()
  199. {
  200. $conditions = array(
  201. 'ca.is_filterable_in_search > 0',
  202. 'ca.is_visible_in_advanced_search > 0',
  203. 'ca.is_filterable > 0'
  204. );
  205. return implode(' OR ', $conditions);
  206. }
  207. /**
  208. * Remove index data from index by attribute id
  209. *
  210. * @param int $attributeId
  211. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  212. */
  213. protected function _removeAttributeIndexData($attributeId)
  214. {
  215. $adapter = $this->_getWriteAdapter();
  216. $adapter->beginTransaction();
  217. try {
  218. $where = $adapter->quoteInto('attribute_id = ?', $attributeId);
  219. $adapter->delete($this->getMainTable(), $where);
  220. $adapter->commit();
  221. } catch (Exception $e) {
  222. $adapter->rollback();
  223. throw $e;
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Synchronize temporary index table with index table by attribute id
  229. *
  230. * @param int $attributeId
  231. * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
  232. * @throws Exception
  233. */
  234. protected function _synchronizeAttributeIndexData($attributeId)
  235. {
  236. $adapter = $this->_getWriteAdapter();
  237. $adapter->beginTransaction();
  238. try {
  239. // remove index by attribute
  240. $where = $adapter->quoteInto('attribute_id = ?', $attributeId);
  241. $adapter->delete($this->getMainTable(), $where);
  242. // insert new index
  243. $this->insertFromTable($this->getIdxTable(), $this->getMainTable());
  244. $adapter->commit();
  245. } catch (Exception $e) {
  246. $adapter->rollback();
  247. throw $e;
  248. }
  249. return $this;
  250. }
  251. }