Save.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Controller\Adminhtml\System\Config;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Config\Controller\Adminhtml\System\AbstractConfig;
  9. /**
  10. * System Configuration Save Controller
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Save extends AbstractConfig implements HttpPostActionInterface
  16. {
  17. /**
  18. * Backend Config Model Factory
  19. *
  20. * @var \Magento\Config\Model\Config\Factory
  21. */
  22. protected $_configFactory;
  23. /**
  24. * @var \Magento\Framework\Cache\FrontendInterface
  25. */
  26. protected $_cache;
  27. /**
  28. * @var \Magento\Framework\Stdlib\StringUtils
  29. */
  30. protected $string;
  31. /**
  32. * @param \Magento\Backend\App\Action\Context $context
  33. * @param \Magento\Config\Model\Config\Structure $configStructure
  34. * @param \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker
  35. * @param \Magento\Config\Model\Config\Factory $configFactory
  36. * @param \Magento\Framework\Cache\FrontendInterface $cache
  37. * @param \Magento\Framework\Stdlib\StringUtils $string
  38. */
  39. public function __construct(
  40. \Magento\Backend\App\Action\Context $context,
  41. \Magento\Config\Model\Config\Structure $configStructure,
  42. \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker,
  43. \Magento\Config\Model\Config\Factory $configFactory,
  44. \Magento\Framework\Cache\FrontendInterface $cache,
  45. \Magento\Framework\Stdlib\StringUtils $string
  46. ) {
  47. parent::__construct($context, $configStructure, $sectionChecker);
  48. $this->_configFactory = $configFactory;
  49. $this->_cache = $cache;
  50. $this->string = $string;
  51. }
  52. /**
  53. * Get groups for save
  54. *
  55. * @return array|null
  56. */
  57. protected function _getGroupsForSave()
  58. {
  59. $groups = $this->getRequest()->getPost('groups');
  60. $files = $this->getRequest()->getFiles('groups');
  61. if ($files && is_array($files)) {
  62. /**
  63. * Carefully merge $_FILES and $_POST information
  64. * None of '+=' or 'array_merge_recursive' can do this correct
  65. */
  66. foreach ($files as $groupName => $group) {
  67. $data = $this->_processNestedGroups($group);
  68. if (!empty($data)) {
  69. if (!empty($groups[$groupName])) {
  70. $groups[$groupName] = array_merge_recursive((array)$groups[$groupName], $data);
  71. } else {
  72. $groups[$groupName] = $data;
  73. }
  74. }
  75. }
  76. }
  77. return $groups;
  78. }
  79. /**
  80. * Process nested groups
  81. *
  82. * @param mixed $group
  83. * @return array
  84. */
  85. protected function _processNestedGroups($group)
  86. {
  87. $data = [];
  88. if (isset($group['fields']) && is_array($group['fields'])) {
  89. foreach ($group['fields'] as $fieldName => $field) {
  90. if (!empty($field['value'])) {
  91. $data['fields'][$fieldName] = ['value' => $field['value']];
  92. }
  93. }
  94. }
  95. if (isset($group['groups']) && is_array($group['groups'])) {
  96. foreach ($group['groups'] as $groupName => $groupData) {
  97. $nestedGroup = $this->_processNestedGroups($groupData);
  98. if (!empty($nestedGroup)) {
  99. $data['groups'][$groupName] = $nestedGroup;
  100. }
  101. }
  102. }
  103. return $data;
  104. }
  105. /**
  106. * Custom save logic for section
  107. *
  108. * @return void
  109. */
  110. protected function _saveSection()
  111. {
  112. $method = '_save' . $this->string->upperCaseWords($this->getRequest()->getParam('section'), '_', '');
  113. if (method_exists($this, $method)) {
  114. $this->{$method}();
  115. }
  116. }
  117. /**
  118. * Advanced save procedure
  119. *
  120. * @return void
  121. */
  122. protected function _saveAdvanced()
  123. {
  124. $this->_cache->clean();
  125. }
  126. /**
  127. * Save configuration
  128. *
  129. * @return \Magento\Backend\Model\View\Result\Redirect
  130. */
  131. public function execute()
  132. {
  133. try {
  134. // custom save logic
  135. $this->_saveSection();
  136. $section = $this->getRequest()->getParam('section');
  137. $website = $this->getRequest()->getParam('website');
  138. $store = $this->getRequest()->getParam('store');
  139. $configData = [
  140. 'section' => $section,
  141. 'website' => $website,
  142. 'store' => $store,
  143. 'groups' => $this->_getGroupsForSave(),
  144. ];
  145. /** @var \Magento\Config\Model\Config $configModel */
  146. $configModel = $this->_configFactory->create(['data' => $configData]);
  147. $configModel->save();
  148. $this->_eventManager->dispatch('admin_system_config_save', [
  149. 'configData' => $configData,
  150. 'request' => $this->getRequest()
  151. ]);
  152. $this->messageManager->addSuccess(__('You saved the configuration.'));
  153. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  154. $messages = explode("\n", $e->getMessage());
  155. foreach ($messages as $message) {
  156. $this->messageManager->addError($message);
  157. }
  158. } catch (\Exception $e) {
  159. $this->messageManager->addException(
  160. $e,
  161. __('Something went wrong while saving this configuration:') . ' ' . $e->getMessage()
  162. );
  163. }
  164. $this->_saveState($this->getRequest()->getPost('config_state'));
  165. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  166. $resultRedirect = $this->resultRedirectFactory->create();
  167. return $resultRedirect->setPath(
  168. 'adminhtml/system_config/edit',
  169. [
  170. '_current' => ['section', 'website', 'store'],
  171. '_nosid' => true
  172. ]
  173. );
  174. }
  175. }