init
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Controller\Adminhtml\Translate;
|
||||
|
||||
use Esun\Translation\Model\Translation\ApplyService;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
|
||||
class Apply extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Magento_Backend::admin';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly ApplyService $applyService
|
||||
) {
|
||||
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');
|
||||
$fields = $this->getRequest()->getParam('fields', []);
|
||||
if ($entityId <= 0) {
|
||||
throw new \InvalidArgumentException('A valid content ID is required.');
|
||||
}
|
||||
if (!is_array($fields)) {
|
||||
throw new \InvalidArgumentException('Fields payload is invalid.');
|
||||
}
|
||||
|
||||
$response = $this->applyService->apply($type, $entityId, $language, $fields);
|
||||
|
||||
return $result->setData(array_merge(['success' => true], $response));
|
||||
} catch (\Throwable $exception) {
|
||||
return $result->setData([
|
||||
'success' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function _isAllowed(): bool
|
||||
{
|
||||
return $this->_authorization->isAllowed($this->getApplyResource());
|
||||
}
|
||||
|
||||
private function getApplyResource(): string
|
||||
{
|
||||
return match ((string)$this->getRequest()->getParam('entity_type', 'product')) {
|
||||
'cms_page' => 'Esun_Translation::apply_cms_page_translation',
|
||||
'cms_block' => 'Esun_Translation::apply_cms_block_translation',
|
||||
default => 'Esun_Translation::apply_product_translation',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Controller\Adminhtml\Translate;
|
||||
|
||||
use Esun\Translation\Model\Entity\EntityPool;
|
||||
use Esun\Translation\Model\StoreView\LanguageGroupProvider;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\Controller\Result\Json;
|
||||
use Magento\Framework\Controller\Result\JsonFactory;
|
||||
|
||||
class Fields extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Magento_Backend::admin';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly JsonFactory $jsonFactory,
|
||||
private readonly EntityPool $entityPool,
|
||||
private readonly LanguageGroupProvider $languageGroupProvider
|
||||
) {
|
||||
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');
|
||||
if ($entityId <= 0) {
|
||||
throw new \InvalidArgumentException('A valid content ID is required.');
|
||||
}
|
||||
|
||||
$group = $this->languageGroupProvider->getGroup($language);
|
||||
if (!$group) {
|
||||
throw new \InvalidArgumentException('Language group was not found.');
|
||||
}
|
||||
|
||||
return $result->setData([
|
||||
'success' => true,
|
||||
'fields' => $this->entityPool->get($type)->getFields($entityId, $group->getStoreIds()),
|
||||
]);
|
||||
} 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',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Controller\Adminhtml\Translate;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
|
||||
class Index extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Magento_Backend::admin';
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly PageFactory $pageFactory
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): ResultInterface
|
||||
{
|
||||
$page = $this->pageFactory->create();
|
||||
$page->getConfig()->getTitle()->prepend(__('Esun Translation'));
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
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',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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