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