Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14 
15 class Save extends \Magento\Backend\App\Action implements HttpPostActionInterface
16 {
22  const ADMIN_RESOURCE = 'Magento_Cms::save';
23 
27  protected $dataProcessor;
28 
32  protected $dataPersistor;
33 
37  private $pageFactory;
38 
42  private $pageRepository;
43 
51  public function __construct(
52  Action\Context $context,
55  \Magento\Cms\Model\PageFactory $pageFactory = null,
56  \Magento\Cms\Api\PageRepositoryInterface $pageRepository = null
57  ) {
58  $this->dataProcessor = $dataProcessor;
59  $this->dataPersistor = $dataPersistor;
60  $this->pageFactory = $pageFactory
61  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Cms\Model\PageFactory::class);
62  $this->pageRepository = $pageRepository
64  ->get(\Magento\Cms\Api\PageRepositoryInterface::class);
65  parent::__construct($context);
66  }
67 
74  public function execute()
75  {
76  $data = $this->getRequest()->getPostValue();
78  $resultRedirect = $this->resultRedirectFactory->create();
79  if ($data) {
80  $data = $this->dataProcessor->filter($data);
81  if (isset($data['is_active']) && $data['is_active'] === 'true') {
82  $data['is_active'] = Page::STATUS_ENABLED;
83  }
84  if (empty($data['page_id'])) {
85  $data['page_id'] = null;
86  }
87 
89  $model = $this->pageFactory->create();
90 
91  $id = $this->getRequest()->getParam('page_id');
92  if ($id) {
93  try {
94  $model = $this->pageRepository->getById($id);
95  } catch (LocalizedException $e) {
96  $this->messageManager->addErrorMessage(__('This page no longer exists.'));
97  return $resultRedirect->setPath('*/*/');
98  }
99  }
100 
101  $model->setData($data);
102 
103  $this->_eventManager->dispatch(
104  'cms_page_prepare_save',
105  ['page' => $model, 'request' => $this->getRequest()]
106  );
107 
108  if (!$this->dataProcessor->validate($data)) {
109  return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]);
110  }
111 
112  try {
113  $this->pageRepository->save($model);
114  $this->messageManager->addSuccessMessage(__('You saved the page.'));
115  return $this->processResultRedirect($model, $resultRedirect, $data);
116  } catch (LocalizedException $e) {
117  $this->messageManager->addExceptionMessage($e->getPrevious() ?: $e);
118  } catch (\Exception $e) {
119  $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the page.'));
120  }
121 
122  $this->dataPersistor->set('cms_page', $data);
123  return $resultRedirect->setPath('*/*/edit', ['page_id' => $this->getRequest()->getParam('page_id')]);
124  }
125  return $resultRedirect->setPath('*/*/');
126  }
127 
137  private function processResultRedirect($model, $resultRedirect, $data)
138  {
139  if ($this->getRequest()->getParam('back', false) === 'duplicate') {
140  $newPage = $this->pageFactory->create(['data' => $data]);
141  $newPage->setId(null);
142  $identifier = $model->getIdentifier() . '-' . uniqid();
143  $newPage->setIdentifier($identifier);
144  $newPage->setIsActive(false);
145  $this->pageRepository->save($newPage);
146  $this->messageManager->addSuccessMessage(__('You duplicated the page.'));
147  return $resultRedirect->setPath(
148  '*/*/edit',
149  [
150  'page_id' => $newPage->getId(),
151  '_current' => true
152  ]
153  );
154  }
155  $this->dataPersistor->clear('cms_page');
156  if ($this->getRequest()->getParam('back')) {
157  return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]);
158  }
159  return $resultRedirect->setPath('*/*/');
160  }
161 }
$id
Definition: fieldset.phtml:14
__construct(Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, \Magento\Cms\Model\PageFactory $pageFactory=null, \Magento\Cms\Api\PageRepositoryInterface $pageRepository=null)
Definition: Save.php:51
__()
Definition: __.php:13
const STATUS_ENABLED
Definition: Page.php:34