Skip to content

Commit 1efc2b2

Browse files
author
damu
committed
updated functionality to catch category and product save/update event and flag to reindex data. @todo:: if saved product is new then flag based on brand/manufacturer assigned to a product
1 parent 585f172 commit 1efc2b2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

app/code/community/Technooze/Tindexer/Model/Indexer.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Technooze_Tindexer_Model_Indexer extends Mage_Index_Model_Indexer_Abstract
3333
// var to protect multiple runs
3434
protected $_registered = false;
3535
protected $_processed = false;
36+
protected $_categoryId = 0;
37+
protected $_productIds = array();
3638

3739
/**
3840
* not sure why this is required.
@@ -94,6 +96,9 @@ protected function _registerProductEvent(Mage_Index_Model_Event $event)
9496

9597
if ($eventType == Mage_Index_Model_Event::TYPE_SAVE || $eventType == Mage_Index_Model_Event::TYPE_MASS_ACTION) {
9698
$process = $event->getProcess();
99+
$this->_productIds = $event->getDataObject()->getData('product_ids');
100+
$this->flagIndexRequired($this->_productIds, 'products');
101+
97102
$process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
98103
}
99104
}
@@ -113,6 +118,9 @@ protected function _registerCategoryEvent(Mage_Index_Model_Event $event)
113118

114119
if ($category->getIsChangedProductList() || $category->getAffectedCategoryIds()) {
115120
$process = $event->getProcess();
121+
$this->_categoryId = $event->getDataObject()->getData('entity_id');
122+
$this->flagIndexRequired($this->_categoryId, 'categories');
123+
116124
$process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
117125
}
118126
}
@@ -123,9 +131,21 @@ protected function _processEvent(Mage_Index_Model_Event $event){
123131
}
124132
}
125133

126-
public function reindexAll(){
127-
// reindex all data | initFilteredProductsCount
134+
public function flagIndexRequired($ids=array(), $type='products'){
135+
$ids = (array)$ids;
128136
$collection = Mage::getModel('tindexer/products')->getCollection();
137+
$filter = array();
138+
foreach($ids as $id){
139+
$filter[] = array('like' => "%,{$id},%");
140+
}
141+
$collection->addFieldToFilter($type, $filter);
142+
$collection->setDataToAll('flag', 1);
143+
$collection->save();
144+
}
145+
146+
public function reindexAll(){
147+
// reindex all data which are flagged 1 | initFilteredProductsCount
148+
$collection = Mage::getModel('tindexer/products')->getCollection()->addFieldToFilter('flag', 1);
129149
foreach($collection as $v){
130150
try{
131151
Mage::getModel('tindexer/products')->initFilteredProductsCount('brand', $v->getData('attr_id'), $v->getData('tindexer_id'));

app/code/community/Technooze/Tindexer/Model/Products.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,15 @@ public function initFilteredProductsCount($filter = 'brand', $optionId = 0, $id
185185
->addAttributeToFilter($filter,$optionId)
186186
->addStoreFilter()
187187
;
188+
189+
// ---
190+
$includedCategories = $includedProducts = array();
188191
if($collection->count()){
189192
foreach($collection as $v){
190193
$cats = $this->getProductCategories($v);
194+
$includedProducts[] = $v->getId();
191195
foreach($cats as $cat){
196+
$includedCategories[] = $cat;
192197
if(isset($this->_filteredProducts[$optionId][$cat])){
193198
$this->_filteredProducts[$optionId][$cat]++;
194199
continue;
@@ -205,18 +210,18 @@ public function initFilteredProductsCount($filter = 'brand', $optionId = 0, $id
205210
$data = array(
206211
'attr_id' => $optionId,
207212
'count' => $counts,
213+
'products' => ','.implode(',', $includedProducts).',',
214+
'categories' => ','.implode(',', $includedCategories).',',
208215
'flag' => 0,
209216
'store_id' => 0,
210217
);
211218
$model->addData($data);
212219
$model->save();
213-
Mage::log('save');
214220
} catch (Exception $e) {
215221
Mage::log($e->getMessage());
216222
return;
217223
}
218224
} else {
219-
Mage::log('nill');
220225
}
221226
return;
222227
}

app/code/community/Technooze/Tindexer/sql/tindexer_setup/mysql4-install-0.1.0.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
`tindexer_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
2626
`attr_id` int(10) DEFAULT NULL,
2727
`count` text,
28+
`categories` text,
29+
`products` text,
2830
`store_id` int(11) NOT NULL DEFAULT '1',
2931
`flag` int(1) NOT NULL DEFAULT '0',
3032
`update` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

0 commit comments

Comments
 (0)