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 
20 {
26  protected $_varNameLimit = 'limit';
27 
31  protected $_varNamePage = 'page';
32 
36  protected $_varNameSort = 'sort';
37 
41  protected $_varNameDir = 'dir';
42 
46  protected $_varNameFilter = 'filter';
47 
51  protected $_defaultLimit = 20;
52 
56  protected $_defaultPage = 1;
57 
61  protected $_defaultSort = false;
62 
66  protected $_defaultDir = 'desc';
67 
71  protected $_defaultFilter = [];
72 
78  protected $_emptyText;
79 
85  protected $_emptyTextCss = 'empty-text';
86 
92  protected $_pagerVisibility = true;
93 
99  protected $_messageBlockVisibility = false;
100 
106  protected $_saveParametersInSession = false;
107 
113  protected $_countTotals = false;
114 
120  protected $_varTotals;
121 
125  protected $_template = 'Magento_Backend::widget/grid.phtml';
126 
130  protected $_backendSession;
131 
135  protected $_backendHelper;
136 
142  public function __construct(
143  \Magento\Backend\Block\Template\Context $context,
144  \Magento\Backend\Helper\Data $backendHelper,
145  array $data = []
146  ) {
147  $this->_backendHelper = $backendHelper;
148  $this->_backendSession = $context->getBackendSession();
149  parent::__construct($context, $data);
150  }
151 
156  protected function _construct()
157  {
158  parent::_construct();
159 
160  if (!$this->getRowClickCallback()) {
161  $this->setRowClickCallback('openGridRow');
162  }
163 
164  if ($this->hasData('id')) {
165  $this->setId($this->getData('id'));
166  }
167 
168  if ($this->hasData('default_sort')) {
169  $this->setDefaultSort($this->getData('default_sort'));
170  }
171 
172  if ($this->hasData('default_dir')) {
173  $this->setDefaultDir($this->getData('default_dir'));
174  }
175 
176  if ($this->hasData('save_parameters_in_session')) {
177  $this->setSaveParametersInSession($this->getData('save_parameters_in_session'));
178  }
179 
180  $this->setPagerVisibility(
181  $this->hasData('pager_visibility') ? (bool)$this->getData('pager_visibility') : true
182  );
183 
184  $this->setData('use_ajax', $this->hasData('use_ajax') ? (bool)$this->getData('use_ajax') : false);
185  }
186 
193  public function setCollection($collection)
194  {
195  $this->setData('dataSource', $collection);
196  }
197 
203  public function getCollection()
204  {
205  return $this->getData('dataSource');
206  }
207 
213  public function getColumnSet()
214  {
215  return $this->getChildBlock('grid.columnSet');
216  }
217 
224  public function getExportBlock()
225  {
226  if (!$this->getChildBlock('grid.export')) {
227  throw new \Magento\Framework\Exception\LocalizedException(
228  __('Export block for grid %1 is not defined', $this->getNameInLayout())
229  );
230  }
231  return $this->getChildBlock('grid.export');
232  }
233 
239  public function getColumns()
240  {
241  return $this->getColumnSet()->getColumns();
242  }
243 
249  public function getColumnCount()
250  {
251  return count($this->getColumns());
252  }
253 
260  public function getColumn($columnId)
261  {
262  return $this->getColumnSet()->getChildBlock($columnId);
263  }
264 
271  protected function _setFilterValues($data)
272  {
273  foreach ($this->getColumns() as $columnId => $column) {
274  if (isset(
275  $data[$columnId]
276  ) && (is_array(
277  $data[$columnId]
278  ) && !empty($data[$columnId]) || strlen(
279  $data[$columnId]
280  ) > 0) && $column->getFilter()
281  ) {
282  $column->getFilter()->setValue($data[$columnId]);
283  $this->_addColumnFilterToCollection($column);
284  }
285  }
286  return $this;
287  }
288 
295  protected function _addColumnFilterToCollection($column)
296  {
297  if ($this->getCollection()) {
298  $field = $column->getFilterIndex() ? $column->getFilterIndex() : $column->getIndex();
299  if ($column->getFilterConditionCallback()) {
300  call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
301  } else {
302  $condition = $column->getFilter()->getCondition();
303  if ($field && isset($condition)) {
304  $this->getCollection()->addFieldToFilter($field, $condition);
305  }
306  }
307  }
308  return $this;
309  }
310 
317  protected function _setCollectionOrder($column)
318  {
319  $collection = $this->getCollection();
320  if ($collection) {
321  $columnIndex = $column->getFilterIndex() ? $column->getFilterIndex() : $column->getIndex();
322  $collection->setOrder($columnIndex, strtoupper($column->getDir()));
323  }
324  return $this;
325  }
326 
332  public function getPreparedCollection()
333  {
334  $this->_prepareCollection();
335  return $this->getCollection();
336  }
337 
344  protected function _prepareCollection()
345  {
346  if ($this->getCollection()) {
347  $this->_preparePage();
348 
349  $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
350  $dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
351  $filter = $this->getParam($this->getVarNameFilter(), null);
352 
353  if ($filter === null) {
354  $filter = $this->_defaultFilter;
355  }
356 
357  if (is_string($filter)) {
358  $data = $this->_backendHelper->prepareFilterString($filter);
359  $data = array_merge($data, (array)$this->getRequest()->getPost($this->getVarNameFilter()));
360  $this->_setFilterValues($data);
361  } elseif ($filter && is_array($filter)) {
362  $this->_setFilterValues($filter);
363  } elseif (0 !== sizeof($this->_defaultFilter)) {
364  $this->_setFilterValues($this->_defaultFilter);
365  }
366 
367  if ($this->getColumn($columnId) && $this->getColumn($columnId)->getIndex()) {
368  $dir = strtolower($dir) == 'desc' ? 'desc' : 'asc';
369  $this->getColumn($columnId)->setDir($dir);
370  $this->_setCollectionOrder($this->getColumn($columnId));
371  }
372  }
373 
374  return $this;
375  }
376 
382  protected function _preparePage()
383  {
384  $this->getCollection()->setPageSize((int)$this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
385  $this->getCollection()->setCurPage((int)$this->getParam($this->getVarNamePage(), $this->_defaultPage));
386  }
387 
393  protected function _prepareGrid()
394  {
395  $this->_eventManager->dispatch(
396  'backend_block_widget_grid_prepare_grid_before',
397  ['grid' => $this, 'collection' => $this->getCollection()]
398  );
399  if ($this->getChildBlock('grid.massaction') && $this->getChildBlock('grid.massaction')->isAvailable()) {
400  $this->getChildBlock('grid.massaction')->prepareMassactionColumn();
401  }
402 
403  $this->_prepareCollection();
404  if ($this->hasColumnRenderers()) {
405  foreach ($this->getColumnRenderers() as $renderer => $rendererClass) {
406  $this->getColumnSet()->setRendererType($renderer, $rendererClass);
407  }
408  }
409  if ($this->hasColumnFilters()) {
410  foreach ($this->getColumnFilters() as $filter => $filterClass) {
411  $this->getColumnSet()->setFilterType($filter, $filterClass);
412  }
413  }
414  $this->getColumnSet()->setSortable($this->getSortable());
415  $this->_prepareFilterButtons();
416  }
417 
423  public function getMassactionBlock()
424  {
425  return $this->getChildBlock('grid.massaction');
426  }
427 
433  protected function _prepareFilterButtons()
434  {
435  $this->setChild(
436  'reset_filter_button',
437  $this->getLayout()->createBlock(
438  \Magento\Backend\Block\Widget\Button::class
439  )->setData(
440  [
441  'label' => __('Reset Filter'),
442  'onclick' => $this->getJsObjectName() . '.resetFilter()',
443  'class' => 'action-reset action-tertiary'
444  ]
445  )->setDataAttribute(['action' => 'grid-filter-reset'])
446  );
447  $this->setChild(
448  'search_button',
449  $this->getLayout()->createBlock(
450  \Magento\Backend\Block\Widget\Button::class
451  )->setData(
452  [
453  'label' => __('Search'),
454  'onclick' => $this->getJsObjectName() . '.doFilter()',
455  'class' => 'action-secondary',
456  ]
457  )->setDataAttribute(['action' => 'grid-filter-apply'])
458  );
459  }
460 
466  protected function _beforeToHtml()
467  {
468  $this->_prepareGrid();
469  return parent::_beforeToHtml();
470  }
471 
477  public function getVarNameLimit()
478  {
479  return $this->_varNameLimit;
480  }
481 
487  public function getVarNamePage()
488  {
489  return $this->_varNamePage;
490  }
491 
497  public function getVarNameSort()
498  {
499  return $this->_varNameSort;
500  }
501 
507  public function getVarNameDir()
508  {
509  return $this->_varNameDir;
510  }
511 
517  public function getVarNameFilter()
518  {
519  return $this->_varNameFilter;
520  }
521 
528  public function setVarNameLimit($name)
529  {
530  $this->_varNameLimit = $name;
531  return $this;
532  }
533 
540  public function setVarNamePage($name)
541  {
542  $this->_varNamePage = $name;
543  return $this;
544  }
545 
552  public function setVarNameSort($name)
553  {
554  $this->_varNameSort = $name;
555  return $this;
556  }
557 
564  public function setVarNameDir($name)
565  {
566  $this->_varNameDir = $name;
567  return $this;
568  }
569 
576  public function setVarNameFilter($name)
577  {
578  $this->_varNameFilter = $name;
579  return $this;
580  }
581 
588  public function setPagerVisibility($visible = true)
589  {
590  $this->_pagerVisibility = $visible;
591  return $this;
592  }
593 
600  public function getPagerVisibility()
601  {
603  }
604 
611  public function setMessageBlockVisibility($visible = true)
612  {
613  $this->_messageBlockVisibility = $visible;
614  }
615 
622  public function getMessageBlockVisibility()
623  {
625  }
626 
633  public function setDefaultLimit($limit)
634  {
635  $this->_defaultLimit = $limit;
636  return $this;
637  }
638 
645  public function setDefaultPage($page)
646  {
647  $this->_defaultPage = $page;
648  return $this;
649  }
650 
657  public function setDefaultSort($sort)
658  {
659  $this->_defaultSort = $sort;
660  return $this;
661  }
662 
669  public function setDefaultDir($dir)
670  {
671  $this->_defaultDir = $dir;
672  return $this;
673  }
674 
681  public function setDefaultFilter($filter)
682  {
683  $this->_defaultFilter = $filter;
684  return $this;
685  }
686 
692  public function canDisplayContainer()
693  {
694  if ($this->getRequest()->getQuery('ajax')) {
695  return false;
696  }
697  return true;
698  }
699 
705  public function getGridUrl()
706  {
707  return $this->hasData('grid_url') ? $this->getData('grid_url') : $this->getAbsoluteGridUrl();
708  }
709 
717  public function getAbsoluteGridUrl($params = [])
718  {
719  return $this->getCurrentUrl($params);
720  }
721 
729  public function getParam($paramName, $default = null)
730  {
731  $sessionParamName = $this->getId() . $paramName;
732  if ($this->getRequest()->has($paramName)) {
733  $param = $this->getRequest()->getParam($paramName);
734  if ($this->_saveParametersInSession) {
735  $this->_backendSession->setData($sessionParamName, $param);
736  }
737  return $param;
738  } elseif ($this->_saveParametersInSession && ($param = $this->_backendSession->getData($sessionParamName))) {
739  return $param;
740  }
741 
742  return $default;
743  }
744 
751  public function setSaveParametersInSession($flag)
752  {
753  $this->_saveParametersInSession = $flag;
754  return $this;
755  }
756 
762  public function getJsObjectName()
763  {
764  return preg_replace("~[^a-z0-9_]*~i", '', $this->getId()) . 'JsObject';
765  }
766 
773  public function setCountTotals($count = true)
774  {
775  $this->_countTotals = $count;
776  return $this;
777  }
778 
785  public function getCountTotals()
786  {
787  return $this->_countTotals;
788  }
789 
796  public function setTotals(\Magento\Framework\DataObject $totals)
797  {
798  $this->_varTotals = $totals;
799  }
800 
806  public function getTotals()
807  {
808  return $this->_varTotals;
809  }
810 
816  public function getMainButtonsHtml()
817  {
818  $html = '';
819  if ($this->getColumnSet()->isFilterVisible()) {
820  $html .= $this->getSearchButtonHtml();
821  $html .= $this->getResetFilterButtonHtml();
822  }
823  return $html;
824  }
825 
831  public function getResetFilterButtonHtml()
832  {
833  return $this->getChildHtml('reset_filter_button');
834  }
835 
841  public function getSearchButtonHtml()
842  {
843  return $this->getChildHtml('search_button');
844  }
845 }
getCurrentUrl($params=[])
Definition: Widget.php:53
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
setTotals(\Magento\Framework\DataObject $totals)
Definition: Grid.php:796
setPagerVisibility($visible=true)
Definition: Grid.php:588
$totals
Definition: totalbar.phtml:10
$page
Definition: pages.php:8
setMessageBlockVisibility($visible=true)
Definition: Grid.php:611
setData($key, $value=null)
Definition: DataObject.php:72
getParam($paramName, $default=null)
Definition: Grid.php:729
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, array $data=[])
Definition: Grid.php:142
if(!isset($_GET['name'])) $name
Definition: log.php:14