/** * @param Mana_Filters_Model_Filter_Attribute $filter * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @return Varien_Db_Select */ public function countItems($filter, $collection) { $select = $collection->getSelect(); $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::GROUP); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $db = $this->_getReadAdapter(); $attribute = $filter->getAttributeModel(); $selectedOptionIds = $filter->getMSelectedValues(); $isSelectedExpr = count($selectedOptionIds) ? "`eav`.`value` IN (" . implode(', ', $selectedOptionIds) . ")" : "NULL"; $fields = array('sort_order' => new Zend_Db_Expr("`o`.`sort_order`"), 'value' => new Zend_Db_Expr("`eav`.`value`"), 'label' => new Zend_Db_Expr("COALESCE(`vs`.`value`, `vg`.`value`)"), 'm_selected' => new Zend_Db_Expr($isSelectedExpr), 'm_show_selected' => new Zend_Db_Expr($filter->getFilterOptions()->getIsReverse() ? "NOT ({$isSelectedExpr})" : $isSelectedExpr)); $select->joinInner(array('eav' => $this->getTable('catalog/product_index_eav')), "`eav`.`entity_id` = `e`.`entity_id` AND\r\n {$db->quoteInto("`eav`.`attribute_id` = ?", $attribute->getAttributeId())} AND\r\n {$db->quoteInto("`eav`.`store_id` = ?", $filter->getStoreId())}", array('count' => "COUNT(DISTINCT `eav`.`entity_id`)"))->joinInner(array('o' => $this->getTable('eav/attribute_option')), "`o`.`option_id` = `eav`.`value`", null)->joinInner(array('vg' => $this->getTable('eav/attribute_option_value')), $db->quoteInto("`vg`.`option_id` = `eav`.`value` AND `vg`.`store_id` = ?", 0), null)->joinLeft(array('vs' => $this->getTable('eav/attribute_option_value')), $db->quoteInto("`vs`.`option_id` = `eav`.`value` AND `vs`.`store_id` = ?", $filter->getStoreId()), null)->columns($fields)->group($fields); //$sql = $select->__toString(); return $select; }
/** * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Mana_Filters_Model_Filter_Attribute $model * @param int[] $attributeIds * @return array */ public function optimizedCountOnCollection($collection, $model, $attributeIds) { $select = $collection->getSelect(); // reset columns, order and limitation conditions $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::GROUP); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $db = $this->_getReadAdapter(); $storeId = $model->getStoreId(); $attributeIds = implode(', ', $attributeIds); $columns = array('attribute_id' => new Zend_Db_Expr("`_oc_o`.`attribute_id`"), 'label' => new Zend_Db_Expr("COALESCE(`_oc_ls`.`value`, `_oc_lg`.`value`)"), 'value' => new Zend_Db_Expr("`_oc_o`.`option_id`"), 'sort_order' => new Zend_Db_Expr("`_oc_o`.`sort_order`"), 'count' => new Zend_Db_Expr("COUNT(DISTINCT `_oc_idx`.`entity_id`)")); $select->from(array('_oc_o' => $this->getTable('eav/attribute_option')), null)->joinLeft(array('_oc_lg' => $this->getTable('eav/attribute_option_value')), "`_oc_lg`.`option_id` = `_oc_o`.`option_id` AND `_oc_lg`.`store_id` = 0", null)->joinLeft(array('_oc_ls' => $this->getTable('eav/attribute_option_value')), $db->quoteInto("`_oc_ls`.`option_id` = `_oc_o`.`option_id` AND `_oc_ls`.`store_id` = ?", $storeId), null)->joinLeft(array('_oc_idx' => $this->getMainTable()), "`_oc_idx`.`entity_id` = `e`.`entity_id` AND " . "`_oc_idx`.`attribute_id` = `_oc_o`.`attribute_id` AND " . "`_oc_idx`.`value` = `_oc_o`.`option_id` AND " . $db->quoteInto("`_oc_idx`.`store_id` = ?", $storeId), null)->where("`_oc_o`.`attribute_id` IN ({$attributeIds})")->columns($columns)->group(array('attribute_id', 'label', 'value', 'sort_order'))->order("sort_order ASC"); $sql = $select->__toString(); return $db->fetchAll($select); }
/** * For each option visible to person as a filter choice counts how many products are there given that all the * other filters are applied * @param Mana_Filters_Model_Filter_Attribute $filter * @return array Each entry in result is int option_id => int count * This method is overridden by copying (method body was pasted from parent class and modified as needed). All * changes are marked with comments. * @see Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute::getCount() */ public function getCount($filter) { // clone select from collection with filters $select = clone $filter->getLayer()->getProductCollection()->getSelect(); // reset columns, order and limitation conditions $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::GROUP); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $connection = $this->_getReadAdapter(); $attribute = $filter->getAttributeModel(); $tableAlias = $attribute->getAttributeCode() . '_idx'; // MANA BEGIN: if there is already applied filter with the same name, then unjoin it from select. // TODO: comment on Mage::registry('mana_cat_index_from_condition') after we edit category filters $from = array(); $catIndexCondition = Mage::registry('mana_cat_index_from_condition'); foreach ($select->getPart(Zend_Db_Select::FROM) as $key => $value) { if ($key != $tableAlias) { if ($catIndexCondition && $catIndexCondition == $value['joinCondition']) { $value['joinCondition'] = Mage::registry('mana_cat_index_to_condition'); } $from[$key] = $value; } } $select->setPart(Zend_Db_Select::FROM, $from); // MANA END $conditions = array("{$tableAlias}.entity_id = e.entity_id", $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()), $connection->quoteInto("{$tableAlias}.store_id = ?", $filter->getStoreId())); $select->join(array($tableAlias => $this->getMainTable()), join(' AND ', $conditions), array('value', 'count' => "COUNT(DISTINCT {$tableAlias}.entity_id)"))->group("{$tableAlias}.value"); return $connection->fetchPairs($select); }