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