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: class Mage_Reports_Model_Grouped_Collection
27: extends Varien_Data_Collection //Mage_Core_Model_Resource_Db_Collection_Abstract
28: {
29: /**
30: * Column name for group by clause
31: *
32: * @var string
33: */
34: protected $_columnGroupBy = null;
35:
36: /**
37: * Collection resource
38: *
39: * @var Mage_Core_Model_Resource_Db_Collection_Abstract
40: */
41: protected $_resourceCollection = null;
42:
43: /**
44: * Set column to group by
45: *
46: * @param string $column
47: * @return Mage_Reports_Model_Grouped_Collection
48: */
49: public function setColumnGroupBy($column)
50: {
51: $this->_columnGroupBy = (string)$column;
52: return $this;
53: }
54:
55: /**
56: * Load collection
57: *
58: * @param boolean $printQuery
59: * @param boolean $logQuery
60: * @return Mage_Reports_Model_Grouped_Collection
61: */
62: public function load($printQuery = false, $logQuery = false)
63: {
64: if ($this->isLoaded()) {
65: return $this;
66: }
67:
68: parent::load($printQuery, $logQuery);
69: $this->_setIsLoaded();
70:
71: if ($this->_columnGroupBy !== null) {
72: $this->_mergeWithEmptyData();
73: $this->_groupResourceData();
74: }
75:
76: return $this;
77: }
78:
79: /**
80: * Setter for resource collection
81: *
82: * @param Varien_Data_Collection_Db $collection
83: * @return Mage_Reports_Model_Grouped_Collection
84: */
85: public function setResourceCollection($collection)
86: {
87: $this->_resourceCollection = $collection;
88: return $this;
89: }
90:
91: /**
92: * Merge empty data collection with resource collection
93: *
94: * @return Mage_Reports_Model_Grouped_Collection
95: */
96: protected function _mergeWithEmptyData()
97: {
98: if (count($this->_items) == 0) {
99: return $this;
100: }
101:
102: foreach ($this->_items as $key => $item) {
103: foreach ($this->_resourceCollection as $dataItem) {
104: if ($item->getData($this->_columnGroupBy) == $dataItem->getData($this->_columnGroupBy)) {
105: if ($this->_items[$key]->getIsEmpty()) {
106: $this->_items[$key] = $dataItem;
107: } else {
108: $this->_items[$key]->addChild($dataItem);
109: }
110: }
111: }
112: }
113:
114: return $this;
115: }
116:
117: /**
118: * Group data in resource collection
119: *
120: * @return Mage_Reports_Model_Grouped_Collection
121: */
122: protected function _groupResourceData()
123: {
124: if (count($this->_items) == 0) {
125: foreach ($this->_resourceCollection as $item) {
126: if (isset($this->_items[$item->getData($this->_columnGroupBy)])) {
127: $this->_items[$item->getData($this->_columnGroupBy)]->addChild($item->setIsEmpty(false));
128: } else {
129: $this->_items[$item->getData($this->_columnGroupBy)] = $item->setIsEmpty(false);
130: }
131: }
132: }
133:
134: return $this;
135: }
136: }
137: