Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ColumnSet.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  protected $_rowUrlGenerator;
20 
26  protected $_headersVisibility = true;
27 
33  protected $_filterVisibility = true;
34 
40  protected $_emptyText;
41 
47  protected $_emptyTextCss = 'empty-text';
48 
54  protected $_emptyCellLabel = '';
55 
61  protected $_countSubTotals = false;
62 
68  protected $_countTotals = false;
69 
75  protected $_groupedColumn = [];
76 
80  protected $_isCollapsed;
81 
87  protected $_template = 'Magento_Backend::widget/grid/column_set.phtml';
88 
92  protected $_subTotals = null;
93 
97  protected $_totals = null;
98 
107  public function __construct(
108  \Magento\Framework\View\Element\Template\Context $context,
109  \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory $generatorFactory,
110  \Magento\Backend\Model\Widget\Grid\SubTotals $subtotals,
111  \Magento\Backend\Model\Widget\Grid\Totals $totals,
112  array $data = []
113  ) {
114  $generatorClassName = \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator::class;
115  if (isset($data['rowUrl'])) {
116  $rowUrlParams = $data['rowUrl'];
117  if (isset($rowUrlParams['generatorClass'])) {
118  $generatorClassName = $rowUrlParams['generatorClass'];
119  }
120  $this->_rowUrlGenerator = $generatorFactory->createUrlGenerator(
121  $generatorClassName,
122  ['args' => $rowUrlParams]
123  );
124  }
125 
126  $this->setFilterVisibility(
127  array_key_exists('filter_visibility', $data) ? (bool)$data['filter_visibility'] : true
128  );
129 
130  parent::__construct($context, $data);
131 
132  $this->setEmptyText(isset($data['empty_text']) ? $data['empty_text'] : __('We couldn\'t find any records.'));
133 
134  $this->setEmptyCellLabel(
135  isset($data['empty_cell_label']) ? $data['empty_cell_label'] : __('We couldn\'t find any records.')
136  );
137 
138  $this->setCountSubTotals(isset($data['count_subtotals']) ? (bool)$data['count_subtotals'] : false);
139  $this->_subTotals = $subtotals;
140 
141  $this->setCountTotals(isset($data['count_totals']) ? (bool)$data['count_totals'] : false);
142  $this->_totals = $totals;
143  }
144 
150  public function getColumns()
151  {
152  $columns = $this->getLayout()->getChildBlocks($this->getNameInLayout());
153  foreach ($columns as $key => $column) {
154  if (!$column->isDisplayed()) {
155  unset($columns[$key]);
156  }
157  }
158  return $columns;
159  }
160 
166  public function getColumnCount()
167  {
168  return count($this->getColumns());
169  }
170 
177  public function setSortable($value)
178  {
179  if ($value === false) {
180  foreach ($this->getColumns() as $column) {
181  $column->setSortable(false);
182  }
183  }
184  return $this;
185  }
186 
194  public function setRendererType($type, $className)
195  {
196  foreach ($this->getColumns() as $column) {
197  $column->setRendererType($type, $className);
198  }
199  return $this;
200  }
201 
209  public function setFilterType($type, $className)
210  {
211  foreach ($this->getColumns() as $column) {
212  $column->setFilterType($type, $className);
213  }
214  return $this;
215  }
216 
222  protected function _beforeToHtml()
223  {
224  $columns = $this->getColumns();
225  foreach ($columns as $columnId => $column) {
226  $column->setId($columnId);
227  $column->setGrid($this->getGrid());
228  if ($column->isGrouped()) {
229  $this->isColumnGrouped($column->getIndex(), true);
230  }
231  }
232  $last = array_pop($columns);
233  if ($last) {
234  $last->addHeaderCssClass('last');
235  }
236  }
237 
244  public function getRowUrl($item)
245  {
246  $url = '#';
247  if (null !== $this->_rowUrlGenerator) {
248  $url = $this->_rowUrlGenerator->getUrl($item);
249  }
250  return $url;
251  }
252 
259  public function getMultipleRows($item)
260  {
261  $children = $item->getChildren();
262  return $children ?: [];
263  }
264 
271  public function hasMultipleRows($item)
272  {
273  return $item->hasChildren() && count($item->getChildren()) > 0;
274  }
275 
280  public function getMultipleRowColumns()
281  {
282  $columns = $this->getColumns();
283  foreach ($this->_groupedColumn as $column) {
284  unset($columns[$column]);
285  }
286  return $columns;
287  }
288 
295  public function shouldRenderSubTotal($item)
296  {
297  return $this->getCountSubTotals() && count($this->getMultipleRows($item)) > 0;
298  }
299 
305  public function shouldRenderTotal()
306  {
307  return $this->getCountTotals() && count($this->getCollection()) > 0;
308  }
309 
317  public function getRowspan($item, $column)
318  {
319  if ($this->isColumnGrouped($column)) {
320  return count(
321  $this->getMultipleRows($item)
322  ) + count(
323  $this->_groupedColumn
324  ) - 1 + (int)$this->shouldRenderSubTotal(
325  $item
326  );
327  }
328  return false;
329  }
330 
338  public function isColumnGrouped($column, $value = null)
339  {
340  if (null === $value) {
341  if (is_object($column)) {
342  return in_array($column->getIndex(), $this->_groupedColumn);
343  }
344  return in_array($column, $this->_groupedColumn);
345  }
346  $this->_groupedColumn[] = $column;
347  return $this;
348  }
349 
357  public function shouldRenderEmptyCell($item, $column)
358  {
359  return $item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn);
360  }
361 
367  public function getEmptyCellColspan()
368  {
369  return $this->getColumnCount() - count($this->_groupedColumn);
370  }
371 
379  public function shouldRenderCell($item, $column)
380  {
381  if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
382  return true;
383  }
384  if (!$item->getIsEmpty()) {
385  return true;
386  }
387  return false;
388  }
389 
396  public function setHeadersVisibility($visible = true)
397  {
398  $this->_headersVisibility = $visible;
399  }
400 
406  public function isHeaderVisible()
407  {
409  }
410 
417  public function setFilterVisibility($visible = true)
418  {
419  $this->_filterVisibility = $visible;
420  }
421 
427  public function isFilterVisible()
428  {
430  }
431 
438  public function setEmptyTextClass($cssClass)
439  {
440  $this->_emptyTextCss = $cssClass;
441  return $this;
442  }
443 
449  public function getEmptyTextClass()
450  {
451  return $this->_emptyTextCss;
452  }
453 
459  public function getEmptyCellLabel()
460  {
461  return $this->_emptyCellLabel;
462  }
463 
470  public function setEmptyCellLabel($label)
471  {
472  $this->_emptyCellLabel = $label;
473  return $this;
474  }
475 
482  public function setIsCollapsed($isCollapsed)
483  {
484  $this->_isCollapsed = $isCollapsed;
485  return $this;
486  }
487 
494  public function getIsCollapsed()
495  {
496  return $this->_isCollapsed;
497  }
498 
504  public function getGrid()
505  {
506  return $this->getParentBlock();
507  }
508 
514  public function getCollection()
515  {
516  return $this->getGrid()->getCollection();
517  }
518 
525  public function setCountSubTotals($flag = true)
526  {
527  $this->_countSubTotals = $flag;
528  return $this;
529  }
530 
537  public function getCountSubTotals()
538  {
539  return $this->_countSubTotals;
540  }
541 
548  public function setCountTotals($flag = true)
549  {
550  $this->_countTotals = $flag;
551  return $this;
552  }
553 
560  public function getCountTotals()
561  {
562  return $this->_countTotals;
563  }
564 
571  public function getSubTotals($item)
572  {
573  $this->_prepareSubTotals();
574  $this->_subTotals->reset();
575  return $this->_subTotals->countTotals($item->getChildren());
576  }
577 
583  public function getTotals()
584  {
585  $this->_prepareTotals();
586  $this->_totals->reset();
587  return $this->_totals->countTotals($this->getCollection());
588  }
589 
596  public function updateItemByFirstMultiRow(\Magento\Framework\DataObject $item)
597  {
598  $multiRows = $this->getMultipleRows($item);
599  if (is_object($multiRows) && $multiRows instanceof \Magento\Framework\Data\Collection) {
601  $item->addData($multiRows->getFirstItem()->getData());
602  } elseif (is_array($multiRows)) {
603  $firstItem = $multiRows[0];
604  $item->addData($firstItem);
605  }
606  }
607 
613  public function _prepareSubTotals()
614  {
615  $columns = $this->_subTotals->getColumns();
616  if (empty($columns)) {
617  foreach ($this->getMultipleRowColumns() as $column) {
618  if ($column->getTotal()) {
619  $this->_subTotals->setColumn($column->getIndex(), $column->getTotal());
620  }
621  }
622  }
623  }
624 
630  public function _prepareTotals()
631  {
632  $columns = $this->_totals->getColumns();
633  if (empty($columns)) {
634  foreach ($this->getColumns() as $column) {
635  if ($column->getTotal()) {
636  $this->_totals->setColumn($column->getIndex(), $column->getTotal());
637  }
638  }
639  }
640  }
641 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory $generatorFactory, \Magento\Backend\Model\Widget\Grid\SubTotals $subtotals, \Magento\Backend\Model\Widget\Grid\Totals $totals, array $data=[])
Definition: ColumnSet.php:107
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$totals
Definition: totalbar.phtml:10
if(!isset($advancedLabel)) $cssClass
Definition: fieldset.phtml:25
$children
Definition: actions.phtml:11
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31