/**
  * Returns CSV file
  *
  * @return array
  * @throws LocalizedException
  */
 public function getCsvFile()
 {
     $component = $this->filter->getComponent();
     $name = md5(microtime());
     $file = 'export/' . $component->getName() . $name . '.csv';
     $this->filter->prepareComponent($component);
     $this->filter->applySelectionOnTargetProvider();
     $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
     $fields = $this->metadataProvider->getFields($component);
     $options = $this->metadataProvider->getOptions();
     $this->directory->create('export');
     $stream = $this->directory->openFile($file, 'w+');
     $stream->lock();
     $stream->writeCsv($this->metadataProvider->getHeaders($component));
     foreach ($searchResult->getItems() as $document) {
         $stream->writeCsv($this->metadataProvider->getRowData($document, $fields, $options));
     }
     $stream->unlock();
     $stream->close();
     return ['type' => 'filename', 'value' => $file, 'rm' => true];
 }
Exemple #2
0
 /**
  * Returns CSV file
  *
  * @return array
  * @throws LocalizedException
  */
 public function getCsvFile()
 {
     $component = $this->filter->getComponent();
     $name = md5(microtime());
     $file = 'export/' . $component->getName() . $name . '.csv';
     $this->filter->prepareComponent($component);
     $this->filter->applySelectionOnTargetProvider();
     $dataProvider = $component->getContext()->getDataProvider();
     $fields = $this->metadataProvider->getFields($component);
     $options = $this->metadataProvider->getOptions();
     $this->directory->create('export');
     $stream = $this->directory->openFile($file, 'w+');
     $stream->lock();
     $stream->writeCsv($this->metadataProvider->getHeaders($component));
     $i = 1;
     $searchCriteria = $dataProvider->getSearchCriteria()->setCurrentPage($i)->setPageSize($this->pageSize);
     $totalCount = (int) $dataProvider->getSearchResult()->getTotalCount();
     while ($totalCount > 0) {
         $items = $dataProvider->getSearchResult()->getItems();
         foreach ($items as $item) {
             $this->metadataProvider->convertDate($item, $component->getName());
             $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
         }
         $searchCriteria->setCurrentPage(++$i);
         $totalCount = $totalCount - $this->pageSize;
     }
     $stream->unlock();
     $stream->close();
     return ['type' => 'filename', 'value' => $file, 'rm' => true];
 }
 /**
  * @param string $filter
  * @param array $options
  * @param array $expected
  * @dataProvider getOptionsDataProvider
  */
 public function testGetOptions($filter, $options, $expected)
 {
     $component = $this->getMockBuilder('Magento\\Framework\\View\\Element\\UiComponentInterface')->getMockForAbstractClass();
     $childComponent = $this->getMockBuilder('Magento\\Framework\\View\\Element\\UiComponentInterface')->getMockForAbstractClass();
     $filters = $this->getMockBuilder('Magento\\Ui\\Component\\Filters')->disableOriginalConstructor()->getMock();
     $select = $this->getMockBuilder('Magento\\Ui\\Component\\Filters\\Type\\Select')->disableOriginalConstructor()->getMock();
     $this->filter->expects($this->once())->method('getComponent')->willReturn($component);
     $component->expects($this->once())->method('getChildComponents')->willReturn(['listing_top' => $childComponent]);
     $childComponent->expects($this->once())->method('getChildComponents')->willReturn([$filters]);
     $filters->expects($this->once())->method('getChildComponents')->willReturn([$select]);
     $select->expects($this->any())->method('getName')->willReturn($filter);
     $select->expects($this->any())->method('getData')->with('config/options')->willReturn($options);
     $result = $this->model->getOptions();
     $this->assertTrue(is_array($result));
     $this->assertCount(1, $result);
     $this->assertEquals($expected, $result);
 }
 /**
  * Returns XML file
  *
  * @return array
  * @throws LocalizedException
  */
 public function getXmlFile()
 {
     $component = $this->filter->getComponent();
     $name = md5(microtime());
     $file = 'export/' . $component->getName() . $name . '.xml';
     $this->filter->prepareComponent($component);
     $this->filter->applySelectionOnTargetProvider();
     /** @var SearchResultInterface $searchResult */
     $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
     /** @var SearchResultIterator $searchResultIterator */
     $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResult->getItems()]);
     /** @var Excel $excel */
     $excel = $this->excelFactory->create(['iterator' => $searchResultIterator, 'rowCallback' => [$this, 'getRowData']]);
     $this->directory->create('export');
     $stream = $this->directory->openFile($file, 'w+');
     $stream->lock();
     $excel->setDataHeader($this->metadataProvider->getHeaders($component));
     $excel->write($stream, $component->getName() . '.xml');
     $stream->unlock();
     $stream->close();
     return ['type' => 'filename', 'value' => $file, 'rm' => true];
 }
 /**
  * @param string $componentName
  * @param array $items
  * @return void
  */
 protected function prepareItems($componentName, array $items = [])
 {
     foreach ($items as $document) {
         $this->metadataProvider->convertDate($document, $componentName);
     }
 }