Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Tag_Block_All
  • Mage_Tag_Block_Customer_Recent
  • Mage_Tag_Block_Customer_Tags
  • Mage_Tag_Block_Customer_View
  • Mage_Tag_Block_Popular
  • Mage_Tag_Block_Product_List
  • Mage_Tag_Block_Product_Result
  • Mage_Tag_CustomerController
  • Mage_Tag_Helper_Data
  • Mage_Tag_IndexController
  • Mage_Tag_ListController
  • Mage_Tag_Model_Api
  • Mage_Tag_Model_Api_V2
  • Mage_Tag_Model_Entity_Customer_Collection
  • Mage_Tag_Model_Indexer_Summary
  • Mage_Tag_Model_Mysql4_Customer_Collection
  • Mage_Tag_Model_Mysql4_Indexer_Summary
  • Mage_Tag_Model_Mysql4_Popular_Collection
  • Mage_Tag_Model_Mysql4_Product_Collection
  • Mage_Tag_Model_Mysql4_Tag
  • Mage_Tag_Model_Mysql4_Tag_Collection
  • Mage_Tag_Model_Mysql4_Tag_Relation
  • Mage_Tag_Model_Resource_Customer_Collection
  • Mage_Tag_Model_Resource_Indexer_Summary
  • Mage_Tag_Model_Resource_Popular_Collection
  • Mage_Tag_Model_Resource_Product_Collection
  • Mage_Tag_Model_Resource_Tag
  • Mage_Tag_Model_Resource_Tag_Collection
  • Mage_Tag_Model_Resource_Tag_Relation
  • Mage_Tag_Model_Session
  • Mage_Tag_Model_Tag
  • Mage_Tag_Model_Tag_Relation
  • Mage_Tag_ProductController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Tag
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Tagged Product(s) Collection
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Tag
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Tag_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
 36: {
 37:     /**
 38:      * Customer Id Filter
 39:      *
 40:      * @var int
 41:      */
 42:     protected $_customerFilterId;
 43: 
 44:     /**
 45:      * Tag Id Filter
 46:      *
 47:      * @var int
 48:      */
 49:     protected $_tagIdFilter;
 50: 
 51:     /**
 52:      * Join Flags
 53:      *
 54:      * @var array
 55:      */
 56:     protected $_joinFlags            = array();
 57: 
 58:     /**
 59:      * Initialize collection select
 60:      *
 61:      * @return Mage_Tag_Model_Resource_Product_Collection
 62:      */
 63:     protected function _initSelect()
 64:     {
 65:         parent::_initSelect();
 66: 
 67:         $this->_joinFields();
 68:         $this->getSelect()->group('e.entity_id');
 69: 
 70:         /*
 71:          * Allow analytic function usage
 72:          */
 73:         $this->_useAnalyticFunction = true;
 74:         
 75:         return $this;
 76:     }
 77: 
 78:     /**
 79:      * Set flag about joined table.
 80:      * setFlag method must be used in future.
 81:      *
 82:      * @deprecated after 1.3.2.3
 83:      *
 84:      * @param string $table
 85:      * @return Mage_Tag_Model_Resource_Product_Collection
 86:      */
 87:     public function setJoinFlag($table)
 88:     {
 89:         $this->setFlag($table, true);
 90:         return $this;
 91:     }
 92: 
 93:     /**
 94:      * Get flag's status about joined table.
 95:      * getFlag method must be used in future.
 96:      *
 97:      * @deprecated after 1.3.2.3
 98:      *
 99:      * @param string $table
100:      * @return bool
101:      */
102:     public function getJoinFlag($table)
103:     {
104:         return $this->getFlag($table);
105:     }
106: 
107:     /**
108:      * Unset value of join flag.
109:      * Set false (bool) value to flag instead in future.
110:      *
111:      * @deprecated after 1.3.2.3
112:      *
113:      * @param string $table
114:      * @return Mage_Tag_Model_Resource_Product_Collection
115:      */
116:     public function unsetJoinFlag($table = null)
117:     {
118:         $this->setFlag($table, false);
119:         return $this;
120:     }
121: 
122:     /**
123:      * Add tag visibility on stores
124:      *
125:      * @return Mage_Tag_Model_Resource_Product_Collection
126:      */
127:     public function addStoresVisibility()
128:     {
129:         $this->setFlag('add_stores_after', true);
130:         return $this;
131:     }
132: 
133:     /**
134:      * Add tag visibility on stores process
135:      *
136:      * @return Mage_Tag_Model_Resource_Product_Collection
137:      */
138:     protected function _addStoresVisibility()
139:     {
140:         $tagIds = array();
141:         foreach ($this as $item) {
142:             $tagIds[] = $item->getTagId();
143:         }
144: 
145:         $tagsStores = array();
146:         if (sizeof($tagIds) > 0) {
147:             $select = $this->getConnection()->select()
148:                 ->from($this->getTable('tag/relation'), array('store_id', 'tag_id'))
149:                 ->where('tag_id IN(?)', $tagIds);
150:             $tagsRaw = $this->getConnection()->fetchAll($select);
151:             foreach ($tagsRaw as $tag) {
152:                 if (!isset($tagsStores[$tag['tag_id']])) {
153:                     $tagsStores[$tag['tag_id']] = array();
154:                 }
155: 
156:                 $tagsStores[$tag['tag_id']][] = $tag['store_id'];
157:             }
158:         }
159: 
160:         foreach ($this as $item) {
161:             if (isset($tagsStores[$item->getTagId()])) {
162:                 $item->setStores($tagsStores[$item->getTagId()]);
163:             } else {
164:                 $item->setStores(array());
165:             }
166:         }
167: 
168:         return $this;
169:     }
170: 
171:     /**
172:      * Add group by tag
173:      *
174:      * @return Mage_Tag_Model_Resource_Product_Collection
175:      */
176:     public function addGroupByTag()
177:     {
178:         $this->getSelect()->group('relation.tag_relation_id');
179:         return $this;
180:     }
181: 
182:     /**
183:      * Add Store ID filter
184:      *
185:      * @param int|array $store
186:      * @return Mage_Tag_Model_Resource_Product_Collection
187:      */
188:     public function addStoreFilter($store = null)
189:     {
190:         if (!is_null($store)) {
191:             $this->getSelect()->where('relation.store_id IN (?)', $store);
192:         }
193:         return $this;
194:     }
195: 
196:     /**
197:      * Set Customer filter
198:      * If incoming parameter is array and has element with key 'null'
199:      * then condition with IS NULL or IS NOT NULL will be added.
200:      * Otherwise condition with IN() will be added
201:      *
202:      * @param int|array $customerId
203:      * @return Mage_Tag_Model_Resource_Product_Collection
204:      */
205:     public function addCustomerFilter($customerId)
206:     {
207:         if (is_array($customerId) && isset($customerId['null'])) {
208:             $condition = ($customerId['null']) ? 'IS NULL' : 'IS NOT NULL';
209:             $this->getSelect()->where('relation.customer_id ' . $condition);
210:             return $this;
211:         }
212:         $this->getSelect()->where('relation.customer_id IN(?)', $customerId);
213:         $this->_customerFilterId = $customerId;
214:         return $this;
215:     }
216: 
217:     /**
218:      * Set tag filter
219:      *
220:      * @param int $tagId
221:      * @return Mage_Tag_Model_Resource_Product_Collection
222:      */
223:     public function addTagFilter($tagId)
224:     {
225:         $this->getSelect()->where('relation.tag_id = ?', $tagId);
226:         $this->setFlag('distinct', true);
227:         return $this;
228:     }
229: 
230:     /**
231:      * Add tag status filter
232:      *
233:      * @param int $status
234:      * @return Mage_Tag_Model_Resource_Product_Collection
235:      */
236:     public function addStatusFilter($status)
237:     {
238:         $this->getSelect()->where('t.status = ?', $status);
239:         return $this;
240:     }
241: 
242:     /**
243:      * Set DESC order to collection
244:      *
245:      * @param string $dir
246:      * @return Mage_Tag_Model_Resource_Product_Collection
247:      */
248:     public function setDescOrder($dir = 'DESC')
249:     {
250:         $this->setOrder('relation.tag_relation_id', $dir);
251:         return $this;
252:     }
253: 
254:     /**
255:      * Add Popularity
256:      *
257:      * @param int $tagId
258:      * @param int $storeId
259:      * @return Mage_Tag_Model_Resource_Product_Collection
260:      */
261:     public function addPopularity($tagId, $storeId = null)
262:     {
263:         $tagRelationTable = $this->getTable('tag/relation');
264: 
265:         $condition = array(
266:             'prelation.product_id=e.entity_id'
267:         );
268: 
269:         if (!is_null($storeId)) {
270:             $condition[] = $this->getConnection()->quoteInto('prelation.store_id = ?', $storeId);
271:         }
272:         $condition = join(' AND ', $condition);
273:         $innerSelect = $this->getConnection()->select()
274:         ->from(
275:             array('relation' => $tagRelationTable),
276:             array('product_id', 'store_id', 'popularity' => 'COUNT(DISTINCT relation.tag_relation_id)')
277:         )
278:         ->where('relation.tag_id = ?', $tagId)
279:         ->group(array('product_id', 'store_id'));
280: 
281:         $this->getSelect()
282:             ->joinLeft(
283:                 array('prelation' => $innerSelect),
284:                 $condition,
285:                 array('popularity' => 'prelation.popularity')
286:             );
287: 
288:         $this->_tagIdFilter = $tagId;
289:         $this->setFlag('prelation', true);
290:         return $this;
291:     }
292: 
293:     /**
294:      * Add Popularity Filter
295:      *
296:      * @param mixed $condition
297:      * @return Mage_Tag_Model_Resource_Product_Collection
298:      */
299:     public function addPopularityFilter($condition)
300:     {
301:         $tagRelationTable = Mage::getSingleton('core/resource')
302:             ->getTableName('tag/relation');
303: 
304:         $select = $this->getConnection()->select()
305:             ->from($tagRelationTable, array('product_id', 'popularity' => 'COUNT(DISTINCT tag_relation_id)'))
306:             ->where('tag_id = :tag_id')
307:             ->group('product_id')
308:             ->having($this->_getConditionSql('popularity', $condition));
309: 
310:         $prodIds = array();
311:         foreach ($this->getConnection()->fetchAll($select, array('tag_id' => $this->_tagIdFilter)) as $item) {
312:             $prodIds[] = $item['product_id'];
313:         }
314: 
315:         if (sizeof($prodIds) > 0) {
316:             $this->getSelect()->where('e.entity_id IN(?)', $prodIds);
317:         } else {
318:             $this->getSelect()->where('e.entity_id IN(0)');
319:         }
320: 
321:         return $this;
322:     }
323: 
324:     /**
325:      * Set tag active filter to collection
326:      *
327:      * @return Mage_Tag_Model_Resource_Product_Collection
328:      */
329:     public function setActiveFilter()
330:     {
331:         $active = Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE;
332:         $this->getSelect()->where('relation.active = ?', $active);
333:         if ($this->getFlag('prelation')) {
334:             $this->getSelect()->where('prelation.active = ?', $active);
335:         }
336:         return $this;
337:     }
338: 
339:     /**
340:      * Add Product Tags
341:      *
342:      * @param int $storeId
343:      * @return Mage_Tag_Model_Resource_Product_Collection
344:      */
345:     public function addProductTags($storeId = null)
346:     {
347:         foreach ($this->getItems() as $item) {
348:             $tagsCollection = Mage::getModel('tag/tag')->getResourceCollection();
349: 
350:             if (!is_null($storeId)) {
351:                 $tagsCollection->addStoreFilter($storeId);
352:             }
353: 
354:             $tagsCollection->addPopularity()
355:                 ->addProductFilter($item->getEntityId())
356:                 ->addCustomerFilter($this->_customerFilterId)
357:                 ->setActiveFilter();
358: 
359:             $tagsCollection->load();
360:             $item->setProductTags($tagsCollection);
361:         }
362: 
363:         return $this;
364:     }
365: 
366:     /**
367:      * Join fields process
368:      *
369:      * @return Mage_Tag_Model_Resource_Product_Collection
370:      */
371:     protected function _joinFields()
372:     {
373:         $tagTable           = $this->getTable('tag/tag');
374:         $tagRelationTable   = $this->getTable('tag/relation');
375: 
376:         $this->addAttributeToSelect('name')
377:             ->addAttributeToSelect('price')
378:             ->addAttributeToSelect('small_image');
379: 
380:         $this->getSelect()
381:             ->join(array('relation' => $tagRelationTable), 'relation.product_id = e.entity_id', array(
382:                 'product_id'    => 'product_id',
383:                 'item_store_id' => 'store_id',
384:             ))
385:             ->join(array('t' => $tagTable),
386:                 't.tag_id = relation.tag_id',
387:                 array(
388:                     'tag_id',
389:                     'tag_status' => 'status',
390:                     'tag_name'   => 'name',
391:                     'store_id'   => $this->getConnection()->getCheckSql(
392:                         't.first_store_id = 0',
393:                         'relation.store_id',
394:                         't.first_store_id'
395:                     )
396:                 )
397:             );
398: 
399:         return $this;
400:     }
401: 
402:     /**
403:      * After load adding data
404:      *
405:      * @return Mage_Tag_Model_Resource_Product_Collection
406:      */
407:     protected function _afterLoad()
408:     {
409:         parent::_afterLoad();
410: 
411:         if ($this->getFlag('add_stores_after')) {
412:             $this->_addStoresVisibility();
413:         }
414: 
415:         if (count($this) > 0) {
416:             Mage::dispatchEvent('tag_tag_product_collection_load_after', array(
417:                 'collection' => $this
418:             ));
419:         }
420: 
421:         return $this;
422:     }
423: 
424:     /**
425:      * Render SQL for retrieve product count
426:      *
427:      * @return string
428:      */
429:     public function getSelectCountSql()
430:     {
431:         $countSelect = clone $this->getSelect();
432: 
433:         $countSelect->reset(Zend_Db_Select::COLUMNS);
434:         $countSelect->reset(Zend_Db_Select::ORDER);
435:         $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
436:         $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
437:         $countSelect->reset(Zend_Db_Select::GROUP);
438: 
439:         if ($this->getFlag('group_tag')) {
440:             $field = 'relation.tag_id';
441:         } else {
442:             $field = 'e.entity_id';
443:         }
444:         $expr = new Zend_Db_Expr('COUNT('
445:             . ($this->getFlag('distinct') ? 'DISTINCT ' : '')
446:             . $field . ')');
447: 
448:         $countSelect->columns($expr);
449: 
450:         return $countSelect;
451:     }
452: 
453:     /**
454:      * Set attribute order
455:      *
456:      * @param string $attribute
457:      * @param string $dir
458:      * @return Mage_Tag_Model_Resource_Product_Collection
459:      */
460:     public function setOrder($attribute, $dir = 'desc')
461:     {
462:         if ($attribute == 'popularity') {
463:             $this->getSelect()->order($attribute . ' ' . $dir);
464:         } else {
465:             parent::setOrder($attribute, $dir);
466:         }
467:         return $this;
468:     }
469: 
470:     /**
471:      * Set Id Fieldname as Tag Relation Id
472:      *
473:      * @return Mage_Tag_Model_Resource_Product_Collection
474:      */
475:     public function setRelationId()
476:     {
477:         $this->_setIdFieldName('tag_relation_id');
478:         return $this;
479:     }
480: }
481: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0