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

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

https://bitbucket.org/jokusafet/magento2
PHP | 344 lines | 196 code | 36 blank | 112 comment | 15 complexity | 1177bd48cf333b676d3a2d961c087e8c MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Theme
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Theme form, general tab
  28. */
  29. class Mage_Theme_Block_Adminhtml_System_Design_Theme_Edit_Tab_General
  30. extends Mage_Backend_Block_Widget_Form
  31. implements Mage_Backend_Block_Widget_Tab_Interface
  32. {
  33. /**
  34. * Whether theme is editable
  35. *
  36. * @var bool
  37. */
  38. protected $_isThemeEditable = false;
  39. /**
  40. * Create a form element with necessary controls
  41. *
  42. * @return Mage_Theme_Block_Adminhtml_System_Design_Theme_Edit_Tab_General|Mage_Backend_Block_Widget_Form
  43. */
  44. protected function _prepareForm()
  45. {
  46. /** @var $session Mage_Backend_Model_Session */
  47. $session = Mage::getSingleton('Mage_Backend_Model_Session');
  48. $formDataFromSession = $session->getThemeData();
  49. $this->_isThemeEditable = $this->_getCurrentTheme()->isVirtual();
  50. $formData = $this->_getCurrentTheme()->getData();
  51. if ($formDataFromSession && isset($formData['theme_id'])) {
  52. unset($formDataFromSession['preview_image']);
  53. $formData = array_merge($formData, $formDataFromSession);
  54. $session->setThemeData(null);
  55. }
  56. $this->setIsThemeExist(isset($formData['theme_id']));
  57. $form = new Varien_Data_Form();
  58. $this->_addThemeFieldset($form, $formData)->_addRequirementsFieldset($form);
  59. if (!$this->getIsThemeExist()) {
  60. $formData = array_merge($formData, $this->_getDefaults());
  61. }
  62. $form->addValues($formData);
  63. $form->setFieldNameSuffix('theme');
  64. $this->setForm($form);
  65. return $this;
  66. }
  67. /**
  68. * Add theme fieldset
  69. *
  70. * @param Varien_Data_Form $form
  71. * @param array $formData
  72. * @return Mage_Theme_Block_Adminhtml_System_Design_Theme_Edit_Tab_General
  73. */
  74. protected function _addThemeFieldset($form, $formData)
  75. {
  76. $themeFieldset = $form->addFieldset('theme', array(
  77. 'legend' => $this->__('Theme Settings'),
  78. ));
  79. $this->_addElementTypes($themeFieldset);
  80. if (isset($formData['theme_id'])) {
  81. $themeFieldset->addField('theme_id', 'hidden', array(
  82. 'name' => 'theme_id'
  83. ));
  84. }
  85. /** @var $themesCollections Mage_Core_Model_Theme_Collection */
  86. $themesCollections = Mage::getResourceModel('Mage_Core_Model_Theme_Collection');
  87. /** @var $helper Mage_Core_Helper_Data */
  88. $helper = Mage::helper('Mage_Core_Helper_Data');
  89. $onChangeScript = sprintf('parentThemeOnChange(this.value, %s)', str_replace(
  90. '"', '\'', $helper->jsonEncode($this->_getDefaultsInherited($themesCollections->addDefaultPattern()))
  91. ));
  92. /** @var $parentTheme Mage_Core_Model_Theme */
  93. $parentTheme = Mage::getModel('Mage_Core_Model_Theme');
  94. if (!empty($formData['parent_id'])) {
  95. $parentTheme->load($formData['parent_id']);
  96. }
  97. if ($this->_isThemeEditable) {
  98. $themeFieldset->addField('parent_id', 'select', array(
  99. 'label' => $this->__('Parent theme'),
  100. 'title' => $this->__('Parent theme'),
  101. 'name' => 'parent_id',
  102. 'values' => $themesCollections->toOptionArray(!$parentTheme->getId()),
  103. 'required' => true,
  104. 'class' => 'no-changes',
  105. 'onchange' => $onChangeScript
  106. ));
  107. } else if (!empty($formData['parent_id'])) {
  108. $themeFieldset->addField('parent_title', 'note', array(
  109. 'label' => $this->__('Parent theme'),
  110. 'title' => $this->__('Parent theme'),
  111. 'name' => 'parent_title',
  112. 'text' => $parentTheme->getId() ? $parentTheme->getThemeTitle() : ''
  113. ));
  114. }
  115. if (!empty($formData['theme_path'])) {
  116. $themeFieldset->addField('theme_path', 'label', array(
  117. 'label' => $this->__('Theme Path'),
  118. 'title' => $this->__('Theme Path'),
  119. 'name' => 'theme_code',
  120. ));
  121. }
  122. $themeFieldset->addField('theme_version', $this->_getFieldTextType(), array(
  123. 'label' => $this->__('Theme Version'),
  124. 'title' => $this->__('Theme Version'),
  125. 'name' => 'theme_version',
  126. 'required' => $this->_getFieldAttrRequired(),
  127. 'note' => $this->_filterFieldNote($this->__('Example: 0.0.0.1 or 123.1.0.25-alpha1'))
  128. ));
  129. $themeFieldset->addField('theme_title', $this->_getFieldTextType(), array(
  130. 'label' => $this->__('Theme Title'),
  131. 'title' => $this->__('Theme Title'),
  132. 'name' => 'theme_title',
  133. 'required' => $this->_getFieldAttrRequired()
  134. ));
  135. if ($this->_isThemeEditable) {
  136. $maxImageSize = Mage::helper('Mage_Core_Helper_File_Storage')->getMaxFileSizeInMb();
  137. if ($maxImageSize) {
  138. $previewImageNote = $this->__('Max image size %sM', $maxImageSize);
  139. } else {
  140. $previewImageNote = $this->__('System doesn\'t allow to get file upload settings');
  141. }
  142. $themeFieldset->addField('preview_image', 'image', array(
  143. 'label' => $this->__('Theme Preview Image'),
  144. 'title' => $this->__('Theme Preview Image'),
  145. 'name' => 'preview_image',
  146. 'required' => false,
  147. 'note' => $previewImageNote
  148. ));
  149. } else if (!empty($formData['preview_image'])) {
  150. $themeFieldset->addField('preview_image', 'note', array(
  151. 'label' => $this->__('Theme Preview Image'),
  152. 'title' => $this->__('Theme Preview Image'),
  153. 'name' => 'preview_image',
  154. 'after_element_html' => '<img width="50" src="' . Mage_Core_Model_Theme::getPreviewImageDirectoryUrl()
  155. . $formData['preview_image'] . '" />'
  156. ));
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Add requirements fieldset
  162. *
  163. * @param Varien_Data_Form $form
  164. * @return Mage_Theme_Block_Adminhtml_System_Design_Theme_Edit_Tab_General
  165. */
  166. protected function _addRequirementsFieldset($form)
  167. {
  168. $requirementsFieldset = $form->addFieldset('requirements', array(
  169. 'legend' => $this->__('Magento Requirements'),
  170. ));
  171. $requirementsFieldset->addField('magento_version_from', $this->_getFieldTextType(), array(
  172. 'label' => $this->__('Magento Version From'),
  173. 'title' => $this->__('Magento Version From'),
  174. 'name' => 'magento_version_from',
  175. 'required' => $this->_getFieldAttrRequired(),
  176. 'note' => $this->_filterFieldNote($this->__('Example: 1.6.0.0 or *'))
  177. ));
  178. $requirementsFieldset->addField('magento_version_to', $this->_getFieldTextType(), array(
  179. 'label' => $this->__('Magento Version To'),
  180. 'title' => $this->__('Magento Version To'),
  181. 'name' => 'magento_version_to',
  182. 'required' => $this->_getFieldAttrRequired(),
  183. 'note' => $this->_filterFieldNote($this->__('Example: 1.6.0.0 or *'))
  184. ));
  185. return $this;
  186. }
  187. /**
  188. * No field notes if theme is not editable
  189. *
  190. * @param $text
  191. * @return string
  192. */
  193. protected function _filterFieldNote($text)
  194. {
  195. return $this->_isThemeEditable ? $text : '';
  196. }
  197. /**
  198. * Field is not marked as required if theme is not editable
  199. *
  200. * @return bool
  201. */
  202. protected function _getFieldAttrRequired()
  203. {
  204. return $this->_isThemeEditable ? true : false;
  205. }
  206. /**
  207. * Text field replaced to label if theme is not editable
  208. *
  209. * @return string
  210. */
  211. protected function _getFieldTextType()
  212. {
  213. return $this->_isThemeEditable ? 'text' : 'label';
  214. }
  215. /**
  216. * Set additional form field type for theme preview image
  217. *
  218. * @return array
  219. */
  220. protected function _getAdditionalElementTypes()
  221. {
  222. $element = Mage::getConfig()
  223. ->getBlockClassName('Mage_Theme_Block_Adminhtml_System_Design_Theme_Edit_Form_Element_Image');
  224. return array('image' => $element);
  225. }
  226. /**
  227. * Get current theme
  228. *
  229. * @return Mage_Core_Model_Theme
  230. */
  231. protected function _getCurrentTheme()
  232. {
  233. return Mage::registry('current_theme');
  234. }
  235. /**
  236. * Prepare label for tab
  237. *
  238. * @return string
  239. */
  240. public function getTabLabel()
  241. {
  242. return $this->__('General');
  243. }
  244. /**
  245. * Prepare title for tab
  246. *
  247. * @return string
  248. */
  249. public function getTabTitle()
  250. {
  251. return $this->__('General');
  252. }
  253. /**
  254. * Returns status flag about this tab can be shown or not
  255. *
  256. * @return bool
  257. */
  258. public function canShowTab()
  259. {
  260. return true;
  261. }
  262. /**
  263. * Returns status flag about this tab hidden or not
  264. *
  265. * @return bool
  266. */
  267. public function isHidden()
  268. {
  269. return false;
  270. }
  271. /**
  272. * Get theme default values
  273. *
  274. * @return array
  275. */
  276. protected function _getDefaults()
  277. {
  278. $defaults = array();
  279. $defaults['magento_version_from'] = Mage::getVersion();
  280. $defaults['magento_version_to'] = '*';
  281. $defaults['theme_version'] = '0.0.0.1';
  282. $defaults['theme_title'] = $this->__('New theme');
  283. return $defaults;
  284. }
  285. /**
  286. * Get theme default values while inheriting other theme
  287. *
  288. * @param $themesCollections
  289. * @return array
  290. */
  291. protected function _getDefaultsInherited($themesCollections)
  292. {
  293. $data = array('' => $this->_getDefaults());
  294. /** @var $theme Mage_Core_Model_Theme */
  295. foreach ($themesCollections as $theme) {
  296. $theme->load($theme->getThemePath(), 'theme_path');
  297. if (!$theme->getId()) {
  298. continue;
  299. }
  300. $data[$theme->getId()] = array(
  301. 'theme_title' => $this->__('Copy of %s', $theme->getThemeTitle()),
  302. 'magento_version_from' => $theme->getMagentoVersionFrom(),
  303. 'magento_version_to' => $theme->getMagentoVersionTo()
  304. );
  305. }
  306. return $data;
  307. }
  308. }