Example #1
0
 /**
  * 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);
 }