123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cms\Controller\Adminhtml\Block;
- use Magento\Framework\App\Action\HttpPostActionInterface;
- use Magento\Backend\App\Action\Context;
- use Magento\Cms\Api\BlockRepositoryInterface;
- use Magento\Cms\Model\Block;
- use Magento\Cms\Model\BlockFactory;
- use Magento\Framework\App\Request\DataPersistorInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Registry;
- /**
- * Save CMS block action.
- */
- class Save extends \Magento\Cms\Controller\Adminhtml\Block implements HttpPostActionInterface
- {
- /**
- * @var DataPersistorInterface
- */
- protected $dataPersistor;
- /**
- * @var BlockFactory
- */
- private $blockFactory;
- /**
- * @var BlockRepositoryInterface
- */
- private $blockRepository;
- /**
- * @param Context $context
- * @param Registry $coreRegistry
- * @param DataPersistorInterface $dataPersistor
- * @param BlockFactory|null $blockFactory
- * @param BlockRepositoryInterface|null $blockRepository
- */
- public function __construct(
- Context $context,
- Registry $coreRegistry,
- DataPersistorInterface $dataPersistor,
- BlockFactory $blockFactory = null,
- BlockRepositoryInterface $blockRepository = null
- ) {
- $this->dataPersistor = $dataPersistor;
- $this->blockFactory = $blockFactory
- ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockFactory::class);
- $this->blockRepository = $blockRepository
- ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockRepositoryInterface::class);
- parent::__construct($context, $coreRegistry);
- }
- /**
- * Save action
- *
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @return \Magento\Framework\Controller\ResultInterface
- */
- public function execute()
- {
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- $data = $this->getRequest()->getPostValue();
- if ($data) {
- if (isset($data['is_active']) && $data['is_active'] === 'true') {
- $data['is_active'] = Block::STATUS_ENABLED;
- }
- if (empty($data['block_id'])) {
- $data['block_id'] = null;
- }
- /** @var \Magento\Cms\Model\Block $model */
- $model = $this->blockFactory->create();
- $id = $this->getRequest()->getParam('block_id');
- if ($id) {
- try {
- $model = $this->blockRepository->getById($id);
- } catch (LocalizedException $e) {
- $this->messageManager->addErrorMessage(__('This block no longer exists.'));
- return $resultRedirect->setPath('*/*/');
- }
- }
- $model->setData($data);
- try {
- $this->blockRepository->save($model);
- $this->messageManager->addSuccessMessage(__('You saved the block.'));
- $this->dataPersistor->clear('cms_block');
- return $this->processBlockReturn($model, $data, $resultRedirect);
- } catch (LocalizedException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- } catch (\Exception $e) {
- $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the block.'));
- }
- $this->dataPersistor->set('cms_block', $data);
- return $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
- }
- return $resultRedirect->setPath('*/*/');
- }
- /**
- * Process and set the block return
- *
- * @param \Magento\Cms\Model\Block $model
- * @param array $data
- * @param \Magento\Framework\Controller\ResultInterface $resultRedirect
- * @return \Magento\Framework\Controller\ResultInterface
- */
- private function processBlockReturn($model, $data, $resultRedirect)
- {
- $redirect = $data['back'] ?? 'close';
- if ($redirect ==='continue') {
- $resultRedirect->setPath('*/*/edit', ['block_id' => $model->getId()]);
- } else if ($redirect === 'close') {
- $resultRedirect->setPath('*/*/');
- } else if ($redirect === 'duplicate') {
- $duplicateModel = $this->blockFactory->create(['data' => $data]);
- $duplicateModel->setId(null);
- $duplicateModel->setIdentifier($data['identifier'] . '-' . uniqid());
- $duplicateModel->setIsActive(Block::STATUS_DISABLED);
- $this->blockRepository->save($duplicateModel);
- $id = $duplicateModel->getId();
- $this->messageManager->addSuccessMessage(__('You duplicated the block.'));
- $this->dataPersistor->set('cms_block', $data);
- $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
- }
- return $resultRedirect;
- }
- }
|