init
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Controller\Adminhtml\Queue;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\Queue\SelectionRegistry;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
|
||||
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory as BlockCollectionFactory;
|
||||
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Framework\Data\Collection\AbstractDb;
|
||||
use Magento\Ui\Component\MassAction\Filter;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory as BlogPostCollectionFactory;
|
||||
|
||||
class Prepare extends Action
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_MultiLocaleTranslation::queue_add';
|
||||
|
||||
public function __construct(
|
||||
Action\Context $context,
|
||||
private readonly Filter $filter,
|
||||
private readonly ProductCollectionFactory $productCollectionFactory,
|
||||
private readonly PageCollectionFactory $pageCollectionFactory,
|
||||
private readonly BlockCollectionFactory $blockCollectionFactory,
|
||||
private readonly BlogPostCollectionFactory $blogPostCollectionFactory,
|
||||
private readonly SelectionRegistry $selectionRegistry
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute(): Redirect
|
||||
{
|
||||
try {
|
||||
$entityType = (string)$this->getRequest()->getParam('entity_type');
|
||||
$this->assertBlogPermission($entityType);
|
||||
$collection = $this->collection($entityType);
|
||||
$selected = $this->filter->getCollection($collection)->getAllIds();
|
||||
if ($selected === []) {
|
||||
throw new \InvalidArgumentException((string)__('Select at least one item.'));
|
||||
}
|
||||
$token = $this->selectionRegistry->save($entityType, array_map('intval', $selected));
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/new', ['token' => $token]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->messageManager->addErrorMessage($exception->getMessage());
|
||||
return $this->resultRedirectFactory->create()->setPath('*/*/new');
|
||||
}
|
||||
}
|
||||
|
||||
private function collection(string $entityType): AbstractDb
|
||||
{
|
||||
return match ($entityType) {
|
||||
'product' => $this->productCollectionFactory->create(),
|
||||
'cms_page' => $this->pageCollectionFactory->create(),
|
||||
'cms_block' => $this->blockCollectionFactory->create(),
|
||||
'blog_post' => $this->blogPostCollectionFactory->create(),
|
||||
default => throw new \InvalidArgumentException((string)__('Unsupported batch entity type.')),
|
||||
};
|
||||
}
|
||||
|
||||
private function assertBlogPermission(string $entityType): void
|
||||
{
|
||||
if ($entityType === 'blog_post'
|
||||
&& !$this->_authorization->isAllowed('Esun_MultiLocaleTranslation::translate_blog')
|
||||
) {
|
||||
throw new \InvalidArgumentException((string)__('You are not allowed to translate blog posts.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user