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: * Popular tags collection model
30: *
31: * @category Mage
32: * @package Mage_Tag
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Tag_Model_Resource_Popular_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Defines resource model and model
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('tag/tag');
44: }
45:
46: /**
47: * Replacing popularity by sum of popularity and base_popularity
48: *
49: * @param int $storeId
50: * @return Mage_Tag_Model_Resource_Popular_Collection
51: */
52: public function joinFields($storeId = 0)
53: {
54: $this->getSelect()
55: ->reset()
56: ->from(
57: array('tag_summary' => $this->getTable('tag/summary')),
58: array('popularity' => 'tag_summary.popularity'))
59: ->joinInner(
60: array('tag' => $this->getTable('tag/tag')),
61: 'tag.tag_id = tag_summary.tag_id AND tag.status = ' . Mage_Tag_Model_Tag::STATUS_APPROVED)
62: ->where('tag_summary.store_id = ?', $storeId)
63: ->where('tag_summary.products > ?', 0)
64: ->order('popularity ' . Varien_Db_Select::SQL_DESC);
65:
66: return $this;
67: }
68:
69: /**
70: * Add filter by specified tag status
71: *
72: * @param string $statusCode
73: * @return Mage_Tag_Model_Resource_Popular_Collection
74: */
75: public function addStatusFilter($statusCode)
76: {
77: $this->getSelect()->where('main_table.status = ?', $statusCode);
78: return $this;
79: }
80:
81: /**
82: * Loads collection
83: *
84: * @param bool $printQuery
85: * @param bool $logQuery
86: * @return Mage_Tag_Model_Resource_Popular_Collection
87: */
88: public function load($printQuery = false, $logQuery = false)
89: {
90: if ($this->isLoaded()) {
91: return $this;
92: }
93: parent::load($printQuery, $logQuery);
94: return $this;
95: }
96:
97: /**
98: * Sets limit
99: *
100: * @param int $limit
101: * @return Mage_Tag_Model_Resource_Popular_Collection
102: */
103: public function limit($limit)
104: {
105: $this->getSelect()->limit($limit);
106: return $this;
107: }
108:
109: /**
110: * Get SQL for get record count
111: *
112: * @return Varien_Db_Select
113: */
114: public function getSelectCountSql()
115: {
116: $this->_renderFilters();
117: $select = clone $this->getSelect();
118: $select->reset(Zend_Db_Select::ORDER);
119: $select->reset(Zend_Db_Select::LIMIT_COUNT);
120: $select->reset(Zend_Db_Select::LIMIT_OFFSET);
121:
122: $countSelect = $this->getConnection()->select();
123: $countSelect->from(array('a' => $select), 'COUNT(popularity)');
124: return $countSelect;
125: }
126: }
127: