Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Grid.php
Go to the documentation of this file.
1 <?php
8 
16 class Grid extends \Magento\Backend\Block\Widget\Grid
17 {
23  protected $_storeSwitcherVisibility = true;
24 
30  protected $_dateFilterVisibility = true;
31 
37  protected $_filters = [];
38 
44  protected $_defaultFilters = ['report_from' => '', 'report_to' => '', 'report_period' => 'day'];
45 
51  protected $_subReportSize = 5;
52 
58  protected $_errors = [];
59 
65  protected $_template = 'Magento_Reports::grid.phtml';
66 
72  protected $_filterValues;
73 
80  protected function _prepareCollection()
81  {
82  $filter = $this->getParam($this->getVarNameFilter(), null);
83 
84  if (null === $filter) {
85  $filter = $this->_defaultFilter;
86  }
87 
88  if (is_string($filter)) {
89  $data = [];
90  $filter = base64_decode($filter);
91  parse_str(urldecode($filter), $data);
92 
93  if (!isset($data['report_from'])) {
94  // getting all reports from 2001 year
95  $date = (new \DateTime())->setTimestamp(mktime(0, 0, 0, 1, 1, 2001));
96  $data['report_from'] = $this->_localeDate->formatDateTime(
97  $date,
98  \IntlDateFormatter::SHORT,
99  \IntlDateFormatter::NONE
100  );
101  }
102 
103  if (!isset($data['report_to'])) {
104  // getting all reports from 2001 year
105  $date = new \DateTime();
106  $data['report_to'] = $this->_localeDate->formatDateTime(
107  $date,
108  \IntlDateFormatter::SHORT,
109  \IntlDateFormatter::NONE
110  );
111  }
112 
113  $this->_setFilterValues($data);
114  } elseif ($filter && is_array($filter)) {
115  $this->_setFilterValues($filter);
116  } elseif (0 !== sizeof($this->_defaultFilter)) {
117  $this->_setFilterValues($this->_defaultFilter);
118  }
119 
121  $collection = $this->getCollection();
122  if ($collection) {
123  $collection->setPeriod($this->getFilter('report_period'));
124 
125  if ($this->getFilter('report_from') && $this->getFilter('report_to')) {
129  try {
130  $from = $this->_localeDate->date($this->getFilter('report_from'), null, false, false);
131  $to = $this->_localeDate->date($this->getFilter('report_to'), null, false, false);
132 
133  $collection->setInterval($from, $to);
134  } catch (\Exception $e) {
135  $this->_errors[] = __('Invalid date specified');
136  }
137  }
138 
139  $collection->setStoreIds($this->_getAllowedStoreIds());
140 
141  if ($this->getSubReportSize() !== null) {
142  $collection->setPageSize($this->getSubReportSize());
143  }
144 
145  $this->_eventManager->dispatch(
146  'adminhtml_widget_grid_filter_collection',
147  ['collection' => $this->getCollection(), 'filter_values' => $this->_filterValues]
148  );
149  }
150 
151  return $this;
152  }
153 
159  protected function _getAllowedStoreIds()
160  {
164  $storeIds = [];
165  if ($this->getRequest()->getParam('store')) {
166  $storeIds = [$this->getParam('store')];
167  } elseif ($this->getRequest()->getParam('website')) {
168  $storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
169  } elseif ($this->getRequest()->getParam('group')) {
170  $storeIds = $storeIds = $this->_storeManager->getGroup(
171  $this->getRequest()->getParam('group')
172  )->getStoreIds();
173  }
174 
175  // By default storeIds array contains only allowed stores
176  $allowedStoreIds = array_keys($this->_storeManager->getStores());
177  // And then array_intersect with post data for prevent unauthorized stores reports
178  $storeIds = array_intersect($allowedStoreIds, $storeIds);
179  // If selected all websites or unauthorized stores use only allowed
180  if (empty($storeIds)) {
181  $storeIds = $allowedStoreIds;
182  }
183  // reset array keys
184  $storeIds = array_values($storeIds);
185 
186  return $storeIds;
187  }
188 
196  protected function _setFilterValues($data)
197  {
198  foreach ($data as $name => $value) {
199  $this->setFilter($name, $data[$name]);
200  }
201  return $this;
202  }
203 
211  public function setStoreSwitcherVisibility($visible = true)
212  {
213  $this->_storeSwitcherVisibility = $visible;
214  }
215 
223  public function getStoreSwitcherVisibility()
224  {
226  }
227 
234  public function getStoreSwitcherHtml()
235  {
236  return $this->getChildHtml('store_switcher');
237  }
238 
246  public function setDateFilterVisibility($visible = true)
247  {
248  $this->_dateFilterVisibility = $visible;
249  }
250 
258  public function getDateFilterVisibility()
259  {
261  }
262 
269  public function getDateFilterHtml()
270  {
271  return $this->getChildHtml('date_filter');
272  }
273 
279  public function getPeriods()
280  {
281  return $this->getCollection()->getPeriods();
282  }
283 
289  public function getDateFormat()
290  {
291  return $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
292  }
293 
300  public function getRefreshButtonHtml()
301  {
302  return $this->getChildHtml('refresh_button');
303  }
304 
313  public function setFilter($name, $value)
314  {
315  if ($name) {
316  $this->_filters[$name] = $value;
317  }
318  }
319 
326  public function getFilter($name)
327  {
328  if (isset($this->_filters[$name])) {
329  return $this->_filters[$name];
330  } else {
331  return $this->getRequest()->getParam($name) ? htmlspecialchars($this->getRequest()->getParam($name)) : '';
332  }
333  }
334 
342  public function setSubReportSize($size)
343  {
344  $this->_subReportSize = $size;
345  }
346 
353  public function getSubReportSize()
354  {
355  return $this->_subReportSize;
356  }
357 
364  public function getErrors()
365  {
366  return $this->_errors;
367  }
368 
374  protected function _prepareFilterButtons()
375  {
376  $this->addChild(
377  'refresh_button',
378  \Magento\Backend\Block\Widget\Button::class,
379  ['label' => __('Refresh'), 'onclick' => "{$this->getJsObjectName()}.doFilter();", 'class' => 'task']
380  );
381  }
382 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
setStoreSwitcherVisibility($visible=true)
Definition: Grid.php:211
$value
Definition: gender.phtml:16
getParam($paramName, $default=null)
Definition: Grid.php:729
setDateFilterVisibility($visible=true)
Definition: Grid.php:246
if(!isset($_GET['name'])) $name
Definition: log.php:14