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,35 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Esun\Agent\Model\AgentFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::delete';
public function __construct(Action\Context $context, private readonly AgentFactory $agentFactory)
{
parent::__construct($context);
}
public function execute(): Redirect
{
$id = (int) $this->getRequest()->getParam('entity_id');
try {
$agent = $this->agentFactory->create()->load($id);
if (!$agent->getId()) {
throw new \RuntimeException('Agent not found.');
}
$agent->delete();
$this->messageManager->addSuccessMessage(__('The agent has been deleted.'));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage(__('The agent could not be deleted.'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Esun\Agent\Model\AgentFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
class Edit extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::save';
public function __construct(
Action\Context $context,
private readonly PageFactory $pageFactory,
private readonly AgentFactory $agentFactory
) {
parent::__construct($context);
}
public function execute(): Page|Redirect
{
$id = (int) $this->getRequest()->getParam('entity_id');
$agent = $this->agentFactory->create();
if ($id) {
$agent->load($id);
if (!$agent->getId()) {
$this->messageManager->addErrorMessage(__('This agent no longer exists.'));
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Agent::agents');
$page->getConfig()->getTitle()->prepend($id ? __('Edit Agent: %1', $agent->getData('name')) : __('New Agent'));
return $page;
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::agents';
public function __construct(Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute(): Page
{
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Agent::agents');
$page->getConfig()->getTitle()->prepend(__('Agents'));
return $page;
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Esun\Agent\Model\ResourceModel\Agent\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Ui\Component\MassAction\Filter;
class MassDelete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::delete';
public function __construct(
Action\Context $context,
private readonly Filter $filter,
private readonly CollectionFactory $collectionFactory
) {
parent::__construct($context);
}
public function execute(): Redirect
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$deleted = 0;
foreach ($collection as $agent) {
$agent->delete();
$deleted++;
}
$this->messageManager->addSuccessMessage(__('A total of %1 agent(s) have been deleted.', $deleted));
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;
class NewAction extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::save';
public function __construct(Action\Context $context, private readonly ForwardFactory $forwardFactory)
{
parent::__construct($context);
}
public function execute(): Forward
{
return $this->forwardFactory->create()->forward('edit');
}
}
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Esun\Agent\Model\AgentFactory;
use Esun\Agent\Model\Map\CountryPosition;
use Magento\Backend\App\Action;
use Magento\Catalog\Model\ImageUploader;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
class Save extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::save';
public function __construct(
Action\Context $context,
private readonly AgentFactory $agentFactory,
private readonly CountryPosition $countryPosition,
private readonly CountryFactory $countryFactory,
private readonly DataPersistorInterface $dataPersistor,
private readonly ImageUploader $imageUploader
) {
parent::__construct($context);
}
public function execute(): Redirect
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = (array) $this->getRequest()->getPostValue();
if ($data === []) {
return $resultRedirect->setPath('*/*/');
}
$id = (int) ($data['entity_id'] ?? 0);
$agent = $this->agentFactory->create();
if ($id) {
$agent->load($id);
if (!$agent->getId()) {
$this->messageManager->addErrorMessage(__('This agent no longer exists.'));
return $resultRedirect->setPath('*/*/');
}
}
try {
$countryCode = strtoupper(trim((string) ($data['country_code'] ?? '')));
$country = $this->countryFactory->create()->loadByCode($countryCode);
if (!$country->getId()) {
throw new LocalizedException(__('Please select a valid country.'));
}
$data['country_code'] = $countryCode;
$data['country'] = (string) $country->getName();
$position = $this->countryPosition->get($countryCode);
if ($position === null) {
throw new LocalizedException(
__('The selected country is not yet available on the agent map. Please contact the website administrator.')
);
}
$data['map_x'] = $position['x'];
$data['map_y'] = $position['y'];
$data['is_active'] = (int) ($data['is_active'] ?? 0);
$data['sort_order'] = max(0, (int) ($data['sort_order'] ?? 0));
$data['website'] = trim((string) ($data['website'] ?? ''));
$data['address'] = trim((string) ($data['address'] ?? ''));
$data['note'] = trim((string) ($data['note'] ?? ''));
$data['logo'] = $this->resolveLogo($data['logo'] ?? null, (string) $agent->getData('logo'));
$agent->addData($data);
$agent->save();
$this->dataPersistor->clear('esun_agent');
$this->messageManager->addSuccessMessage(__('The agent has been saved.'));
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['entity_id' => $agent->getId()]);
}
return $resultRedirect->setPath('*/*/');
} catch (LocalizedException $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
} catch (\Throwable $exception) {
$this->messageManager->addExceptionMessage($exception, __('The agent could not be saved.'));
}
$this->dataPersistor->set('esun_agent', $data);
return $resultRedirect->setPath('*/*/edit', ['entity_id' => $id ?: null]);
}
private function resolveLogo(mixed $logoData, string $currentLogo): string
{
if (!is_array($logoData) || !isset($logoData[0]) || !is_array($logoData[0])) {
return $currentLogo;
}
$file = $logoData[0];
if (!empty($file['tmp_name']) && !empty($file['name'])) {
return $this->imageUploader->moveFileFromTmp((string) $file['name'], true);
}
return $currentLogo;
}
}
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Agent;
use Magento\Backend\App\Action;
use Magento\Catalog\Model\ImageUploader;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
class Upload extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::save';
public function __construct(
Action\Context $context,
private readonly JsonFactory $jsonFactory,
private readonly ImageUploader $imageUploader
) {
parent::__construct($context);
}
public function execute(): Json
{
$result = $this->jsonFactory->create();
try {
return $result->setData($this->imageUploader->saveFileToTmpDir('logo'));
} catch (\Throwable $exception) {
return $result->setData([
'error' => $exception->getMessage(),
'errorcode' => $exception->getCode(),
]);
}
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Application;
use Esun\Agent\Model\ApplicationFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::applications_delete';
public function __construct(Action\Context $context, private readonly ApplicationFactory $applicationFactory)
{
parent::__construct($context);
}
public function execute(): Redirect
{
try {
$application = $this->applicationFactory->create()->load((int)$this->getRequest()->getParam('entity_id'));
if (!$application->getId()) {
throw new \RuntimeException('Application not found.');
}
$application->delete();
$this->messageManager->addSuccessMessage(__('The application has been deleted.'));
} catch (\Throwable $exception) {
$this->messageManager->addErrorMessage(__('The application could not be deleted.'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Application;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::applications';
public function __construct(Context $context, private readonly PageFactory $pageFactory)
{
parent::__construct($context);
}
public function execute(): Page
{
$page = $this->pageFactory->create();
$page->setActiveMenu('Esun_Agent::applications');
$page->getConfig()->getTitle()->prepend(__('Agent Applications'));
return $page;
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Esun\Agent\Controller\Adminhtml\Application;
use Esun\Agent\Model\ResourceModel\Application\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Ui\Component\MassAction\Filter;
class MassDelete extends Action
{
public const ADMIN_RESOURCE = 'Esun_Agent::applications_delete';
public function __construct(
Action\Context $context,
private readonly Filter $filter,
private readonly CollectionFactory $collectionFactory
) {
parent::__construct($context);
}
public function execute(): Redirect
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$deleted = 0;
foreach ($collection as $application) {
$application->delete();
$deleted++;
}
$this->messageManager->addSuccessMessage(
__('A total of %1 application(s) have been deleted.', $deleted)
);
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}