PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Block/Report/Grid/Abstract.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 302 lines | 204 code | 34 blank | 64 comment | 32 complexity | 6840eb8c388d5255bf747772c7515f20 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Adminhtml_Block_Report_Grid_Abstract extends Mage_Adminhtml_Block_Widget_Grid
  27. {
  28. protected $_resourceCollectionName = '';
  29. protected $_currentCurrencyCode = null;
  30. protected $_storeIds = array();
  31. protected $_aggregatedColumns = null;
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->setFilterVisibility(false);
  36. $this->setPagerVisibility(false);
  37. $this->setUseAjax(false);
  38. if (isset($this->_columnGroupBy)) {
  39. $this->isColumnGrouped($this->_columnGroupBy, true);
  40. }
  41. $this->setEmptyCellLabel(Mage::helper('adminhtml')->__('No records found for this period.'));
  42. }
  43. public function getResourceCollectionName()
  44. {
  45. return $this->_resourceCollectionName;
  46. }
  47. public function getCollection()
  48. {
  49. if (is_null($this->_collection)) {
  50. $this->setCollection(Mage::getModel('reports/grouped_collection'));
  51. }
  52. return $this->_collection;
  53. }
  54. protected function _getAggregatedColumns()
  55. {
  56. if (is_null($this->_aggregatedColumns)) {
  57. foreach ($this->getColumns() as $column) {
  58. if (!is_array($this->_aggregatedColumns)) {
  59. $this->_aggregatedColumns = array();
  60. }
  61. if ($column->hasTotal()) {
  62. $this->_aggregatedColumns[$column->getId()] = "{$column->getTotal()}({$column->getIndex()})";
  63. }
  64. }
  65. }
  66. return $this->_aggregatedColumns;
  67. }
  68. /**
  69. * Add column to grid
  70. * Overriden to add support for visibility_filter column option
  71. * It stands for conditional visibility of the column depending on filter field values
  72. * Value of visibility_filter supports (filter_field_name => filter_field_value) pairs
  73. *
  74. * @param string $columnId
  75. * @param array $column
  76. * @return Mage_Adminhtml_Block_Report_Grid_Abstract
  77. */
  78. public function addColumn($columnId, $column)
  79. {
  80. if (is_array($column) && array_key_exists('visibility_filter', $column)) {
  81. $filterData = $this->getFilterData();
  82. $visibilityFilter = $column['visibility_filter'];
  83. if (!is_array($visibilityFilter)) {
  84. $visibilityFilter = array($visibilityFilter);
  85. }
  86. foreach ($visibilityFilter as $k => $v) {
  87. if (is_int($k)) {
  88. $filterFieldId = $v;
  89. $filterFieldValue = true;
  90. } else {
  91. $filterFieldId = $k;
  92. $filterFieldValue = $v;
  93. }
  94. if (
  95. !$filterData->hasData($filterFieldId) ||
  96. $filterData->getData($filterFieldId) != $filterFieldValue
  97. ) {
  98. return $this; // don't add column
  99. }
  100. }
  101. }
  102. return parent::addColumn($columnId, $column);
  103. }
  104. /**
  105. * Get allowed store ids array intersected with selected scope in store switcher
  106. *
  107. * @return array
  108. */
  109. protected function _getStoreIds()
  110. {
  111. $filterData = $this->getFilterData();
  112. if ($filterData) {
  113. $storeIds = explode(',', $filterData->getData('store_ids'));
  114. } else {
  115. $storeIds = array();
  116. }
  117. // By default storeIds array contains only allowed stores
  118. $allowedStoreIds = array_keys(Mage::app()->getStores());
  119. // And then array_intersect with post data for prevent unauthorized stores reports
  120. $storeIds = array_intersect($allowedStoreIds, $storeIds);
  121. // If selected all websites or unauthorized stores use only allowed
  122. if (empty($storeIds)) {
  123. $storeIds = $allowedStoreIds;
  124. }
  125. // reset array keys
  126. $storeIds = array_values($storeIds);
  127. return $storeIds;
  128. }
  129. protected function _prepareCollection()
  130. {
  131. $filterData = $this->getFilterData();
  132. if ($filterData->getData('from') == null || $filterData->getData('to') == null) {
  133. $this->setCountTotals(false);
  134. $this->setCountSubTotals(false);
  135. return parent::_prepareCollection();
  136. }
  137. $storeIds = $this->_getStoreIds();;
  138. $orderStatuses = $filterData->getData('order_statuses');
  139. if (is_array($orderStatuses)) {
  140. if (count($orderStatuses) == 1 && strpos($orderStatuses[0],',')!== false) {
  141. $filterData->setData('order_statuses', explode(',',$orderStatuses[0]));
  142. }
  143. }
  144. $resourceCollection = Mage::getResourceModel($this->getResourceCollectionName())
  145. ->setPeriod($filterData->getData('period_type'))
  146. ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
  147. ->addStoreFilter($storeIds)
  148. ->setAggregatedColumns($this->_getAggregatedColumns());
  149. $this->_addOrderStatusFilter($resourceCollection, $filterData);
  150. $this->_addCustomFilter($resourceCollection, $filterData);
  151. if ($this->_isExport) {
  152. $this->setCollection($resourceCollection);
  153. return $this;
  154. }
  155. if ($filterData->getData('show_empty_rows', false)) {
  156. Mage::helper('reports')->prepareIntervalsCollection(
  157. $this->getCollection(),
  158. $filterData->getData('from', null),
  159. $filterData->getData('to', null),
  160. $filterData->getData('period_type')
  161. );
  162. }
  163. if ($this->getCountSubTotals()) {
  164. $this->getSubTotals();
  165. }
  166. if ($this->getCountTotals()) {
  167. $totalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
  168. ->setPeriod($filterData->getData('period_type'))
  169. ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
  170. ->addStoreFilter($storeIds)
  171. ->setAggregatedColumns($this->_getAggregatedColumns())
  172. ->isTotals(true);
  173. $this->_addOrderStatusFilter($totalsCollection, $filterData);
  174. $this->_addCustomFilter($totalsCollection, $filterData);
  175. foreach ($totalsCollection as $item) {
  176. $this->setTotals($item);
  177. break;
  178. }
  179. }
  180. $this->getCollection()->setColumnGroupBy($this->_columnGroupBy);
  181. $this->getCollection()->setResourceCollection($resourceCollection);
  182. return parent::_prepareCollection();
  183. }
  184. public function getCountTotals()
  185. {
  186. if (!$this->getTotals()) {
  187. $filterData = $this->getFilterData();
  188. $totalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
  189. ->setPeriod($filterData->getData('period_type'))
  190. ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
  191. ->addStoreFilter($this->_getStoreIds())
  192. ->setAggregatedColumns($this->_getAggregatedColumns())
  193. ->isTotals(true);
  194. $this->_addOrderStatusFilter($totalsCollection, $filterData);
  195. if (count($totalsCollection->getItems()) < 1 || !$filterData->getData('from')) {
  196. $this->setTotals(new Varien_Object());
  197. } else {
  198. foreach ($totalsCollection->getItems() as $item) {
  199. $this->setTotals($item);
  200. break;
  201. }
  202. }
  203. }
  204. return parent::getCountTotals();
  205. }
  206. public function getSubTotals()
  207. {
  208. $filterData = $this->getFilterData();
  209. $subTotalsCollection = Mage::getResourceModel($this->getResourceCollectionName())
  210. ->setPeriod($filterData->getData('period_type'))
  211. ->setDateRange($filterData->getData('from', null), $filterData->getData('to', null))
  212. ->addStoreFilter($this->_getStoreIds())
  213. ->setAggregatedColumns($this->_getAggregatedColumns())
  214. ->isSubTotals(true);
  215. $this->_addOrderStatusFilter($subTotalsCollection, $filterData);
  216. $this->_addCustomFilter($subTotalsCollection, $filterData);
  217. $this->setSubTotals($subTotalsCollection->getItems());
  218. return parent::getSubTotals();
  219. }
  220. public function setStoreIds($storeIds)
  221. {
  222. $this->_storeIds = $storeIds;
  223. return $this;
  224. }
  225. public function getCurrentCurrencyCode()
  226. {
  227. if (is_null($this->_currentCurrencyCode)) {
  228. $this->_currentCurrencyCode = (count($this->_storeIds) > 0)
  229. ? Mage::app()->getStore(array_shift($this->_storeIds))->getBaseCurrencyCode()
  230. : Mage::app()->getStore()->getBaseCurrencyCode();
  231. }
  232. return $this->_currentCurrencyCode;
  233. }
  234. /**
  235. * Get currency rate (base to given currency)
  236. *
  237. * @param string|Mage_Directory_Model_Currency $currencyCode
  238. * @return double
  239. */
  240. public function getRate($toCurrency)
  241. {
  242. return Mage::app()->getStore()->getBaseCurrency()->getRate($toCurrency);
  243. }
  244. /**
  245. * Add order status filter
  246. *
  247. * @param Mage_Reports_Model_Resource_Report_Collection_Abstract $collection
  248. * @param Varien_Object $filterData
  249. * @return Mage_Adminhtml_Block_Report_Grid_Abstract
  250. */
  251. protected function _addOrderStatusFilter($collection, $filterData)
  252. {
  253. $collection->addOrderStatusFilter($filterData->getData('order_statuses'));
  254. return $this;
  255. }
  256. /**
  257. * Adds custom filter to resource collection
  258. * Can be overridden in child classes if custom filter needed
  259. *
  260. * @param Mage_Reports_Model_Resource_Report_Collection_Abstract $collection
  261. * @param Varien_Object $filterData
  262. * @return Mage_Adminhtml_Block_Report_Grid_Abstract
  263. */
  264. protected function _addCustomFilter($collection, $filterData)
  265. {
  266. return $this;
  267. }
  268. }