Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Api/ProductManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ interface ProductManagementInterface
/**
* Get product
*
* @param string $sku
* @param mixed $sku
* @param string $storeCode
* @return mixed
*/
public function getProduct(string $sku, string $storeCode);
public function getProduct($sku, string $storeCode);
}
20 changes: 20 additions & 0 deletions Block/Quickorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public function getAddToCartUrl()
return $this->getUrl('quickorder/index/addtocart');
}

/**
* Get download list url
*
* @return string
*/
public function getDownloadListUrl()
{
return $this->getUrl('quickorder/index/getlist');
}

/**
* Get upload file url
*
Expand All @@ -155,6 +165,16 @@ public function getUploadFileUrl()
return $this->getUrl('quickorder/index/upload');
}

/**
* Get csv uploader url
*
* @return string
*/
public function getCsvUploaderUrl()
{
return $this->getUrl('quickorder/index/uploadcsv');
}

/**
* Get form key
*
Expand Down
94 changes: 94 additions & 0 deletions Controller/Index/GetList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright (c) 2025 BroSolutions
* All rights reserved
*
* This product includes proprietary software developed at BroSolutions, Ukraine
* For more information see https://www.brosolutions.net/
*
* To obtain a valid license for using this software please contact us at
* contact@brosolutions.net
*/
declare(strict_types=1);

namespace BroSolutions\QuickOrder\Controller\Index;

use BroSolutions\QuickOrder\Model\GetListManagement;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;

/**
* @copyright Copyright (c) 2025 BroSolutions
* @link https://www.brosolutions.net/
*/
class GetList implements HttpPostActionInterface
{
/**
* @var Http
*/
private $request;

/**
* @var FormKeyValidator
*/
private $formKeyValidator;

/**
* @var GetListManagement
*/
private $getListManagement;

/**
* @var FileFactory
*/
private $fileFactory;

/**
* @param Context $context
* @param Http $request
* @param FormKeyValidator $formKeyValidator
* @param GetListManagement $getListManagement
* @param FileFactory $fileFactory
* @param SerializerInterface $serializer
*/
public function __construct(
Context $context,
Http $request,
FormKeyValidator $formKeyValidator,
GetListManagement $getListManagement,
FileFactory $fileFactory,
SerializerInterface $serializer
) {
$this->request = $request;
$this->formKeyValidator = $formKeyValidator;
$this->getListManagement = $getListManagement;
$this->fileFactory = $fileFactory;
}

/**
* @return ResponseInterface|null
*/
public function execute(): ?ResponseInterface
{
if (!$this->formKeyValidator->validate($this->request)) {
return null;
}

try {
$data = $this->getListManagement->getList($this->request->getParam('jsonData'));

return $this->fileFactory->create($data['csvFileName'], $data['content'], $data['baseDir']);
} catch (Exception | LocalizedException| NoSuchEntityException $e) {

return null;
}
}
}
174 changes: 174 additions & 0 deletions Controller/Index/UploadCsv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* Copyright (c) 2025 BroSolutions
* All rights reserved
*
* This product includes proprietary software developed at BroSolutions, Ukraine
* For more information see https://www.brosolutions.net/
*
* To obtain a valid license for using this software please contact us at
* contact@brosolutions.net
*/
declare(strict_types=1);

namespace BroSolutions\QuickOrder\Controller\Index;

use BroSolutions\QuickOrder\Model\ProductManagement;
use BroSolutions\QuickOrder\Service\CsvProductParser;
use BroSolutions\QuickOrder\Service\FileUploader;
use BroSolutions\QuickOrder\Service\FilterProducts;
use Exception;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
use Magento\Framework\Filesystem;

/**
* @copyright Copyright (c) 2025 BroSolutions
* @link https://www.brosolutions.net/
*/
class UploadCsv implements HttpPostActionInterface
{
/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @var Http
*/
private $request;

/**
* @var FormKeyValidator
*/
private $formKeyValidator;

/**
* @var FileUploader
*/
private $fileUploader;

/**
* @var CsvProductParser
*/
private $csvProductParser;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @var ProductManagement
*/
private $productManagement;

/**
* @var FilterProducts
*/
private $filterProducts;

/**
* @param Http $request
* @param JsonFactory $resultJsonFactory
* @param FormKeyValidator $formKeyValidator
* @param FileUploader $fileUploader
* @param CsvProductParser $csvProductParser
* @param Filesystem $filesystem
* @param ProductManagement $productManagement
* @param FilterProducts $filterProducts
*/
public function __construct(
Http $request,
JsonFactory $resultJsonFactory,
FormKeyValidator $formKeyValidator,
FileUploader $fileUploader,
CsvProductParser $csvProductParser,
Filesystem $filesystem,
ProductManagement $productManagement,
FilterProducts $filterProducts
) {
$this->request = $request;
$this->resultJsonFactory = $resultJsonFactory;
$this->formKeyValidator = $formKeyValidator;
$this->fileUploader = $fileUploader;
$this->csvProductParser = $csvProductParser;
$this->filesystem = $filesystem;
$this->productManagement = $productManagement;
$this->filterProducts = $filterProducts;
}

/**
* Add to cart
*
* @return ResultInterface
*/
public function execute(): ResultInterface
{
$resultJson = $this->resultJsonFactory->create();
if (!$this->formKeyValidator->validate($this->request)) {
return $resultJson->setData(
[
'success' => false,
'message' => __('Invalid Form Key. Please refresh the page.')
]
);
}

try {

$file = $this->request->getFiles('csv');
if (!$file || empty($file['tmp_name'])) {
return $resultJson->setData(
[
'success' => false,
'message' => __('No file uploaded.')
]
);
}

// basic MIME/extension sanity checks
$allowed = ['text/csv', 'text/plain', 'application/vnd.ms-excel'];
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (!in_array($file['type'], $allowed) || !in_array(strtolower($ext), ['csv'])) {

return $resultJson->setData(
[
'success' => false,
'message' => __('Please upload a .csv file.')
]
);

}

// make a unique file name
$filename = preg_replace('/[^A-Za-z0-9_\.\-]/', '_', $file['name']);

$filePath = $this->fileUploader->execute($filename, 'csv', DirectoryList::VAR_DIR);
$varDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
$filePath = $varDir->getAbsolutePath($filePath);
$csvFileContentArray = $this->csvProductParser->execute($filePath);

$products = $this->productManagement->getProduct($csvFileContentArray, $this->request->getParam('storeCode'));

$filteredProducts = $this->filterProducts->execute($products, $csvFileContentArray);

} catch (Exception $e) {
return $resultJson->setData(
[
'success' => false,
'message' => $e->getMessage()
]
);
}

return $resultJson->setData([
'success' => true,
'products' => $filteredProducts
]);
}
}
Loading