PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Ui/Model/Export/ConvertToXml.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 177 lines | 89 code | 25 blank | 63 comment | 2 complexity | bdbdb050e9e333de9daf99f54e67e305 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Model\Export;
  7. use Magento\Framework\Api\Search\DocumentInterface;
  8. use Magento\Framework\Api\Search\SearchResultInterface;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Convert\Excel;
  11. use Magento\Framework\Convert\ExcelFactory;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\Filesystem\Directory\WriteInterface;
  15. use Magento\Ui\Component\MassAction\Filter;
  16. /**
  17. * Class ConvertToXml
  18. */
  19. class ConvertToXml
  20. {
  21. /**
  22. * @var WriteInterface
  23. */
  24. protected $directory;
  25. /**
  26. * @var MetadataProvider
  27. */
  28. protected $metadataProvider;
  29. /**
  30. * @var ExcelFactory
  31. */
  32. protected $excelFactory;
  33. /**
  34. * @var array
  35. */
  36. protected $options;
  37. /**
  38. * @var SearchResultIteratorFactory
  39. */
  40. protected $iteratorFactory;
  41. /**
  42. * @var array
  43. */
  44. protected $fields;
  45. /**
  46. * @param Filesystem $filesystem
  47. * @param Filter $filter
  48. * @param MetadataProvider $metadataProvider
  49. * @param ExcelFactory $excelFactory
  50. * @param SearchResultIteratorFactory $iteratorFactory
  51. */
  52. public function __construct(
  53. Filesystem $filesystem,
  54. Filter $filter,
  55. MetadataProvider $metadataProvider,
  56. ExcelFactory $excelFactory,
  57. SearchResultIteratorFactory $iteratorFactory
  58. ) {
  59. $this->filter = $filter;
  60. $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  61. $this->metadataProvider = $metadataProvider;
  62. $this->excelFactory = $excelFactory;
  63. $this->iteratorFactory = $iteratorFactory;
  64. }
  65. /**
  66. * Returns Filters with options
  67. *
  68. * @return array
  69. */
  70. protected function getOptions()
  71. {
  72. if (!$this->options) {
  73. $this->options = $this->metadataProvider->getOptions();
  74. }
  75. return $this->options;
  76. }
  77. /**
  78. * Returns DB fields list
  79. *
  80. * @return array
  81. */
  82. protected function getFields()
  83. {
  84. if (!$this->fields) {
  85. $component = $this->filter->getComponent();
  86. $this->fields = $this->metadataProvider->getFields($component);
  87. }
  88. return $this->fields;
  89. }
  90. /**
  91. * Returns row data
  92. *
  93. * @param DocumentInterface $document
  94. * @return array
  95. */
  96. public function getRowData(DocumentInterface $document)
  97. {
  98. return $this->metadataProvider->getRowData($document, $this->getFields(), $this->getOptions());
  99. }
  100. /**
  101. * Returns XML file
  102. *
  103. * @return array
  104. * @throws LocalizedException
  105. */
  106. public function getXmlFile()
  107. {
  108. $component = $this->filter->getComponent();
  109. $name = md5(microtime());
  110. $file = 'export/'. $component->getName() . $name . '.xml';
  111. $this->filter->prepareComponent($component);
  112. $this->filter->applySelectionOnTargetProvider();
  113. $component->getContext()->getDataProvider()->setLimit(0, 0);
  114. /** @var SearchResultInterface $searchResult */
  115. $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
  116. /** @var DocumentInterface[] $searchResultItems */
  117. $searchResultItems = $searchResult->getItems();
  118. $this->prepareItems($component->getName(), $searchResultItems);
  119. /** @var SearchResultIterator $searchResultIterator */
  120. $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResultItems]);
  121. /** @var Excel $excel */
  122. $excel = $this->excelFactory->create(
  123. [
  124. 'iterator' => $searchResultIterator,
  125. 'rowCallback'=> [$this, 'getRowData'],
  126. ]
  127. );
  128. $this->directory->create('export');
  129. $stream = $this->directory->openFile($file, 'w+');
  130. $stream->lock();
  131. $excel->setDataHeader($this->metadataProvider->getHeaders($component));
  132. $excel->write($stream, $component->getName() . '.xml');
  133. $stream->unlock();
  134. $stream->close();
  135. return [
  136. 'type' => 'filename',
  137. 'value' => $file,
  138. 'rm' => true // can delete file after use
  139. ];
  140. }
  141. /**
  142. * @param string $componentName
  143. * @param array $items
  144. * @return void
  145. */
  146. protected function prepareItems($componentName, array $items = [])
  147. {
  148. foreach ($items as $document) {
  149. $this->metadataProvider->convertDate($document, $componentName);
  150. }
  151. }
  152. }