Save.php 4.2 KB

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