1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Report_StatisticsController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_adminSession = null;
42:
43: public function _initAction()
44: {
45: $act = $this->getRequest()->getActionName();
46: if(!$act) {
47: $act = 'default';
48: }
49:
50: $this->loadLayout()
51: ->_addBreadcrumb(Mage::helper('reports')->__('Reports'), Mage::helper('reports')->__('Reports'))
52: ->_addBreadcrumb(Mage::helper('reports')->__('Statistics'), Mage::helper('reports')->__('Statistics'));
53: return $this;
54: }
55:
56: public function _initReportAction($blocks)
57: {
58: if (!is_array($blocks)) {
59: $blocks = array($blocks);
60: }
61:
62: $requestData = Mage::helper('adminhtml')->prepareFilterString($this->getRequest()->getParam('filter'));
63: $requestData = $this->_filterDates($requestData, array('from', 'to'));
64: $requestData['store_ids'] = $this->getRequest()->getParam('store_ids');
65: $params = new Varien_Object();
66:
67: foreach ($requestData as $key => $value) {
68: if (!empty($value)) {
69: $params->setData($key, $value);
70: }
71: }
72:
73: foreach ($blocks as $block) {
74: if ($block) {
75: $block->setPeriodType($params->getData('period_type'));
76: $block->setFilterData($params);
77: }
78: }
79:
80: return $this;
81: }
82:
83: 84: 85: 86: 87: 88:
89: protected function _getCollectionNames()
90: {
91: $codes = $this->getRequest()->getParam('code');
92: if (!$codes) {
93: throw new Exception(Mage::helper('adminhtml')->__('No report code specified.'));
94: }
95:
96: if(!is_array($codes) && strpos($codes, ',') === false) {
97: $codes = array($codes);
98: } elseif (!is_array($codes)) {
99: $codes = explode(',', $codes);
100: }
101:
102: $aliases = array(
103: 'sales' => 'sales/report_order',
104: 'tax' => 'tax/report_tax',
105: 'shipping' => 'sales/report_shipping',
106: 'invoiced' => 'sales/report_invoiced',
107: 'refunded' => 'sales/report_refunded',
108: 'coupons' => 'salesrule/report_rule',
109: 'bestsellers' => 'sales/report_bestsellers',
110: 'viewed' => 'reports/report_product_viewed',
111: );
112: $out = array();
113: foreach ($codes as $code) {
114: $out[] = $aliases[$code];
115: }
116: return $out;
117: }
118:
119: 120: 121: 122: 123:
124: public function refreshRecentAction()
125: {
126: try {
127: $collectionsNames = $this->_getCollectionNames();
128: $currentDate = Mage::app()->getLocale()->date();
129: $date = $currentDate->subHour(25);
130: foreach ($collectionsNames as $collectionName) {
131: Mage::getResourceModel($collectionName)->aggregate($date);
132: }
133: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Recent statistics have been updated.'));
134: } catch (Mage_Core_Exception $e) {
135: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
136: } catch (Exception $e) {
137: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to refresh recent statistics.'));
138: Mage::logException($e);
139: }
140:
141: if($this->_getSession()->isFirstPageAfterLogin()) {
142: $this->_redirect('*/*');
143: } else {
144: $this->_redirectReferer('*/*');
145: }
146: return $this;
147: }
148:
149: 150: 151: 152: 153:
154: public function refreshLifetimeAction()
155: {
156: try {
157: $collectionsNames = $this->_getCollectionNames();
158: foreach ($collectionsNames as $collectionName) {
159: Mage::getResourceModel($collectionName)->aggregate();
160: }
161: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Lifetime statistics have been updated.'));
162: } catch (Mage_Core_Exception $e) {
163: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
164: } catch (Exception $e) {
165: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to refresh lifetime statistics.'));
166: Mage::logException($e);
167: }
168:
169: if($this->_getSession()->isFirstPageAfterLogin()) {
170: $this->_redirect('*/*');
171: } else {
172: $this->_redirectReferer('*/*');
173: }
174:
175: return $this;
176: }
177:
178: public function indexAction()
179: {
180: $this->_title($this->__('Reports'))->_title($this->__('Sales'))->_title($this->__('Refresh Statistics'));
181:
182: $this->_initAction()
183: ->_setActiveMenu('report/statistics/refreshstatistics')
184: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Refresh Statistics'), Mage::helper('adminhtml')->__('Refresh Statistics'))
185: ->renderLayout();
186: }
187:
188: protected function _isAllowed()
189: {
190: return $this->_getSession()->isAllowed('report/statistics');
191: }
192:
193: 194: 195: 196: 197:
198: protected function _getSession()
199: {
200: if (is_null($this->_adminSession)) {
201: $this->_adminSession = Mage::getSingleton('admin/session');
202: }
203: return $this->_adminSession;
204: }
205: }
206: