/** * @param Mana_Filters_Model_Filter_Attribute $filter * @param Mana_Filters_Model_Item[] $items */ public function registerItems($filter, $items) { if ($this->isEnabled()) { $this->_attributeItems[$filter->getAttributeModel()->getId()] = $items; $this->_allItems += $items; } }
/** * 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); }
/** * @param Mana_Filters_Model_Filter_Attribute $filter * @param Mana_Filters_Model_Item[] $items */ public function registerItems($filter, $items) { if ($this->isEnabled()) { $storeId = $this->_prepareArraysForCurrentStore(); $this->_attributeItems[$storeId][$filter->getAttributeModel()->getId()] = $items; $this->_allItems[$storeId] += $items; } }
/** * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Mana_Filters_Model_Filter_Attribute $model * @param array $value * @return Mana_Filters_Resource_Filter_Attribute */ public function applyToCollection($collection, $model, $value) { $attribute = $model->getAttributeModel(); $connection = $this->_getReadAdapter(); $tableAlias = $attribute->getAttributeCode() . '_idx'; $conditions = array("{$tableAlias}.entity_id = e.entity_id", $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()), $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()), "{$tableAlias}.value in (" . implode(',', array_filter($value)) . ")"); $conditions = join(' AND ', $conditions); $collection->getSelect()->distinct()->join(array($tableAlias => $this->getMainTable()), $conditions, array()); return $this; }
/** * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Mana_Filters_Model_Filter_Attribute $model * @return Mana_Filters_Resource_Filter_Decimal */ public function countOnCollection($collection, $model) { $select = $collection->getSelect(); $select->reset(Zend_Db_Select::COLUMNS); $select->reset(Zend_Db_Select::ORDER); $select->reset(Zend_Db_Select::LIMIT_COUNT); $select->reset(Zend_Db_Select::LIMIT_OFFSET); $attributeId = $model->getAttributeModel()->getId(); $storeId = $collection->getStoreId(); $select->join(array('decimal_index' => $this->getMainTable()), 'e.entity_id = decimal_index.entity_id' . ' AND ' . $this->_getReadAdapter()->quoteInto('decimal_index.attribute_id = ?', $attributeId) . ' AND ' . $this->_getReadAdapter()->quoteInto('decimal_index.store_id = ?', $storeId), array()); $adapter = $this->_getReadAdapter(); $countExpr = new Zend_Db_Expr("COUNT(*)"); $rangeExpr = new Zend_Db_Expr("FLOOR(decimal_index.value / {$model->getRange()}) + 1"); $select->columns(array('range' => $rangeExpr, 'count' => $countExpr)); $select->group('range'); return $adapter->fetchPairs($select); }
/** * @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; }