Save.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Theme\Controller\Adminhtml\System\Design\Theme;
  8. /**
  9. * Class Save
  10. * @deprecated 100.2.0
  11. */
  12. class Save extends \Magento\Theme\Controller\Adminhtml\System\Design\Theme
  13. {
  14. /**
  15. * Save action
  16. *
  17. * @return void
  18. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  19. */
  20. public function execute()
  21. {
  22. $redirectBack = (bool)$this->getRequest()->getParam('back', false);
  23. $themeData = $this->getRequest()->getParam('theme');
  24. $customCssData = $this->getRequest()->getParam('custom_css_content');
  25. $removeJsFiles = (array)$this->getRequest()->getParam('js_removed_files');
  26. $reorderJsFiles = array_keys($this->getRequest()->getParam('js_order', []));
  27. /** @var $themeFactory \Magento\Framework\View\Design\Theme\FlyweightFactory */
  28. $themeFactory = $this->_objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
  29. /** @var $cssService \Magento\Theme\Model\Theme\Customization\File\CustomCss */
  30. $cssService = $this->_objectManager->get(\Magento\Theme\Model\Theme\Customization\File\CustomCss::class);
  31. /** @var $singleFile \Magento\Theme\Model\Theme\SingleFile */
  32. $singleFile = $this->_objectManager->create(
  33. \Magento\Theme\Model\Theme\SingleFile::class,
  34. ['fileService' => $cssService]
  35. );
  36. try {
  37. if ($this->getRequest()->getPostValue()) {
  38. /** @var $theme \Magento\Theme\Model\Theme */
  39. if (!empty($themeData['theme_id'])) {
  40. $theme = $themeFactory->create($themeData['theme_id']);
  41. } else {
  42. $parentTheme = $themeFactory->create($themeData['parent_id']);
  43. $theme = $parentTheme->getDomainModel(
  44. \Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL
  45. )->createVirtualTheme(
  46. $parentTheme
  47. );
  48. }
  49. if ($theme && !$theme->isEditable()) {
  50. throw new \Magento\Framework\Exception\LocalizedException(__('This theme is not editable.'));
  51. }
  52. $theme->addData($themeData);
  53. if (isset($themeData['preview']['delete'])) {
  54. $theme->getThemeImage()->removePreviewImage();
  55. }
  56. $theme->getThemeImage()->uploadPreviewImage('preview');
  57. $theme->setType(\Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL);
  58. $theme->save();
  59. $customization = $theme->getCustomization();
  60. $customization->reorder(
  61. \Magento\Framework\View\Design\Theme\Customization\File\Js::TYPE,
  62. $reorderJsFiles
  63. );
  64. $customization->delete($removeJsFiles);
  65. $singleFile->update($theme, $customCssData);
  66. $this->messageManager->addSuccess(__('You saved the theme.'));
  67. }
  68. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  69. $this->messageManager->addError($e->getMessage());
  70. $this->_getSession()->setThemeData($themeData);
  71. $this->_getSession()->setThemeCustomCssData($customCssData);
  72. $redirectBack = true;
  73. } catch (\Exception $e) {
  74. $this->messageManager->addError('The theme was not saved');
  75. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  76. }
  77. $redirectBack
  78. ? $this->_redirect('adminhtml/*/edit', ['id' => $theme->getId()])
  79. : $this->_redirect('adminhtml/*/');
  80. }
  81. }