init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Backend;
|
||||
|
||||
use Esun\Catalog\Model\PrintingParameter\ExcelParser;
|
||||
use Magento\Config\Model\Config\Backend\File;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Module\Dir\Reader as ModuleDirReader;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
|
||||
class PrintingParameterExcel extends File
|
||||
{
|
||||
public function __construct(
|
||||
\Magento\Framework\Model\Context $context,
|
||||
\Magento\Framework\Registry $registry,
|
||||
\Magento\Framework\App\Config\ScopeConfigInterface $config,
|
||||
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
|
||||
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
|
||||
\Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData,
|
||||
Filesystem $filesystem,
|
||||
private readonly ExcelParser $excelParser,
|
||||
private readonly Json $json,
|
||||
private readonly ModuleDirReader $moduleDirReader,
|
||||
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
||||
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct(
|
||||
$context,
|
||||
$registry,
|
||||
$config,
|
||||
$cacheTypeList,
|
||||
$uploaderFactory,
|
||||
$requestData,
|
||||
$filesystem,
|
||||
$resource,
|
||||
$resourceCollection,
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
public function beforeSave()
|
||||
{
|
||||
$file = $this->getFileData();
|
||||
parent::beforeSave();
|
||||
|
||||
if (!$file || !(string)$this->getValue()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$sourcePath = $this->_mediaDirectory->getAbsolutePath('esun/catalog/printing_parameter/' . ltrim((string)$this->getValue(), '/'));
|
||||
$data = $this->excelParser->parse($sourcePath);
|
||||
$this->writeJson($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _getAllowedExtensions(): array
|
||||
{
|
||||
return ['xlsx'];
|
||||
}
|
||||
|
||||
private function writeJson(array $data): void
|
||||
{
|
||||
$moduleDir = $this->moduleDirReader->getModuleDir('', 'Esun_Catalog');
|
||||
$dataDir = $moduleDir . '/data';
|
||||
if (!is_dir($dataDir) && !mkdir($dataDir, 0775, true) && !is_dir($dataDir)) {
|
||||
throw new LocalizedException(__('Unable to create the Esun Catalog data directory.'));
|
||||
}
|
||||
|
||||
$json = $this->json->serialize($data);
|
||||
$path = $dataDir . '/printing-parameters.json';
|
||||
if (file_put_contents($path, json_encode(json_decode($json, true), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) === false) {
|
||||
throw new LocalizedException(__('Unable to write the printing parameter JSON file.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user