Skip to content

Commit

Permalink
Merge pull request magento#179 from magento-engcom/download-sample-file
Browse files Browse the repository at this point in the history
Download sample file
  • Loading branch information
maghamed authored Nov 25, 2017
2 parents d33c139 + 2cb3210 commit 5e692c8
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\ImportExport\Controller\Adminhtml\Import;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\ImportExport\Controller\Adminhtml\Import as ImportController;
use Magento\Framework\App\Filesystem\DirectoryList;

Expand Down Expand Up @@ -36,21 +37,28 @@ class Download extends ImportController
*/
protected $fileFactory;

/**
* @var \Magento\ImportExport\Model\Import\SampleFileProvider
*/
private $sampleFileProvider;

/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
* @param \Magento\ImportExport\Model\Import\SampleFileProvider $sampleFileProvider
* @param ComponentRegistrar $componentRegistrar
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
\Magento\Framework\Component\ComponentRegistrar $componentRegistrar
\Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
\Magento\ImportExport\Model\Import\SampleFileProvider $sampleFileProvider = null
) {
parent::__construct(
$context
Expand All @@ -59,6 +67,9 @@ public function __construct(
$this->resultRawFactory = $resultRawFactory;
$this->readFactory = $readFactory;
$this->componentRegistrar = $componentRegistrar;
$this->sampleFileProvider = $sampleFileProvider
?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\ImportExport\Model\Import\SampleFileProvider::class);
}

/**
Expand All @@ -68,22 +79,20 @@ public function __construct(
*/
public function execute()
{
$fileName = $this->getRequest()->getParam('filename') . '.csv';
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, self::SAMPLE_FILES_MODULE);
$fileAbsolutePath = $moduleDir . '/Files/Sample/' . $fileName;
$directoryRead = $this->readFactory->create($moduleDir);
$filePath = $directoryRead->getRelativePath($fileAbsolutePath);
$entityName = $this->getRequest()->getParam('filename');

if (!$directoryRead->isFile($filePath)) {
try {
$fileContents = $this->sampleFileProvider->getFileContents($entityName);
} catch (NoSuchEntityException $e) {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$this->messageManager->addError(__('There is no sample file for this entity.'));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/import');
return $resultRedirect;
}

$fileSize = isset($directoryRead->stat($filePath)['size'])
? $directoryRead->stat($filePath)['size'] : null;
$fileSize = $this->sampleFileProvider->getSize($entityName);
$fileName = $entityName . '.csv';

$this->fileFactory->create(
$fileName,
Expand All @@ -95,7 +104,7 @@ public function execute()

/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents($directoryRead->readFile($filePath));
$resultRaw->setContents($fileContents);
return $resultRaw;
}
}
127 changes: 127 additions & 0 deletions app/code/Magento/ImportExport/Model/Import/SampleFileProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ImportExport\Model\Import;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Filesystem\Directory\ReadInterface;

/**
* Import Sample File Provider model
*/
class SampleFileProvider
{
/**
* Associate an import entity to its module, e.g ['entity_name' => 'module_name']
* @var array
*/
private $samples;

/**
* @var \Magento\Framework\Component\ComponentRegistrar
*/
private $componentRegistrar;

/**
* @var \Magento\Framework\Filesystem\Directory\ReadFactory
*/
private $readFactory;

/**
* @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory
* @param ComponentRegistrar $componentRegistrar
* @param array $samples
*/
public function __construct(
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
\Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
array $samples = []
) {
$this->readFactory = $readFactory;
$this->componentRegistrar = $componentRegistrar;
$this->samples = $samples;
}

/**
* Returns the Size for the given file associated to an Import entity
*
* @param string $fileName
* @throws NoSuchEntityException
* @return int
*/
public function getSize(string $entityName): int
{
$directoryRead = $this->getDirectoryRead($entityName);
$filePath = $this->getPath($entityName);
$fileSize = isset($directoryRead->stat($filePath)['size'])
? $directoryRead->stat($filePath)['size'] : null;

return $fileSize;
}

/**
* Returns Content for the given file associated to an Import entity
*
* @param string $entityName
* @throws NoSuchEntityException
* @return string
*/
public function getFileContents(string $entityName): string
{
$directoryRead = $this->getDirectoryRead($entityName);
$filePath = $this->getPath($entityName);

return $directoryRead->readFile($filePath);
}

/**
* @param $entityName
* @return string $entityName
* @throws NoSuchEntityException
*/
private function getPath(string $entityName): string
{
$directoryRead = $this->getDirectoryRead($entityName);
$moduleName = $this->getModuleName($entityName);
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
$fileAbsolutePath = $moduleDir . '/Files/Sample/' . $entityName . '.csv';

$filePath = $directoryRead->getRelativePath($fileAbsolutePath);

if (!$directoryRead->isFile($filePath)) {
throw new NoSuchEntityException(__("There is no file: %s", $filePath));
}

return $filePath;
}

/**
* @param string $entityName
* @return ReadInterface
*/
private function getDirectoryRead(string $entityName): ReadInterface
{
$moduleName = $this->getModuleName($entityName);
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
$directoryRead = $this->readFactory->create($moduleDir);

return $directoryRead;
}

/**
* @param string $entityName
* @return string
* @throws NoSuchEntityException
*/
private function getModuleName(string $entityName): string
{
if (!isset($this->samples[$entityName])) {
throw new NoSuchEntityException();
}

return $this->samples[$entityName];
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/ImportExport/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@
</argument>
</arguments>
</type>
<type name="Magento\ImportExport\Model\Import\SampleFileProvider">
<arguments>
<argument name="samples" xsi:type="array">
<item name="advanced_pricing" xsi:type="string">Magento_ImportExport</item>
<item name="catalog_product" xsi:type="string">Magento_ImportExport</item>
<item name="customer" xsi:type="string">Magento_ImportExport</item>
<item name="customer_address" xsi:type="string">Magento_ImportExport</item>
<item name="customer_composite" xsi:type="string">Magento_ImportExport</item>
<item name="customer_finance" xsi:type="string">Magento_ImportExport</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source_id,sku,status,quantity
1,sku1,1,10
2,sku2,1,10
3,sku3,1,10
4,sku4,1,15
7 changes: 7 additions & 0 deletions app/code/Magento/InventoryImportExport/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@
</argument>
</arguments>
</type>
<type name="Magento\ImportExport\Model\Import\SampleFileProvider">
<arguments>
<argument name="samples" xsi:type="array">
<item name="stock_sources" xsi:type="string">Magento_InventoryImportExport</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 5e692c8

Please sign in to comment.