Skip to content

Commit 90f788d

Browse files
Siarhei AndreyeuSiarhei Andreyeu
authored andcommitted
MAGNIMEX-14: Downloading Sample File
- changed sample files location - fixed PhpDoc
1 parent 841d42c commit 90f788d

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ImportExport\Controller\Adminhtml\Import;
7+
8+
use Magento\ImportExport\Controller\Adminhtml\Import as ImportController;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
11+
class Download extends ImportController
12+
{
13+
const SAMPLE_FILES_DIRECTORY = 'Magento/ImportExport/Files/Sample/';
14+
15+
/**
16+
* @var \Magento\Framework\Controller\Result\RawFactory
17+
*/
18+
protected $resultRawFactory;
19+
20+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface */
21+
protected $fileDirectory;
22+
23+
/**
24+
* @param \Magento\Backend\App\Action\Context $context
25+
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
26+
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
27+
*/
28+
public function __construct(
29+
\Magento\Backend\App\Action\Context $context,
30+
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
31+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
32+
\Magento\Framework\Filesystem $filesystem
33+
) {
34+
parent::__construct(
35+
$context
36+
);
37+
$this->fileFactory = $fileFactory;
38+
$this->resultRawFactory = $resultRawFactory;
39+
$this->fileDirectory = $filesystem->getDirectoryWrite(DirectoryList::MODULES);
40+
}
41+
42+
/**
43+
* Download sample file action
44+
*
45+
* @return void|\Magento\Backend\App\Action
46+
*/
47+
public function execute()
48+
{
49+
$fileName = $this->getRequest()->getParam('filename') . '.csv';
50+
$filePath = self::SAMPLE_FILES_DIRECTORY . $fileName;
51+
52+
if (!$this->fileDirectory->isFile($filePath)) {
53+
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
54+
$this->messageManager->addError(__('There is no sample file for this entity.'));
55+
$resultRedirect = $this->resultRedirectFactory->create();
56+
$resultRedirect->setPath('*/import');
57+
return $resultRedirect;
58+
}
59+
60+
$fileSize = isset($this->fileDirectory->stat($filePath)['size'])
61+
? $this->fileDirectory->stat($filePath)['size'] : null;
62+
63+
$this->fileFactory->create(
64+
$fileName,
65+
null,
66+
DirectoryList::VAR_DIR,
67+
'application/octet-stream',
68+
$fileSize
69+
);
70+
71+
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
72+
$resultRaw = $this->resultRawFactory->create();
73+
$resultRaw->setContents($this->fileDirectory->readFile($filePath));
74+
return $resultRaw;
75+
}
76+
}

app/code/Magento/ImportExport/Model/Import/AbstractEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ public function isRowAllowedToImport(array $rowData, $rowNumber)
677677
return $this->validateRow($rowData, $rowNumber) && !isset($this->_skippedRows[$rowNumber]);
678678
}
679679

680-
/*
680+
/**
681681
* Is import need to log in history.
682682
*
683683
* @return bool

app/code/Magento/ImportExport/view/adminhtml/templates/import/form/before.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require(['jquery', 'prototype'], function(jQuery){
3535
* Base url
3636
* @type {string}
3737
*/
38-
sampleFilesBaseUrl: '<?php echo $block->getBaseUrl() ?>' + 'pub/',
38+
sampleFilesBaseUrl: '<?php echo $block->getUrl('*/*/download/', ['filename' => 'entity-name']) ?>',
3939

4040
/**
4141
* Reset selected index
@@ -105,7 +105,7 @@ require(['jquery', 'prototype'], function(jQuery){
105105
showSampleFile: function(entityId) {
106106
var sampleFileSpan = jQuery('#sample-file-span');
107107
if (entityId) {
108-
var sampleFileLink = this.sampleFilesBaseUrl + entityId + '.csv';
108+
var sampleFileLink = this.sampleFilesBaseUrl.replace('entity-name', entityId);
109109
jQuery('#sample-file-link').attr('href', sampleFileLink);
110110
if (sampleFileSpan.is(':hidden')) {
111111
sampleFileSpan.show();

0 commit comments

Comments
 (0)