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_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00036 {
00037
00038
00039
00040
00041
00042 protected $_customerFilterId;
00043
00044
00045
00046
00047
00048
00049 protected $_tagIdFilter;
00050
00051
00052
00053
00054
00055
00056 protected $_joinFlags = array();
00057
00058
00059
00060
00061
00062
00063 protected function _initSelect()
00064 {
00065 parent::_initSelect();
00066
00067 $this->_joinFields();
00068 $this->getSelect()->group('e.entity_id');
00069
00070 return $this;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079 public function setJoinFlag($table)
00080 {
00081 $this->_joinFlags[$table] = true;
00082 return $this;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091 public function getJoinFlag($table)
00092 {
00093 return isset($this->_joinFlags[$table]);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102 public function unsetJoinFlag($table = null)
00103 {
00104 if (is_null($table)) {
00105 $this->_joinFlags = array();
00106 }
00107 elseif ($this->getJoinFlag($table)) {
00108 unset($this->_joinFlags[$table]);
00109 }
00110
00111 return $this;
00112 }
00113
00114
00115
00116
00117
00118
00119 public function addStoresVisibility()
00120 {
00121 $this->setJoinFlag('add_stores_after');
00122 return $this;
00123 }
00124
00125
00126
00127
00128
00129
00130 protected function _addStoresVisibility()
00131 {
00132 $tagIds = array();
00133 foreach ($this as $item) {
00134 $tagIds[] = $item->getTagId();
00135 }
00136
00137 $tagsStores = array();
00138 if (sizeof($tagIds) > 0) {
00139 $select = $this->getConnection()->select()
00140 ->from($this->getTable('tag/summary'), array('store_id', 'tag_id'))
00141 ->where('tag_id IN(?)', $tagIds);
00142 $tagsRaw = $this->getConnection()->fetchAll($select);
00143 foreach ($tagsRaw as $tag) {
00144 if (!isset($tagsStores[$tag['tag_id']])) {
00145 $tagsStores[$tag['tag_id']] = array();
00146 }
00147
00148 $tagsStores[$tag['tag_id']][] = $tag['store_id'];
00149 }
00150 }
00151
00152 foreach ($this as $item) {
00153 if (isset($tagsStores[$item->getTagId()])) {
00154 $item->setStores($tagsStores[$item->getTagId()]);
00155 }
00156 else {
00157 $item->setStores(array());
00158 }
00159 }
00160
00161 return $this;
00162 }
00163
00164
00165
00166
00167
00168
00169 public function addGroupByTag()
00170 {
00171 $this->getSelect()->group('relation.tag_relation_id');
00172 return $this;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 public function addCustomerFilter($customerId)
00182 {
00183 $this->getSelect()
00184 ->where('relation.customer_id = ?', $customerId);
00185 $this->_customerFilterId = $customerId;
00186 return $this;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 public function addTagFilter($tagId)
00196 {
00197 $this->getSelect()->where('relation.tag_id = ?', $tagId);
00198 $this->setJoinFlag('distinct');
00199 return $this;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208 public function addStatusFilter($status)
00209 {
00210 $this->getSelect()->where('t.status = ?', $status);
00211 return $this;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 public function setDescOrder($dir = 'DESC')
00221 {
00222 $this->setOrder('relation.tag_relation_id', $dir);
00223 return $this;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233 public function addPopularity($tagId, $storeId=null)
00234 {
00235 $tagRelationTable = $this->getTable('tag/relation');
00236
00237 $condition = array(
00238 'prelation.product_id=e.entity_id'
00239 );
00240 if (!is_null($storeId)) {
00241 $condition[] = $this->getConnection()
00242 ->quoteInto('prelation.store_id=?', $storeId);
00243 }
00244 $condition = join(' AND ', $condition);
00245
00246 $this->getSelect()
00247 ->joinLeft(
00248 array('prelation' => $tagRelationTable),
00249 $condition,
00250 array('popularity' => 'COUNT(DISTINCT prelation.tag_relation_id)'))
00251 ->where('prelation.tag_id = ?', $tagId);
00252
00253 $this->_tagIdFilter = $tagId;
00254 $this->setJoinFlag('prelation');
00255 return $this;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 public function addPopularityFilter($condition)
00265 {
00266 $tagRelationTable = Mage::getSingleton('core/resource')
00267 ->getTableName('tag/relation');
00268
00269 $select = $this->getConnection()->select()
00270 ->from($tagRelationTable, array('product_id', 'popularity' => 'COUNT(DISTINCT tag_relation_id)'))
00271 ->where('tag_id = ?', $this->_tagIdFilter)
00272 ->group('product_id')
00273 ->having($this->_getConditionSql('popularity', $condition));
00274
00275 $prodIds = array();
00276 foreach ($this->getConnection()->fetchAll($select) as $item) {
00277 $prodIds[] = $item['product_id'];
00278 }
00279
00280 if (sizeof($prodIds) > 0) {
00281 $this->getSelect()->where('e.entity_id IN(?)', $prodIds);
00282 }
00283 else {
00284 $this->getSelect()->where('e.entity_id IN(0)');
00285 }
00286
00287 return $this;
00288 }
00289
00290
00291
00292
00293
00294
00295 public function setActiveFilter()
00296 {
00297 $active = Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE;
00298 $this->getSelect()->where('relation.active=?', $active);
00299 if ($this->getJoinFlag('prelation')) {
00300 $this->getSelect()->where('prelation.active=?', $active);
00301 }
00302 return $this;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 public function addProductTags($storeId = null)
00312 {
00313 foreach ($this->getItems() as $item) {
00314 $tagsCollection = Mage::getModel('tag/tag')->getResourceCollection();
00315
00316 if (!is_null($storeId)) {
00317 $tagsCollection->addStoreFilter($storeId);
00318 }
00319
00320 $tagsCollection->addPopularity()
00321 ->addProductFilter($item->getEntityId())
00322 ->addCustomerFilter($this->_customerFilterId)
00323 ->setActiveFilter();
00324
00325 $tagsCollection->load();
00326 $item->setProductTags($tagsCollection);
00327 }
00328
00329 return $this;
00330 }
00331
00332
00333
00334
00335
00336
00337 protected function _joinFields()
00338 {
00339 $tagTable = $this->getTable('tag/tag');
00340 $tagRelationTable = $this->getTable('tag/relation');
00341
00342 $this->addAttributeToSelect('name')
00343 ->addAttributeToSelect('price')
00344 ->addAttributeToSelect('small_image');
00345
00346 $this->getSelect()
00347 ->join(array('relation' => $tagRelationTable), 'relation.product_id = e.entity_id')
00348 ->join(array('t' => $tagTable),
00349 't.tag_id = relation.tag_id',
00350 array('tag_id', 'name', 'tag_status' => 'status', 'tag_name' => 'name')
00351 );
00352 return $this;
00353 }
00354
00355
00356
00357
00358
00359
00360 protected function _afterLoad()
00361 {
00362 parent::_afterLoad();
00363
00364 if ($this->getJoinFlag('add_stores_after')) {
00365 $this->_addStoresVisibility();
00366 }
00367
00368 if (count($this) > 0) {
00369 Mage::dispatchEvent('tag_tag_product_collection_load_after', array(
00370 'collection' => $this
00371 ));
00372 }
00373
00374 return $this;
00375 }
00376
00377
00378
00379
00380
00381
00382 public function getSelectCountSql()
00383 {
00384 $countSelect = clone $this->getSelect();
00385
00386 $countSelect->reset(Zend_Db_Select::COLUMNS);
00387 $countSelect->reset(Zend_Db_Select::ORDER);
00388 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00389 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00390 $countSelect->reset(Zend_Db_Select::GROUP);
00391
00392 if ($this->getJoinFlag('group_tag')) {
00393 $field = 'relation.tag_id';
00394 }
00395 else {
00396 $field = 'e.entity_id';
00397 }
00398 $expr = new Zend_Db_Expr('COUNT('
00399 . ($this->getJoinFlag('distinct') ? 'DISTINCT ' : '')
00400 . $field . ')');
00401
00402 $countSelect->from(null, $expr);
00403
00404 return $countSelect;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414 public function setOrder($attribute, $dir='desc')
00415 {
00416 if ($attribute == 'popularity') {
00417 $this->getSelect()->order($attribute . ' ' . $dir);
00418 }
00419 else {
00420 parent::setOrder($attribute, $dir);
00421 }
00422 return $this;
00423 }
00424
00425
00426
00427
00428
00429
00430 public function setRelationId()
00431 {
00432 $this->_setIdFieldName('tag_relation_id');
00433 return $this;
00434 }
00435 }