UploadJs.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 UploadJs
  10. * @deprecated 101.0.0
  11. */
  12. class UploadJs extends \Magento\Theme\Controller\Adminhtml\System\Design\Theme
  13. {
  14. /**
  15. * Upload js file
  16. *
  17. * @return void
  18. * @throws \Magento\Framework\Exception\LocalizedException
  19. */
  20. public function execute()
  21. {
  22. $themeId = $this->getRequest()->getParam('id');
  23. /** @var $serviceModel \Magento\Theme\Model\Uploader\Service */
  24. $serviceModel = $this->_objectManager->get(\Magento\Theme\Model\Uploader\Service::class);
  25. /** @var $themeFactory \Magento\Framework\View\Design\Theme\FlyweightFactory */
  26. $themeFactory = $this->_objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
  27. /** @var $jsService \Magento\Framework\View\Design\Theme\Customization\File\Js */
  28. $jsService = $this->_objectManager->get(\Magento\Framework\View\Design\Theme\Customization\File\Js::class);
  29. try {
  30. $theme = $themeFactory->create($themeId);
  31. if (!$theme) {
  32. throw new \Magento\Framework\Exception\LocalizedException(
  33. __('We cannot find a theme with id "%1".', $themeId)
  34. );
  35. }
  36. $jsFileData = $serviceModel->uploadJsFile('js_files_uploader');
  37. $jsFile = $jsService->create();
  38. $jsFile->setTheme($theme);
  39. $jsFile->setFileName($jsFileData['filename']);
  40. $jsFile->setData('content', $jsFileData['content']);
  41. $jsFile->save();
  42. /** @var $customization \Magento\Framework\View\Design\Theme\Customization */
  43. $customization = $this->_objectManager->create(
  44. \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
  45. ['theme' => $theme]
  46. );
  47. $customJsFiles = $customization->getFilesByType(
  48. \Magento\Framework\View\Design\Theme\Customization\File\Js::TYPE
  49. );
  50. $result = ['error' => false, 'files' => $customization->generateFileInfo($customJsFiles)];
  51. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  52. $result = ['error' => true, 'message' => $e->getMessage()];
  53. } catch (\Exception $e) {
  54. $result = ['error' => true, 'message' => __('We can\'t upload the JS file right now.')];
  55. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  56. }
  57. $this->getResponse()->representJson(
  58. $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
  59. );
  60. }
  61. }