Extended.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * @api
  10. * @deprecated 100.2.0 in favour of UI component implementation
  11. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  12. * @SuppressWarnings(PHPMD.TooManyFields)
  13. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  14. * @SuppressWarnings(PHPMD.NumberOfChildren)
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @since 100.0.2
  17. */
  18. class Extended extends \Magento\Backend\Block\Widget\Grid implements \Magento\Backend\Block\Widget\Grid\ExportInterface
  19. {
  20. /**
  21. * Columns array
  22. *
  23. * array(
  24. * 'header' => string,
  25. * 'width' => int,
  26. * 'sortable' => bool,
  27. * 'index' => string,
  28. * //'renderer' => \Magento\Backend\Block\Widget\Grid\Column\Renderer\Interface,
  29. * 'format' => string
  30. * 'total' => string (sum, avg)
  31. * )
  32. * @var array
  33. */
  34. protected $_columns = [];
  35. /**
  36. * Collection object
  37. *
  38. * @var \Magento\Framework\Data\Collection
  39. */
  40. protected $_collection;
  41. /**
  42. * Export flag
  43. *
  44. * @var bool
  45. */
  46. protected $_isExport = false;
  47. /**
  48. * Grid export types
  49. *
  50. * @var \Magento\Framework\DataObject[]
  51. */
  52. protected $_exportTypes = [];
  53. /**
  54. * Rows per page for import
  55. *
  56. * @var int
  57. */
  58. protected $_exportPageSize = 1000;
  59. /**
  60. * Identifier of last grid column
  61. *
  62. * @var string
  63. */
  64. protected $_lastColumnId;
  65. /**
  66. * Massaction row id field
  67. *
  68. * @var string
  69. */
  70. protected $_massactionIdField;
  71. /**
  72. * Massaction row id filter
  73. *
  74. * @var string
  75. */
  76. protected $_massactionIdFilter;
  77. /**
  78. * Massaction block name
  79. *
  80. * @var string
  81. */
  82. protected $_massactionBlockName = \Magento\Backend\Block\Widget\Grid\Massaction\Extended::class;
  83. /**
  84. * Columns view order
  85. *
  86. * @var array
  87. */
  88. protected $_columnsOrder = [];
  89. /**
  90. * Label for empty cell
  91. *
  92. * @var string
  93. */
  94. protected $_emptyCellLabel = '';
  95. /**
  96. * Columns to group by
  97. *
  98. * @var string[]
  99. */
  100. protected $_groupedColumn = [];
  101. /**
  102. * Column headers visibility
  103. *
  104. * @var boolean
  105. */
  106. protected $_headersVisibility = true;
  107. /**
  108. * Filter visibility
  109. *
  110. * @var boolean
  111. */
  112. protected $_filterVisibility = true;
  113. /**
  114. * Empty grid text
  115. *
  116. * @var string|null
  117. */
  118. protected $_emptyText;
  119. /**
  120. * Empty grid text CSS class
  121. *
  122. * @var string|null
  123. */
  124. protected $_emptyTextCss = 'empty-text';
  125. /**
  126. * @var bool
  127. */
  128. protected $_isCollapsed;
  129. /**
  130. * Count subtotals
  131. *
  132. * @var boolean
  133. */
  134. protected $_countSubTotals = false;
  135. /**
  136. * SubTotals
  137. *
  138. * @var \Magento\Framework\DataObject[]
  139. */
  140. protected $_subtotals = [];
  141. /**
  142. * @var string
  143. */
  144. protected $_template = 'Magento_Backend::widget/grid/extended.phtml';
  145. /**
  146. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  147. */
  148. protected $_directory;
  149. /**
  150. * Additional path to folder
  151. *
  152. * @var string
  153. */
  154. protected $_path = 'export';
  155. /**
  156. * @return void
  157. */
  158. protected function _construct()
  159. {
  160. parent::_construct();
  161. $this->_emptyText = __('We couldn\'t find any records.');
  162. $this->_directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  163. }
  164. /**
  165. * Initialize child blocks
  166. *
  167. * @return $this
  168. */
  169. protected function _prepareLayout()
  170. {
  171. $this->setChild(
  172. 'export_button',
  173. $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Button::class)->setData(
  174. [
  175. 'label' => __('Export'),
  176. 'onclick' => $this->getJsObjectName() . '.doExport()',
  177. 'class' => 'task',
  178. ]
  179. )
  180. );
  181. $this->setChild(
  182. 'reset_filter_button',
  183. $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Button::class)->setData(
  184. [
  185. 'label' => __('Reset Filter'),
  186. 'onclick' => $this->getJsObjectName() . '.resetFilter()',
  187. 'class' => 'action-reset action-tertiary'
  188. ]
  189. )->setDataAttribute(
  190. [
  191. 'action' => 'grid-filter-reset'
  192. ]
  193. )
  194. );
  195. $this->setChild(
  196. 'search_button',
  197. $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Button::class)->setData(
  198. [
  199. 'label' => __('Search'),
  200. 'onclick' => $this->getJsObjectName() . '.doFilter()',
  201. 'class' => 'task action-secondary',
  202. ]
  203. )->setDataAttribute(
  204. [
  205. 'action' => 'grid-filter-apply'
  206. ]
  207. )
  208. );
  209. return parent::_prepareLayout();
  210. }
  211. /**
  212. * Retrieve column set block
  213. *
  214. * @return \Magento\Framework\View\Element\AbstractBlock
  215. */
  216. public function getColumnSet()
  217. {
  218. if (!$this->getChildBlock('grid.columnSet')) {
  219. $this->setChild(
  220. 'grid.columnSet',
  221. $this->getLayout()->createBlock(\Magento\Backend\Block\Widget\Grid\ColumnSet::class)
  222. );
  223. }
  224. return parent::getColumnSet();
  225. }
  226. /**
  227. * Generate export button
  228. *
  229. * @return string
  230. */
  231. public function getExportButtonHtml()
  232. {
  233. return $this->getChildHtml('export_button');
  234. }
  235. /**
  236. * Add new export type to grid
  237. *
  238. * @param string $url
  239. * @param string $label
  240. * @return $this
  241. */
  242. public function addExportType($url, $label)
  243. {
  244. $this->_exportTypes[] = new \Magento\Framework\DataObject(
  245. ['url' => $this->getUrl($url, ['_current' => true]), 'label' => $label]
  246. );
  247. return $this;
  248. }
  249. /**
  250. * Add column to grid
  251. *
  252. * @param string $columnId
  253. * @param array|\Magento\Framework\DataObject $column
  254. * @return $this
  255. * @throws \Exception
  256. */
  257. public function addColumn($columnId, $column)
  258. {
  259. if (is_array($column)) {
  260. $this->getColumnSet()->setChild(
  261. $columnId,
  262. $this->getLayout()
  263. ->createBlock(\Magento\Backend\Block\Widget\Grid\Column\Extended::class)
  264. ->setData($column)
  265. ->setId($columnId)
  266. ->setGrid($this)
  267. );
  268. $this->getColumnSet()->getChildBlock($columnId)->setGrid($this);
  269. } else {
  270. throw new \Exception(__('Please correct the column format and try again.'));
  271. }
  272. $this->_lastColumnId = $columnId;
  273. return $this;
  274. }
  275. /**
  276. * Remove existing column
  277. *
  278. * @param string $columnId
  279. * @return $this
  280. */
  281. public function removeColumn($columnId)
  282. {
  283. if ($this->getColumnSet()->getChildBlock($columnId)) {
  284. $this->getColumnSet()->unsetChild($columnId);
  285. if ($this->_lastColumnId == $columnId) {
  286. $names = $this->getColumnSet()->getChildNames();
  287. $this->_lastColumnId = array_pop($names);
  288. }
  289. }
  290. return $this;
  291. }
  292. /**
  293. * Add column to grid after specified column.
  294. *
  295. * @param string $columnId
  296. * @param array|\Magento\Framework\DataObject $column
  297. * @param string $after
  298. * @return $this
  299. */
  300. public function addColumnAfter($columnId, $column, $after)
  301. {
  302. $this->addColumn($columnId, $column);
  303. $this->addColumnsOrder($columnId, $after);
  304. return $this;
  305. }
  306. /**
  307. * Add column view order
  308. *
  309. * @param string $columnId
  310. * @param string $after
  311. * @return $this
  312. */
  313. public function addColumnsOrder($columnId, $after)
  314. {
  315. $this->_columnsOrder[$columnId] = $after;
  316. return $this;
  317. }
  318. /**
  319. * Retrieve columns order
  320. *
  321. * @return array
  322. */
  323. public function getColumnsOrder()
  324. {
  325. return $this->_columnsOrder;
  326. }
  327. /**
  328. * Sort columns by predefined order
  329. *
  330. * @return $this
  331. */
  332. public function sortColumnsByOrder()
  333. {
  334. foreach ($this->getColumnsOrder() as $columnId => $after) {
  335. $this->getLayout()->reorderChild(
  336. $this->getColumnSet()->getNameInLayout(),
  337. $this->getColumn($columnId)->getNameInLayout(),
  338. $this->getColumn($after)->getNameInLayout()
  339. );
  340. }
  341. $columns = $this->getColumnSet()->getChildNames();
  342. $this->_lastColumnId = array_pop($columns);
  343. return $this;
  344. }
  345. /**
  346. * Retrieve identifier of last column
  347. *
  348. * @return string
  349. */
  350. public function getLastColumnId()
  351. {
  352. return $this->_lastColumnId;
  353. }
  354. /**
  355. * Initialize grid columns
  356. *
  357. * @return $this
  358. */
  359. protected function _prepareColumns()
  360. {
  361. $this->sortColumnsByOrder();
  362. return $this;
  363. }
  364. /**
  365. * Prepare grid massaction block
  366. *
  367. * @return $this
  368. */
  369. protected function _prepareMassactionBlock()
  370. {
  371. $this->setChild('massaction', $this->getLayout()->createBlock($this->getMassactionBlockName()));
  372. $this->_prepareMassaction();
  373. if ($this->getMassactionBlock()->isAvailable()) {
  374. $this->_prepareMassactionColumn();
  375. }
  376. return $this;
  377. }
  378. /**
  379. * Prepare grid massaction actions
  380. *
  381. * @return $this
  382. */
  383. protected function _prepareMassaction()
  384. {
  385. return $this;
  386. }
  387. /**
  388. * Prepare grid massaction column
  389. *
  390. * @return $this
  391. */
  392. protected function _prepareMassactionColumn()
  393. {
  394. $columnId = 'massaction';
  395. $massactionColumn = $this->getLayout()
  396. ->createBlock(\Magento\Backend\Block\Widget\Grid\Column::class)
  397. ->setData(
  398. [
  399. 'index' => $this->getMassactionIdField(),
  400. 'filter_index' => $this->getMassactionIdFilter(),
  401. 'type' => 'massaction',
  402. 'name' => $this->getMassactionBlock()->getFormFieldName(),
  403. 'is_system' => true,
  404. 'header_css_class' => 'col-select',
  405. 'column_css_class' => 'col-select',
  406. ]
  407. );
  408. if ($this->getNoFilterMassactionColumn()) {
  409. $massactionColumn->setData('filter', false);
  410. }
  411. $massactionColumn->setSelected($this->getMassactionBlock()->getSelected())->setGrid($this)->setId($columnId);
  412. $this->getColumnSet()->insert(
  413. $massactionColumn,
  414. count($this->getColumnSet()->getColumns()) + 1,
  415. false,
  416. $columnId
  417. );
  418. return $this;
  419. }
  420. /**
  421. * Apply sorting and filtering to collection
  422. *
  423. * @return $this
  424. */
  425. protected function _prepareCollection()
  426. {
  427. if ($this->getCollection()) {
  428. if ($this->getCollection()->isLoaded()) {
  429. $this->getCollection()->clear();
  430. }
  431. parent::_prepareCollection();
  432. if (!$this->_isExport) {
  433. $this->getCollection()->load();
  434. $this->_afterLoadCollection();
  435. }
  436. }
  437. return $this;
  438. }
  439. /**
  440. * Process collection after loading
  441. *
  442. * @return $this
  443. */
  444. protected function _afterLoadCollection()
  445. {
  446. return $this;
  447. }
  448. /**
  449. * Initialize grid before rendering
  450. *
  451. * @return $this
  452. */
  453. protected function _prepareGrid()
  454. {
  455. $this->_prepareColumns();
  456. $this->_prepareMassactionBlock();
  457. parent::_prepareGrid();
  458. return $this;
  459. }
  460. /**
  461. * Retrieve grid HTML
  462. *
  463. * @return string
  464. */
  465. public function getHtml()
  466. {
  467. return $this->toHtml();
  468. }
  469. /**
  470. * Retrieve massaction row identifier field
  471. *
  472. * @return string
  473. */
  474. public function getMassactionIdField()
  475. {
  476. return $this->_massactionIdField;
  477. }
  478. /**
  479. * Set massaction row identifier field
  480. *
  481. * @param string $idField
  482. * @return $this
  483. */
  484. public function setMassactionIdField($idField)
  485. {
  486. $this->_massactionIdField = $idField;
  487. return $this;
  488. }
  489. /**
  490. * Retrieve massaction row identifier filter
  491. *
  492. * @return string
  493. */
  494. public function getMassactionIdFilter()
  495. {
  496. return $this->_massactionIdFilter;
  497. }
  498. /**
  499. * Set massaction row identifier filter
  500. *
  501. * @param string $idFilter
  502. * @return $this
  503. */
  504. public function setMassactionIdFilter($idFilter)
  505. {
  506. $this->_massactionIdFilter = $idFilter;
  507. return $this;
  508. }
  509. /**
  510. * Retrieve massaction block name
  511. *
  512. * @return string
  513. */
  514. public function getMassactionBlockName()
  515. {
  516. return $this->_massactionBlockName;
  517. }
  518. /**
  519. * Set massaction block name
  520. *
  521. * @param string $blockName
  522. * @return $this
  523. */
  524. public function setMassactionBlockName($blockName)
  525. {
  526. $this->_massactionBlockName = $blockName;
  527. return $this;
  528. }
  529. /**
  530. * Retrieve massaction block
  531. *
  532. * @return $this
  533. */
  534. public function getMassactionBlock()
  535. {
  536. return $this->getChildBlock('massaction');
  537. }
  538. /**
  539. * Generate massaction block
  540. *
  541. * @return string
  542. */
  543. public function getMassactionBlockHtml()
  544. {
  545. return $this->getChildHtml('massaction');
  546. }
  547. /**
  548. * Retrieve columns to render
  549. *
  550. * @return array
  551. */
  552. public function getSubTotalColumns()
  553. {
  554. return $this->getColumns();
  555. }
  556. /**
  557. * Check whether should render cell
  558. *
  559. * @param \Magento\Framework\DataObject $item
  560. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  561. * @return boolean
  562. */
  563. public function shouldRenderCell($item, $column)
  564. {
  565. if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
  566. return true;
  567. }
  568. if (!$item->getIsEmpty()) {
  569. return true;
  570. }
  571. return false;
  572. }
  573. /**
  574. * Retrieve label for empty cell
  575. *
  576. * @return string
  577. */
  578. public function getEmptyCellLabel()
  579. {
  580. return $this->_emptyCellLabel;
  581. }
  582. /**
  583. * Set label for empty cell
  584. *
  585. * @param string $label
  586. * @return $this
  587. */
  588. public function setEmptyCellLabel($label)
  589. {
  590. $this->_emptyCellLabel = $label;
  591. return $this;
  592. }
  593. /**
  594. * Return row url for js event handlers
  595. *
  596. * @param \Magento\Catalog\Model\Product|\Magento\Framework\DataObject $item
  597. * @return string
  598. */
  599. public function getRowUrl($item)
  600. {
  601. $res = parent::getRowUrl($item);
  602. return $res ? $res : '#';
  603. }
  604. /**
  605. * Get children of specified item
  606. *
  607. * @param \Magento\Framework\DataObject $item
  608. * @return array
  609. */
  610. public function getMultipleRows($item)
  611. {
  612. return $item->getChildren();
  613. }
  614. /**
  615. * Retrieve columns for multiple rows
  616. * @return array
  617. */
  618. public function getMultipleRowColumns()
  619. {
  620. $columns = $this->getColumns();
  621. foreach ($this->_groupedColumn as $column) {
  622. unset($columns[$column]);
  623. }
  624. return $columns;
  625. }
  626. /**
  627. * Check whether subtotal should be rendered
  628. *
  629. * @param \Magento\Framework\DataObject $item
  630. * @return boolean
  631. */
  632. public function shouldRenderSubTotal($item)
  633. {
  634. return $this->_countSubTotals && count($this->_subtotals) > 0 && count($this->getMultipleRows($item)) > 0;
  635. }
  636. /**
  637. * Retrieve rowspan number
  638. *
  639. * @param \Magento\Framework\DataObject $item
  640. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  641. * @return int|false
  642. */
  643. public function getRowspan($item, $column)
  644. {
  645. if ($this->isColumnGrouped($column)) {
  646. return count($this->getMultipleRows($item)) + count($this->_groupedColumn);
  647. }
  648. return false;
  649. }
  650. /**
  651. * Check whether given column is grouped
  652. *
  653. * @param string|object $column
  654. * @param string $value
  655. * @return boolean|$this
  656. */
  657. public function isColumnGrouped($column, $value = null)
  658. {
  659. if (null === $value) {
  660. if (is_object($column)) {
  661. return in_array($column->getIndex(), $this->_groupedColumn);
  662. }
  663. return in_array($column, $this->_groupedColumn);
  664. }
  665. $this->_groupedColumn[] = $column;
  666. return $this;
  667. }
  668. /**
  669. * Check whether should render empty cell
  670. *
  671. * @param \Magento\Framework\DataObject $item
  672. * @param \Magento\Backend\Block\Widget\Grid\Column $column
  673. * @return boolean
  674. */
  675. public function shouldRenderEmptyCell($item, $column)
  676. {
  677. return $item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn);
  678. }
  679. /**
  680. * Retrieve colspan for empty cell
  681. *
  682. * @return int
  683. */
  684. public function getEmptyCellColspan()
  685. {
  686. return $this->getColumnCount() - count($this->_groupedColumn);
  687. }
  688. /**
  689. * Retrieve subtotal item
  690. *
  691. * @param \Magento\Framework\DataObject $item
  692. * @return \Magento\Framework\DataObject|string
  693. */
  694. public function getSubTotalItem($item)
  695. {
  696. foreach ($this->_subtotals as $subtotalItem) {
  697. foreach ($this->_groupedColumn as $groupedColumn) {
  698. if ($subtotalItem->getData($groupedColumn) == $item->getData($groupedColumn)) {
  699. return $subtotalItem;
  700. }
  701. }
  702. }
  703. return '';
  704. }
  705. /**
  706. * Count columns
  707. *
  708. * @return int
  709. */
  710. public function getColumnCount()
  711. {
  712. return count($this->getColumns());
  713. }
  714. /**
  715. * Set visibility of column headers
  716. *
  717. * @param bool $visible
  718. * @return void
  719. */
  720. public function setHeadersVisibility($visible = true)
  721. {
  722. $this->_headersVisibility = $visible;
  723. }
  724. /**
  725. * Return visibility of column headers
  726. *
  727. * @return boolean
  728. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  729. */
  730. public function getHeadersVisibility()
  731. {
  732. return $this->_headersVisibility;
  733. }
  734. /**
  735. * Set visibility of filter
  736. *
  737. * @param bool $visible
  738. * @return void
  739. */
  740. public function setFilterVisibility($visible = true)
  741. {
  742. $this->_filterVisibility = $visible;
  743. }
  744. /**
  745. * Return visibility of filter
  746. *
  747. * @return boolean
  748. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  749. */
  750. public function getFilterVisibility()
  751. {
  752. return $this->_filterVisibility;
  753. }
  754. /**
  755. * Set empty text for grid
  756. *
  757. * @param string $text
  758. * @return $this
  759. */
  760. public function setEmptyText($text)
  761. {
  762. $this->_emptyText = $text;
  763. return $this;
  764. }
  765. /**
  766. * Return empty text for grid
  767. *
  768. * @return string
  769. */
  770. public function getEmptyText()
  771. {
  772. return $this->_emptyText;
  773. }
  774. /**
  775. * Set empty text CSS class
  776. *
  777. * @param string $cssClass
  778. * @return $this
  779. */
  780. public function setEmptyTextClass($cssClass)
  781. {
  782. $this->_emptyTextCss = $cssClass;
  783. return $this;
  784. }
  785. /**
  786. * Return empty text CSS class
  787. *
  788. * @return string
  789. */
  790. public function getEmptyTextClass()
  791. {
  792. return $this->_emptyTextCss;
  793. }
  794. /**
  795. * Set flag whether is collapsed
  796. *
  797. * @param bool $isCollapsed
  798. * @return $this
  799. */
  800. public function setIsCollapsed($isCollapsed)
  801. {
  802. $this->_isCollapsed = $isCollapsed;
  803. return $this;
  804. }
  805. /**
  806. * Retrieve flag is collapsed
  807. *
  808. * @return bool
  809. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  810. */
  811. public function getIsCollapsed()
  812. {
  813. return $this->_isCollapsed;
  814. }
  815. /**
  816. * Retrieve file content from file container array
  817. *
  818. * @param array $fileData
  819. * @return string
  820. */
  821. protected function _getFileContainerContent(array $fileData)
  822. {
  823. return $this->_directory->readFile('export/' . $fileData['value']);
  824. }
  825. /**
  826. * Retrieve Headers row array for Export
  827. *
  828. * @return string[]
  829. */
  830. protected function _getExportHeaders()
  831. {
  832. $row = [];
  833. foreach ($this->getColumns() as $column) {
  834. if (!$column->getIsSystem()) {
  835. $row[] = $column->getExportHeader();
  836. }
  837. }
  838. return $row;
  839. }
  840. /**
  841. * Retrieve Totals row array for Export
  842. *
  843. * @return string[]
  844. */
  845. protected function _getExportTotals()
  846. {
  847. $totals = $this->getTotals();
  848. $row = [];
  849. foreach ($this->getColumns() as $column) {
  850. if (!$column->getIsSystem()) {
  851. $row[] = $column->hasTotalsLabel() ? $column->getTotalsLabel() : $column->getRowFieldExport($totals);
  852. }
  853. }
  854. return $row;
  855. }
  856. /**
  857. * Iterate collection and call callback method per item
  858. * For callback method first argument always is item object
  859. *
  860. * @param string $callback
  861. * @param array $args additional arguments for callback method
  862. * @return void
  863. */
  864. public function _exportIterateCollection($callback, array $args)
  865. {
  866. $originalCollection = $this->getCollection();
  867. $count = null;
  868. $page = 1;
  869. $lPage = null;
  870. $break = false;
  871. while ($break !== true) {
  872. $collection = clone $originalCollection;
  873. $collection->setPageSize($this->_exportPageSize);
  874. $collection->setCurPage($page);
  875. $collection->load();
  876. if ($count === null) {
  877. $count = $collection->getSize();
  878. $lPage = $collection->getLastPageNumber();
  879. }
  880. if ($lPage == $page) {
  881. $break = true;
  882. }
  883. $page++;
  884. foreach ($collection as $item) {
  885. call_user_func_array([$this, $callback], array_merge([$item], $args));
  886. }
  887. }
  888. }
  889. /**
  890. * Write item data to csv export file
  891. *
  892. * @param \Magento\Framework\DataObject $item
  893. * @param \Magento\Framework\Filesystem\File\WriteInterface $stream
  894. * @return void
  895. */
  896. protected function _exportCsvItem(
  897. \Magento\Framework\DataObject $item,
  898. \Magento\Framework\Filesystem\File\WriteInterface $stream
  899. ) {
  900. $row = [];
  901. foreach ($this->getColumns() as $column) {
  902. if (!$column->getIsSystem()) {
  903. $row[] = $column->getRowFieldExport($item);
  904. }
  905. }
  906. $stream->writeCsv($row);
  907. }
  908. /**
  909. * Retrieve a file container array by grid data as CSV
  910. *
  911. * Return array with keys type and value
  912. *
  913. * @return array
  914. */
  915. public function getCsvFile()
  916. {
  917. $this->_isExport = true;
  918. $this->_prepareGrid();
  919. $name = md5(microtime());
  920. $file = $this->_path . '/' . $name . '.csv';
  921. $this->_directory->create($this->_path);
  922. $stream = $this->_directory->openFile($file, 'w+');
  923. $stream->lock();
  924. $stream->writeCsv($this->_getExportHeaders());
  925. $this->_exportIterateCollection('_exportCsvItem', [$stream]);
  926. if ($this->getCountTotals()) {
  927. $stream->writeCsv($this->_getExportTotals());
  928. }
  929. $stream->unlock();
  930. $stream->close();
  931. return [
  932. 'type' => 'filename',
  933. 'value' => $file,
  934. 'rm' => true // can delete file after use
  935. ];
  936. }
  937. /**
  938. * Retrieve Grid data as CSV
  939. *
  940. * @return string
  941. */
  942. public function getCsv()
  943. {
  944. $csv = '';
  945. $this->_isExport = true;
  946. $this->_prepareGrid();
  947. $this->getCollection()->getSelect()->limit();
  948. $this->getCollection()->setPageSize(0);
  949. $this->getCollection()->load();
  950. $this->_afterLoadCollection();
  951. $data = [];
  952. foreach ($this->getColumns() as $column) {
  953. if (!$column->getIsSystem()) {
  954. $data[] = '"' . $column->getExportHeader() . '"';
  955. }
  956. }
  957. $csv .= implode(',', $data) . "\n";
  958. foreach ($this->getCollection() as $item) {
  959. $data = [];
  960. foreach ($this->getColumns() as $column) {
  961. if (!$column->getIsSystem()) {
  962. $data[] = '"' . str_replace(
  963. ['"', '\\'],
  964. ['""', '\\\\'],
  965. $column->getRowFieldExport($item)
  966. ) . '"';
  967. }
  968. }
  969. $csv .= implode(',', $data) . "\n";
  970. }
  971. if ($this->getCountTotals()) {
  972. $data = [];
  973. foreach ($this->getColumns() as $column) {
  974. if (!$column->getIsSystem()) {
  975. $data[] = '"' . str_replace(
  976. ['"', '\\'],
  977. ['""', '\\\\'],
  978. $column->getRowFieldExport($this->getTotals())
  979. ) . '"';
  980. }
  981. }
  982. $csv .= implode(',', $data) . "\n";
  983. }
  984. return $csv;
  985. }
  986. /**
  987. * Retrieve data in xml
  988. *
  989. * @return string
  990. */
  991. public function getXml()
  992. {
  993. $this->_isExport = true;
  994. $this->_prepareGrid();
  995. $this->getCollection()->getSelect()->limit();
  996. $this->getCollection()->setPageSize(0);
  997. $this->getCollection()->load();
  998. $this->_afterLoadCollection();
  999. $indexes = [];
  1000. foreach ($this->getColumns() as $column) {
  1001. if (!$column->getIsSystem()) {
  1002. $indexes[] = $column->getIndex();
  1003. }
  1004. }
  1005. $xml = '<?xml version="1.0" encoding="UTF-8"?>';
  1006. $xml .= '<items>';
  1007. foreach ($this->getCollection() as $item) {
  1008. $xml .= $item->toXml($indexes);
  1009. }
  1010. if ($this->getCountTotals()) {
  1011. $xml .= $this->getTotals()->toXml($indexes);
  1012. }
  1013. $xml .= '</items>';
  1014. return $xml;
  1015. }
  1016. /**
  1017. * Get a row data of the particular columns
  1018. *
  1019. * @param \Magento\Framework\DataObject $data
  1020. * @return string[]
  1021. */
  1022. public function getRowRecord(\Magento\Framework\DataObject $data)
  1023. {
  1024. $row = [];
  1025. foreach ($this->getColumns() as $column) {
  1026. if (!$column->getIsSystem()) {
  1027. $row[] = $column->getRowFieldExport($data);
  1028. }
  1029. }
  1030. return $row;
  1031. }
  1032. /**
  1033. * Retrieve a file container array by grid data as MS Excel 2003 XML Document
  1034. *
  1035. * Return array with keys type and value
  1036. *
  1037. * @param string $sheetName
  1038. * @return array
  1039. */
  1040. public function getExcelFile($sheetName = '')
  1041. {
  1042. $this->_isExport = true;
  1043. $this->_prepareGrid();
  1044. $convert = new \Magento\Framework\Convert\Excel(
  1045. $this->getCollection()->getIterator(),
  1046. [$this, 'getRowRecord']
  1047. );
  1048. $name = md5(microtime());
  1049. $file = $this->_path . '/' . $name . '.xml';
  1050. $this->_directory->create($this->_path);
  1051. $stream = $this->_directory->openFile($file, 'w+');
  1052. $stream->lock();
  1053. $convert->setDataHeader($this->_getExportHeaders());
  1054. if ($this->getCountTotals()) {
  1055. $convert->setDataFooter($this->_getExportTotals());
  1056. }
  1057. $convert->write($stream, $sheetName);
  1058. $stream->unlock();
  1059. $stream->close();
  1060. return [
  1061. 'type' => 'filename',
  1062. 'value' => $file,
  1063. 'rm' => true // can delete file after use
  1064. ];
  1065. }
  1066. /**
  1067. * Retrieve grid data as MS Excel 2003 XML Document
  1068. *
  1069. * @return string
  1070. */
  1071. public function getExcel()
  1072. {
  1073. $this->_isExport = true;
  1074. $this->_prepareGrid();
  1075. $this->getCollection()->getSelect()->limit();
  1076. $this->getCollection()->setPageSize(0);
  1077. $this->getCollection()->load();
  1078. $this->_afterLoadCollection();
  1079. $headers = [];
  1080. $data = [];
  1081. foreach ($this->getColumns() as $column) {
  1082. if (!$column->getIsSystem()) {
  1083. $headers[] = $column->getHeader();
  1084. }
  1085. }
  1086. $data[] = $headers;
  1087. foreach ($this->getCollection() as $item) {
  1088. $row = [];
  1089. foreach ($this->getColumns() as $column) {
  1090. if (!$column->getIsSystem()) {
  1091. $row[] = $column->getRowField($item);
  1092. }
  1093. }
  1094. $data[] = $row;
  1095. }
  1096. if ($this->getCountTotals()) {
  1097. $row = [];
  1098. foreach ($this->getColumns() as $column) {
  1099. if (!$column->getIsSystem()) {
  1100. $row[] = $column->getRowField($this->getTotals());
  1101. }
  1102. }
  1103. $data[] = $row;
  1104. }
  1105. $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($data));
  1106. return $convert->convert('single_sheet');
  1107. }
  1108. /**
  1109. * Retrieve grid export types
  1110. *
  1111. * @return \Magento\Framework\DataObject[]|false
  1112. */
  1113. public function getExportTypes()
  1114. {
  1115. return empty($this->_exportTypes) ? false : $this->_exportTypes;
  1116. }
  1117. /**
  1118. * Set collection object
  1119. *
  1120. * @param \Magento\Framework\Data\Collection $collection
  1121. * @return void
  1122. */
  1123. public function setCollection($collection)
  1124. {
  1125. $this->_collection = $collection;
  1126. }
  1127. /**
  1128. * get collection object
  1129. *
  1130. * @return \Magento\Framework\Data\Collection
  1131. */
  1132. public function getCollection()
  1133. {
  1134. return $this->_collection;
  1135. }
  1136. /**
  1137. * Set subtotals
  1138. *
  1139. * @param boolean $flag
  1140. * @return $this
  1141. */
  1142. public function setCountSubTotals($flag = true)
  1143. {
  1144. $this->_countSubTotals = $flag;
  1145. return $this;
  1146. }
  1147. /**
  1148. * Return count subtotals
  1149. *
  1150. * @return boolean
  1151. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  1152. */
  1153. public function getCountSubTotals()
  1154. {
  1155. return $this->_countSubTotals;
  1156. }
  1157. /**
  1158. * Set subtotal items
  1159. *
  1160. * @param \Magento\Framework\DataObject[] $items
  1161. * @return $this
  1162. */
  1163. public function setSubTotals(array $items)
  1164. {
  1165. $this->_subtotals = $items;
  1166. return $this;
  1167. }
  1168. /**
  1169. * Retrieve subtotal items
  1170. *
  1171. * @return \Magento\Framework\DataObject[]
  1172. */
  1173. public function getSubTotals()
  1174. {
  1175. return $this->_subtotals;
  1176. }
  1177. /**
  1178. * Generate list of grid buttons
  1179. *
  1180. * @return string
  1181. */
  1182. public function getMainButtonsHtml()
  1183. {
  1184. $html = '';
  1185. if ($this->getFilterVisibility()) {
  1186. $html .= $this->getSearchButtonHtml();
  1187. $html .= $this->getResetFilterButtonHtml();
  1188. }
  1189. return $html;
  1190. }
  1191. }