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,46 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Category;
use Esun\ImageManager\Model\CategoryFactory;
use Esun\ImageManager\Model\ResourceModel\Image\CollectionFactory as ImageCollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::categories_delete';
public function __construct(
Action\Context $context,
private readonly CategoryFactory $categoryFactory,
private readonly ImageCollectionFactory $imageCollectionFactory
) {
parent::__construct($context);
}
public function execute()
{
try {
$category = $this->categoryFactory->create()->load((int) $this->getRequest()->getParam('id'));
if (!$category->getId()) {
throw new LocalizedException(__('The category no longer exists.'));
}
$imageCount = $this->imageCollectionFactory->create()
->addFieldToFilter('main_table.category_id', (int) $category->getId())
->getSize();
if ($imageCount > 0) {
throw new LocalizedException(
__('This category contains %1 image(s). Move or delete them first.', $imageCount)
);
}
$category->delete();
$this->messageManager->addSuccessMessage(__('The image category has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_image_manager/category/index');
}
}
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Category;
use Esun\ImageManager\Model\CategoryFactory;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::categories_save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly CategoryFactory $categoryFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('id');
if ($id > 0 && !$this->categoryFactory->create()->load($id)->getId()) {
$this->messageManager->addErrorMessage(__('The category no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('esun_image_manager/category/index');
}
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend($id ? __('Edit Image Category') : __('New Image Category'));
return $page;
}
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Category;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::categories';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend(__('Image Categories'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Category;
class NewAction extends Edit
{
}
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Category;
use Esun\ImageManager\Model\CategoryFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::categories_save';
public function __construct(Action\Context $context, private readonly CategoryFactory $categoryFactory)
{
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('category_id');
$redirect = $this->resultRedirectFactory->create();
try {
$category = $this->categoryFactory->create();
if ($id > 0) {
$category->load($id);
if (!$category->getId()) {
throw new LocalizedException(__('The category no longer exists.'));
}
}
$name = trim((string) $this->getRequest()->getParam('name'));
$identifier = strtolower(trim((string) $this->getRequest()->getParam('identifier')));
$identifier = trim((string) preg_replace('/[^a-z0-9_-]+/', '-', $identifier), '-');
if ($name === '' || $identifier === '') {
throw new LocalizedException(__('Name and identifier are required.'));
}
$category->addData([
'name' => $name,
'identifier' => $identifier,
'description' => trim((string) $this->getRequest()->getParam('description')),
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int) $this->getRequest()->getParam('sort_order'),
])->save();
$this->messageManager->addSuccessMessage(__('The image category has been saved.'));
return $redirect->setPath('esun_image_manager/category/index');
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath(
$id ? 'esun_image_manager/category/edit' : 'esun_image_manager/category/new',
$id ? ['id' => $id] : []
);
}
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
use Esun\ImageManager\Model\ImageFactory;
use Esun\ImageManager\Model\MediaUploader;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::images_delete';
public function __construct(
Action\Context $context,
private readonly ImageFactory $imageFactory,
private readonly MediaUploader $mediaUploader
) {
parent::__construct($context);
}
public function execute()
{
try {
$image = $this->imageFactory->create()->load((int) $this->getRequest()->getParam('id'));
if (!$image->getId()) {
throw new LocalizedException(__('The image no longer exists.'));
}
$paths = [
(string) $image->getData('thumbnail_path'),
(string) $image->getData('image_path'),
(string) $image->getData('icon_path'),
];
$image->delete();
foreach ($paths as $path) {
$this->mediaUploader->delete($path);
}
$this->messageManager->addSuccessMessage(__('The image has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_image_manager/image/index');
}
}
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
use Esun\ImageManager\Model\ImageFactory;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::images_save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly ImageFactory $imageFactory
) {
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('id');
if ($id > 0 && !$this->imageFactory->create()->load($id)->getId()) {
$this->messageManager->addErrorMessage(__('The image no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('esun_image_manager/image/index');
}
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend($id ? __('Edit Image') : __('New Image'));
return $page;
}
}
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::images';
public function __construct(Action\Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute()
{
$page = $this->pageFactory->create();
$page->getConfig()->getTitle()->prepend(__('Image Manager'));
return $page;
}
}
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
class NewAction extends Edit
{
}
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
use Esun\ImageManager\Model\CategoryFactory;
use Esun\ImageManager\Model\ImageFactory;
use Esun\ImageManager\Model\MediaUploader;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::images_save';
public function __construct(
Action\Context $context,
private readonly ImageFactory $imageFactory,
private readonly CategoryFactory $categoryFactory,
private readonly MediaUploader $mediaUploader
) {
parent::__construct($context);
}
public function execute()
{
$id = (int) $this->getRequest()->getParam('image_id');
$redirect = $this->resultRedirectFactory->create();
$uploadedPaths = [];
try {
$image = $this->imageFactory->create();
if ($id > 0) {
$image->load($id);
if (!$image->getId()) {
throw new LocalizedException(__('The image no longer exists.'));
}
}
$title = trim((string) $this->getRequest()->getParam('title'));
$categoryId = (int) $this->getRequest()->getParam('category_id');
if ($title === '' || !$this->categoryFactory->create()->load($categoryId)->getId()) {
throw new LocalizedException(__('Title and a valid category are required.'));
}
$oldPaths = [];
foreach (['thumbnail', 'image', 'icon'] as $field) {
$pathField = $field . '_path';
$oldPaths[$field] = (string) $image->getData($pathField);
$newPath = $this->mediaUploader->save($field);
if ($newPath !== null) {
$uploadedPaths[$field] = $newPath;
$image->setData($pathField, $newPath);
} elseif ($this->getRequest()->getParam('delete_' . $field)) {
$image->setData($pathField, '');
}
}
$image->addData([
'category_id' => $categoryId,
'title' => $title,
'description' => trim((string) $this->getRequest()->getParam('description')),
'link_url' => trim((string) $this->getRequest()->getParam('link_url')),
'status' => $this->getRequest()->getParam('status') === '0' ? 0 : 1,
'sort_order' => (int) $this->getRequest()->getParam('sort_order'),
])->save();
foreach ($oldPaths as $field => $oldPath) {
$currentPath = (string) $image->getData($field . '_path');
if ($oldPath !== '' && $oldPath !== $currentPath) {
$this->mediaUploader->delete($oldPath);
}
}
$this->messageManager->addSuccessMessage(__('The image has been saved.'));
return $redirect->setPath('esun_image_manager/image/index');
} catch (\Throwable $e) {
foreach ($uploadedPaths as $path) {
$this->mediaUploader->delete($path);
}
$this->messageManager->addErrorMessage($e->getMessage());
return $redirect->setPath(
$id ? 'esun_image_manager/image/edit' : 'esun_image_manager/image/new',
$id ? ['id' => $id] : []
);
}
}
}