This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\CertificationType;
use Esun\Catalog\Model\CertificationTypeFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::certification_types_delete';
public function __construct(
Action\Context $context,
private readonly CertificationTypeFactory $typeFactory
) {
parent::__construct($context);
}
public function execute()
{
$redirect = $this->resultRedirectFactory->create();
try {
$type = $this->typeFactory->create()->load((int)$this->getRequest()->getParam('id'));
if (!$type->getId()) {
throw new LocalizedException(__('The certification type no longer exists.'));
}
$type->delete();
$this->messageManager->addSuccessMessage(__('The certification type has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $redirect->setPath('esun_catalog/certificationtype/index');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\CertificationType;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::certification_types';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::certification_types');
$page->getConfig()->getTitle()->prepend(__('Edit Product Certification Type'));
return $page;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\CertificationType;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::certification_types';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::certification_types');
$page->getConfig()->getTitle()->prepend(__('Product Certification Type Management'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\CertificationType;
class NewAction extends Edit
{
}
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\CertificationType;
use Esun\Catalog\Model\CertificationTypeFactory;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::certification_types_save';
private const ICON_PATH = 'esun/catalog/certification-type';
public function __construct(
Action\Context $context,
private readonly CertificationTypeFactory $typeFactory,
private readonly UploaderFactory $uploaderFactory,
private readonly Filesystem $filesystem
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('type_id');
$redirect = $this->resultRedirectFactory->create();
try {
$type = $this->typeFactory->create();
if ($id) {
$type->load($id);
if (!$type->getId()) {
throw new LocalizedException(__('The certification type no longer exists.'));
}
}
$name = trim((string)$this->getRequest()->getParam('name'));
if ($name === '') {
throw new LocalizedException(__('Certification Type Name is required.'));
}
$icon = (string)$type->getData('icon');
$deleteIcon = (bool)$this->getRequest()->getParam('delete_icon');
if ($deleteIcon) {
$icon = '';
}
$uploadedIcon = $this->uploadIcon();
if ($uploadedIcon !== '') {
$icon = $uploadedIcon;
}
$type->addData([
'name' => $name,
'icon' => $icon,
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int)$this->getRequest()->getParam('sort_order'),
])->save();
$this->messageManager->addSuccessMessage(__('The certification type has been saved.'));
return $redirect->setPath('esun_catalog/certificationtype/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath($id ? 'esun_catalog/certificationtype/edit' : 'esun_catalog/certificationtype/new', $id ? ['id' => $id] : []);
}
}
private function uploadIcon(): string
{
$file = $_FILES['icon'] ?? null;
if (!is_array($file) || empty($file['name']) || (int)($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) {
return '';
}
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$mediaDirectory->create(self::ICON_PATH);
$uploader = $this->uploaderFactory->create(['fileId' => 'icon']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg']);
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($mediaDirectory->getAbsolutePath(self::ICON_PATH));
if (empty($result['file'])) {
return '';
}
return self::ICON_PATH . '/' . ltrim((string)$result['file'], '/');
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ColorOption;
use Esun\Catalog\Model\ColorOptionFactory;
use Esun\Catalog\Model\ColorOption\CacheInvalidator;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::color_options_delete';
public function __construct(
Action\Context $context,
private readonly ColorOptionFactory $colorOptionFactory,
private readonly CacheInvalidator $cacheInvalidator
) {
parent::__construct($context);
}
public function execute()
{
$redirect = $this->resultRedirectFactory->create();
try {
$item = $this->colorOptionFactory->create()->load((int)$this->getRequest()->getParam('id'));
if (!$item->getId()) {
throw new LocalizedException(__('The color option no longer exists.'));
}
$item->delete();
$this->cacheInvalidator->invalidate();
$this->messageManager->addSuccessMessage(__('The color option has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $redirect->setPath('esun_catalog/coloroption/index');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ColorOption;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::color_options';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::color_options');
$page->getConfig()->getTitle()->prepend(__('Edit Color Option'));
return $page;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ColorOption;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::color_options';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::color_options');
$page->getConfig()->getTitle()->prepend(__('Color Option Management'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ColorOption;
class NewAction extends Edit
{
}
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ColorOption;
use Esun\Catalog\Model\ColorOptionFactory;
use Esun\Catalog\Model\ColorOptionImageUploader;
use Esun\Catalog\Model\ColorOption\CacheInvalidator;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::color_options_save';
public function __construct(
Action\Context $context,
private readonly ColorOptionFactory $colorOptionFactory,
private readonly ColorOptionImageUploader $imageUploader,
private readonly CacheInvalidator $cacheInvalidator
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('entity_id');
$redirect = $this->resultRedirectFactory->create();
try {
$item = $this->colorOptionFactory->create();
if ($id) {
$item->load($id);
if (!$item->getId()) {
throw new LocalizedException(__('The color option no longer exists.'));
}
}
$optionId = (int)$this->getRequest()->getParam('option_id');
if ($optionId <= 0) {
throw new LocalizedException(__('Color Option is required.'));
}
$ballImage = (string)$item->getData('ball_image');
if ((bool)$this->getRequest()->getParam('delete_ball_image')) {
$ballImage = '';
}
$uploadedImage = $this->imageUploader->upload('ball_image');
if ($uploadedImage !== '') {
$ballImage = $uploadedImage;
}
$item->addData([
'option_id' => $optionId,
'display_name' => trim((string)$this->getRequest()->getParam('display_name')),
'swatch_colors' => $this->normalizeSwatchColors((string)$this->getRequest()->getParam('swatch_colors')),
'ball_image' => $ballImage,
'is_hot_sale' => (int)$this->getRequest()->getParam('is_hot_sale') === 1 ? 1 : 0,
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int)$this->getRequest()->getParam('sort_order'),
])->save();
$this->cacheInvalidator->invalidate();
$this->messageManager->addSuccessMessage(__('The color option has been saved.'));
return $redirect->setPath('esun_catalog/coloroption/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath($id ? 'esun_catalog/coloroption/edit' : 'esun_catalog/coloroption/new', $id ? ['id' => $id] : []);
}
}
private function normalizeSwatchColors(string $value): string
{
$parts = preg_split('/[\s,;]+/', trim($value)) ?: [];
$colors = [];
foreach ($parts as $part) {
$color = trim($part);
if ($color === '') {
continue;
}
if (!str_starts_with($color, '#')) {
$color = '#' . $color;
}
if (!preg_match('/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/', $color)) {
throw new LocalizedException(__('Invalid swatch color "%1". Use hex values such as #FF0000.', $part));
}
$colors[] = strtoupper($color);
}
return implode(',', array_values(array_unique($colors)));
}
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Faq;
use Esun\Catalog\Model\FaqFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::faqs_delete';
public function __construct(
Action\Context $context,
private readonly FaqFactory $faqFactory
) {
parent::__construct($context);
}
public function execute()
{
$redirect = $this->resultRedirectFactory->create();
try {
$faq = $this->faqFactory->create()->load((int)$this->getRequest()->getParam('id'));
if (!$faq->getId()) {
throw new LocalizedException(__('The FAQ no longer exists.'));
}
$faq->delete();
$this->messageManager->addSuccessMessage(__('The FAQ has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $redirect->setPath('esun_catalog/faq/index');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Faq;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::faqs';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::faqs');
$page->getConfig()->getTitle()->prepend(__('Edit Product FAQ'));
return $page;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Faq;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::faqs';
public function __construct(
Action\Context $context,
private readonly PageFactory $resultPageFactory
) {
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->setActiveMenu('Esun_Catalog::faqs');
$page->getConfig()->getTitle()->prepend(__('Product FAQ'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Faq;
class NewAction extends Edit
{
}
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Faq;
use Esun\Catalog\Model\FaqFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::faqs_save';
public function __construct(
Action\Context $context,
private readonly FaqFactory $faqFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('faq_id');
$redirect = $this->resultRedirectFactory->create();
try {
$faq = $this->faqFactory->create();
if ($id) {
$faq->load($id);
if (!$faq->getId()) {
throw new LocalizedException(__('The FAQ no longer exists.'));
}
}
$title = trim((string)$this->getRequest()->getParam('title'));
if ($title === '') {
throw new LocalizedException(__('Title is required.'));
}
$storeIds = (array)$this->getRequest()->getParam('store_ids', [0]);
$storeIds = array_map('intval', $storeIds);
if (!$storeIds) {
$storeIds = [0];
}
$faq->addData([
'title' => $title,
'content' => (string)$this->getRequest()->getParam('content'),
'store_ids' => implode(',', array_unique($storeIds)),
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int)$this->getRequest()->getParam('sort_order'),
])->save();
$this->messageManager->addSuccessMessage(__('The FAQ has been saved.'));
return $redirect->setPath('esun_catalog/faq/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath($id ? 'esun_catalog/faq/edit' : 'esun_catalog/faq/new', $id ? ['id' => $id] : []);
}
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_delete';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->setActiveMenu(self::ADMIN_RESOURCE);
$page->getConfig()->getTitle()->prepend(__('Batch Delete Mix Step Pricing'));
return $page;
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\TemplateGenerator;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\RawFactory;
class DownloadDeleteTemplate extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_delete';
public function __construct(
Action\Context $context,
private readonly TemplateGenerator $templateGenerator,
private readonly RawFactory $rawFactory
) {
parent::__construct($context);
}
public function execute()
{
return $this->rawFactory->create()
->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->setHeader('Content-Disposition', 'attachment; filename="mix_step_pricing_delete.xlsx"')
->setContents($this->templateGenerator->createDelete());
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\TemplateGenerator;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\RawFactory;
class DownloadImportTemplate extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_import';
public function __construct(
Action\Context $context,
private readonly TemplateGenerator $templateGenerator,
private readonly RawFactory $rawFactory
) {
parent::__construct($context);
}
public function execute()
{
return $this->rawFactory->create()
->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->setHeader('Content-Disposition', 'attachment; filename="mix_step_pricing_import.xlsx"')
->setContents($this->templateGenerator->createImport());
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Import extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_import';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->setActiveMenu(self::ADMIN_RESOURCE);
$page->getConfig()->getTitle()->prepend(__('Batch Import Mix Step Pricing'));
return $page;
}
}
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\DeleteService;
use Esun\Catalog\Model\MixStepPricing\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverInterface;
class RunDelete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_delete';
public function __construct(
Action\Context $context,
private readonly DeleteService $deleteService,
private readonly ValidationState $validationState,
private readonly DriverInterface $filesystemDriver
) {
parent::__construct($context);
}
public function execute()
{
$state = $this->validationState->get('delete');
try {
if ((string)$this->getRequest()->getParam('confirm_delete') !== '1') {
throw new LocalizedException(__('Confirm that you understand this operation deletes all tiers.'));
}
$path = (string)($state['path'] ?? '');
if (empty($state['valid']) || $path === '' || !$this->filesystemDriver->isFile($path)) {
throw new LocalizedException(__('Validate an Excel file before running the delete.'));
}
$stats = $this->deleteService->run($path);
$this->validationState->clear('delete');
$this->filesystemDriver->deleteFile($path);
$this->messageManager->addSuccessMessage(__(
'Deleted Mix Step Pricing for %1 sku + website groups across %2 Store Views.',
$stats['groups'],
$stats['store_views']
));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/delete');
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\ImportService;
use Esun\Catalog\Model\MixStepPricing\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverInterface;
class RunImport extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_import';
public function __construct(
Action\Context $context,
private readonly ImportService $importService,
private readonly ValidationState $validationState,
private readonly DriverInterface $filesystemDriver
) {
parent::__construct($context);
}
public function execute()
{
$state = $this->validationState->get('import');
try {
$path = (string)($state['path'] ?? '');
if (empty($state['valid']) || $path === '' || !$this->filesystemDriver->isFile($path)) {
throw new LocalizedException(__('Validate an Excel file before running the import.'));
}
$stats = $this->importService->run($path);
$this->validationState->clear('import');
$this->filesystemDriver->deleteFile($path);
$this->messageManager->addSuccessMessage(__(
'Imported %1 sku + website groups across %2 Store Views.',
$stats['groups'],
$stats['store_views']
));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/import');
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\DeleteService;
use Esun\Catalog\Model\MixStepPricing\FileUploader;
use Esun\Catalog\Model\MixStepPricing\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
class ValidateDelete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_delete';
public function __construct(
Action\Context $context,
private readonly FileUploader $fileUploader,
private readonly DeleteService $deleteService,
private readonly ValidationState $validationState
) {
parent::__construct($context);
}
public function execute()
{
$this->validationState->clear('delete');
try {
$path = $this->fileUploader->upload('excel_file');
$result = $this->deleteService->validate($path);
$this->validationState->save('delete', $path, $result);
if ($result['valid']) {
$this->messageManager->addSuccessMessage(__('Delete validation passed. Review the preview carefully.'));
}
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/delete');
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\MixStepPricing;
use Esun\Catalog\Model\MixStepPricing\FileUploader;
use Esun\Catalog\Model\MixStepPricing\ImportService;
use Esun\Catalog\Model\MixStepPricing\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
class ValidateImport extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::mix_step_pricing_import';
public function __construct(
Action\Context $context,
private readonly FileUploader $fileUploader,
private readonly ImportService $importService,
private readonly ValidationState $validationState
) {
parent::__construct($context);
}
public function execute()
{
$this->validationState->clear('import');
try {
$path = $this->fileUploader->upload('excel_file');
$result = $this->importService->validate($path);
$this->validationState->save('import', $path, $result);
if ($result['valid']) {
$this->messageManager->addSuccessMessage(
__('Import validation passed. Review the preview before running the import.')
);
}
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/import');
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Product\Certification;
use Esun\Catalog\Model\CertificationFileUploader;
use Magento\Backend\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
class Upload extends Action implements HttpPostActionInterface
{
public const ADMIN_RESOURCE = 'Magento_Catalog::products';
public function __construct(
Action\Context $context,
private readonly CertificationFileUploader $fileUploader
) {
parent::__construct($context);
}
public function execute()
{
$fileId = (string)$this->getRequest()->getParam('param_name', 'file');
try {
$result = $this->fileUploader->saveFileToTmpDir($fileId);
} catch (\Exception $exception) {
$result = ['error' => $exception->getMessage(), 'errorcode' => $exception->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\Product\Image;
use Magento\Backend\App\Action;
use Magento\Catalog\Model\ImageUploader;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
class Upload extends Action implements HttpPostActionInterface
{
public const ADMIN_RESOURCE = 'Magento_Catalog::products';
public function __construct(
Action\Context $context,
private readonly ImageUploader $imageUploader
) {
parent::__construct($context);
}
public function execute()
{
$imageId = (string)$this->getRequest()->getParam('param_name', 'image');
try {
$result = $this->imageUploader->saveFileToTmpDir($imageId);
} catch (\Exception $exception) {
$result = ['error' => $exception->getMessage(), 'errorcode' => $exception->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ProductRecommendation;
use Esun\Catalog\Model\ProductRecommendation\TemplateGenerator;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\RawFactory;
class DownloadTemplate extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::product_recommendation_import';
public function __construct(
Action\Context $context,
private readonly TemplateGenerator $templateGenerator,
private readonly RawFactory $rawFactory
) {
parent::__construct($context);
}
public function execute()
{
return $this->rawFactory->create()
->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->setHeader('Content-Disposition', 'attachment; filename="product_recommendations.xlsx"')
->setContents($this->templateGenerator->create());
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ProductRecommendation;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Import extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::product_recommendation_import';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->setActiveMenu(self::ADMIN_RESOURCE);
$page->getConfig()->getTitle()->prepend(__('Import Product Recommendations'));
return $page;
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ProductRecommendation;
use Esun\Catalog\Model\ProductRecommendation\ImportService;
use Esun\Catalog\Model\ProductRecommendation\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverInterface;
class RunImport extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::product_recommendation_import';
public function __construct(
Action\Context $context,
private readonly ImportService $importService,
private readonly ValidationState $validationState,
private readonly DriverInterface $filesystemDriver
) {
parent::__construct($context);
}
public function execute()
{
$state = $this->validationState->get();
try {
$path = (string)($state['path'] ?? '');
if (empty($state['valid']) || $path === '' || !$this->filesystemDriver->isFile($path)) {
throw new LocalizedException(__('Validate an Excel file before running the import.'));
}
$stats = $this->importService->run($path);
$this->validationState->clear();
$this->filesystemDriver->deleteFile($path);
$this->messageManager->addSuccessMessage(__(
'Replaced %1 product + website recommendation sets with %2 recommendations.',
$stats['groups'],
$stats['recommendations']
));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/import');
}
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\ProductRecommendation;
use Esun\Catalog\Model\ProductRecommendation\FileUploader;
use Esun\Catalog\Model\ProductRecommendation\ImportService;
use Esun\Catalog\Model\ProductRecommendation\ValidationState;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
class ValidateImport extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::product_recommendation_import';
public function __construct(
Action\Context $context,
private readonly FileUploader $fileUploader,
private readonly ImportService $importService,
private readonly ValidationState $validationState
) {
parent::__construct($context);
}
public function execute()
{
$this->validationState->clear();
try {
$path = $this->fileUploader->upload('excel_file');
$result = $this->importService->validate($path);
$this->validationState->save($path, $result);
if ($result['valid']) {
$this->messageManager->addSuccessMessage(
__('Validation passed. Review the complete replacement preview before importing.')
);
} else {
foreach (array_slice((array)$result['errors'], 0, 20) as $error) {
$this->messageManager->addErrorMessage($error);
}
}
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/import');
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationBrand;
use Esun\Catalog\Model\SpecificationBrandFactory;
use Esun\Catalog\Model\SpecificationMedia;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_brands_delete';
public function __construct(
Action\Context $context,
private readonly SpecificationBrandFactory $brandFactory,
private readonly SpecificationMedia $media
) {
parent::__construct($context);
}
public function execute()
{
try {
$brand = $this->brandFactory->create()->load((int)$this->getRequest()->getParam('id'));
if (!$brand->getId()) {
throw new LocalizedException(__('The brand no longer exists.'));
}
$logo = (string)$brand->getData('brand_logo');
$brandId = (int)$brand->getId();
$brand->delete();
$this->media->delete($logo);
$this->media->deleteBrandFiles($brandId);
$this->messageManager->addSuccessMessage(__('The brand has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_catalog/specificationbrand/index');
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationBrand;
use Esun\Catalog\Model\SpecificationBrandFactory;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_brands_save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly SpecificationBrandFactory $brandFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('id');
if ($id > 0 && !$this->brandFactory->create()->load($id)->getId()) {
$this->messageManager->addErrorMessage(__('The brand no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('esun_catalog/specificationbrand/index');
}
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Catalog::specification_brands');
$page->getConfig()->getTitle()->prepend($id ? __('Edit Specification Brand') : __('New Specification Brand'));
return $page;
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationBrand;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_brands';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Catalog::specification_brands');
$page->getConfig()->getTitle()->prepend(__('Download Specification Brands'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationBrand;
class NewAction extends Edit
{
}
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationBrand;
use Esun\Catalog\Model\SpecificationBrandFactory;
use Esun\Catalog\Model\SpecificationMedia;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_brands_save';
public function __construct(
Action\Context $context,
private readonly SpecificationBrandFactory $brandFactory,
private readonly SpecificationMedia $media
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('brand_id');
$redirect = $this->resultRedirectFactory->create();
try {
$brand = $this->brandFactory->create();
if ($id > 0) {
$brand->load($id);
if (!$brand->getId()) {
throw new LocalizedException(__('The brand no longer exists.'));
}
}
$brandName = trim((string)$this->getRequest()->getParam('brand_name'));
if ($brandName === '') {
throw new LocalizedException(__('Brand Name is required.'));
}
$oldLogo = (string)$brand->getData('brand_logo');
$logo = $oldLogo;
if ($this->getRequest()->getParam('delete_logo')) {
$logo = '';
}
$newLogo = $this->media->saveLogo('brand_logo');
if ($newLogo !== null) {
$logo = $newLogo;
}
$brand->addData([
'brand_name' => $brandName,
'brand_logo' => $logo,
'is_product_specific' => $this->getRequest()->getParam('is_product_specific') === '0' ? 0 : 1,
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int)$this->getRequest()->getParam('sort_order'),
])->save();
if ($oldLogo !== '' && $oldLogo !== $logo) {
$this->media->delete($oldLogo);
}
$this->messageManager->addSuccessMessage(__('The brand has been saved.'));
return $redirect->setPath('esun_catalog/specificationbrand/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath(
$id ? 'esun_catalog/specificationbrand/edit' : 'esun_catalog/specificationbrand/new',
$id ? ['id' => $id] : []
);
}
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationImport;
use Esun\Catalog\Model\SpecificationImportFactory;
use Esun\Catalog\Model\SpecificationMedia;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_imports_delete';
public function __construct(
Action\Context $context,
private readonly SpecificationImportFactory $importFactory,
private readonly SpecificationMedia $media
) {
parent::__construct($context);
}
public function execute()
{
try {
$import = $this->importFactory->create()->load((int)$this->getRequest()->getParam('id'));
if (!$import->getId()) {
throw new LocalizedException(__('The import no longer exists.'));
}
$brandId = (int)$import->getData('brand_id');
$archive = (string)$import->getData('archive_file');
$import->delete();
$this->media->delete($archive);
$this->media->deleteBrandParsedFiles($brandId);
$this->messageManager->addSuccessMessage(__('The import has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_catalog/specificationimport/index');
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationImport;
use Esun\Catalog\Model\SpecificationImportFactory;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_imports_save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly SpecificationImportFactory $importFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('id');
if ($id > 0 && !$this->importFactory->create()->load($id)->getId()) {
$this->messageManager->addErrorMessage(__('The import no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('esun_catalog/specificationimport/index');
}
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Catalog::specification_imports');
$page->getConfig()->getTitle()->prepend($id ? __('Edit Specifications ZIP') : __('Upload Specifications ZIP'));
return $page;
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationImport;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_imports';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Catalog::specification_imports');
$page->getConfig()->getTitle()->prepend(__('Specifications Management'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationImport;
class NewAction extends Edit
{
}
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Esun\Catalog\Controller\Adminhtml\SpecificationImport;
use Esun\Catalog\Model\SpecificationBrandFactory;
use Esun\Catalog\Model\SpecificationImportFactory;
use Esun\Catalog\Model\SpecificationMedia;
use Esun\Catalog\Model\SpecificationZipImporter;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Catalog::specification_imports_save';
public function __construct(
Action\Context $context,
private readonly SpecificationBrandFactory $brandFactory,
private readonly SpecificationImportFactory $importFactory,
private readonly SpecificationMedia $media,
private readonly SpecificationZipImporter $zipImporter
) {
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('import_id');
$redirect = $this->resultRedirectFactory->create();
try {
$brandId = (int)$this->getRequest()->getParam('brand_id');
$brand = $this->brandFactory->create()->load($brandId);
if (!$brand->getId()) {
throw new LocalizedException(__('Please select a valid brand.'));
}
$archivePath = null;
if ($this->media->hasUpload('archive_file')) {
$this->media->deleteBrandArchiveFiles($brandId);
$archivePath = $this->media->saveArchive('archive_file', $brandId);
}
if ($archivePath === null) {
if ($id <= 0) {
throw new LocalizedException(__('Please upload a ZIP file.'));
}
$import = $this->importFactory->create()->load($id);
$archivePath = (string)$import->getData('archive_file');
}
$import = $this->zipImporter->import($brand, $archivePath);
$this->messageManager->addSuccessMessage(
__('The ZIP file has been imported. %1 file(s) were parsed.', (int)$import->getData('file_count'))
);
return $redirect->setPath('esun_catalog/specificationimport/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath(
$id ? 'esun_catalog/specificationimport/edit' : 'esun_catalog/specificationimport/new',
$id ? ['id' => $id] : []
);
}
}
}