Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractGrid.php
Go to the documentation of this file.
1 <?php
8 
12 class AbstractGrid extends \Magento\Backend\Block\Widget\Grid\Extended
13 {
17  protected $_resourceCollectionName = '';
18 
22  protected $_currentCurrencyCode = null;
23 
27  protected $_storeIds = [];
28 
32  protected $_aggregatedColumns = null;
33 
39  protected $_reportsData = null;
40 
47 
53  protected $_resourceFactory;
54 
63  public function __construct(
64  \Magento\Backend\Block\Template\Context $context,
65  \Magento\Backend\Helper\Data $backendHelper,
66  \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory,
67  \Magento\Reports\Model\Grouped\CollectionFactory $collectionFactory,
68  \Magento\Reports\Helper\Data $reportsData,
69  array $data = []
70  ) {
71  $this->_resourceFactory = $resourceFactory;
72  $this->_collectionFactory = $collectionFactory;
73  $this->_reportsData = $reportsData;
74  parent::__construct($context, $backendHelper, $data);
75  }
76 
82  protected function _construct()
83  {
84  parent::_construct();
85  $this->setFilterVisibility(false);
86  $this->setPagerVisibility(false);
87  $this->setUseAjax(false);
88  if (isset($this->_columnGroupBy)) {
89  $this->isColumnGrouped($this->_columnGroupBy, true);
90  }
91  $this->setEmptyCellLabel(__('We can\'t find records for this period.'));
92  }
93 
100  public function getResourceCollectionName()
101  {
103  }
104 
110  public function getCollection()
111  {
112  if ($this->_collection === null) {
113  $this->setCollection($this->_collectionFactory->create());
114  }
115  return $this->_collection;
116  }
117 
123  protected function _getAggregatedColumns()
124  {
125  if ($this->_aggregatedColumns === null) {
126  foreach ($this->getColumns() as $column) {
127  if (!is_array($this->_aggregatedColumns)) {
128  $this->_aggregatedColumns = [];
129  }
130  if ($column->hasTotal()) {
131  $this->_aggregatedColumns[$column->getId()] = "{$column->getTotal()}({$column->getIndex()})";
132  }
133  }
134  }
136  }
137 
148  public function addColumn($columnId, $column)
149  {
150  if (is_array($column) && array_key_exists('visibility_filter', $column)) {
151  $filterData = $this->getFilterData();
152  $visibilityFilter = $column['visibility_filter'];
153  if (!is_array($visibilityFilter)) {
154  $visibilityFilter = [$visibilityFilter];
155  }
156  foreach ($visibilityFilter as $k => $v) {
157  if (is_int($k)) {
158  $filterFieldId = $v;
159  $filterFieldValue = true;
160  } else {
161  $filterFieldId = $k;
162  $filterFieldValue = $v;
163  }
164  if (!$filterData->hasData($filterFieldId) || $filterData->getData($filterFieldId) != $filterFieldValue
165  ) {
166  return $this; // don't add column
167  }
168  }
169  }
170  return parent::addColumn($columnId, $column);
171  }
172 
178  protected function _getStoreIds()
179  {
180  $storeIds = $this->getFilteredStores();
181  // By default storeIds array contains only allowed stores
182  $allowedStoreIds = array_keys($this->_storeManager->getStores());
183  // And then array_intersect with post data for prevent unauthorized stores reports
184  $storeIds = array_intersect($allowedStoreIds, $storeIds);
185  // If selected all websites or unauthorized stores use only allowed
186  if (empty($storeIds)) {
187  $storeIds = $allowedStoreIds;
188  }
189  // reset array keys
190  $storeIds = array_values($storeIds);
191 
192  return $storeIds;
193  }
194 
202  protected function _prepareCollection()
203  {
204  $filterData = $this->getFilterData();
205 
206  if ($filterData->getData('from') == null || $filterData->getData('to') == null) {
207  $this->setCountTotals(false);
208  $this->setCountSubTotals(false);
209  return parent::_prepareCollection();
210  }
211 
212  $storeIds = $this->_getStoreIds();
213 
214  $orderStatuses = $filterData->getData('order_statuses');
215  if (is_array($orderStatuses)) {
216  if (count($orderStatuses) == 1 && strpos($orderStatuses[0], ',') !== false) {
217  $filterData->setData('order_statuses', explode(',', $orderStatuses[0]));
218  }
219  }
220 
221  $resourceCollection = $this->_resourceFactory->create(
223  )->setPeriod(
224  $filterData->getData('period_type')
225  )->setDateRange(
226  $filterData->getData('from', null),
227  $filterData->getData('to', null)
228  )->addStoreFilter(
229  $storeIds
230  )->setAggregatedColumns(
231  $this->_getAggregatedColumns()
232  );
233 
234  $this->_addOrderStatusFilter($resourceCollection, $filterData);
235  $this->_addCustomFilter($resourceCollection, $filterData);
236 
237  if ($this->_isExport) {
238  $this->setCollection($resourceCollection);
239  return $this;
240  }
241 
242  if ($filterData->getData('show_empty_rows', false)) {
243  $this->_reportsData->prepareIntervalsCollection(
244  $this->getCollection(),
245  $filterData->getData('from', null),
246  $filterData->getData('to', null),
247  $filterData->getData('period_type')
248  );
249  }
250 
251  if ($this->getCountSubTotals()) {
252  $this->getSubTotals();
253  }
254 
255  if ($this->getCountTotals()) {
256  $totalsCollection = $this->_resourceFactory->create(
258  )->setPeriod(
259  $filterData->getData('period_type')
260  )->setDateRange(
261  $filterData->getData('from', null),
262  $filterData->getData('to', null)
263  )->addStoreFilter(
264  $storeIds
265  )->setAggregatedColumns(
266  $this->_getAggregatedColumns()
267  )->isTotals(
268  true
269  );
270 
271  $this->_addOrderStatusFilter($totalsCollection, $filterData);
272  $this->_addCustomFilter($totalsCollection, $filterData);
273 
274  foreach ($totalsCollection as $item) {
275  $this->setTotals($item);
276  break;
277  }
278  }
279 
280  $this->getCollection()->setColumnGroupBy($this->_columnGroupBy);
281  $this->getCollection()->setResourceCollection($resourceCollection);
282 
283  return parent::_prepareCollection();
284  }
285 
291  public function getCountTotals()
292  {
293  if (!$this->getTotals()) {
294  $filterData = $this->getFilterData();
295  $totalsCollection = $this->_resourceFactory->create(
297  )->setPeriod(
298  $filterData->getData('period_type')
299  )->setDateRange(
300  $filterData->getData('from', null),
301  $filterData->getData('to', null)
302  )->addStoreFilter(
303  $this->_getStoreIds()
304  )->setAggregatedColumns(
305  $this->_getAggregatedColumns()
306  )->isTotals(
307  true
308  );
309 
310  $this->_addOrderStatusFilter($totalsCollection, $filterData);
311  $this->_addCustomFilter($totalsCollection, $filterData);
312 
313  if ($totalsCollection->load()->getSize() < 1 || !$filterData->getData('from')) {
314  $this->setTotals(new \Magento\Framework\DataObject());
315  $this->setCountTotals(false);
316  } else {
317  foreach ($totalsCollection->getItems() as $item) {
318  $this->setTotals($item);
319  break;
320  }
321  }
322  }
323 
324  return parent::getCountTotals();
325  }
326 
332  public function getSubTotals()
333  {
334  $filterData = $this->getFilterData();
335  $subTotalsCollection = $this->_resourceFactory->create(
337  )->setPeriod(
338  $filterData->getData('period_type')
339  )->setDateRange(
340  $filterData->getData('from', null),
341  $filterData->getData('to', null)
342  )->addStoreFilter(
343  $this->_getStoreIds()
344  )->setAggregatedColumns(
345  $this->_getAggregatedColumns()
346  )->setIsSubTotals(
347  true
348  );
349 
350  $this->_addOrderStatusFilter($subTotalsCollection, $filterData);
351  $this->_addCustomFilter($subTotalsCollection, $filterData);
352 
353  $this->setSubTotals($subTotalsCollection->getItems());
354  return parent::getSubTotals();
355  }
356 
364  public function setStoreIds($storeIds)
365  {
366  $this->_storeIds = $storeIds;
367  return $this;
368  }
369 
375  public function getCurrentCurrencyCode()
376  {
377  if ($this->_currentCurrencyCode === null) {
378  $this->_currentCurrencyCode = count($this->_storeIds) > 0
379  ? $this->_storeManager->getStore(array_shift($this->_storeIds))->getCurrentCurrencyCode()
380  : $this->_storeManager->getStore()->getBaseCurrencyCode();
381  }
382 
384  }
385 
392  public function getRate($toCurrency)
393  {
394  return $this->_storeManager->getStore()->getBaseCurrency()->getRate($toCurrency);
395  }
396 
404  protected function _addOrderStatusFilter($collection, $filterData)
405  {
406  $collection->addOrderStatusFilter($filterData->getData('order_statuses'));
407  return $this;
408  }
409 
421  protected function _addCustomFilter($collection, $filterData)
422  {
423  return $this;
424  }
425 
432  private function getFilteredStores(): array
433  {
434  $storeIds = [];
435 
436  $filterData = $this->getFilterData();
437  if ($filterData) {
438  if ($filterData->getWebsite()) {
439  $storeIds = array_keys(
440  $this->_storeManager->getWebsite($filterData->getWebsite())->getStores()
441  );
442  }
443 
444  if ($filterData->getGroup()) {
445  $storeIds = array_keys(
446  $this->_storeManager->getGroup($filterData->getGroup())->getStores()
447  );
448  }
449 
450  if ($filterData->getData('store_ids')) {
451  $storeIds = explode(',', $filterData->getData('store_ids'));
452  }
453  }
454  return is_array($storeIds) ? $storeIds : [];
455  }
456 }
__()
Definition: __.php:13
setTotals(\Magento\Framework\DataObject $totals)
Definition: Grid.php:796
setPagerVisibility($visible=true)
Definition: Grid.php:588
$this _collection
Definition: coupons.php:7
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory, \Magento\Reports\Model\Grouped\CollectionFactory $collectionFactory, \Magento\Reports\Helper\Data $reportsData, array $data=[])