PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Theme/Block/Adminhtml/System/Design/Theme/Edit/Tab/Css.php

https://gitlab.com/svillegas/magento2
PHP | 301 lines | 189 code | 26 blank | 86 comment | 3 complexity | ebd745b1fa277bf8c6ba7c4c78e64346 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\Tab;
  7. use Magento\Theme\Helper\Storage;
  8. /**
  9. * Theme form, Css editor tab
  10. *
  11. * @method \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\Tab\Css setFiles(array $files)
  12. * @method array getFiles()
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  16. */
  17. class Css extends \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\AbstractTab
  18. {
  19. /**
  20. * Uploader service
  21. *
  22. * @var \Magento\Theme\Model\Uploader\Service
  23. */
  24. protected $_uploaderService;
  25. /**
  26. * Theme custom css file
  27. *
  28. * @var \Magento\Theme\Model\Theme\File
  29. */
  30. protected $_customCssFile;
  31. /**
  32. * @var \Magento\Framework\Encryption\UrlCoder
  33. */
  34. protected $urlCoder;
  35. /**
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\Data\FormFactory $formFactory
  39. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  40. * @param \Magento\Theme\Model\Uploader\Service $uploaderService
  41. * @param \Magento\Framework\Encryption\UrlCoder $urlCoder
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Backend\Block\Template\Context $context,
  46. \Magento\Framework\Registry $registry,
  47. \Magento\Framework\Data\FormFactory $formFactory,
  48. \Magento\Framework\ObjectManagerInterface $objectManager,
  49. \Magento\Theme\Model\Uploader\Service $uploaderService,
  50. \Magento\Framework\Encryption\UrlCoder $urlCoder,
  51. array $data = []
  52. ) {
  53. parent::__construct($context, $registry, $formFactory, $objectManager, $data);
  54. $this->_uploaderService = $uploaderService;
  55. $this->urlCoder = $urlCoder;
  56. }
  57. /**
  58. * Create a form element with necessary controls
  59. *
  60. * @return $this
  61. */
  62. protected function _prepareForm()
  63. {
  64. /** @var \Magento\Framework\Data\Form $form */
  65. $form = $this->_formFactory->create();
  66. $this->setForm($form);
  67. $this->_addThemeCssFieldset();
  68. $customFiles = $this->_getCurrentTheme()->getCustomization()->getFilesByType(
  69. \Magento\Theme\Model\Theme\Customization\File\CustomCss::TYPE
  70. );
  71. $this->_customCssFile = reset($customFiles);
  72. $this->_addCustomCssFieldset();
  73. $formData['custom_css_content'] = $this->_customCssFile ? $this->_customCssFile->getContent() : null;
  74. /** @var $session \Magento\Backend\Model\Session */
  75. $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
  76. $cssFileContent = $session->getThemeCustomCssData();
  77. if ($cssFileContent) {
  78. $formData['custom_css_content'] = $cssFileContent;
  79. $session->unsThemeCustomCssData();
  80. }
  81. $form->addValues($formData);
  82. parent::_prepareForm();
  83. return $this;
  84. }
  85. /**
  86. * Set theme css fieldset
  87. *
  88. * @return $this
  89. */
  90. protected function _addThemeCssFieldset()
  91. {
  92. $form = $this->getForm();
  93. $themeFieldset = $form->addFieldset(
  94. 'theme_css',
  95. ['legend' => __('Theme CSS'), 'class' => 'fieldset-wide']
  96. );
  97. $this->_addElementTypes($themeFieldset);
  98. $links = [];
  99. /** @var \Magento\Framework\View\Asset\LocalInterface $asset */
  100. foreach ($this->getFiles() as $fileId => $asset) {
  101. $links[$fileId] = [
  102. 'href' => $this->getDownloadUrl($fileId, $this->_getCurrentTheme()->getId()),
  103. 'label' => $fileId,
  104. 'title' => $asset->getPath(),
  105. 'delimiter' => '<br />',
  106. ];
  107. }
  108. $themeFieldset->addField(
  109. 'theme_css_view_assets',
  110. 'links',
  111. [
  112. 'label' => __('Theme CSS Assets'),
  113. 'title' => __('Theme CSS Assets'),
  114. 'name' => 'links',
  115. 'values' => $links,
  116. ]
  117. );
  118. return $this;
  119. }
  120. /**
  121. * Set custom css fieldset
  122. *
  123. * @return $this
  124. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  125. */
  126. protected function _addCustomCssFieldset()
  127. {
  128. $form = $this->getForm();
  129. $themeFieldset = $form->addFieldset(
  130. 'custom_css',
  131. ['legend' => __('Custom CSS'), 'class' => 'fieldset-wide']
  132. );
  133. $this->_addElementTypes($themeFieldset);
  134. $themeFieldset->addField(
  135. 'css_file_uploader',
  136. 'css_file',
  137. [
  138. 'name' => 'css_file_uploader',
  139. 'label' => __('Select CSS File to Upload'),
  140. 'title' => __('Select CSS File to Upload'),
  141. 'accept' => 'text/css',
  142. 'note' => $this->_getUploadCssFileNote()
  143. ]
  144. );
  145. $themeFieldset->addField(
  146. 'css_uploader_button',
  147. 'button',
  148. ['name' => 'css_uploader_button', 'value' => __('Upload CSS File'), 'disabled' => 'disabled']
  149. );
  150. $downloadButtonConfig = [
  151. 'name' => 'css_download_button',
  152. 'value' => __('Download CSS File'),
  153. 'onclick' => "setLocation('" . $this->getUrl(
  154. 'adminhtml/*/downloadCustomCss',
  155. ['theme_id' => $this->_getCurrentTheme()->getId()]
  156. ) . "');",
  157. ];
  158. if (!$this->_customCssFile) {
  159. $downloadButtonConfig['disabled'] = 'disabled';
  160. }
  161. $themeFieldset->addField('css_download_button', 'button', $downloadButtonConfig);
  162. /** @var $imageButton \Magento\Backend\Block\Widget\Button */
  163. $imageButton = $this->getLayout()->createBlock(
  164. \Magento\Backend\Block\Widget\Button::class
  165. )->setData(
  166. [
  167. 'id' => 'css_images_manager',
  168. 'label' => __('Manage'),
  169. 'class' => 'button',
  170. 'onclick' => "MediabrowserUtility.openDialog('" . $this->getUrl(
  171. 'adminhtml/system_design_wysiwyg_files/index',
  172. [
  173. 'target_element_id' => 'custom_css_content',
  174. Storage::PARAM_THEME_ID => $this->_getCurrentTheme()->getId(),
  175. Storage::PARAM_CONTENT_TYPE => \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE
  176. ]
  177. ) . "', null, null,'" . $this->escapeJs(
  178. __('Upload Images'),
  179. true
  180. ) . "');",
  181. ]
  182. );
  183. $themeFieldset->addField(
  184. 'css_browse_image_button',
  185. 'note',
  186. ['label' => __("Images Assets"), 'text' => $imageButton->toHtml()]
  187. );
  188. /** @var $fontButton \Magento\Backend\Block\Widget\Button */
  189. $fontButton = $this->getLayout()->createBlock(
  190. \Magento\Backend\Block\Widget\Button::class
  191. )->setData(
  192. [
  193. 'id' => 'css_fonts_manager',
  194. 'label' => __('Manage'),
  195. 'class' => 'button',
  196. 'onclick' => "MediabrowserUtility.openDialog('" . $this->getUrl(
  197. 'adminhtml/system_design_wysiwyg_files/index',
  198. [
  199. 'target_element_id' => 'custom_css_content',
  200. Storage::PARAM_THEME_ID => $this->_getCurrentTheme()->getId(),
  201. Storage::PARAM_CONTENT_TYPE => \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT
  202. ]
  203. ) . "', null, null,'" . $this->escapeJs(
  204. __('Upload Fonts'),
  205. true
  206. ) . "');",
  207. ]
  208. );
  209. $themeFieldset->addField(
  210. 'css_browse_font_button',
  211. 'note',
  212. ['label' => __("Fonts Assets"), 'text' => $fontButton->toHtml()]
  213. );
  214. $themeFieldset->addField(
  215. 'custom_css_content',
  216. 'textarea',
  217. ['label' => __('Edit custom.css'), 'title' => __('Edit custom.css'), 'name' => 'custom_css_content']
  218. );
  219. return $this;
  220. }
  221. /**
  222. * Get note string for css file to Upload
  223. *
  224. * @return string
  225. */
  226. protected function _getUploadCssFileNote()
  227. {
  228. $messages = [
  229. __('Allowed file types *.css.'),
  230. __('This file will replace the current custom.css file and can\'t be more than 2 MB.'),
  231. ];
  232. $maxFileSize = $this->_objectManager->get(\Magento\Framework\File\Size::class)->getMaxFileSizeInMb();
  233. if ($maxFileSize) {
  234. $messages[] = __('Max file size to upload %1M', $maxFileSize);
  235. } else {
  236. $messages[] = __('Something is wrong with the file upload settings.');
  237. }
  238. return implode('<br />', $messages);
  239. }
  240. /**
  241. * Set additional form field type for theme preview image
  242. *
  243. * @return array
  244. */
  245. protected function _getAdditionalElementTypes()
  246. {
  247. $linksElement = \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\Form\Element\Links::class;
  248. $fileElement = \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\Form\Element\File::class;
  249. return ['links' => $linksElement, 'css_file' => $fileElement];
  250. }
  251. /**
  252. * Return Tab label
  253. *
  254. * @return \Magento\Framework\Phrase
  255. */
  256. public function getTabLabel()
  257. {
  258. return __('CSS Editor');
  259. }
  260. /**
  261. * Get URL to download CSS file
  262. *
  263. * @param string $fileId
  264. * @param int $themeId
  265. * @return string
  266. */
  267. public function getDownloadUrl($fileId, $themeId)
  268. {
  269. return $this->getUrl(
  270. 'adminhtml/*/downloadCss',
  271. ['theme_id' => $themeId, 'file' => $this->urlCoder->encode($fileId)]
  272. );
  273. }
  274. }