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
7 
11 
13 {
21  protected function validatePath(array $data)
22  {
23  if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) {
24  $data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/');
25  $path = rtrim($data['sitemap_path'], '\\/') . '/' . $data['sitemap_filename'];
27  $validator = $this->_objectManager->create(\Magento\MediaStorage\Model\File\Validator\AvailablePath::class);
29  $helper = $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
30  $validator->setPaths($helper->getValidPaths());
31  if (!$validator->isValid($path)) {
32  foreach ($validator->getMessages() as $message) {
33  $this->messageManager->addErrorMessage($message);
34  }
35  // save data in session
36  $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
37  // redirect to edit form
38  return false;
39  }
40  }
41  return true;
42  }
43 
50  protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
51  {
53  $directory = $this->_objectManager->get(\Magento\Framework\Filesystem::class)
54  ->getDirectoryWrite(DirectoryList::ROOT);
55 
56  if ($this->getRequest()->getParam('sitemap_id')) {
57  $model->load($this->getRequest()->getParam('sitemap_id'));
58  $fileName = $model->getSitemapFilename();
59 
60  $path = $model->getSitemapPath() . '/' . $fileName;
61  if ($fileName && $directory->isFile($path)) {
62  $directory->delete($path);
63  }
64  }
65  }
66 
73  protected function saveData($data)
74  {
75  // init model and set data
77  $model = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
78  $this->clearSiteMap($model);
79  $model->setData($data);
80 
81  // try to save it
82  try {
83  // save the data
84  $model->save();
85  // display success message
86  $this->messageManager->addSuccessMessage(__('You saved the sitemap.'));
87  // clear previously saved data from session
88  $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData(false);
89  return $model->getId();
90  } catch (\Exception $e) {
91  // display error message
92  $this->messageManager->addErrorMessage($e->getMessage());
93  // save data in session
94  $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
95  }
96  return false;
97  }
98 
105  protected function getResult($id)
106  {
108  $resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);
109  if ($id) {
110  // check if 'Save and Continue'
111  if ($this->getRequest()->getParam('back')) {
112  $resultRedirect->setPath('adminhtml/*/edit', ['sitemap_id' => $id]);
113  return $resultRedirect;
114  }
115  // go to grid or forward to generate action
116  if ($this->getRequest()->getParam('generate')) {
117  $this->getRequest()->setParam('sitemap_id', $id);
118  return $this->resultFactory->create(Controller\ResultFactory::TYPE_FORWARD)
119  ->forward('generate');
120  }
121  $resultRedirect->setPath('adminhtml/*/');
122  return $resultRedirect;
123  }
124  $resultRedirect->setPath(
125  'adminhtml/*/edit',
126  ['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]
127  );
128  return $resultRedirect;
129  }
130 
136  public function execute()
137  {
138  // check if data sent
139  $data = $this->getRequest()->getPostValue();
141  $resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT);
142  if ($data) {
143  if (!$this->validatePath($data)) {
144  $resultRedirect->setPath(
145  'adminhtml/*/edit',
146  ['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]
147  );
148  return $resultRedirect;
149  }
150  return $this->getResult($this->saveData($data));
151  }
152  $resultRedirect->setPath('adminhtml/*/');
153  return $resultRedirect;
154  }
155 }
$helper
Definition: iframe.phtml:13
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$message
$fileName
Definition: translate.phtml:15