71 lines
2.8 KiB
PHP
71 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Plugin\Adminhtml;
|
|
|
|
use Esun\MultiLocaleTranslation\Model\BlogPostFields;
|
|
use Esun\MultiLocaleTranslation\Model\Entity\BlogPostEntity;
|
|
use Magento\Framework\App\Cache\Manager as CacheManager;
|
|
use Magento\Framework\App\RequestInterface;
|
|
use Magento\Framework\AuthorizationInterface;
|
|
use Magento\Framework\Controller\Result\Redirect;
|
|
use Magento\Framework\Controller\Result\RedirectFactory;
|
|
use Magento\Framework\Message\ManagerInterface;
|
|
use Mageplaza\Blog\Controller\Adminhtml\Post\Save;
|
|
|
|
class BlogPostSave
|
|
{
|
|
public function __construct(
|
|
private readonly RequestInterface $request,
|
|
private readonly BlogPostEntity $blogPostEntity,
|
|
private readonly RedirectFactory $redirectFactory,
|
|
private readonly ManagerInterface $messageManager,
|
|
private readonly CacheManager $cacheManager,
|
|
private readonly AuthorizationInterface $authorization
|
|
) {
|
|
}
|
|
|
|
public function aroundExecute(Save $subject, callable $proceed): mixed
|
|
{
|
|
$postData = $this->request->getParam('post', []);
|
|
$postData = is_array($postData) ? $postData : [];
|
|
$storeId = max(0, (int)($postData['translation_store_id'] ?? 0));
|
|
if ($storeId <= 0) {
|
|
return $proceed();
|
|
}
|
|
|
|
$postId = (int)$this->request->getParam('id');
|
|
if ($postId <= 0) {
|
|
$this->messageManager->addErrorMessage(__('The blog post no longer exists.'));
|
|
return $this->redirectFactory->create()->setPath('mageplaza_blog/post/index');
|
|
}
|
|
if (!$this->authorization->isAllowed('Esun_MultiLocaleTranslation::translate_blog')) {
|
|
$this->messageManager->addErrorMessage(__('You are not allowed to save blog translations.'));
|
|
return $this->redirectToEdit($postId, $storeId);
|
|
}
|
|
|
|
try {
|
|
$fields = array_intersect_key($postData, array_flip(BlogPostFields::codes()));
|
|
$useDefault = is_array($postData['use_default'] ?? null) ? $postData['use_default'] : [];
|
|
$this->blogPostEntity->apply($postId, $storeId, $fields, $useDefault);
|
|
$this->cacheManager->clean(['full_page', 'block_html']);
|
|
$this->messageManager->addSuccessMessage(__('You saved the blog post translation.'));
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage(
|
|
__('Something went wrong while saving the blog post translation: %1', $exception->getMessage())
|
|
);
|
|
}
|
|
|
|
return $this->redirectToEdit($postId, $storeId);
|
|
}
|
|
|
|
private function redirectToEdit(int $postId, int $storeId): Redirect
|
|
{
|
|
return $this->redirectFactory->create()->setPath(
|
|
'mageplaza_blog/post/edit',
|
|
['id' => $postId, 'store' => $storeId]
|
|
);
|
|
}
|
|
}
|