123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Block\Widget\Grid;
- /**
- * @api
- * @deprecated 100.2.0 in favour of UI component implementation
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class ColumnSet extends \Magento\Framework\View\Element\Template
- {
- /**
- * @var \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator
- */
- protected $_rowUrlGenerator;
- /**
- * Column headers visibility
- *
- * @var boolean
- */
- protected $_headersVisibility = true;
- /**
- * Filter visibility
- *
- * @var boolean
- */
- protected $_filterVisibility = true;
- /**
- * Empty grid text
- *
- * @var string|null
- */
- protected $_emptyText;
- /**
- * Empty grid text CSS class
- *
- * @var string
- */
- protected $_emptyTextCss = 'empty-text';
- /**
- * Label for empty cell
- *
- * @var string
- */
- protected $_emptyCellLabel = '';
- /**
- * Count subtotals
- *
- * @var boolean
- */
- protected $_countSubTotals = false;
- /**
- * Count totals
- *
- * @var boolean
- */
- protected $_countTotals = false;
- /**
- * Columns to group by
- *
- * @var string[]
- */
- protected $_groupedColumn = [];
- /**
- * @var boolean
- */
- protected $_isCollapsed;
- /**
- * Path to template file in theme
- *
- * @var string
- */
- protected $_template = 'Magento_Backend::widget/grid/column_set.phtml';
- /**
- * @var \Magento\Backend\Model\Widget\Grid\SubTotals
- */
- protected $_subTotals = null;
- /**
- * @var \Magento\Backend\Model\Widget\Grid\Totals
- */
- protected $_totals = null;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory $generatorFactory
- * @param \Magento\Backend\Model\Widget\Grid\SubTotals $subtotals
- * @param \Magento\Backend\Model\Widget\Grid\Totals $totals
- * @param array $data
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function __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 = []
- ) {
- $generatorClassName = \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator::class;
- if (isset($data['rowUrl'])) {
- $rowUrlParams = $data['rowUrl'];
- if (isset($rowUrlParams['generatorClass'])) {
- $generatorClassName = $rowUrlParams['generatorClass'];
- }
- $this->_rowUrlGenerator = $generatorFactory->createUrlGenerator(
- $generatorClassName,
- ['args' => $rowUrlParams]
- );
- }
- $this->setFilterVisibility(
- array_key_exists('filter_visibility', $data) ? (bool)$data['filter_visibility'] : true
- );
- parent::__construct($context, $data);
- $this->setEmptyText(isset($data['empty_text']) ? $data['empty_text'] : __('We couldn\'t find any records.'));
- $this->setEmptyCellLabel(
- isset($data['empty_cell_label']) ? $data['empty_cell_label'] : __('We couldn\'t find any records.')
- );
- $this->setCountSubTotals(isset($data['count_subtotals']) ? (bool)$data['count_subtotals'] : false);
- $this->_subTotals = $subtotals;
- $this->setCountTotals(isset($data['count_totals']) ? (bool)$data['count_totals'] : false);
- $this->_totals = $totals;
- }
- /**
- * Retrieve the list of columns
- *
- * @return array
- */
- public function getColumns()
- {
- $columns = $this->getLayout()->getChildBlocks($this->getNameInLayout());
- foreach ($columns as $key => $column) {
- if (!$column->isDisplayed()) {
- unset($columns[$key]);
- }
- }
- return $columns;
- }
- /**
- * Count columns
- *
- * @return int
- */
- public function getColumnCount()
- {
- return count($this->getColumns());
- }
- /**
- * Set sortability flag for columns
- *
- * @param bool $value
- * @return $this
- */
- public function setSortable($value)
- {
- if ($value === false) {
- foreach ($this->getColumns() as $column) {
- $column->setSortable(false);
- }
- }
- return $this;
- }
- /**
- * Set custom renderer type for columns
- *
- * @param string $type
- * @param string $className
- * @return $this
- */
- public function setRendererType($type, $className)
- {
- foreach ($this->getColumns() as $column) {
- $column->setRendererType($type, $className);
- }
- return $this;
- }
- /**
- * Set custom filter type for columns
- *
- * @param string $type
- * @param string $className
- * @return $this
- */
- public function setFilterType($type, $className)
- {
- foreach ($this->getColumns() as $column) {
- $column->setFilterType($type, $className);
- }
- return $this;
- }
- /**
- * Prepare block for rendering
- *
- * @return void
- */
- protected function _beforeToHtml()
- {
- $columns = $this->getColumns();
- foreach ($columns as $columnId => $column) {
- $column->setId($columnId);
- $column->setGrid($this->getGrid());
- if ($column->isGrouped()) {
- $this->isColumnGrouped($column->getIndex(), true);
- }
- }
- $last = array_pop($columns);
- if ($last) {
- $last->addHeaderCssClass('last');
- }
- }
- /**
- * Return row url for js event handlers
- *
- * @param \Magento\Framework\DataObject $item
- * @return string
- */
- public function getRowUrl($item)
- {
- $url = '#';
- if (null !== $this->_rowUrlGenerator) {
- $url = $this->_rowUrlGenerator->getUrl($item);
- }
- return $url;
- }
- /**
- * Get children of specified item
- *
- * @param \Magento\Framework\DataObject $item
- * @return array
- */
- public function getMultipleRows($item)
- {
- $children = $item->getChildren();
- return $children ?: [];
- }
- /**
- * Has children of specified item
- *
- * @param \Magento\Framework\DataObject $item
- * @return bool
- */
- public function hasMultipleRows($item)
- {
- return $item->hasChildren() && count($item->getChildren()) > 0;
- }
- /**
- * Retrieve columns for multiple rows
- * @return array
- */
- public function getMultipleRowColumns()
- {
- $columns = $this->getColumns();
- foreach ($this->_groupedColumn as $column) {
- unset($columns[$column]);
- }
- return $columns;
- }
- /**
- * Check whether subtotal should be rendered
- *
- * @param \Magento\Framework\DataObject $item
- * @return boolean
- */
- public function shouldRenderSubTotal($item)
- {
- return $this->getCountSubTotals() && count($this->getMultipleRows($item)) > 0;
- }
- /**
- * Check whether total should be rendered
- *
- * @return boolean
- */
- public function shouldRenderTotal()
- {
- return $this->getCountTotals() && count($this->getCollection()) > 0;
- }
- /**
- * Retrieve rowspan number
- *
- * @param \Magento\Framework\DataObject $item
- * @param \Magento\Backend\Block\Widget\Grid\Column $column
- * @return int|false
- */
- public function getRowspan($item, $column)
- {
- if ($this->isColumnGrouped($column)) {
- return count(
- $this->getMultipleRows($item)
- ) + count(
- $this->_groupedColumn
- ) - 1 + (int)$this->shouldRenderSubTotal(
- $item
- );
- }
- return false;
- }
- /**
- * Check whether given column is grouped
- *
- * @param string|object $column
- * @param string $value
- * @return bool|$this
- */
- public function isColumnGrouped($column, $value = null)
- {
- if (null === $value) {
- if (is_object($column)) {
- return in_array($column->getIndex(), $this->_groupedColumn);
- }
- return in_array($column, $this->_groupedColumn);
- }
- $this->_groupedColumn[] = $column;
- return $this;
- }
- /**
- * Check whether should render empty cell
- *
- * @param \Magento\Framework\DataObject $item
- * @param \Magento\Backend\Block\Widget\Grid\Column $column
- * @return boolean
- */
- public function shouldRenderEmptyCell($item, $column)
- {
- return $item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn);
- }
- /**
- * Retrieve colspan for empty cell
- *
- * @return int
- */
- public function getEmptyCellColspan()
- {
- return $this->getColumnCount() - count($this->_groupedColumn);
- }
- /**
- * Check whether should render cell
- *
- * @param \Magento\Framework\DataObject $item
- * @param \Magento\Backend\Block\Widget\Grid\Column $column
- * @return boolean
- */
- public function shouldRenderCell($item, $column)
- {
- if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
- return true;
- }
- if (!$item->getIsEmpty()) {
- return true;
- }
- return false;
- }
- /**
- * Set visibility of column headers
- *
- * @param boolean $visible
- * @return void
- */
- public function setHeadersVisibility($visible = true)
- {
- $this->_headersVisibility = $visible;
- }
- /**
- * Return visibility of column headers
- *
- * @return boolean
- */
- public function isHeaderVisible()
- {
- return $this->_headersVisibility;
- }
- /**
- * Set visibility of filter
- *
- * @param bool $visible
- * @return void
- */
- public function setFilterVisibility($visible = true)
- {
- $this->_filterVisibility = $visible;
- }
- /**
- * Return visibility of filter
- *
- * @return boolean
- */
- public function isFilterVisible()
- {
- return $this->_filterVisibility;
- }
- /**
- * Set empty text CSS class
- *
- * @param string $cssClass
- * @return $this
- */
- public function setEmptyTextClass($cssClass)
- {
- $this->_emptyTextCss = $cssClass;
- return $this;
- }
- /**
- * Return empty text CSS class
- *
- * @return string
- */
- public function getEmptyTextClass()
- {
- return $this->_emptyTextCss;
- }
- /**
- * Retrieve label for empty cell
- *
- * @return string
- */
- public function getEmptyCellLabel()
- {
- return $this->_emptyCellLabel;
- }
- /**
- * Set label for empty cell
- *
- * @param string $label
- * @return $this
- */
- public function setEmptyCellLabel($label)
- {
- $this->_emptyCellLabel = $label;
- return $this;
- }
- /**
- * Set flag whether is collapsed
- *
- * @param bool $isCollapsed
- * @return $this
- */
- public function setIsCollapsed($isCollapsed)
- {
- $this->_isCollapsed = $isCollapsed;
- return $this;
- }
- /**
- * Retrieve flag is collapsed
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getIsCollapsed()
- {
- return $this->_isCollapsed;
- }
- /**
- * Return grid of current column set
- *
- * @return \Magento\Backend\Block\Widget\Grid
- */
- public function getGrid()
- {
- return $this->getParentBlock();
- }
- /**
- * Return collection of current grid
- *
- * @return \Magento\Framework\Data\Collection
- */
- public function getCollection()
- {
- return $this->getGrid()->getCollection();
- }
- /**
- * Set subtotals
- *
- * @param bool $flag
- * @return $this
- */
- public function setCountSubTotals($flag = true)
- {
- $this->_countSubTotals = $flag;
- return $this;
- }
- /**
- * Return count subtotals
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getCountSubTotals()
- {
- return $this->_countSubTotals;
- }
- /**
- * Set totals
- *
- * @param bool $flag
- * @return $this
- */
- public function setCountTotals($flag = true)
- {
- $this->_countTotals = $flag;
- return $this;
- }
- /**
- * Return count totals
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getCountTotals()
- {
- return $this->_countTotals;
- }
- /**
- * Retrieve subtotal for item
- *
- * @param \Magento\Framework\DataObject $item
- * @return \Magento\Framework\DataObject
- */
- public function getSubTotals($item)
- {
- $this->_prepareSubTotals();
- $this->_subTotals->reset();
- return $this->_subTotals->countTotals($item->getChildren());
- }
- /**
- * Retrieve subtotal items
- *
- * @return \Magento\Framework\DataObject
- */
- public function getTotals()
- {
- $this->_prepareTotals();
- $this->_totals->reset();
- return $this->_totals->countTotals($this->getCollection());
- }
- /**
- * Update item with first sub-item data
- *
- * @param \Magento\Framework\DataObject $item
- * @return void
- */
- public function updateItemByFirstMultiRow(\Magento\Framework\DataObject $item)
- {
- $multiRows = $this->getMultipleRows($item);
- if (is_object($multiRows) && $multiRows instanceof \Magento\Framework\Data\Collection) {
- /** @var $multiRows \Magento\Framework\Data\Collection */
- $item->addData($multiRows->getFirstItem()->getData());
- } elseif (is_array($multiRows)) {
- $firstItem = $multiRows[0];
- $item->addData($firstItem);
- }
- }
- /**
- * Prepare sub-total object for counting sub-totals
- *
- * @return void
- */
- public function _prepareSubTotals()
- {
- $columns = $this->_subTotals->getColumns();
- if (empty($columns)) {
- foreach ($this->getMultipleRowColumns() as $column) {
- if ($column->getTotal()) {
- $this->_subTotals->setColumn($column->getIndex(), $column->getTotal());
- }
- }
- }
- }
- /**
- * Prepare total object for counting totals
- *
- * @return void
- */
- public function _prepareTotals()
- {
- $columns = $this->_totals->getColumns();
- if (empty($columns)) {
- foreach ($this->getColumns() as $column) {
- if ($column->getTotal()) {
- $this->_totals->setColumn($column->getIndex(), $column->getTotal());
- }
- }
- }
- }
- }
|