<?php
class
Company_Catalog_Model_Resource_Category_Indexer_Product
extends
Mage_Catalog_Model_Resource_Category_Indexer_Product
{
public
function
reindexAll()
{
$this
->useIdxTable(true);
$this
->beginTransaction();
try
{
$this
->clearTemporaryIndexTable();
$idxTable
=
$this
->getIdxTable();
$idxAdapter
=
$this
->_getIndexAdapter();
$stores
=
$this
->_getStoresInfo();
foreach
(
$stores
as
$storeData
) {
$storeId
=
$storeData
[
'store_id'
];
$websiteId
=
$storeData
[
'website_id'
];
$rootPath
=
$storeData
[
'root_path'
];
$rootId
=
$storeData
[
'root_id'
];
$enabledTable
=
$this
->_prepareEnabledProductsVisibility(
$websiteId
,
$storeId
);
$anchorTable
=
$this
->_prepareAnchorCategories(
$storeId
,
$rootPath
);
$select
=
$idxAdapter
->select();
$select
->from(
array
(
'cp'
=>
$this
->_categoryProductTable),
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
=>
new
Zend_Db_Expr(
'1'
),
'store_id'
=>
new
Zend_Db_Expr(
$storeId
))
)
->joinInner(
array
(
'pv'
=>
$enabledTable
),
'pv.product_id=cp.product_id'
,
array
(
'visibility'
))
->joinLeft(
array
(
'ac'
=>
$anchorTable
),
'ac.category_id=cp.category_id'
,
array
())
->where(
'ac.category_id IS NULL'
);
$query
=
$select
->insertFromSelect(
$idxTable
,
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
,
'store_id'
,
'visibility'
),
false
);
$idxAdapter
->query(
$query
);
$select
=
$idxAdapter
->select();
$select
->from(
array
(
'pv'
=>
$enabledTable
),
array
(
new
Zend_Db_Expr(
$rootId
),
'product_id'
,
new
Zend_Db_Expr(
'0'
),
new
Zend_Db_Expr(
'1'
),
new
Zend_Db_Expr(
$storeId
),
'visibility'
)
)
->joinLeft(
array
(
'cp'
=>
$this
->_categoryProductTable),
'pv.product_id=cp.product_id'
,
array
())
->where(
'cp.product_id IS NULL'
);
$query
=
$select
->insertFromSelect(
$idxTable
,
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
,
'store_id'
,
'visibility'
),
false
);
$idxAdapter
->query(
$query
);
$anchorProductsTable
=
$this
->_getAnchorCategoriesProductsTemporaryTable();
$idxAdapter
->
delete
(
$anchorProductsTable
);
$position
=
'MIN('
.
$idxAdapter
->getCheckSql(
'ca.category_id = ce.entity_id'
,
$idxAdapter
->quoteIdentifier(
'cp.position'
),
'('
.
$idxAdapter
->quoteIdentifier(
'ce.position'
).
' + 1) * '
.
'('
.
$idxAdapter
->quoteIdentifier(
'ce.level'
).
' + 1 * 10000)'
.
' + '
.
$idxAdapter
->quoteIdentifier(
'cp.position'
)
)
.
')'
;
$attributeModel
= Mage::getModel(
'eav/entity_attribute'
)->loadByCode(Mage_Catalog_Model_Category::ENTITY,
'is_active'
);
$attributeTable
=
'catalog_category_entity_'
.
$attributeModel
->getBackendType();
$select
=
$idxAdapter
->select()
->useStraightJoin(true)
->distinct(true)
->from(
array
(
'ca'
=>
$anchorTable
),
array
(
'category_id'
))
->joinInner(
array
(
'ce'
=>
$this
->_categoryTable),
$idxAdapter
->quoteIdentifier(
'ce.path'
) .
' LIKE '
.
$idxAdapter
->quoteIdentifier(
'ca.path'
) .
' OR ce.entity_id = ca.category_id'
,
array
()
)
->joinInner(
array
(
'cp'
=>
$this
->_categoryProductTable),
'cp.category_id = ce.entity_id'
,
array
(
'product_id'
)
)
->joinInner(
array
(
'ccei'
=>
$attributeTable
),
'ccei.entity_id = cp.category_id'
,
array
()
)
->joinInner(
array
(
'pv'
=>
$enabledTable
),
'pv.product_id = cp.product_id'
,
array
(
'position'
=>
$position
)
)
->where(
'ccei.attribute_id='
.
$attributeModel
->getAttributeId())
->where(
'ccei.value=1'
)
->group(
array
(
'ca.category_id'
,
'cp.product_id'
));
$query
=
$select
->insertFromSelect(
$anchorProductsTable
,
array
(
'category_id'
,
'product_id'
,
'position'
), false);
$idxAdapter
->query(
$query
);
$select
=
$idxAdapter
->select()
->from(
array
(
'ap'
=>
$anchorProductsTable
),
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
=>
$idxAdapter
->getCheckSql(
'cp.product_id > 0'
, 1, 0),
'store_id'
=>
new
Zend_Db_Expr(
$storeId
))
)
->joinLeft(
array
(
'cp'
=>
$this
->_categoryProductTable),
'cp.category_id=ap.category_id AND cp.product_id=ap.product_id'
,
array
()
)
->joinInner(
array
(
'pv'
=>
$enabledTable
),
'pv.product_id = ap.product_id'
,
array
(
'visibility'
));
$query
=
$select
->insertFromSelect(
$idxTable
,
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
,
'store_id'
,
'visibility'
),
false
);
$idxAdapter
->query(
$query
);
$select
=
$idxAdapter
->select()
->from(
array
(
'e'
=>
$this
->getTable(
'catalog/product'
)), null)
->join(
array
(
'ei'
=>
$enabledTable
),
'ei.product_id = e.entity_id'
,
array
())
->joinLeft(
array
(
'i'
=>
$idxTable
),
'i.product_id = e.entity_id AND i.category_id = :category_id AND i.store_id = :store_id'
,
array
())
->where(
'i.product_id IS NULL'
)
->columns(
array
(
'category_id'
=>
new
Zend_Db_Expr(
$rootId
),
'product_id'
=>
'e.entity_id'
,
'position'
=>
new
Zend_Db_Expr(
'0'
),
'is_parent'
=>
new
Zend_Db_Expr(
'1'
),
'store_id'
=>
new
Zend_Db_Expr(
$storeId
),
'visibility'
=>
'ei.visibility'
));
$query
=
$select
->insertFromSelect(
$idxTable
,
array
(
'category_id'
,
'product_id'
,
'position'
,
'is_parent'
,
'store_id'
,
'visibility'
),
false
);
$idxAdapter
->query(
$query
,
array
(
'store_id'
=>
$storeId
,
'category_id'
=>
$rootId
));
}
$this
->syncData();
$this
->clearTemporaryIndexTable();
$idxAdapter
->
delete
(
$enabledTable
);
$idxAdapter
->
delete
(
$anchorTable
);
$idxAdapter
->
delete
(
$anchorProductsTable
);
$this
->commit();
}
catch
(Exception
$e
) {
$this
->rollBack();
throw
$e
;
}
return
$this
;
}
}