Example #1
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];
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function log($str)
 {
     $str = '## ' . date('Y-m-d H:i:s') . "\r\n" . $str;
     $stream = $this->dir->openFile($this->debugFile, 'a');
     $stream->lock();
     $stream->write($str);
     $stream->unlock();
     $stream->close();
 }
Example #3
0
 /**
  * Get file handler by process ID
  *
  * @param string $processId
  * @return File
  */
 public function getFile($processId)
 {
     if (!isset($this->_fileHandlers[$processId])) {
         $this->_varDirectory->create('locks');
         $fileName = 'locks/index_process_' . $processId . '.lock';
         $stream = $this->_varDirectory->openFile($fileName, 'w+');
         $stream->write(date('r'));
         $this->_fileHandlers[$processId] = $this->_fileFactory->create(array('streamHandler' => $stream));
     }
     return $this->_fileHandlers[$processId];
 }
Example #4
0
 /**
  * Save config in cache file
  *
  * @return void
  */
 public function save()
 {
     /** @var Write $file */
     $file = $this->rootDirectory->openFile($this->rootDirectory->getRelativePath($this->cacheFilePath), 'w');
     try {
         $file->lock();
         $file->write(json_encode($this->config));
         $file->unlock();
         $file->close();
     } catch (FileSystemException $e) {
         $file->close();
     }
 }
 /**
  * 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();
     $component->getContext()->getDataProvider()->setLimit(0, 0);
     /** @var SearchResultInterface $searchResult */
     $searchResult = $component->getContext()->getDataProvider()->getSearchResult();
     /** @var DocumentInterface[] $searchResultItems */
     $searchResultItems = $searchResult->getItems();
     $this->prepareItems($component->getName(), $searchResultItems);
     /** @var SearchResultIterator $searchResultIterator */
     $searchResultIterator = $this->iteratorFactory->create(['items' => $searchResultItems]);
     /** @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];
 }
Example #6
0
 /**
  * Check file in database storage if needed and place it on file system
  *
  * @param string $filePath
  * @return bool
  */
 protected function _processDatabaseFile($filePath)
 {
     if (!$this->_fileStorageDatabase->checkDbUsage()) {
         return false;
     }
     $relativePath = $this->_fileStorageDatabase->getMediaRelativePath($filePath);
     $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath);
     if (!$file->getId()) {
         return false;
     }
     $stream = $this->_rootDir->openFile($filePath, 'w+');
     $stream->lock();
     $stream->write($filePath, $file->getContent());
     $stream->unlock();
     $stream->close();
     return true;
 }
 /**
  * Synchronize file
  *
  * @param string $relativeFileName
  * @return void
  * @throws \LogicException
  */
 public function synchronize($relativeFileName)
 {
     /** @var $storage \Magento\MediaStorage\Model\File\Storage\Database */
     $storage = $this->storageFactory->create();
     try {
         $storage->loadByFilename($relativeFileName);
     } catch (\Exception $e) {
     }
     if ($storage->getId()) {
         /** @var Write $file */
         $file = $this->mediaDirectory->openFile($relativeFileName, 'w');
         try {
             $file->lock();
             $file->write($storage->getContent());
             $file->unlock();
             $file->close();
         } catch (FileSystemException $e) {
             $file->close();
         }
     }
 }
 /**
  * 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];
 }
Example #9
0
 /**
  * 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(), array($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 array('type' => 'filename', 'value' => $file, 'rm' => true);
 }
Example #10
0
 /**
  * Parse CSV file and collect report rows
  *
  * @param string $localCsv Path to CSV file
  * @param string $format CSV format(column names)
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function parseCsv($localCsv, $format = 'new')
 {
     $this->_rows = [];
     $sectionColumns = $this->_csvColumns[$format]['section_columns'];
     $rowMap = $this->_csvColumns[$format]['rowmap'];
     $flippedSectionColumns = array_flip($sectionColumns);
     $stream = $this->_tmpDirectory->openFile($localCsv);
     while ($line = $stream->readCsv()) {
         if (empty($line)) {
             // The line was empty, so skip it.
             continue;
         }
         $lineType = $line[0];
         switch ($lineType) {
             case 'RH':
                 // Report header.
                 $lastModified = new \DateTime($line[1]);
                 $this->setReportLastModified($lastModified->format('Y-m-d H:i:s'));
                 //$this->setAccountId($columns[2]); -- probably we'll just take that from the section header...
                 break;
             case 'FH':
                 // File header.
                 // Nothing interesting here, move along
                 break;
             case 'SH':
                 // Section header.
                 $this->setAccountId($line[3]);
                 $this->loadByAccountAndDate();
                 break;
             case 'CH':
                 // Section columns.
                 // In case ever the column order is changed, we will have the items recorded properly
                 // anyway. We have named, not numbered columns.
                 for ($i = 1; $i < count($line); $i++) {
                     $sectionColumns[$line[$i]] = $i;
                 }
                 $flippedSectionColumns = array_flip($sectionColumns);
                 break;
             case 'SB':
                 // Section body.
                 $bodyItem = [];
                 for ($i = 1; $i < count($line); $i++) {
                     $bodyItem[$rowMap[$flippedSectionColumns[$i]]] = $line[$i];
                 }
                 $this->_rows[] = $bodyItem;
                 break;
             case 'SC':
                 // Section records count.
             // Section records count.
             case 'RC':
                 // Report records count.
             // Report records count.
             case 'SF':
                 // Section footer.
             // Section footer.
             case 'FF':
                 // File footer.
             // File footer.
             case 'RF':
                 // Report footer.
                 // Nothing to see here, move along
                 break;
             default:
                 break;
         }
     }
     return $this;
 }
Example #11
0
 /**
  * Open test file
  */
 protected function _openFile()
 {
     $this->_model = $this->_objectManager->create('Magento\\Index\\Model\\Process\\File', array('streamHandler' => $this->_varDirectory->openFile(self::FILE_PATH, 'w+')));
 }