38 protected $_template =
"Magento_Backend::widget/grid/export.phtml";
64 \
Magento\Framework\Data\CollectionFactory $collectionFactory,
67 $this->_collectionFactory = $collectionFactory;
68 parent::__construct($context,
$data);
78 if ($this->
hasData(
'exportTypes')) {
80 if (!isset(
$type[
'urlPath']) || !isset(
$type[
'label'])) {
81 throw new \Magento\Framework\Exception\LocalizedException(
82 __(
'Invalid export type supplied for grid export block')
119 return $this->
getParentBlock()->getColumnSet()->shouldRenderTotal();
165 'label' =>
__(
'Export'),
171 return parent::_prepareLayout();
193 $this->_exportTypes[] = new \Magento\Framework\DataObject(
207 return $this->_directory->readFile(
'export/' . $fileData[
'value']);
219 if (!$column->getIsSystem()) {
220 $row[] = $column->getExportHeader();
236 if (!$column->getIsSystem()) {
237 $row[] = $column->hasTotalsLabel() ? $column->getTotalsLabel() : $column->getRowFieldExport(
$totals);
251 public function _exportIterateCollection($callback, array $args)
254 $originalCollection = $this->
getParentBlock()->getPreparedCollection();
260 while ($break !==
true) {
261 $originalCollection->clear();
263 $originalCollection->setCurPage(
$page);
264 $originalCollection->load();
266 $count = $originalCollection->getSize();
267 $lPage = $originalCollection->getLastPageNumber();
269 if ($lPage ==
$page) {
274 $collection = $this->_getRowCollection($originalCollection);
276 call_user_func_array([$this, $callback], array_merge([
$item], $args));
294 if (!$column->getIsSystem()) {
295 $row[] = $column->getRowFieldExport(
$item);
298 $stream->writeCsv(
$row);
310 $name = md5(microtime());
311 $file = $this->_path .
'/' .
$name .
'.csv';
313 $this->_directory->create($this->_path);
314 $stream = $this->_directory->openFile($file,
'w+');
318 $this->_exportIterateCollection(
'_exportCsvItem', [$stream]);
326 'type' =>
'filename',
344 if (!$column->getIsSystem()) {
345 $data[] =
'"' . $column->getExportHeader() .
'"';
348 $csv .= implode(
',',
$data) .
"\n";
353 if (!$column->getIsSystem()) {
354 $data[] =
'"' . str_replace(
357 $column->getRowFieldExport(
$item)
361 $csv .= implode(
',',
$data) .
"\n";
367 if (!$column->getIsSystem()) {
368 $data[] =
'"' . str_replace(
371 $column->getRowFieldExport($this->_getTotals())
375 $csv .= implode(
',',
$data) .
"\n";
392 if (!$column->getIsSystem()) {
393 $indexes[] = $column->getIndex();
396 $xml =
'<?xml version="1.0" encoding="UTF-8"?>';
399 $xml .=
$item->toXml($indexes);
418 if (!$column->getIsSystem()) {
419 $row[] = $column->getRowFieldExport(
$data);
437 $convert = new \Magento\Framework\Convert\Excel(
$collection->getIterator(), [$this,
'getRowRecord']);
439 $name = md5(microtime());
440 $file = $this->_path .
'/' .
$name .
'.xml';
442 $this->_directory->create($this->_path);
443 $stream = $this->_directory->openFile($file,
'w+');
451 $convert->write($stream, $sheetName);
456 'type' =>
'filename',
474 if (!$column->getIsSystem()) {
475 $headers[] = $column->getHeader();
483 if (!$column->getIsSystem()) {
493 if (!$column->getIsSystem()) {
500 $convert = new \Magento\Framework\Convert\Excel(
new \ArrayIterator(
$data));
501 return $convert->convert(
'single_sheet');
510 protected function _getRowCollection(\
Magento\Framework\Data\Collection $baseCollection =
null)
512 if (
null === $baseCollection) {
513 $baseCollection = $this->
getParentBlock()->getPreparedCollection();
518 foreach ($baseCollection as
$item) {
519 if (
$item->getIsEmpty()) {
522 if (
$item->hasChildren() && count(
$item->getChildren()) > 0) {
524 foreach (
$item->getChildren() as $subItem) {
525 $tmpItem = clone
$item;
526 $tmpItem->unsChildren();
527 $tmpItem->addData($subItem->getData());
543 public function _getPreparedCollection()
getData($key='', $index=null)
getUrl($route='', $params=[])
setData($key, $value=null)
getChildHtml($alias='', $useCache=true)
if(!isset($_GET['name'])) $name