92 lines
3.2 KiB
PHP
92 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Wiki\Controller\Adminhtml\Homepage;
|
|
|
|
use Esun\Wiki\Model\HomepageFactory;
|
|
use Esun\Wiki\Model\ResourceModel\Homepage as HomepageResource;
|
|
use Esun\Wiki\Model\ResourceModel\Homepage\CollectionFactory;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Framework\App\Cache\TypeListInterface;
|
|
use Magento\Framework\App\ResourceConnection;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
|
|
class Save extends Action
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_Wiki::homepage';
|
|
|
|
public function __construct(
|
|
Action\Context $context,
|
|
private readonly HomepageFactory $homepageFactory,
|
|
private readonly HomepageResource $homepageResource,
|
|
private readonly CollectionFactory $collectionFactory,
|
|
private readonly ResourceConnection $resourceConnection,
|
|
private readonly TypeListInterface $cacheTypeList
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$redirect = $this->resultRedirectFactory->create();
|
|
|
|
try {
|
|
$this->assertSchema();
|
|
|
|
$title = trim((string) $this->getRequest()->getParam('title'));
|
|
if ($title === '') {
|
|
throw new LocalizedException(__('Title is required.'));
|
|
}
|
|
|
|
$homepage = $this->collectionFactory->create()
|
|
->setPageSize(1)
|
|
->getFirstItem();
|
|
if (!$homepage->getId()) {
|
|
$homepage = $this->homepageFactory->create();
|
|
}
|
|
|
|
$homepage->addData([
|
|
'title' => $title,
|
|
'content' => (string) $this->getRequest()->getParam('content'),
|
|
'meta_title' => trim((string) $this->getRequest()->getParam('meta_title')),
|
|
'meta_description' => trim((string) $this->getRequest()->getParam('meta_description')),
|
|
'meta_keywords' => trim((string) $this->getRequest()->getParam('meta_keywords')),
|
|
]);
|
|
$this->homepageResource->save($homepage);
|
|
|
|
$this->cacheTypeList->cleanType('block_html');
|
|
$this->cacheTypeList->cleanType('full_page');
|
|
$this->messageManager->addSuccessMessage(__('The wiki homepage has been saved.'));
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage($exception->getMessage());
|
|
}
|
|
|
|
return $redirect->setPath('esun_wiki/homepage/edit');
|
|
}
|
|
|
|
private function assertSchema(): void
|
|
{
|
|
$connection = $this->resourceConnection->getConnection();
|
|
$table = $this->resourceConnection->getTableName('esun_wiki_homepage');
|
|
|
|
if (!$connection->isTableExists($table)) {
|
|
throw new LocalizedException(__(
|
|
'The Wiki homepage is not installed. Run "bin/magento setup:upgrade" and try again.'
|
|
));
|
|
}
|
|
|
|
$columns = $connection->describeTable($table);
|
|
if (!isset(
|
|
$columns['title'],
|
|
$columns['content'],
|
|
$columns['meta_title'],
|
|
$columns['meta_description'],
|
|
$columns['meta_keywords']
|
|
)) {
|
|
throw new LocalizedException(__(
|
|
'The Wiki homepage schema is incomplete. Run "bin/magento setup:upgrade" and try again.'
|
|
));
|
|
}
|
|
}
|
|
}
|