![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Controller/Adminhtml/ShippingMethods/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\ShippingMethods; use Extmag\Shiplab\Model\ResourceModel\ShippingMethods\Collection; use Extmag\Shiplab\Model\ResourceModel\ShippingMethods\CollectionFactory; use Extmag\Shiplab\Model\ShippingMethods; use LogicException; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\App\ResponseInterface; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\File\Csv; use Magento\Framework\Filesystem\Driver\File; class Export extends Action { /** * @var CollectionFactory */ private $collection; /** * @var Csv */ private $csvProcessor; /** * @var DirectoryList */ private $directoryList; /** * @var FileFactory */ private $fileFactory; /** * @var File */ private $file; /** * @param Context $context * @param CollectionFactory $collection * @param Csv $csvProcessor * @param DirectoryList $directoryList * @param FileFactory $fileFactory * @param File $file */ public function __construct( Context $context, CollectionFactory $collection, Csv $csvProcessor, DirectoryList $directoryList, FileFactory $fileFactory, File $file ) { parent::__construct($context); $this->collection = $collection; $this->csvProcessor = $csvProcessor; $this->directoryList = $directoryList; $this->fileFactory = $fileFactory; $this->file = $file; } /** * Authorization level of a basic admin session * * @see _isAllowed() */ public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_shippingmethods_export'; /** * @return ResponseInterface|Redirect|ResultInterface * @throws FileSystemException */ public function execute() { try { /** * @var Collection $collection */ $collection = $this->collection->create(); $itemsSize = $collection->count(); $fileDirectoryPath = $this->directoryList->getPath(DirectoryList::VAR_DIR); $fileName = 'export_shipping_methods.csv'; $filePath = $fileDirectoryPath . '/' . $fileName; $fields = array_keys($collection->getFields()); unset($fields[0]); $this->csvProcessor ->setEnclosure('"') ->setDelimiter(','); if ($itemsSize > 0) { $items = []; /** * @var ShippingMethods $item */ foreach ($collection as $item) { $datum = $item->getData(); unset($datum['entity_id']); $items[] = $datum; } $this->csvProcessor->appendData($filePath, array_merge([$fields], $items)); } else { $this->csvProcessor->appendData($filePath, [$fields]); } return $this->fileFactory->create( $fileName, $this->file->fileGetContents($filePath), \Magento\Framework\Filesystem\DirectoryList::SYS_TMP, 'text/csv' ); } catch (LogicException $e) { $this->messageManager->addErrorMessage($e->getMessage()); return $this->resultRedirectFactory->create()->setRefererOrBaseUrl(); } } }