Grid.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Block\Adminhtml;
  7. /**
  8. * Backend report grid block
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Grid extends \Magento\Backend\Block\Widget\Grid
  15. {
  16. /**
  17. * Should Store Switcher block be visible
  18. *
  19. * @var bool
  20. */
  21. protected $_storeSwitcherVisibility = true;
  22. /**
  23. * Should Date Filter block be visible
  24. *
  25. * @var bool
  26. */
  27. protected $_dateFilterVisibility = true;
  28. /**
  29. * Filters array
  30. *
  31. * @var array
  32. */
  33. protected $_filters = [];
  34. /**
  35. * Default filters values
  36. *
  37. * @var array
  38. */
  39. protected $_defaultFilters = ['report_from' => '', 'report_to' => '', 'report_period' => 'day'];
  40. /**
  41. * Sub-report rows count
  42. *
  43. * @var int
  44. */
  45. protected $_subReportSize = 5;
  46. /**
  47. * Errors messages aggregated array
  48. *
  49. * @var array
  50. */
  51. protected $_errors = [];
  52. /**
  53. * Block template file name
  54. *
  55. * @var string
  56. */
  57. protected $_template = 'Magento_Reports::grid.phtml';
  58. /**
  59. * Filter values array
  60. *
  61. * @var array
  62. */
  63. protected $_filterValues;
  64. /**
  65. * Apply sorting and filtering to collection
  66. *
  67. * @return $this
  68. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  69. */
  70. protected function _prepareCollection()
  71. {
  72. $filter = $this->getParam($this->getVarNameFilter(), null);
  73. if (null === $filter) {
  74. $filter = $this->_defaultFilter;
  75. }
  76. if (is_string($filter)) {
  77. $data = [];
  78. $filter = base64_decode($filter);
  79. parse_str(urldecode($filter), $data);
  80. if (!isset($data['report_from'])) {
  81. // getting all reports from 2001 year
  82. $date = (new \DateTime())->setTimestamp(mktime(0, 0, 0, 1, 1, 2001));
  83. $data['report_from'] = $this->_localeDate->formatDateTime(
  84. $date,
  85. \IntlDateFormatter::SHORT,
  86. \IntlDateFormatter::NONE
  87. );
  88. }
  89. if (!isset($data['report_to'])) {
  90. // getting all reports from 2001 year
  91. $date = new \DateTime();
  92. $data['report_to'] = $this->_localeDate->formatDateTime(
  93. $date,
  94. \IntlDateFormatter::SHORT,
  95. \IntlDateFormatter::NONE
  96. );
  97. }
  98. $this->_setFilterValues($data);
  99. } elseif ($filter && is_array($filter)) {
  100. $this->_setFilterValues($filter);
  101. } elseif (0 !== sizeof($this->_defaultFilter)) {
  102. $this->_setFilterValues($this->_defaultFilter);
  103. }
  104. /** @var $collection \Magento\Reports\Model\ResourceModel\Report\Collection */
  105. $collection = $this->getCollection();
  106. if ($collection) {
  107. $collection->setPeriod($this->getFilter('report_period'));
  108. if ($this->getFilter('report_from') && $this->getFilter('report_to')) {
  109. /**
  110. * Validate from and to date
  111. */
  112. try {
  113. $from = $this->_localeDate->date($this->getFilter('report_from'), null, false, false);
  114. $to = $this->_localeDate->date($this->getFilter('report_to'), null, false, false);
  115. $collection->setInterval($from, $to);
  116. } catch (\Exception $e) {
  117. $this->_errors[] = __('Invalid date specified');
  118. }
  119. }
  120. $collection->setStoreIds($this->_getAllowedStoreIds());
  121. if ($this->getSubReportSize() !== null) {
  122. $collection->setPageSize($this->getSubReportSize());
  123. }
  124. $this->_eventManager->dispatch(
  125. 'adminhtml_widget_grid_filter_collection',
  126. ['collection' => $this->getCollection(), 'filter_values' => $this->_filterValues]
  127. );
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Get allowed stores
  133. *
  134. * @return array|\int[]
  135. */
  136. protected function _getAllowedStoreIds()
  137. {
  138. /**
  139. * Getting and saving store ids for website & group
  140. */
  141. $storeIds = [];
  142. if ($this->getRequest()->getParam('store')) {
  143. $storeIds = [$this->getParam('store')];
  144. } elseif ($this->getRequest()->getParam('website')) {
  145. $storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
  146. } elseif ($this->getRequest()->getParam('group')) {
  147. $storeIds = $storeIds = $this->_storeManager->getGroup(
  148. $this->getRequest()->getParam('group')
  149. )->getStoreIds();
  150. }
  151. // By default storeIds array contains only allowed stores
  152. $allowedStoreIds = array_keys($this->_storeManager->getStores());
  153. // And then array_intersect with post data for prevent unauthorized stores reports
  154. $storeIds = array_intersect($allowedStoreIds, $storeIds);
  155. // If selected all websites or unauthorized stores use only allowed
  156. if (empty($storeIds)) {
  157. $storeIds = $allowedStoreIds;
  158. }
  159. // reset array keys
  160. $storeIds = array_values($storeIds);
  161. return $storeIds;
  162. }
  163. /**
  164. * Set filter values
  165. *
  166. * @param array $data
  167. * @return $this
  168. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  169. */
  170. protected function _setFilterValues($data)
  171. {
  172. foreach ($data as $name => $value) {
  173. $this->setFilter($name, $data[$name]);
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Set visibility of store switcher
  179. *
  180. * @param bool $visible
  181. * @codeCoverageIgnore
  182. * @return void
  183. */
  184. public function setStoreSwitcherVisibility($visible = true)
  185. {
  186. $this->_storeSwitcherVisibility = $visible;
  187. }
  188. /**
  189. * Return visibility of store switcher
  190. *
  191. * @codeCoverageIgnore
  192. * @return bool
  193. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  194. */
  195. public function getStoreSwitcherVisibility()
  196. {
  197. return $this->_storeSwitcherVisibility;
  198. }
  199. /**
  200. * Return store switcher html
  201. *
  202. * @codeCoverageIgnore
  203. * @return string
  204. */
  205. public function getStoreSwitcherHtml()
  206. {
  207. return $this->getChildHtml('store_switcher');
  208. }
  209. /**
  210. * Set visibility of date filter
  211. *
  212. * @param bool $visible
  213. * @return void
  214. * @codeCoverageIgnore
  215. */
  216. public function setDateFilterVisibility($visible = true)
  217. {
  218. $this->_dateFilterVisibility = $visible;
  219. }
  220. /**
  221. * Return visibility of date filter
  222. *
  223. * @codeCoverageIgnore
  224. * @return bool
  225. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  226. */
  227. public function getDateFilterVisibility()
  228. {
  229. return $this->_dateFilterVisibility;
  230. }
  231. /**
  232. * Return date filter html
  233. *
  234. * @codeCoverageIgnore
  235. * @return string
  236. */
  237. public function getDateFilterHtml()
  238. {
  239. return $this->getChildHtml('date_filter');
  240. }
  241. /**
  242. * Get periods
  243. *
  244. * @return mixed
  245. */
  246. public function getPeriods()
  247. {
  248. return $this->getCollection()->getPeriods();
  249. }
  250. /**
  251. * Get date format according the locale
  252. *
  253. * @return string
  254. */
  255. public function getDateFormat()
  256. {
  257. return $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
  258. }
  259. /**
  260. * Return refresh button html
  261. *
  262. * @codeCoverageIgnore
  263. * @return string
  264. */
  265. public function getRefreshButtonHtml()
  266. {
  267. return $this->getChildHtml('refresh_button');
  268. }
  269. /**
  270. * Set filter
  271. *
  272. * @param string $name
  273. * @param string $value
  274. * @return void
  275. * @codeCoverageIgnore
  276. */
  277. public function setFilter($name, $value)
  278. {
  279. if ($name) {
  280. $this->_filters[$name] = $value;
  281. }
  282. }
  283. /**
  284. * Get filter by key
  285. *
  286. * @param string $name
  287. * @return string
  288. */
  289. public function getFilter($name)
  290. {
  291. if (isset($this->_filters[$name])) {
  292. return $this->_filters[$name];
  293. } else {
  294. return $this->getRequest()->getParam($name) ? htmlspecialchars($this->getRequest()->getParam($name)) : '';
  295. }
  296. }
  297. /**
  298. * Set sub-report rows count
  299. *
  300. * @param int $size
  301. * @return void
  302. * @codeCoverageIgnore
  303. */
  304. public function setSubReportSize($size)
  305. {
  306. $this->_subReportSize = $size;
  307. }
  308. /**
  309. * Return sub-report rows count
  310. *
  311. * @codeCoverageIgnore
  312. * @return int
  313. */
  314. public function getSubReportSize()
  315. {
  316. return $this->_subReportSize;
  317. }
  318. /**
  319. * Retrieve errors
  320. *
  321. * @return array
  322. * @codeCoverageIgnore
  323. */
  324. public function getErrors()
  325. {
  326. return $this->_errors;
  327. }
  328. /**
  329. * Prepare grid filter buttons
  330. *
  331. * @return void
  332. */
  333. protected function _prepareFilterButtons()
  334. {
  335. $this->addChild(
  336. 'refresh_button',
  337. \Magento\Backend\Block\Widget\Button::class,
  338. ['label' => __('Refresh'), 'onclick' => "{$this->getJsObjectName()}.doFilter();", 'class' => 'task']
  339. );
  340. }
  341. }