init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Controller\Adminhtml\Translate;
|
||||
|
||||
use Esun\Translation\Model\Translation\TranslationService;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
|
||||
class Run extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Magento_Backend::admin';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly TranslationService $translationService
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Json
|
||||
{
|
||||
$result = $this->jsonFactory->create();
|
||||
|
||||
try {
|
||||
$type = (string)$this->getRequest()->getParam('entity_type', 'product');
|
||||
$entityId = (int)$this->getRequest()->getParam('entity_id', $this->getRequest()->getParam('product_id'));
|
||||
$language = (string)$this->getRequest()->getParam('language');
|
||||
$sourceOverrides = $this->getRequest()->getParam('source_overrides', []);
|
||||
if ($entityId <= 0) {
|
||||
throw new \InvalidArgumentException('A valid content ID is required.');
|
||||
}
|
||||
if (!is_array($sourceOverrides)) {
|
||||
$sourceOverrides = [];
|
||||
}
|
||||
|
||||
return $result->setData([
|
||||
'success' => true,
|
||||
'translations' => $this->translationService->translate($type, $entityId, $language, $sourceOverrides),
|
||||
]);
|
||||
} catch (\Throwable $exception) {
|
||||
return $result->setData([
|
||||
'success' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function _isAllowed(): bool
|
||||
{
|
||||
return $this->_authorization->isAllowed($this->getTranslateResource());
|
||||
}
|
||||
|
||||
private function getTranslateResource(): string
|
||||
{
|
||||
return match ((string)$this->getRequest()->getParam('entity_type', 'product')) {
|
||||
'cms_page' => 'Esun_Translation::translate_cms_page',
|
||||
'cms_block' => 'Esun_Translation::translate_cms_block',
|
||||
default => 'Esun_Translation::translate_product',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user