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_Reports
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: * Report Products Tags collection
30: *
31: * @category Mage
32: * @package Mage_Reports
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Reports_Model_Resource_Tag_Product_Collection extends Mage_Tag_Model_Resource_Product_Collection
36: {
37: protected function _construct()
38: {
39: parent::_construct();
40: /**
41: * Allow to use analytic function
42: */
43: $this->_useAnalyticFunction = true;
44: }
45: /**
46: * Add unique target count to result
47: *
48: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
49: */
50: public function addUniqueTagedCount()
51: {
52: $select = clone $this->getSelect();
53:
54: $select->reset()
55: ->from(array('rel' => $this->getTable('tag/relation')), 'COUNT(DISTINCT rel.tag_id)')
56: ->where('rel.product_id = e.entity_id');
57:
58: $this->getSelect()
59: ->columns(array('utaged' => new Zend_Db_Expr(sprintf('(%s)', $select))));
60: return $this;
61: }
62:
63: /**
64: * Add all target count to result
65: *
66: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
67: */
68: public function addAllTagedCount()
69: {
70: $this->getSelect()
71: ->columns(array('taged' => 'COUNT(relation.tag_id)'));
72: return $this;
73: }
74:
75: /**
76: * Add target count to result
77: *
78: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
79: */
80: public function addTagedCount()
81: {
82: $this->getSelect()
83: ->columns(array('taged' => 'COUNT(relation.tag_relation_id)'));
84:
85: return $this;
86: }
87:
88: /**
89: * Add group by product to result
90: *
91: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
92: */
93: public function addGroupByProduct()
94: {
95: $this->getSelect()
96: ->group('relation.product_id');
97: $this->setJoinFlag('distinct');
98: return $this;
99: }
100:
101: /**
102: * Add group by tag to result
103: *
104: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
105: */
106: public function addGroupByTag()
107: {
108: $this->getSelect()
109: ->group('relation.tag_id');
110: $this->setJoinFlag('distinct');
111: $this->setJoinFlag('group_tag');
112: return $this;
113: }
114:
115: /**
116: * Add product filter
117: *
118: * @param int $customerId
119: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
120: */
121: public function addProductFilter($customerId)
122: {
123: $this->getSelect()
124: ->where('relation.product_id = ?', (int)$customerId);
125: $this->_customerFilterId = (int)$customerId;
126: return $this;
127: }
128:
129: /**
130: * Set order
131: *
132: * @param string $attribute
133: * @param string $dir
134: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
135: */
136: public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
137: {
138: if ($attribute == 'utaged' || $attribute == 'taged' || $attribute == 'tag_name') {
139: $this->getSelect()->order($attribute . ' ' . $dir);
140: } else {
141: parent::setOrder($attribute, $dir);
142: }
143:
144: return $this;
145: }
146:
147: /**
148: * Join fields
149: *
150: * @return Mage_Reports_Model_Resource_Tag_Product_Collection
151: */
152: protected function _joinFields()
153: {
154: $this->addAttributeToSelect('name');
155: $this->getSelect()
156: ->join(
157: array('relation' => $this->getTable('tag/relation')),
158: 'relation.product_id = e.entity_id',
159: array())
160: ->join(
161: array('t' => $this->getTable('tag/tag')),
162: 't.tag_id = relation.tag_id',
163: array('tag_id', 'status', 'tag_name' => 'name')
164: );
165:
166: return $this;
167: }
168: }
169: