123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Config\Controller\Adminhtml\System\Config;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Config\Controller\Adminhtml\System\AbstractConfig;
- /**
- * System Configuration Save Controller
- *
- * @author Magento Core Team <core@magentocommerce.com>
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Save extends AbstractConfig implements HttpPostActionInterface
- {
- /**
- * Backend Config Model Factory
- *
- * @var \Magento\Config\Model\Config\Factory
- */
- protected $_configFactory;
- /**
- * @var \Magento\Framework\Cache\FrontendInterface
- */
- protected $_cache;
- /**
- * @var \Magento\Framework\Stdlib\StringUtils
- */
- protected $string;
- /**
- * @param \Magento\Backend\App\Action\Context $context
- * @param \Magento\Config\Model\Config\Structure $configStructure
- * @param \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker
- * @param \Magento\Config\Model\Config\Factory $configFactory
- * @param \Magento\Framework\Cache\FrontendInterface $cache
- * @param \Magento\Framework\Stdlib\StringUtils $string
- */
- public function __construct(
- \Magento\Backend\App\Action\Context $context,
- \Magento\Config\Model\Config\Structure $configStructure,
- \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker,
- \Magento\Config\Model\Config\Factory $configFactory,
- \Magento\Framework\Cache\FrontendInterface $cache,
- \Magento\Framework\Stdlib\StringUtils $string
- ) {
- parent::__construct($context, $configStructure, $sectionChecker);
- $this->_configFactory = $configFactory;
- $this->_cache = $cache;
- $this->string = $string;
- }
- /**
- * Get groups for save
- *
- * @return array|null
- */
- protected function _getGroupsForSave()
- {
- $groups = $this->getRequest()->getPost('groups');
- $files = $this->getRequest()->getFiles('groups');
- if ($files && is_array($files)) {
- /**
- * Carefully merge $_FILES and $_POST information
- * None of '+=' or 'array_merge_recursive' can do this correct
- */
- foreach ($files as $groupName => $group) {
- $data = $this->_processNestedGroups($group);
- if (!empty($data)) {
- if (!empty($groups[$groupName])) {
- $groups[$groupName] = array_merge_recursive((array)$groups[$groupName], $data);
- } else {
- $groups[$groupName] = $data;
- }
- }
- }
- }
- return $groups;
- }
- /**
- * Process nested groups
- *
- * @param mixed $group
- * @return array
- */
- protected function _processNestedGroups($group)
- {
- $data = [];
- if (isset($group['fields']) && is_array($group['fields'])) {
- foreach ($group['fields'] as $fieldName => $field) {
- if (!empty($field['value'])) {
- $data['fields'][$fieldName] = ['value' => $field['value']];
- }
- }
- }
- if (isset($group['groups']) && is_array($group['groups'])) {
- foreach ($group['groups'] as $groupName => $groupData) {
- $nestedGroup = $this->_processNestedGroups($groupData);
- if (!empty($nestedGroup)) {
- $data['groups'][$groupName] = $nestedGroup;
- }
- }
- }
- return $data;
- }
- /**
- * Custom save logic for section
- *
- * @return void
- */
- protected function _saveSection()
- {
- $method = '_save' . $this->string->upperCaseWords($this->getRequest()->getParam('section'), '_', '');
- if (method_exists($this, $method)) {
- $this->{$method}();
- }
- }
- /**
- * Advanced save procedure
- *
- * @return void
- */
- protected function _saveAdvanced()
- {
- $this->_cache->clean();
- }
- /**
- * Save configuration
- *
- * @return \Magento\Backend\Model\View\Result\Redirect
- */
- public function execute()
- {
- try {
- // custom save logic
- $this->_saveSection();
- $section = $this->getRequest()->getParam('section');
- $website = $this->getRequest()->getParam('website');
- $store = $this->getRequest()->getParam('store');
- $configData = [
- 'section' => $section,
- 'website' => $website,
- 'store' => $store,
- 'groups' => $this->_getGroupsForSave(),
- ];
- /** @var \Magento\Config\Model\Config $configModel */
- $configModel = $this->_configFactory->create(['data' => $configData]);
- $configModel->save();
- $this->_eventManager->dispatch('admin_system_config_save', [
- 'configData' => $configData,
- 'request' => $this->getRequest()
- ]);
- $this->messageManager->addSuccess(__('You saved the configuration.'));
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $messages = explode("\n", $e->getMessage());
- foreach ($messages as $message) {
- $this->messageManager->addError($message);
- }
- } catch (\Exception $e) {
- $this->messageManager->addException(
- $e,
- __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
- );
- }
- $this->_saveState($this->getRequest()->getPost('config_state'));
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- return $resultRedirect->setPath(
- 'adminhtml/system_config/edit',
- [
- '_current' => ['section', 'website', 'store'],
- '_nosid' => true
- ]
- );
- }
- }
|