s->getFlag('summary')) { $tableAlias = 'summary'; $joinCondition = $this->getConnection()->quoteInto(' AND '. $tableAlias .'.store_id IN(?)', $storeId); $this->getSelect() ->joinLeft( array($tableAlias => $this->getTable('tag/summary')), 'main_table.tag_id='. $tableAlias .'.tag_id' . $joinCondition, array('store_id','popularity', 'customers', 'products' )); $this->addFilterToMap('store_id', $tableAlias . '.store_id'); $this->addFilterToMap('popularity', $tableAlias . '.popularity'); $this->addFilterToMap('customers', $tableAlias . '.customers'); $this->addFilterToMap('products', $tableAlias . '.products'); $this->setFlag('summary', true); } return $this; } public function addStoresVisibility() { $this->setFlag('add_stores_after', true); return $this; } protected function _addStoresVisibility() { $tagIds = $this->getColumnValues('tag_id'); $tagsStores = array(); if (sizeof($tagIds)>0) { $select = $this->getConnection()->select() ->from($this->getTable('summary'), array('store_id', 'tag_id')) ->where('tag_id IN(?)', $tagIds); $tagsRaw = $this->getConnection()->fetchAll($select); foreach ($tagsRaw as $tag) { if (!isset($tagsStores[$tag['tag_id']])) { $tagsStores[$tag['tag_id']] = array(); } $tagsStores[$tag['tag_id']][] = $tag['store_id']; } } foreach ($this as $item) { if(isset($tagsStores[$item->getId()])) { $item->setStores($tagsStores[$item->getId()]); } else { $item->setStores(array()); } } return $this; } public function addFieldToFilter($field, $condition=null) { if ($this->getFlag('relation') && 'popularity' == $field) { // TOFIX $this->getSelect()->having($this->_getConditionSql('count(relation.tag_relation_id)', $condition)); } elseif ($this->getFlag('summary') && in_array($field, array('customers', 'products', 'uses', 'historical_uses', 'popularity'))) { $this->getSelect()->where($this->_getConditionSql('summary.'.$field, $condition)); } else { parent::addFieldToFilter($field, $condition); } return $this; } /** * Get sql for get record count * * @return string */ public function getSelectCountSql() { $this->_renderFilters(); $countSelect = clone $this->_select; $countSelect->reset(Zend_Db_Select::ORDER); $countSelect->reset(Zend_Db_Select::GROUP); $countSelect->reset(Zend_Db_Select::LIMIT_COUNT); $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET); $sql = $countSelect->__toString(); // TOFIX $sql = preg_replace('/^select\s+.+?\s+from\s+/is', 'select COUNT(DISTINCT main_table.tag_id) from ', $sql); return $sql; } /** * Add filter by store * * @param array | int $storeId * @param bool $allFilter * @return Mage_Tag_Model_Mysql4_Tag_Collection */ public function addStoreFilter($storeId, $allFilter = true) { if (!$this->getFlag('store_filter')) { $this->getSelect()->joinLeft( array('summary_store'=>$this->getTable('summary')), 'main_table.tag_id = summary_store.tag_id' ); $this->getSelect()->where('summary_store.store_id IN (?)', $storeId); $this->getSelect()->group('summary_store.tag_id'); if($this->getFlag('relation') && $allFilter) { $this->getSelect()->where('relation.store_id IN (?)', $storeId); } if($this->getFlag('prelation') && $allFilter) { $this->getSelect()->where('prelation.store_id IN (?)', $storeId); } $this->setFlag('store_filter', true); } return $this; } public function setActiveFilter() { $statusActive = Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE; $this->getSelect()->where('relation.active = ?', $statusActive); if($this->getFlag('prelation')) { $this->getSelect()->where('prelation.active = ?', $statusActive); } return $this; } public function addStatusFilter($status) { $this->getSelect()->where('main_table.status = ?', $status); return $this; } public function addProductFilter($productId) { $this->addFieldToFilter('relation.product_id', $productId); if($this->getFlag('prelation')) { $this->addFieldToFilter('prelation.product_id', $productId); } return $this; } public function addCustomerFilter($customerId) { $this->getSelect() ->where('relation.customer_id = ?', $customerId); if($this->getFlag('prelation')) { $this->getSelect() ->where('prelation.customer_id = ?', $customerId); } return $this; } public function addTagGroup() { $this->getSelect()->group('main_table.tag_id'); return $this; } public function joinRel() { $this->setFlag('relation', true); $this->getSelect()->joinLeft(array('relation'=>$this->getTable('tag/relation')), 'main_table.tag_id=relation.tag_id'); return $this; } } €€€ƒX€€€‡@$€€€Œ$j#]e#]èà3­#]­#]ÅiVF!]·í+¶Þ€€€„ @l€