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.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Source;
|
||||
|
||||
use Esun\Catalog\Model\ResourceModel\CertificationType\CollectionFactory;
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class CertificationTypeOptions implements OptionSourceInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionFactory $collectionFactory
|
||||
) {
|
||||
}
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$options = [['value' => '', 'label' => __('Select a certification type')]];
|
||||
$collection = $this->collectionFactory->create()
|
||||
->addFieldToFilter('status', 1)
|
||||
->setOrder('sort_order', 'ASC')
|
||||
->setOrder('name', 'ASC');
|
||||
|
||||
foreach ($collection as $type) {
|
||||
$options[] = [
|
||||
'value' => (string)$type->getId(),
|
||||
'label' => (string)$type->getData('name'),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Source;
|
||||
|
||||
use Magento\Eav\Model\Config as EavConfig;
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class ColorOptionOptions implements OptionSourceInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EavConfig $eavConfig
|
||||
) {
|
||||
}
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$attribute = $this->eavConfig->getAttribute('catalog_product', 'color');
|
||||
$options = [];
|
||||
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
|
||||
$value = (int)($option['value'] ?? 0);
|
||||
if ($value <= 0) {
|
||||
continue;
|
||||
}
|
||||
$options[] = [
|
||||
'value' => $value,
|
||||
'label' => (string)($option['label'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Source;
|
||||
|
||||
use Magento\Customer\Model\Group;
|
||||
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class MixCustomerGroupOptions implements OptionSourceInterface
|
||||
{
|
||||
public function __construct(private readonly CollectionFactory $collectionFactory)
|
||||
{
|
||||
}
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$options = [
|
||||
['value' => Group::CUST_GROUP_ALL, 'label' => __('ALL GROUPS')],
|
||||
];
|
||||
$collection = $this->collectionFactory->create()->setOrder('customer_group_code', 'ASC');
|
||||
|
||||
foreach ($collection as $group) {
|
||||
$options[] = [
|
||||
'value' => (int)$group->getId(),
|
||||
'label' => (string)$group->getCode(),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Source;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class MixPriceTypeOptions implements OptionSourceInterface
|
||||
{
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
return [
|
||||
['value' => 'percent', 'label' => __('Discount Percentage')],
|
||||
['value' => 'fixed', 'label' => __('Fixed Unit Price')],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\Config\Source;
|
||||
|
||||
use Esun\Video\Model\ResourceModel\Video\CollectionFactory;
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class VideoOptions implements OptionSourceInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionFactory $collectionFactory
|
||||
) {
|
||||
}
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$options = [['value' => '', 'label' => __('Select a video')]];
|
||||
|
||||
foreach ($this->collectionFactory->create()->addFieldToFilter('status', 1)->setOrder('video_id', 'DESC') as $video) {
|
||||
$options[] = [
|
||||
'value' => (string)$video->getId(),
|
||||
'label' => sprintf('#%d - %s', (int)$video->getId(), (string)$video->getData('title')),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user