/** * 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]; }
/** * Returns DB fields list * * @return array */ protected function getFields() { if (!$this->fields) { $component = $this->filter->getComponent(); $this->fields = $this->metadataProvider->getFields($component); } return $this->fields; }
public function testGetFields() { $componentName = 'component_name'; $columnName = 'column_name'; $columnLabel = 'column_label'; $component = $this->prepareColumns($componentName, $columnName, $columnLabel); $result = $this->model->getFields($component); $this->assertTrue(is_array($result)); $this->assertCount(1, $result); $this->assertEquals($columnName, $result[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(); $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]; }