00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Tag_Model_Mysql4_Tag_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00036 {
00037 protected $_isStoreFilter = false;
00038 protected $_joinFlags = array();
00039
00040 var $_map = array(
00041 'fields' => array(
00042 'tag_id' => 'main_table.tag_id'
00043 ),
00044 );
00045
00046 protected function _construct()
00047 {
00048 $this->_init('tag/tag');
00049 }
00050
00051 public function load($printQuery = false, $logQuery = false)
00052 {
00053 if ($this->isLoaded()) {
00054 return $this;
00055 }
00056 parent::load($printQuery, $logQuery);
00057 if ($this->getJoinFlag('add_stores_after')) {
00058 $this->_addStoresVisibility();
00059 }
00060 return $this;
00061 }
00062
00063 public function setJoinFlag($table)
00064 {
00065 $this->_joinFlags[$table] = true;
00066 return $this;
00067 }
00068
00069 public function getJoinFlag($table)
00070 {
00071 return isset($this->_joinFlags[$table]);
00072 }
00073
00074 public function unsetJoinFlag($table=null)
00075 {
00076 if (is_null($table)) {
00077 $this->_joinFlags = array();
00078 } elseif ($this->getJoinFlag($table)) {
00079 unset($this->_joinFlags[$table]);
00080 }
00081
00082 return $this;
00083 }
00084
00085
00086 public function limit($limit)
00087 {
00088 $this->getSelect()->limit($limit);
00089 return $this;
00090 }
00091
00092
00093 public function addPopularity($limit=null)
00094 {
00095 $this->getSelect()
00096 ->joinLeft(
00097 array('prelation'=>$this->getTable('tag/relation')),
00098 'main_table.tag_id=prelation.tag_id',
00099 array('popularity' => 'COUNT(DISTINCT relation.tag_relation_id)'
00100 ))
00101 ->group('main_table.tag_id');
00102 $this->joinRel();
00103 if (! is_null($limit)) {
00104 $this->getSelect()->limit($limit);
00105 }
00106 $this->setJoinFlag('prelation');
00107 return $this;
00108 }
00109
00110 public function addSummary($storeId)
00111 {
00112 $joinCondition = '';
00113 if (is_array($storeId)) {
00114 $joinCondition = ' AND summary.store_id IN (' . implode(',', $storeId) . ')';
00115 } else {
00116 $joinCondition = $this->getConnection()->quoteInto(' AND summary.store_id = ?', (int)$storeId);
00117 }
00118
00119 $this->getSelect()
00120 ->joinLeft(
00121 array('summary'=>$this->getTable('tag/summary')),
00122 'main_table.tag_id=summary.tag_id' . $joinCondition,
00123 array('store_id','popularity', 'customers', 'products', 'uses', 'historical_uses'
00124 ));
00125
00126 $this->setJoinFlag('summary');
00127 return $this;
00128 }
00129
00130 public function addStoresVisibility()
00131 {
00132 $this->setJoinFlag('add_stores_after');
00133 return $this;
00134 }
00135
00136 protected function _addStoresVisibility()
00137 {
00138 $tagIds = $this->getColumnValues('tag_id');
00139
00140 $tagsStores = array();
00141 if (sizeof($tagIds)>0) {
00142 $select = $this->getConnection()->select()
00143 ->from($this->getTable('summary'), array('store_id', 'tag_id'))
00144 ->where('tag_id IN(?)', $tagIds);
00145 $tagsRaw = $this->getConnection()->fetchAll($select);
00146 foreach ($tagsRaw as $tag) {
00147 if (!isset($tagsStores[$tag['tag_id']])) {
00148 $tagsStores[$tag['tag_id']] = array();
00149 }
00150
00151 $tagsStores[$tag['tag_id']][] = $tag['store_id'];
00152 }
00153 }
00154
00155 foreach ($this as $item) {
00156 if(isset($tagsStores[$item->getId()])) {
00157 $item->setStores($tagsStores[$item->getId()]);
00158 } else {
00159 $item->setStores(array());
00160 }
00161 }
00162
00163 return $this;
00164 }
00165
00166 public function addFieldToFilter($field, $condition=null)
00167 {
00168 if ($this->getJoinFlag('relation') && 'popularity' == $field) {
00169
00170 $this->getSelect()->having($this->_getConditionSql('count(relation.tag_relation_id)', $condition));
00171 } elseif ($this->getJoinFlag('summary') && in_array($field, array('customers', 'products', 'uses', 'historical_uses', 'popularity'))) {
00172 $this->getSelect()->where($this->_getConditionSql('summary.'.$field, $condition));
00173 } else {
00174 parent::addFieldToFilter($field, $condition);
00175 }
00176 return $this;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 public function getSelectCountSql()
00186 {
00187 $this->_renderFilters();
00188 $countSelect = clone $this->_select;
00189 $countSelect->reset(Zend_Db_Select::ORDER);
00190 $countSelect->reset(Zend_Db_Select::GROUP);
00191 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00192 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00193
00194 $sql = $countSelect->__toString();
00195
00196 $sql = preg_replace('/^select\s+.+?\s+from\s+/is', 'select COUNT(DISTINCT main_table.tag_id) from ', $sql);
00197 return $sql;
00198 }
00199
00200 public function addStoreFilter($storeId, $allFilter = true)
00201 {
00202 if ($this->_isStoreFilter) {
00203 return $this;
00204 }
00205 if (!is_array($storeId)) {
00206 $storeId = array($storeId);
00207 }
00208 $this->getSelect()->join(array(
00209 'summary_store'=>$this->getTable('summary')),
00210 'main_table.tag_id = summary_store.tag_id
00211 AND summary_store.store_id IN (' . implode(',', $storeId) . ')',
00212 array());
00213
00214 $this->getSelect()->group('summary_store.tag_id');
00215
00216 if($this->getJoinFlag('relation') && $allFilter) {
00217 $this->getSelect()->where('relation.store_id IN (' . implode(',', $storeId) . ')');
00218 }
00219
00220 if($this->getJoinFlag('prelation') && $allFilter) {
00221 $this->getSelect()->where('prelation.store_id IN (' . implode(',', $storeId) . ')');
00222 }
00223 $this->_isStoreFilter = true;
00224
00225 return $this;
00226 }
00227
00228 public function setActiveFilter()
00229 {
00230 $this->getSelect()->where('relation.active = 1');
00231 if($this->getJoinFlag('prelation')) {
00232 $this->getSelect()->where('prelation.active = 1');
00233 }
00234 return $this;
00235 }
00236
00237 public function addStatusFilter($status)
00238 {
00239 $this->addFieldToFilter('main_table.status', $status);
00240 return $this;
00241 }
00242
00243 public function addProductFilter($productId)
00244 {
00245 $this->addFieldToFilter('relation.product_id', $productId);
00246 if($this->getJoinFlag('prelation')) {
00247 $this->addFieldToFilter('prelation.product_id', $productId);
00248 }
00249 return $this;
00250 }
00251
00252 public function addCustomerFilter($customerId)
00253 {
00254 $this->getSelect()
00255 ->where('relation.customer_id = ?', $customerId);
00256 if($this->getJoinFlag('prelation')) {
00257 $this->getSelect()
00258 ->where('prelation.customer_id = ?', $customerId);
00259 }
00260 return $this;
00261 }
00262
00263
00264 public function addTagGroup()
00265 {
00266 $this->getSelect()->group('main_table.tag_id');
00267 return $this;
00268 }
00269
00270
00271 public function joinRel()
00272 {
00273 $this->setJoinFlag('relation');
00274 $this->getSelect()->joinLeft(array('relation'=>$this->getTable('tag/relation')), 'main_table.tag_id=relation.tag_id');
00275 return $this;
00276 }
00277 }