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: * Product Downloads Report collection
30: *
31: * @category Mage
32: * @package Mage_Reports
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Reports_Model_Resource_Product_Downloads_Collection extends Mage_Catalog_Model_Resource_Product_Collection
36: {
37: /**
38: * Identifier field name
39: *
40: * @var string
41: */
42: protected $_idFieldName = 'link_id';
43:
44: protected function _construct()
45: {
46: parent::_construct();
47:
48:
49: }
50: /**
51: * Add downloads summary grouping by product
52: *
53: * @return Mage_Reports_Model_Resource_Product_Downloads_Collection
54: */
55: public function addSummary()
56: {
57: $adapter = $this->getConnection();
58: $linkExpr = $adapter->getIfNullSql('l_store.title', 'l.title');
59:
60: $this->getSelect()
61: ->joinInner(
62: array('d' => $this->getTable('downloadable/link_purchased_item')),
63: 'e.entity_id = d.product_id',
64: array(
65: 'purchases' => new Zend_Db_Expr('SUM(d.number_of_downloads_bought)'),
66: 'downloads' => new Zend_Db_Expr('SUM(d.number_of_downloads_used)'),
67: ))
68: ->joinInner(
69: array('l' => $this->getTable('downloadable/link_title')),
70: 'd.link_id = l.link_id',
71: array('l.link_id'))
72: ->joinLeft(
73: array('l_store' => $this->getTable('downloadable/link_title')),
74: $adapter->quoteInto('l.link_id = l_store.link_id AND l_store.store_id = ?', (int)$this->getStoreId()),
75: array('link_title' => $linkExpr))
76: ->where(implode(' OR ', array(
77: $adapter->quoteInto('d.number_of_downloads_bought > ?', 0),
78: $adapter->quoteInto('d.number_of_downloads_used > ?', 0),
79: )))
80: ->group('d.link_id');
81: /**
82: * Allow to use analytic function
83: */
84: $this->_useAnalyticFunction = true;
85:
86: return $this;
87: }
88:
89: /**
90: * Add sorting
91: *
92: * @param string $attribute
93: * @param string $dir
94: * @return Mage_Reports_Model_Resource_Product_Downloads_Collection
95: */
96: public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
97: {
98: if ($attribute == 'purchases' || $attribute == 'downloads' || $attribute == 'link_title') {
99: $this->getSelect()->order($attribute . ' ' . $dir);
100: } else {
101: parent::setOrder($attribute, $dir);
102: }
103: return $this;
104: }
105:
106: /**
107: * Add filtering
108: *
109: * @param string $field
110: * @param string $condition
111: * @return Mage_Reports_Model_Resource_Product_Downloads_Collection
112: */
113: public function addFieldToFilter($field, $condition = null)
114: {
115: if ($field == 'link_title') {
116: $conditionSql = $this->_getConditionSql('l.title', $condition);
117: $this->getSelect()->where($conditionSql);
118: } else {
119: parent::addFieldToFilter($field, $condition);
120: }
121: return $this;
122: }
123: }
124: