|
8 | 8 |
|
9 | 9 | use Magento\Backend\App\Action;
|
10 | 10 | use Magento\Framework\App\Filesystem\DirectoryList;
|
| 11 | +use Magento\Framework\Controller; |
11 | 12 |
|
12 | 13 | class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
|
13 | 14 | {
|
14 | 15 | /**
|
15 |
| - * Save action |
| 16 | + * Validate path to generate |
16 | 17 | *
|
17 |
| - * @return void |
18 |
| - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 18 | + * @param $data |
| 19 | + * @return bool |
| 20 | + * @throws \Exception |
19 | 21 | */
|
20 |
| - public function execute() |
| 22 | + protected function validatePathToGenerate($data) |
21 | 23 | {
|
22 |
| - // check if data sent |
23 |
| - $data = $this->getRequest()->getPostValue(); |
24 |
| - if ($data) { |
25 |
| - // init model and set data |
26 |
| - /** @var \Magento\Sitemap\Model\Sitemap $model */ |
27 |
| - $model = $this->_objectManager->create('Magento\Sitemap\Model\Sitemap'); |
28 |
| - |
29 |
| - //validate path to generate |
30 |
| - if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) { |
31 |
| - $data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/'); |
32 |
| - $path = rtrim($data['sitemap_path'], '\\/') . '/' . $data['sitemap_filename']; |
33 |
| - /** @var $validator \Magento\MediaStorage\Model\File\Validator\AvailablePath */ |
34 |
| - $validator = $this->_objectManager->create('Magento\MediaStorage\Model\File\Validator\AvailablePath'); |
35 |
| - /** @var $helper \Magento\Sitemap\Helper\Data */ |
36 |
| - $helper = $this->_objectManager->get('Magento\Sitemap\Helper\Data'); |
37 |
| - $validator->setPaths($helper->getValidPaths()); |
38 |
| - if (!$validator->isValid($path)) { |
39 |
| - foreach ($validator->getMessages() as $message) { |
40 |
| - $this->messageManager->addError($message); |
41 |
| - } |
42 |
| - // save data in session |
43 |
| - $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data); |
44 |
| - // redirect to edit form |
45 |
| - $this->_redirect( |
46 |
| - 'adminhtml/*/edit', |
47 |
| - ['sitemap_id' => $this->getRequest()->getParam('sitemap_id')] |
48 |
| - ); |
49 |
| - return; |
| 24 | + if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) { |
| 25 | + $data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/'); |
| 26 | + $path = rtrim($data['sitemap_path'], '\\/') . '/' . $data['sitemap_filename']; |
| 27 | + /** @var $validator \Magento\MediaStorage\Model\File\Validator\AvailablePath */ |
| 28 | + $validator = $this->_objectManager->create('Magento\MediaStorage\Model\File\Validator\AvailablePath'); |
| 29 | + /** @var $helper \Magento\Sitemap\Helper\Data */ |
| 30 | + $helper = $this->_objectManager->get('Magento\Sitemap\Helper\Data'); |
| 31 | + $validator->setPaths($helper->getValidPaths()); |
| 32 | + if (!$validator->isValid($path)) { |
| 33 | + foreach ($validator->getMessages() as $message) { |
| 34 | + $this->messageManager->addError($message); |
50 | 35 | }
|
| 36 | + // save data in session |
| 37 | + $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data); |
| 38 | + // redirect to edit form |
| 39 | + return false; |
51 | 40 | }
|
| 41 | + } |
| 42 | + return true; |
| 43 | + } |
52 | 44 |
|
53 |
| - /** @var \Magento\Framework\Filesystem\Directory\Write $directory */ |
54 |
| - $directory = $this->_objectManager->get( |
55 |
| - 'Magento\Framework\Filesystem' |
56 |
| - )->getDirectoryWrite( |
57 |
| - DirectoryList::ROOT |
58 |
| - ); |
| 45 | + /** |
| 46 | + * Clear sitemap |
| 47 | + * |
| 48 | + * @param $model |
| 49 | + */ |
| 50 | + protected function clearSiteMap($model) |
| 51 | + { |
| 52 | + /** @var \Magento\Framework\Filesystem\Directory\Write $directory */ |
| 53 | + $directory = $this->_objectManager->get('Magento\Framework\Filesystem') |
| 54 | + ->getDirectoryWrite(DirectoryList::ROOT); |
59 | 55 |
|
60 |
| - if ($this->getRequest()->getParam('sitemap_id')) { |
61 |
| - $model->load($this->getRequest()->getParam('sitemap_id')); |
62 |
| - $fileName = $model->getSitemapFilename(); |
| 56 | + if ($this->getRequest()->getParam('sitemap_id')) { |
| 57 | + $model->load($this->getRequest()->getParam('sitemap_id')); |
| 58 | + $fileName = $model->getSitemapFilename(); |
63 | 59 |
|
64 |
| - $path = $model->getSitemapPath() . '/' . $fileName; |
65 |
| - if ($fileName && $directory->isFile($path)) { |
66 |
| - $directory->delete($path); |
67 |
| - } |
| 60 | + $path = $model->getSitemapPath() . '/' . $fileName; |
| 61 | + if ($fileName && $directory->isFile($path)) { |
| 62 | + $directory->delete($path); |
68 | 63 | }
|
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Save model |
| 69 | + * |
| 70 | + * @param array $data |
| 71 | + * @return Controller\Result\Redirect|Controller\Result\Forward |
| 72 | + */ |
| 73 | + protected function saveModel($data) |
| 74 | + { |
| 75 | + // init model and set data |
| 76 | + /** @var \Magento\Sitemap\Model\Sitemap $model */ |
| 77 | + $model = $this->_objectManager->create('Magento\Sitemap\Model\Sitemap'); |
| 78 | + $this->clearSiteMap($model); |
| 79 | + $model->setData($data); |
69 | 80 |
|
70 |
| - $model->setData($data); |
| 81 | + // try to save it |
| 82 | + try { |
| 83 | + // save the data |
| 84 | + $model->save(); |
| 85 | + // display success message |
| 86 | + $this->messageManager->addSuccess(__('You saved the sitemap.')); |
| 87 | + // clear previously saved data from session |
| 88 | + $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false); |
71 | 89 |
|
72 |
| - // try to save it |
73 |
| - try { |
74 |
| - // save the data |
75 |
| - $model->save(); |
76 |
| - // display success message |
77 |
| - $this->messageManager->addSuccess(__('You saved the sitemap.')); |
78 |
| - // clear previously saved data from session |
79 |
| - $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false); |
| 90 | + // check if 'Save and Continue' |
| 91 | + if ($this->getRequest()->getParam('back')) { |
| 92 | + return $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT) |
| 93 | + ->setPath('adminhtml/*/edit', ['sitemap_id' => $model->getId()]); |
| 94 | + } |
| 95 | + // go to grid or forward to generate action |
| 96 | + if ($this->getRequest()->getParam('generate')) { |
| 97 | + $this->getRequest()->setParam('sitemap_id', $model->getId()); |
| 98 | + return $this->resultFactory->create(Controller\ResultFactory::TYPE_FORWARD)->forward('generate'); |
| 99 | + } |
| 100 | + return $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*/'); |
| 101 | + } catch (\Exception $e) { |
| 102 | + // display error message |
| 103 | + $this->messageManager->addError($e->getMessage()); |
| 104 | + // save data in session |
| 105 | + $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data); |
| 106 | + // redirect to edit form |
| 107 | + return $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT) |
| 108 | + ->setPath('adminhtml/*/edit',['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]); |
| 109 | + } |
| 110 | + } |
80 | 111 |
|
81 |
| - // check if 'Save and Continue' |
82 |
| - if ($this->getRequest()->getParam('back')) { |
83 |
| - $this->_redirect('adminhtml/*/edit', ['sitemap_id' => $model->getId()]); |
84 |
| - return; |
85 |
| - } |
86 |
| - // go to grid or forward to generate action |
87 |
| - if ($this->getRequest()->getParam('generate')) { |
88 |
| - $this->getRequest()->setParam('sitemap_id', $model->getId()); |
89 |
| - $this->_forward('generate'); |
90 |
| - return; |
91 |
| - } |
92 |
| - $this->_redirect('adminhtml/*/'); |
93 |
| - return; |
94 |
| - } catch (\Exception $e) { |
95 |
| - // display error message |
96 |
| - $this->messageManager->addError($e->getMessage()); |
97 |
| - // save data in session |
98 |
| - $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data); |
99 |
| - // redirect to edit form |
100 |
| - $this->_redirect( |
| 112 | + /** |
| 113 | + * Save action |
| 114 | + * |
| 115 | + * @return Controller\Result\Redirect |
| 116 | + */ |
| 117 | + public function execute() |
| 118 | + { |
| 119 | + // check if data sent |
| 120 | + $data = $this->getRequest()->getPostValue(); |
| 121 | + if ($data) { |
| 122 | + if (!$this->validatePathToGenerate($data)) { |
| 123 | + /** @var Controller\Result\Redirect $resultRedirect */ |
| 124 | + $resultRedirect = $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT); |
| 125 | + return $resultRedirect->setPath( |
101 | 126 | 'adminhtml/*/edit',
|
102 | 127 | ['sitemap_id' => $this->getRequest()->getParam('sitemap_id')]
|
103 | 128 | );
|
104 |
| - return; |
105 | 129 | }
|
| 130 | + $this->saveModel($data); |
106 | 131 | }
|
107 |
| - $this->_redirect('adminhtml/*/'); |
| 132 | + return $this->resultFactory->create(Controller\ResultFactory::TYPE_REDIRECT)->setPath('adminhtml/*/'); |
108 | 133 | }
|
| 134 | + |
109 | 135 | }
|
0 commit comments