PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Flat/Indexer.php

https://bitbucket.org/acidel/buykoala
PHP | 309 lines | 146 code | 32 blank | 131 comment | 12 complexity | add5e1b1e5d4c051011e53dd542ea020 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 Flat Indexer Model
  28. *
  29. * @method Mage_Catalog_Model_Resource_Product_Flat_Indexer _getResource()
  30. * @method Mage_Catalog_Model_Resource_Product_Flat_Indexer getResource()
  31. * @method int getEntityTypeId()
  32. * @method Mage_Catalog_Model_Product_Flat_Indexer setEntityTypeId(int $value)
  33. * @method int getAttributeSetId()
  34. * @method Mage_Catalog_Model_Product_Flat_Indexer setAttributeSetId(int $value)
  35. * @method string getTypeId()
  36. * @method Mage_Catalog_Model_Product_Flat_Indexer setTypeId(string $value)
  37. * @method string getSku()
  38. * @method Mage_Catalog_Model_Product_Flat_Indexer setSku(string $value)
  39. * @method int getHasOptions()
  40. * @method Mage_Catalog_Model_Product_Flat_Indexer setHasOptions(int $value)
  41. * @method int getRequiredOptions()
  42. * @method Mage_Catalog_Model_Product_Flat_Indexer setRequiredOptions(int $value)
  43. * @method string getCreatedAt()
  44. * @method Mage_Catalog_Model_Product_Flat_Indexer setCreatedAt(string $value)
  45. * @method string getUpdatedAt()
  46. * @method Mage_Catalog_Model_Product_Flat_Indexer setUpdatedAt(string $value)
  47. *
  48. * @category Mage
  49. * @package Mage_Catalog
  50. * @author Magento Core Team <core@magentocommerce.com>
  51. */
  52. class Mage_Catalog_Model_Product_Flat_Indexer extends Mage_Core_Model_Abstract
  53. {
  54. /**
  55. * Catalog product flat entity for indexers
  56. */
  57. const ENTITY = 'catalog_product_flat';
  58. /**
  59. * Indexers rebuild event type
  60. */
  61. const EVENT_TYPE_REBUILD = 'catalog_product_flat_rebuild';
  62. /**
  63. * Standart model resource initialization
  64. *
  65. */
  66. protected function _construct()
  67. {
  68. $this->_init('catalog/product_flat_indexer');
  69. }
  70. /**
  71. * Rebuild Catalog Product Flat Data
  72. *
  73. * @param mixed $store
  74. * @return Mage_Catalog_Model_Product_Flat_Indexer
  75. */
  76. public function rebuild($store = null)
  77. {
  78. if (is_null($store)) {
  79. $this->_getResource()->prepareFlatTables();
  80. } else {
  81. $this->_getResource()->prepareFlatTable($store);
  82. }
  83. Mage::getSingleton('index/indexer')->processEntityAction(
  84. new Varien_Object(array('id' => $store)),
  85. self::ENTITY,
  86. self::EVENT_TYPE_REBUILD
  87. );
  88. return $this;
  89. }
  90. /**
  91. * Update attribute data for flat table
  92. *
  93. * @param string $attributeCode
  94. * @param int $store
  95. * @param int|array $productIds
  96. * @return Mage_Catalog_Model_Product_Flat_Indexer
  97. */
  98. public function updateAttribute($attributeCode, $store = null, $productIds = null)
  99. {
  100. if (is_null($store)) {
  101. foreach (Mage::app()->getStores() as $store) {
  102. $this->updateAttribute($attributeCode, $store->getId(), $productIds);
  103. }
  104. return $this;
  105. }
  106. $this->_getResource()->prepareFlatTable($store);
  107. $attribute = $this->_getResource()->getAttribute($attributeCode);
  108. $this->_getResource()->updateAttribute($attribute, $store, $productIds);
  109. $this->_getResource()->updateChildrenDataFromParent($store, $productIds);
  110. return $this;
  111. }
  112. /**
  113. * Prepare datastorage for catalog product flat
  114. *
  115. * @param int $store
  116. * @return Mage_Catalog_Model_Product_Flat_Indexer
  117. */
  118. public function prepareDataStorage($store = null)
  119. {
  120. if (is_null($store)) {
  121. foreach (Mage::app()->getStores() as $store) {
  122. $this->prepareDataStorage($store->getId());
  123. }
  124. return $this;
  125. }
  126. $this->_getResource()->prepareFlatTable($store);
  127. return $this;
  128. }
  129. /**
  130. * Update events observer attributes
  131. *
  132. * @param int $store
  133. * @return Mage_Catalog_Model_Product_Flat_Indexer
  134. */
  135. public function updateEventAttributes($store = null)
  136. {
  137. if (is_null($store)) {
  138. foreach (Mage::app()->getStores() as $store) {
  139. $this->updateEventAttributes($store->getId());
  140. }
  141. return $this;
  142. }
  143. $this->_getResource()->prepareFlatTable($store);
  144. $this->_getResource()->updateEventAttributes($store);
  145. $this->_getResource()->updateRelationProducts($store);
  146. return $this;
  147. }
  148. /**
  149. * Update product status
  150. *
  151. * @param int $productId
  152. * @param int $status
  153. * @param int $store
  154. * @return Mage_Catalog_Model_Product_Flat_Indexer
  155. */
  156. public function updateProductStatus($productId, $status, $store = null)
  157. {
  158. if (is_null($store)) {
  159. foreach (Mage::app()->getStores() as $store) {
  160. $this->updateProductStatus($productId, $status, $store->getId());
  161. }
  162. return $this;
  163. }
  164. if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
  165. $this->_getResource()->updateProduct($productId, $store);
  166. $this->_getResource()->updateChildrenDataFromParent($store, $productId);
  167. }
  168. else {
  169. $this->_getResource()->removeProduct($productId, $store);
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Update Catalog Product Flat data
  175. *
  176. * @param int|array $productIds
  177. * @param int $store
  178. * @return Mage_Catalog_Model_Product_Flat_Indexer
  179. */
  180. public function updateProduct($productIds, $store = null)
  181. {
  182. if (is_null($store)) {
  183. foreach (Mage::app()->getStores() as $store) {
  184. $this->updateProduct($productIds, $store->getId());
  185. }
  186. return $this;
  187. }
  188. $resource = $this->_getResource();
  189. $resource->beginTransaction();
  190. try {
  191. $resource->removeProduct($productIds, $store);
  192. $resource->updateProduct($productIds, $store);
  193. $resource->updateRelationProducts($store, $productIds);
  194. $resource->commit();
  195. } catch (Exception $e){
  196. $resource->rollBack();
  197. throw $e;
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Save Catalog Product(s) Flat data
  203. *
  204. * @param int|array $productIds
  205. * @param int $store
  206. * @return Mage_Catalog_Model_Product_Flat_Indexer
  207. */
  208. public function saveProduct($productIds, $store = null)
  209. {
  210. if (is_null($store)) {
  211. foreach (Mage::app()->getStores() as $store) {
  212. $this->saveProduct($productIds, $store->getId());
  213. }
  214. return $this;
  215. }
  216. $resource = $this->_getResource();
  217. $resource->beginTransaction();
  218. try {
  219. $resource->removeProduct($productIds, $store);
  220. $resource->saveProduct($productIds, $store);
  221. $resource->updateRelationProducts($store, $productIds);
  222. $resource->commit();
  223. } catch (Exception $e){
  224. $resource->rollBack();
  225. throw $e;
  226. }
  227. return $this;
  228. }
  229. /**
  230. * Remove product from flat
  231. *
  232. * @param int|array $productIds
  233. * @param int $store
  234. * @return Mage_Catalog_Model_Product_Flat_Indexer
  235. */
  236. public function removeProduct($productIds, $store = null)
  237. {
  238. if (is_null($store)) {
  239. foreach (Mage::app()->getStores() as $store) {
  240. $this->removeProduct($productIds, $store->getId());
  241. }
  242. return $this;
  243. }
  244. $this->_getResource()->removeProduct($productIds, $store);
  245. return $this;
  246. }
  247. /**
  248. * Delete store process
  249. *
  250. * @param int $store
  251. * @return Mage_Catalog_Model_Product_Flat_Indexer
  252. */
  253. public function deleteStore($store)
  254. {
  255. $this->_getResource()->deleteFlatTable($store);
  256. return $this;
  257. }
  258. /**
  259. * Rebuild Catalog Product Flat Data for all stores
  260. *
  261. * @return Mage_Catalog_Model_Product_Flat_Indexer
  262. */
  263. public function reindexAll()
  264. {
  265. $this->_getResource()->reindexAll();
  266. return $this;
  267. }
  268. /**
  269. * Retrieve list of attribute codes for flat
  270. *
  271. * @return array
  272. */
  273. public function getAttributeCodes()
  274. {
  275. return $this->_getResource()->getAttributeCodes();
  276. }
  277. }