PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/crazybutterfly815/magento2
PHP | 317 lines | 193 code | 29 blank | 95 comment | 11 complexity | 27018a8c77e4d653dc983608c97313b8 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\Framework\App\Area;
  8. use Magento\Framework\View\Design\ThemeInterface;
  9. use Magento\Theme\Model\Theme\Collection;
  10. /**
  11. * Theme form, general tab
  12. *
  13. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  14. */
  15. class General extends \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\AbstractTab
  16. {
  17. /**
  18. * Whether theme is editable
  19. *
  20. * @var bool
  21. */
  22. protected $_isThemeEditable = false;
  23. /**
  24. * @var \Magento\Framework\File\Size
  25. */
  26. protected $_fileSize;
  27. /**
  28. * Constructor
  29. *
  30. * @param \Magento\Backend\Block\Template\Context $context
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magento\Framework\Data\FormFactory $formFactory
  33. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  34. * @param \Magento\Framework\File\Size $fileSize
  35. * @param array $data
  36. */
  37. public function __construct(
  38. \Magento\Backend\Block\Template\Context $context,
  39. \Magento\Framework\Registry $registry,
  40. \Magento\Framework\Data\FormFactory $formFactory,
  41. \Magento\Framework\ObjectManagerInterface $objectManager,
  42. \Magento\Framework\File\Size $fileSize,
  43. array $data = []
  44. ) {
  45. $this->_fileSize = $fileSize;
  46. parent::__construct($context, $registry, $formFactory, $objectManager, $data);
  47. }
  48. /**
  49. * Create a form element with necessary controls
  50. *
  51. * @return $this
  52. */
  53. protected function _prepareForm()
  54. {
  55. /** @var \Magento\Backend\Model\Session $session */
  56. $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
  57. $formDataFromSession = $session->getThemeData();
  58. $this->_isThemeEditable = $this->_getCurrentTheme()->isEditable();
  59. /** @var ThemeInterface $currentTheme */
  60. $currentTheme = $this->_getCurrentTheme();
  61. $formData = $currentTheme->getData();
  62. if ($formDataFromSession && isset($formData['theme_id'])) {
  63. unset($formDataFromSession['preview_image']);
  64. $formData = array_merge($formData, $formDataFromSession);
  65. $session->setThemeData(null);
  66. }
  67. $this->setIsThemeExist(isset($formData['theme_id']));
  68. /** @var \Magento\Framework\Data\Form $form */
  69. $form = $this->_formFactory->create();
  70. $this->_addThemeFieldset($form, $formData, $currentTheme);
  71. if (!$this->getIsThemeExist()) {
  72. $formData = array_merge($formData, $this->_getDefaults());
  73. }
  74. $form->addValues($formData);
  75. $form->setFieldNameSuffix('theme');
  76. $this->setForm($form);
  77. return $this;
  78. }
  79. /**
  80. * Add theme fieldset
  81. *
  82. * @param \Magento\Framework\Data\Form $form
  83. * @param array $formData
  84. * @param ThemeInterface $theme
  85. * @return $this
  86. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  87. */
  88. protected function _addThemeFieldset($form, $formData, ThemeInterface $theme)
  89. {
  90. $themeFieldset = $form->addFieldset('theme', ['legend' => __('Theme Settings')]);
  91. $this->_addElementTypes($themeFieldset);
  92. if (isset($formData['theme_id'])) {
  93. $themeFieldset->addField('theme_id', 'hidden', ['name' => 'theme_id']);
  94. }
  95. /** @var Collection $themesCollections */
  96. $themesCollections = $this->_objectManager->create(\Magento\Theme\Model\Theme\Collection::class);
  97. /** @var \Magento\Framework\Json\Helper\Data $helper */
  98. $helper = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class);
  99. $themesCollections->addConstraint(Collection::CONSTRAINT_AREA, Area::AREA_FRONTEND);
  100. $onChangeScript = sprintf(
  101. 'parentThemeOnChange(this.value, %s)',
  102. str_replace(
  103. '"',
  104. '\'',
  105. $helper->jsonEncode($this->_getDefaultsInherited($themesCollections))
  106. )
  107. );
  108. /** @var ThemeInterface $parentTheme */
  109. $parentTheme = $this->_objectManager->create(\Magento\Framework\View\Design\ThemeInterface::class);
  110. if (!empty($formData['parent_id'])) {
  111. $parentTheme->load($formData['parent_id']);
  112. }
  113. if ($this->_getCurrentTheme()->isObjectNew()) {
  114. $themeFieldset->addField(
  115. 'parent_id',
  116. 'select',
  117. [
  118. 'label' => __('Parent Theme'),
  119. 'title' => __('Parent Theme'),
  120. 'name' => 'parent_id',
  121. 'values' => $themesCollections->toOptionArray(!$parentTheme->getId()),
  122. 'required' => true,
  123. 'class' => 'no-changes',
  124. 'onchange' => $onChangeScript
  125. ]
  126. );
  127. } elseif (!empty($formData['parent_id'])) {
  128. $themeFieldset->addField(
  129. 'parent_title',
  130. 'note',
  131. [
  132. 'label' => __('Parent Theme'),
  133. 'title' => __('Parent Theme'),
  134. 'name' => 'parent_title',
  135. 'text' => $parentTheme->getId() ? $parentTheme->getThemeTitle() : ''
  136. ]
  137. );
  138. }
  139. if (!empty($formData['theme_path'])) {
  140. $themeFieldset->addField(
  141. 'theme_path',
  142. 'label',
  143. ['label' => __('Theme Path'), 'title' => __('Theme Path'), 'name' => 'theme_code']
  144. );
  145. }
  146. $themeFieldset->addField(
  147. 'theme_title',
  148. $this->_getFieldTextType(),
  149. [
  150. 'label' => __('Theme Title'),
  151. 'title' => __('Theme Title'),
  152. 'name' => 'theme_title',
  153. 'required' => $this->_isFieldAttrRequired()
  154. ]
  155. );
  156. if ($this->_isThemeEditable) {
  157. $themeFieldset->addField(
  158. 'preview_image',
  159. 'image',
  160. [
  161. 'label' => __('Theme Preview Image'),
  162. 'title' => __('Theme Preview Image'),
  163. 'name' => 'preview',
  164. 'required' => false,
  165. 'note' => $this->_getPreviewImageNote(),
  166. 'theme' => $theme
  167. ]
  168. );
  169. } elseif ($theme->hasPreviewImage()) {
  170. $themeFieldset->addField(
  171. 'preview_image',
  172. 'note',
  173. [
  174. 'label' => __('Theme Preview Image'),
  175. 'title' => __('Theme Preview Image'),
  176. 'name' => 'preview',
  177. 'after_element_html' => '<a href="'
  178. . $theme->getThemeImage()->getPreviewImageUrl()
  179. . '" onclick="imagePreview(\'theme_preview_image\'); return false;">'
  180. . '<img width="50" src="'
  181. . $theme->getThemeImage()->getPreviewImageUrl()
  182. . '" id="theme_preview_image" /></a>'
  183. ]
  184. );
  185. }
  186. return $this;
  187. }
  188. /**
  189. * No field notes if theme is not editable
  190. *
  191. * @param string $text
  192. * @return string
  193. */
  194. protected function _filterFieldNote($text)
  195. {
  196. return $this->_isThemeEditable ? $text : '';
  197. }
  198. /**
  199. * Field is not marked as required if theme is not editable
  200. *
  201. * @return bool
  202. */
  203. protected function _isFieldAttrRequired()
  204. {
  205. return $this->_isThemeEditable ? true : false;
  206. }
  207. /**
  208. * Text field replaced to label if theme is not editable
  209. *
  210. * @return string
  211. */
  212. protected function _getFieldTextType()
  213. {
  214. return $this->_isThemeEditable ? 'text' : 'label';
  215. }
  216. /**
  217. * Set additional form field type for theme preview image
  218. *
  219. * @return array
  220. */
  221. protected function _getAdditionalElementTypes()
  222. {
  223. $element = \Magento\Theme\Block\Adminhtml\System\Design\Theme\Edit\Form\Element\Image::class;
  224. return ['image' => $element];
  225. }
  226. /**
  227. * Prepare label for tab
  228. *
  229. * @return \Magento\Framework\Phrase
  230. */
  231. public function getTabLabel()
  232. {
  233. return __('General');
  234. }
  235. /**
  236. * Returns status flag about this tab can be shown or not
  237. *
  238. * @return bool
  239. */
  240. public function canShowTab()
  241. {
  242. return true;
  243. }
  244. /**
  245. * Get theme default values
  246. *
  247. * @return array
  248. */
  249. protected function _getDefaults()
  250. {
  251. $defaults = [];
  252. $defaults['theme_title'] = __('New Theme');
  253. return $defaults;
  254. }
  255. /**
  256. * Get theme default values while inheriting other theme
  257. *
  258. * @param array $themesCollections
  259. * @return array
  260. */
  261. protected function _getDefaultsInherited($themesCollections)
  262. {
  263. $data = ['' => $this->_getDefaults()];
  264. /** @var ThemeInterface $theme */
  265. foreach ($themesCollections as $theme) {
  266. $theme->load($theme->getThemePath(), 'theme_path');
  267. if (!$theme->getId()) {
  268. continue;
  269. }
  270. $data[$theme->getId()] = ['theme_title' => __('Copy of %1', $theme->getThemeTitle())];
  271. }
  272. return $data;
  273. }
  274. /**
  275. * Get note string for theme's preview image
  276. *
  277. * @return \Magento\Framework\Phrase
  278. */
  279. protected function _getPreviewImageNote()
  280. {
  281. $maxImageSize = $this->_fileSize->getMaxFileSizeInMb();
  282. if ($maxImageSize) {
  283. return __('Max image size %1M', $maxImageSize);
  284. } else {
  285. return __('Something is wrong with the file upload settings.');
  286. }
  287. }
  288. }