ColumnSet.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid;
  7. /**
  8. * @api
  9. * @deprecated 100.2.0 in favour of UI component implementation
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. * @since 100.0.2
  12. */
  13. class ColumnSet extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * @var \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator
  17. */
  18. protected $_rowUrlGenerator;
  19. /**
  20. * Column headers visibility
  21. *
  22. * @var boolean
  23. */
  24. protected $_headersVisibility = true;
  25. /**
  26. * Filter visibility
  27. *
  28. * @var boolean
  29. */
  30. protected $_filterVisibility = true;
  31. /**
  32. * Empty grid text
  33. *
  34. * @var string|null
  35. */
  36. protected $_emptyText;
  37. /**
  38. * Empty grid text CSS class
  39. *
  40. * @var string
  41. */
  42. protected $_emptyTextCss = 'empty-text';
  43. /**
  44. * Label for empty cell
  45. *
  46. * @var string
  47. */
  48. protected $_emptyCellLabel = '';
  49. /**
  50. * Count subtotals
  51. *
  52. * @var boolean
  53. */
  54. protected $_countSubTotals = false;
  55. /**
  56. * Count totals
  57. *
  58. * @var boolean
  59. */
  60. protected $_countTotals = false;
  61. /**
  62. * Columns to group by
  63. *
  64. * @var string[]
  65. */
  66. protected $_groupedColumn = [];
  67. /**
  68. * @var boolean
  69. */
  70. protected $_isCollapsed;
  71. /**
  72. * Path to template file in theme
  73. *
  74. * @var string
  75. */
  76. protected $_template = 'Magento_Backend::widget/grid/column_set.phtml';
  77. /**
  78. * @var \Magento\Backend\Model\Widget\Grid\SubTotals
  79. */
  80. protected $_subTotals = null;
  81. /**
  82. * @var \Magento\Backend\Model\Widget\Grid\Totals
  83. */
  84. protected $_totals = null;
  85. /**
  86. * @param \Magento\Framework\View\Element\Template\Context $context
  87. * @param \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory $generatorFactory
  88. * @param \Magento\Backend\Model\Widget\Grid\SubTotals $subtotals
  89. * @param \Magento\Backend\Model\Widget\Grid\Totals $totals
  90. * @param array $data
  91. * @SuppressWarnings(PHPMD.NPathComplexity)
  92. */
  93. public function __construct(
  94. \Magento\Framework\View\Element\Template\Context $context,
  95. \Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory $generatorFactory,
  96. \Magento\Backend\Model\Widget\Grid\SubTotals $subtotals,
  97. \Magento\Backend\Model\Widget\Grid\Totals $totals,
  98. array $data = []
  99. ) {
  100. $generatorClassName = \Magento\Backend\Model\Widget\Grid\Row\UrlGenerator::class;
  101. if (isset($data['rowUrl'])) {
  102. $rowUrlParams = $data['rowUrl'];
  103. if (isset($rowUrlParams['generatorClass'])) {
  104. $generatorClassName = $rowUrlParams['generatorClass'];
  105. }
  106. $this->_rowUrlGenerator = $generatorFactory->createUrlGenerator(
  107. $generatorClassName,
  108. ['args' => $rowUrlParams]
  109. );
  110. }
  111. $this->setFilterVisibility(
  112. array_key_exists('filter_visibility', $data) ? (bool)$data['filter_visibility'] : true
  113. );
  114. parent::__construct($context, $data);
  115. $this->setEmptyText(isset($data['empty_text']) ? $data['empty_text'] : __('We couldn\'t find any records.'));
  116. $this->setEmptyCellLabel(
  117. isset($data['empty_cell_label']) ? $data['empty_cell_label'] : __('We couldn\'t find any records.')
  118. );
  119. $this->setCountSubTotals(isset($data['count_subtotals']) ? (bool)$data['count_subtotals'] : false);
  120. $this->_subTotals = $subtotals;
  121. $this->setCountTotals(isset($data['count_totals']) ? (bool)$data['count_totals'] : false);
  122. $this->_totals = $totals;
  123. }
  124. /**
  125. * Retrieve the list of columns
  126. *
  127. * @return array
  128. */
  129. public function getColumns()
  130. {
  131. $columns = $this->getLayout()->getChildBlocks($this->getNameInLayout());
  132. foreach ($columns as $key => $column) {
  133. if (!$column->isDisplayed()) {
  134. unset($columns[$key]);
  135. }
  136. }
  137. return $columns;
  138. }
  139. /**
  140. * Count columns
  141. *
  142. * @return int
  143. */
  144. public function getColumnCount()
  145. {
  146. return count($this->getColumns());
  147. }
  148. /**
  149. * Set sortability flag for columns
  150. *
  151. * @param bool $value
  152. * @return $this
  153. */
  154. public function setSortable($value)
  155. {
  156. if ($value === false) {
  157. foreach ($this->getColumns() as $column) {
  158. $column->setSortable(false);
  159. }
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Set custom renderer type for columns
  165. *
  166. * @param string $type
  167. * @param string $className
  168. * @return $this
  169. */
  170. public function setRendererType($type, $className)
  171. {
  172. foreach ($this->getColumns() as $column) {
  173. $column->setRendererType($type, $className);
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Set custom filter type for columns
  179. *
  180. * @param string $type
  181. * @param string $className
  182. * @return $this
  183. */
  184. public function setFilterType($type, $className)
  185. {
  186. foreach ($this->getColumns() as $column) {
  187. $column->setFilterType($type, $className);
  188. }
  189. return $this;
  190. }
  191. /**
  192. * Prepare block for rendering
  193. *
  194. * @return void
  195. */
  196. protected function _beforeToHtml()
  197. {
  198. $columns = $this->getColumns();
  199. foreach ($columns as $columnId => $column) {
  200. $column->setId($columnId);
  201. $column->setGrid($this->getGrid());
  202. if ($column->isGrouped()) {
  203. $this->isColumnGrouped($column->getIndex(), true);
  204. }
  205. }
  206. $last = array_pop($columns);
  207. if ($last) {
  208. $last->addHeaderCssClass('last');
  209. }
  210. }
  211. /**
  212. * Return row url for js event handlers
  213. *
  214. * @param \Magento\Framework\DataObject $item
  215. * @return string
  216. */
  217. public function getRowUrl($item)
  218. {
  219. $url = '#';
  220. if (null !== $this->_rowUrlGenerator) {
  221. $url = $this->_rowUrlGenerator->getUrl($item);
  222. }
  223. return $url;
  224. }
  225. /**
  226. * Get children of specified item
  227. *
  228. * @param \Magento\Framework\DataObject $item
  229. * @return array
  230. */
  231. public function getMultipleRows($item)
  232. {
  233. $children = $item->getChildren();
  234. return $children ?: [];
  235. }
  236. /**
  237. * Has children of specified item
  238. *
  239. * @param \Magento\Framework\DataObject $item
  240. * @return bool
  241. */
  242. public function hasMultipleRows($item)
  243. {
  244. return $item->hasChildren() && count($item->getChildren()) > 0;
  245. }
  246. /**
  247. * Retrieve columns for multiple rows
  248. * @return array
  249. */
  250. public function getMultipleRowColumns()
  251. {
  252. $columns = $this->getColumns();
  253. foreach ($this->_groupedColumn as $column) {
  254. unset($columns[$column]);
  255. }
  256. return $columns;
  257. }
  258. /**
  259. * Check whether subtotal should be rendered
  260. *
  261. * @param \Magento\Framework\DataObject $item
  262. * @return boolean
  263. */
  264. public function shouldRenderSubTotal($item)
  265. {
  266. return $this->getCountSubTotals() && count($this->getMultipleRows($item)) > 0;
  267. }
  268. /**
  269. * Check whether total should be rendered
  270. *
  271. * @return boolean
  272. */
  273. public function shouldRenderTotal()
  274. {
  275. return $this->getCountTotals() && count($this->getCollection()) > 0;
  276. }
  277. /**
  278. * Retrieve rowspan number
  279. *
  280. * @param \Magento\Framework\DataObject $item
  281. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  282. * @return int|false
  283. */
  284. public function getRowspan($item, $column)
  285. {
  286. if ($this->isColumnGrouped($column)) {
  287. return count(
  288. $this->getMultipleRows($item)
  289. ) + count(
  290. $this->_groupedColumn
  291. ) - 1 + (int)$this->shouldRenderSubTotal(
  292. $item
  293. );
  294. }
  295. return false;
  296. }
  297. /**
  298. * Check whether given column is grouped
  299. *
  300. * @param string|object $column
  301. * @param string $value
  302. * @return bool|$this
  303. */
  304. public function isColumnGrouped($column, $value = null)
  305. {
  306. if (null === $value) {
  307. if (is_object($column)) {
  308. return in_array($column->getIndex(), $this->_groupedColumn);
  309. }
  310. return in_array($column, $this->_groupedColumn);
  311. }
  312. $this->_groupedColumn[] = $column;
  313. return $this;
  314. }
  315. /**
  316. * Check whether should render empty cell
  317. *
  318. * @param \Magento\Framework\DataObject $item
  319. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  320. * @return boolean
  321. */
  322. public function shouldRenderEmptyCell($item, $column)
  323. {
  324. return $item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn);
  325. }
  326. /**
  327. * Retrieve colspan for empty cell
  328. *
  329. * @return int
  330. */
  331. public function getEmptyCellColspan()
  332. {
  333. return $this->getColumnCount() - count($this->_groupedColumn);
  334. }
  335. /**
  336. * Check whether should render cell
  337. *
  338. * @param \Magento\Framework\DataObject $item
  339. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  340. * @return boolean
  341. */
  342. public function shouldRenderCell($item, $column)
  343. {
  344. if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
  345. return true;
  346. }
  347. if (!$item->getIsEmpty()) {
  348. return true;
  349. }
  350. return false;
  351. }
  352. /**
  353. * Set visibility of column headers
  354. *
  355. * @param boolean $visible
  356. * @return void
  357. */
  358. public function setHeadersVisibility($visible = true)
  359. {
  360. $this->_headersVisibility = $visible;
  361. }
  362. /**
  363. * Return visibility of column headers
  364. *
  365. * @return boolean
  366. */
  367. public function isHeaderVisible()
  368. {
  369. return $this->_headersVisibility;
  370. }
  371. /**
  372. * Set visibility of filter
  373. *
  374. * @param bool $visible
  375. * @return void
  376. */
  377. public function setFilterVisibility($visible = true)
  378. {
  379. $this->_filterVisibility = $visible;
  380. }
  381. /**
  382. * Return visibility of filter
  383. *
  384. * @return boolean
  385. */
  386. public function isFilterVisible()
  387. {
  388. return $this->_filterVisibility;
  389. }
  390. /**
  391. * Set empty text CSS class
  392. *
  393. * @param string $cssClass
  394. * @return $this
  395. */
  396. public function setEmptyTextClass($cssClass)
  397. {
  398. $this->_emptyTextCss = $cssClass;
  399. return $this;
  400. }
  401. /**
  402. * Return empty text CSS class
  403. *
  404. * @return string
  405. */
  406. public function getEmptyTextClass()
  407. {
  408. return $this->_emptyTextCss;
  409. }
  410. /**
  411. * Retrieve label for empty cell
  412. *
  413. * @return string
  414. */
  415. public function getEmptyCellLabel()
  416. {
  417. return $this->_emptyCellLabel;
  418. }
  419. /**
  420. * Set label for empty cell
  421. *
  422. * @param string $label
  423. * @return $this
  424. */
  425. public function setEmptyCellLabel($label)
  426. {
  427. $this->_emptyCellLabel = $label;
  428. return $this;
  429. }
  430. /**
  431. * Set flag whether is collapsed
  432. *
  433. * @param bool $isCollapsed
  434. * @return $this
  435. */
  436. public function setIsCollapsed($isCollapsed)
  437. {
  438. $this->_isCollapsed = $isCollapsed;
  439. return $this;
  440. }
  441. /**
  442. * Retrieve flag is collapsed
  443. *
  444. * @return bool
  445. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  446. */
  447. public function getIsCollapsed()
  448. {
  449. return $this->_isCollapsed;
  450. }
  451. /**
  452. * Return grid of current column set
  453. *
  454. * @return \Magento\Backend\Block\Widget\Grid
  455. */
  456. public function getGrid()
  457. {
  458. return $this->getParentBlock();
  459. }
  460. /**
  461. * Return collection of current grid
  462. *
  463. * @return \Magento\Framework\Data\Collection
  464. */
  465. public function getCollection()
  466. {
  467. return $this->getGrid()->getCollection();
  468. }
  469. /**
  470. * Set subtotals
  471. *
  472. * @param bool $flag
  473. * @return $this
  474. */
  475. public function setCountSubTotals($flag = true)
  476. {
  477. $this->_countSubTotals = $flag;
  478. return $this;
  479. }
  480. /**
  481. * Return count subtotals
  482. *
  483. * @return bool
  484. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  485. */
  486. public function getCountSubTotals()
  487. {
  488. return $this->_countSubTotals;
  489. }
  490. /**
  491. * Set totals
  492. *
  493. * @param bool $flag
  494. * @return $this
  495. */
  496. public function setCountTotals($flag = true)
  497. {
  498. $this->_countTotals = $flag;
  499. return $this;
  500. }
  501. /**
  502. * Return count totals
  503. *
  504. * @return bool
  505. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  506. */
  507. public function getCountTotals()
  508. {
  509. return $this->_countTotals;
  510. }
  511. /**
  512. * Retrieve subtotal for item
  513. *
  514. * @param \Magento\Framework\DataObject $item
  515. * @return \Magento\Framework\DataObject
  516. */
  517. public function getSubTotals($item)
  518. {
  519. $this->_prepareSubTotals();
  520. $this->_subTotals->reset();
  521. return $this->_subTotals->countTotals($item->getChildren());
  522. }
  523. /**
  524. * Retrieve subtotal items
  525. *
  526. * @return \Magento\Framework\DataObject
  527. */
  528. public function getTotals()
  529. {
  530. $this->_prepareTotals();
  531. $this->_totals->reset();
  532. return $this->_totals->countTotals($this->getCollection());
  533. }
  534. /**
  535. * Update item with first sub-item data
  536. *
  537. * @param \Magento\Framework\DataObject $item
  538. * @return void
  539. */
  540. public function updateItemByFirstMultiRow(\Magento\Framework\DataObject $item)
  541. {
  542. $multiRows = $this->getMultipleRows($item);
  543. if (is_object($multiRows) && $multiRows instanceof \Magento\Framework\Data\Collection) {
  544. /** @var $multiRows \Magento\Framework\Data\Collection */
  545. $item->addData($multiRows->getFirstItem()->getData());
  546. } elseif (is_array($multiRows)) {
  547. $firstItem = $multiRows[0];
  548. $item->addData($firstItem);
  549. }
  550. }
  551. /**
  552. * Prepare sub-total object for counting sub-totals
  553. *
  554. * @return void
  555. */
  556. public function _prepareSubTotals()
  557. {
  558. $columns = $this->_subTotals->getColumns();
  559. if (empty($columns)) {
  560. foreach ($this->getMultipleRowColumns() as $column) {
  561. if ($column->getTotal()) {
  562. $this->_subTotals->setColumn($column->getIndex(), $column->getTotal());
  563. }
  564. }
  565. }
  566. }
  567. /**
  568. * Prepare total object for counting totals
  569. *
  570. * @return void
  571. */
  572. public function _prepareTotals()
  573. {
  574. $columns = $this->_totals->getColumns();
  575. if (empty($columns)) {
  576. foreach ($this->getColumns() as $column) {
  577. if ($column->getTotal()) {
  578. $this->_totals->setColumn($column->getIndex(), $column->getTotal());
  579. }
  580. }
  581. }
  582. }
  583. }