Save.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Controller\Adminhtml\Block;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Cms\Api\BlockRepositoryInterface;
  10. use Magento\Cms\Model\Block;
  11. use Magento\Cms\Model\BlockFactory;
  12. use Magento\Framework\App\Request\DataPersistorInterface;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Framework\Registry;
  15. /**
  16. * Save CMS block action.
  17. */
  18. class Save extends \Magento\Cms\Controller\Adminhtml\Block implements HttpPostActionInterface
  19. {
  20. /**
  21. * @var DataPersistorInterface
  22. */
  23. protected $dataPersistor;
  24. /**
  25. * @var BlockFactory
  26. */
  27. private $blockFactory;
  28. /**
  29. * @var BlockRepositoryInterface
  30. */
  31. private $blockRepository;
  32. /**
  33. * @param Context $context
  34. * @param Registry $coreRegistry
  35. * @param DataPersistorInterface $dataPersistor
  36. * @param BlockFactory|null $blockFactory
  37. * @param BlockRepositoryInterface|null $blockRepository
  38. */
  39. public function __construct(
  40. Context $context,
  41. Registry $coreRegistry,
  42. DataPersistorInterface $dataPersistor,
  43. BlockFactory $blockFactory = null,
  44. BlockRepositoryInterface $blockRepository = null
  45. ) {
  46. $this->dataPersistor = $dataPersistor;
  47. $this->blockFactory = $blockFactory
  48. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockFactory::class);
  49. $this->blockRepository = $blockRepository
  50. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(BlockRepositoryInterface::class);
  51. parent::__construct($context, $coreRegistry);
  52. }
  53. /**
  54. * Save action
  55. *
  56. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  57. * @return \Magento\Framework\Controller\ResultInterface
  58. */
  59. public function execute()
  60. {
  61. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  62. $resultRedirect = $this->resultRedirectFactory->create();
  63. $data = $this->getRequest()->getPostValue();
  64. if ($data) {
  65. if (isset($data['is_active']) && $data['is_active'] === 'true') {
  66. $data['is_active'] = Block::STATUS_ENABLED;
  67. }
  68. if (empty($data['block_id'])) {
  69. $data['block_id'] = null;
  70. }
  71. /** @var \Magento\Cms\Model\Block $model */
  72. $model = $this->blockFactory->create();
  73. $id = $this->getRequest()->getParam('block_id');
  74. if ($id) {
  75. try {
  76. $model = $this->blockRepository->getById($id);
  77. } catch (LocalizedException $e) {
  78. $this->messageManager->addErrorMessage(__('This block no longer exists.'));
  79. return $resultRedirect->setPath('*/*/');
  80. }
  81. }
  82. $model->setData($data);
  83. try {
  84. $this->blockRepository->save($model);
  85. $this->messageManager->addSuccessMessage(__('You saved the block.'));
  86. $this->dataPersistor->clear('cms_block');
  87. return $this->processBlockReturn($model, $data, $resultRedirect);
  88. } catch (LocalizedException $e) {
  89. $this->messageManager->addErrorMessage($e->getMessage());
  90. } catch (\Exception $e) {
  91. $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the block.'));
  92. }
  93. $this->dataPersistor->set('cms_block', $data);
  94. return $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
  95. }
  96. return $resultRedirect->setPath('*/*/');
  97. }
  98. /**
  99. * Process and set the block return
  100. *
  101. * @param \Magento\Cms\Model\Block $model
  102. * @param array $data
  103. * @param \Magento\Framework\Controller\ResultInterface $resultRedirect
  104. * @return \Magento\Framework\Controller\ResultInterface
  105. */
  106. private function processBlockReturn($model, $data, $resultRedirect)
  107. {
  108. $redirect = $data['back'] ?? 'close';
  109. if ($redirect ==='continue') {
  110. $resultRedirect->setPath('*/*/edit', ['block_id' => $model->getId()]);
  111. } else if ($redirect === 'close') {
  112. $resultRedirect->setPath('*/*/');
  113. } else if ($redirect === 'duplicate') {
  114. $duplicateModel = $this->blockFactory->create(['data' => $data]);
  115. $duplicateModel->setId(null);
  116. $duplicateModel->setIdentifier($data['identifier'] . '-' . uniqid());
  117. $duplicateModel->setIsActive(Block::STATUS_DISABLED);
  118. $this->blockRepository->save($duplicateModel);
  119. $id = $duplicateModel->getId();
  120. $this->messageManager->addSuccessMessage(__('You duplicated the block.'));
  121. $this->dataPersistor->set('cms_block', $data);
  122. $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
  123. }
  124. return $resultRedirect;
  125. }
  126. }