123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Block\Widget\Grid;
- use Magento\Framework\App\Filesystem\DirectoryList;
- /**
- * @api
- * @deprecated 100.2.0 in favour of UI component implementation
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Export extends \Magento\Backend\Block\Widget implements \Magento\Backend\Block\Widget\Grid\ExportInterface
- {
- /**
- * Grid export types
- *
- * @var \Magento\Framework\DataObject[]
- */
- protected $_exportTypes = [];
- /**
- * Rows per page for import
- *
- * @var int
- */
- protected $_exportPageSize = 1000;
- /**
- * Template file name
- *
- * @var string
- */
- protected $_template = "Magento_Backend::widget/grid/export.phtml";
- /**
- * @var \Magento\Framework\Data\CollectionFactory
- */
- protected $_collectionFactory;
- /**
- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
- */
- protected $_directory;
- /**
- * Additional path to folder
- *
- * @var string
- */
- protected $_path = 'export';
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Framework\Data\CollectionFactory $collectionFactory
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Framework\Data\CollectionFactory $collectionFactory,
- array $data = []
- ) {
- $this->_collectionFactory = $collectionFactory;
- parent::__construct($context, $data);
- }
- /**
- * @return void
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- protected function _construct()
- {
- parent::_construct();
- if ($this->hasData('exportTypes')) {
- foreach ($this->getData('exportTypes') as $type) {
- if (!isset($type['urlPath']) || !isset($type['label'])) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('Invalid export type supplied for grid export block')
- );
- }
- $this->addExportType($type['urlPath'], $type['label']);
- }
- }
- $this->_directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
- }
- /**
- * Retrieve grid columns
- *
- * @return \Magento\Backend\Block\Widget\Grid\Column[]
- */
- protected function _getColumns()
- {
- return $this->getParentBlock()->getColumns();
- }
- /**
- * Retrieve totals
- *
- * @return \Magento\Framework\DataObject
- */
- protected function _getTotals()
- {
- return $this->getParentBlock()->getColumnSet()->getTotals();
- }
- /**
- * Return count totals
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getCountTotals()
- {
- return $this->getParentBlock()->getColumnSet()->shouldRenderTotal();
- }
- /**
- * Get collection object
- *
- * @return \Magento\Framework\Data\Collection
- */
- protected function _getCollection()
- {
- return $this->getParentBlock()->getCollection();
- }
- /**
- * Retrieve grid export types
- *
- * @return \Magento\Framework\DataObject[]|false
- */
- public function getExportTypes()
- {
- return empty($this->_exportTypes) ? false : $this->_exportTypes;
- }
- /**
- * Retrieve grid id
- *
- * @return string
- */
- public function getId()
- {
- return $this->getParentBlock()->getId();
- }
- /**
- * Prepare export button
- *
- * @return $this
- */
- protected function _prepareLayout()
- {
- $this->setChild(
- 'export_button',
- $this->getLayout()->createBlock(
- \Magento\Backend\Block\Widget\Button::class
- )->setData(
- [
- 'label' => __('Export'),
- 'onclick' => $this->getParentBlock()->getJsObjectName() . '.doExport()',
- 'class' => 'task',
- ]
- )
- );
- return parent::_prepareLayout();
- }
- /**
- * Render export button
- *
- * @return string
- */
- public function getExportButtonHtml()
- {
- return $this->getChildHtml('export_button');
- }
- /**
- * Add new export type to grid
- *
- * @param string $url
- * @param string $label
- * @return $this
- */
- public function addExportType($url, $label)
- {
- $this->_exportTypes[] = new \Magento\Framework\DataObject(
- ['url' => $this->getUrl($url, ['_current' => true]), 'label' => $label]
- );
- return $this;
- }
- /**
- * Retrieve file content from file container array
- *
- * @param array $fileData
- * @return string
- */
- protected function _getFileContainerContent(array $fileData)
- {
- return $this->_directory->readFile('export/' . $fileData['value']);
- }
- /**
- * Retrieve Headers row array for Export
- *
- * @return string[]
- */
- protected function _getExportHeaders()
- {
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->getExportHeader();
- }
- }
- return $row;
- }
- /**
- * Retrieve Totals row array for Export
- *
- * @return string[]
- */
- protected function _getExportTotals()
- {
- $totals = $this->_getTotals();
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->hasTotalsLabel() ? $column->getTotalsLabel() : $column->getRowFieldExport($totals);
- }
- }
- return $row;
- }
- /**
- * Iterate collection and call callback method per item
- * For callback method first argument always is item object
- *
- * @param string $callback
- * @param array $args additional arguments for callback method
- * @return void
- */
- public function _exportIterateCollection($callback, array $args)
- {
- /** @var $originalCollection \Magento\Framework\Data\Collection */
- $originalCollection = $this->getParentBlock()->getPreparedCollection();
- $count = null;
- $page = 1;
- $lPage = null;
- $break = false;
- while ($break !== true) {
- $originalCollection->clear();
- $originalCollection->setPageSize($this->getExportPageSize());
- $originalCollection->setCurPage($page);
- $originalCollection->load();
- if ($count === null) {
- $count = $originalCollection->getSize();
- $lPage = $originalCollection->getLastPageNumber();
- }
- if ($lPage == $page) {
- $break = true;
- }
- $page++;
- $collection = $this->_getRowCollection($originalCollection);
- foreach ($collection as $item) {
- call_user_func_array([$this, $callback], array_merge([$item], $args));
- }
- }
- }
- /**
- * Write item data to csv export file
- *
- * @param \Magento\Framework\DataObject $item
- * @param \Magento\Framework\Filesystem\File\WriteInterface $stream
- * @return void
- */
- protected function _exportCsvItem(
- \Magento\Framework\DataObject $item,
- \Magento\Framework\Filesystem\File\WriteInterface $stream
- ) {
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->getRowFieldExport($item);
- }
- }
- $stream->writeCsv($row);
- }
- /**
- * Retrieve a file container array by grid data as CSV
- *
- * Return array with keys type and value
- *
- * @return array
- */
- public function getCsvFile()
- {
- $name = md5(microtime());
- $file = $this->_path . '/' . $name . '.csv';
- $this->_directory->create($this->_path);
- $stream = $this->_directory->openFile($file, 'w+');
- $stream->writeCsv($this->_getExportHeaders());
- $stream->lock();
- $this->_exportIterateCollection('_exportCsvItem', [$stream]);
- if ($this->getCountTotals()) {
- $stream->writeCsv($this->_getExportTotals());
- }
- $stream->unlock();
- $stream->close();
- return [
- 'type' => 'filename',
- 'value' => $file,
- 'rm' => true // can delete file after use
- ];
- }
- /**
- * Retrieve Grid data as CSV
- *
- * @return string
- */
- public function getCsv()
- {
- $csv = '';
- $collection = $this->_getPreparedCollection();
- $data = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $data[] = '"' . $column->getExportHeader() . '"';
- }
- }
- $csv .= implode(',', $data) . "\n";
- foreach ($collection as $item) {
- $data = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $data[] = '"' . str_replace(
- ['"', '\\'],
- ['""', '\\\\'],
- $column->getRowFieldExport($item)
- ) . '"';
- }
- }
- $csv .= implode(',', $data) . "\n";
- }
- if ($this->getCountTotals()) {
- $data = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $data[] = '"' . str_replace(
- ['"', '\\'],
- ['""', '\\\\'],
- $column->getRowFieldExport($this->_getTotals())
- ) . '"';
- }
- }
- $csv .= implode(',', $data) . "\n";
- }
- return $csv;
- }
- /**
- * Retrieve data in xml
- *
- * @return string
- */
- public function getXml()
- {
- $collection = $this->_getPreparedCollection();
- $indexes = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $indexes[] = $column->getIndex();
- }
- }
- $xml = '<?xml version="1.0" encoding="UTF-8"?>';
- $xml .= '<items>';
- foreach ($collection as $item) {
- $xml .= $item->toXml($indexes);
- }
- if ($this->getCountTotals()) {
- $xml .= $this->_getTotals()->toXml($indexes);
- }
- $xml .= '</items>';
- return $xml;
- }
- /**
- * Get a row data of the particular columns
- *
- * @param \Magento\Framework\DataObject $data
- * @return string[]
- */
- public function getRowRecord(\Magento\Framework\DataObject $data)
- {
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->getRowFieldExport($data);
- }
- }
- return $row;
- }
- /**
- * Retrieve a file container array by grid data as MS Excel 2003 XML Document
- *
- * Return array with keys type and value
- *
- * @param string $sheetName
- * @return array
- */
- public function getExcelFile($sheetName = '')
- {
- $collection = $this->_getRowCollection();
- $convert = new \Magento\Framework\Convert\Excel($collection->getIterator(), [$this, 'getRowRecord']);
- $name = md5(microtime());
- $file = $this->_path . '/' . $name . '.xml';
- $this->_directory->create($this->_path);
- $stream = $this->_directory->openFile($file, 'w+');
- $stream->lock();
- $convert->setDataHeader($this->_getExportHeaders());
- if ($this->getCountTotals()) {
- $convert->setDataFooter($this->_getExportTotals());
- }
- $convert->write($stream, $sheetName);
- $stream->unlock();
- $stream->close();
- return [
- 'type' => 'filename',
- 'value' => $file,
- 'rm' => true // can delete file after use
- ];
- }
- /**
- * Retrieve grid data as MS Excel 2003 XML Document
- *
- * @return string
- */
- public function getExcel()
- {
- $collection = $this->_getPreparedCollection();
- $headers = [];
- $data = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $headers[] = $column->getHeader();
- }
- }
- $data[] = $headers;
- foreach ($collection as $item) {
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->getRowField($item);
- }
- }
- $data[] = $row;
- }
- if ($this->getCountTotals()) {
- $row = [];
- foreach ($this->_getColumns() as $column) {
- if (!$column->getIsSystem()) {
- $row[] = $column->getRowField($this->_getTotals());
- }
- }
- $data[] = $row;
- }
- $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($data));
- return $convert->convert('single_sheet');
- }
- /**
- * Reformat base collection into collection without sub-collection in items
- *
- * @param \Magento\Framework\Data\Collection $baseCollection
- * @return \Magento\Framework\Data\Collection
- */
- protected function _getRowCollection(\Magento\Framework\Data\Collection $baseCollection = null)
- {
- if (null === $baseCollection) {
- $baseCollection = $this->getParentBlock()->getPreparedCollection();
- }
- $collection = $this->_collectionFactory->create();
- /** @var $item \Magento\Framework\DataObject */
- foreach ($baseCollection as $item) {
- if ($item->getIsEmpty()) {
- continue;
- }
- if ($item->hasChildren() && count($item->getChildren()) > 0) {
- /** @var $subItem \Magento\Framework\DataObject */
- foreach ($item->getChildren() as $subItem) {
- $tmpItem = clone $item;
- $tmpItem->unsChildren();
- $tmpItem->addData($subItem->getData());
- $collection->addItem($tmpItem);
- }
- } else {
- $collection->addItem($item);
- }
- }
- return $collection;
- }
- /**
- * Return prepared collection as row collection with additional conditions
- *
- * @return \Magento\Framework\Data\Collection
- */
- public function _getPreparedCollection()
- {
- /** @var $collection \Magento\Framework\Data\Collection */
- $collection = $this->getParentBlock()->getPreparedCollection();
- $collection->setPageSize(0);
- $collection->load();
- return $this->_getRowCollection($collection);
- }
- /**
- * @return int
- */
- public function getExportPageSize()
- {
- return $this->_exportPageSize;
- }
- }
|