/
Save.php
117 lines (104 loc) · 3.59 KB
/
Save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Controller\Adminhtml\Design\Config;
use Magento\Backend\App\Action;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Theme\Model\DesignConfigRepository;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Theme\Model\Data\Design\ConfigFactory;
class Save extends Action
{
/**
* @var DesignConfigRepository
*/
protected $designConfigRepository;
/**
* @var ConfigFactory
*/
protected $configFactory;
/**
* @var DataPersistorInterface
*/
protected $dataPersistor;
/**
* @param Context $context
* @param DesignConfigRepository $designConfigRepository
* @param ConfigFactory $configFactory
* @param DataPersistorInterface $dataPersistor
*/
public function __construct(
Context $context,
DesignConfigRepository $designConfigRepository,
ConfigFactory $configFactory,
DataPersistorInterface $dataPersistor
) {
$this->designConfigRepository = $designConfigRepository;
$this->configFactory = $configFactory;
$this->dataPersistor = $dataPersistor;
parent::__construct($context);
}
/**
* Check the permission to manage themes
*
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_Config::config_design');
}
/**
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$scope = $this->getRequest()->getParam('scope');
$scopeId = (int)$this->getRequest()->getParam('scope_id');
$data = $this->getRequestData();
try {
$designConfigData = $this->configFactory->create($scope, $scopeId, $data);
$this->designConfigRepository->save($designConfigData);
$this->messageManager->addSuccess(__('You saved the configuration.'));
$this->dataPersistor->clear('theme_design_config');
$returnToEdit = (bool)$this->getRequest()->getParam('back', false);
$resultRedirect->setPath('theme/design_config/');
if ($returnToEdit) {
$resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]);
}
return $resultRedirect;
} catch (LocalizedException $e) {
$messages = explode("\n", $e->getMessage());
foreach ($messages as $message) {
$this->messageManager->addError(__('%1', $message));
}
} catch (\Exception $e) {
$this->messageManager->addException(
$e,
__('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
);
}
$this->dataPersistor->set('theme_design_config', $data);
$resultRedirect->setPath('theme/design_config/edit', ['scope' => $scope, 'scope_id' => $scopeId]);
return $resultRedirect;
}
/**
* Extract data from request
*
* @return array
*/
protected function getRequestData()
{
$data = array_merge(
$this->getRequest()->getParams(),
$this->getRequest()->getFiles()->toArray()
);
$data = array_filter($data, function ($param) {
return isset($param['error']) && $param['error'] > 0 ? false : true;
});
return $data;
}
}