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

/app/code/Mage/DesignEditor/Model/Editor/Tools/Controls/Configuration.php

https://github.com/JackoPlane/magento2
PHP | 300 lines | 130 code | 24 blank | 146 comment | 6 complexity | 2af5cb77597ed4a89b178a082cf6aa84 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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_DesignEditor
  23. * @copyright Copyright (c) 2013 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. * Configuration of controls
  28. */
  29. class Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  30. {
  31. /**
  32. * Module name used for saving data to the view configuration
  33. */
  34. const SEPARATOR_MODULE = '::';
  35. /**
  36. * Application Event Dispatcher
  37. *
  38. * @var Mage_Core_Model_Event_Manager
  39. */
  40. protected $_eventDispatcher;
  41. /**
  42. * @var Mage_DesignEditor_Model_Config_Control_Abstract
  43. */
  44. protected $_configuration;
  45. /**
  46. * @var Mage_Core_Model_Design_Package
  47. */
  48. protected $_design;
  49. /**
  50. * @var Magento_Filesystem
  51. */
  52. protected $_filesystem;
  53. /**
  54. * @var Mage_Core_Model_Theme
  55. */
  56. protected $_theme;
  57. /**
  58. * @var Magento_Config_View
  59. */
  60. protected $_viewConfig;
  61. /**
  62. * @var Magento_Config_View
  63. */
  64. protected $_viewConfigParent;
  65. /**
  66. * Controls data
  67. *
  68. * @var array
  69. */
  70. protected $_data;
  71. /**
  72. * List of controls
  73. *
  74. * @var array
  75. */
  76. protected $_controlList = array();
  77. /**
  78. * Initialize dependencies
  79. *
  80. * @param Mage_Core_Model_Design_Package $design
  81. * @param Magento_Filesystem $filesystem
  82. * @param Mage_Core_Model_Event_Manager $eventDispatcher
  83. * @param Mage_DesignEditor_Model_Config_Control_Abstract|null $configuration
  84. * @param Mage_Core_Model_Theme|null $theme
  85. */
  86. public function __construct(
  87. Mage_Core_Model_Design_Package $design,
  88. Magento_Filesystem $filesystem,
  89. Mage_Core_Model_Event_Manager $eventDispatcher,
  90. Mage_DesignEditor_Model_Config_Control_Abstract $configuration = null,
  91. Mage_Core_Model_Theme $theme = null
  92. ) {
  93. $this->_configuration = $configuration;
  94. $this->_theme = $theme;
  95. $this->_design = $design;
  96. $this->_filesystem = $filesystem;
  97. $this->_eventDispatcher = $eventDispatcher;
  98. $this->_initViewConfigs()->_loadControlsData();
  99. }
  100. /**
  101. * Initialize view configurations
  102. *
  103. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  104. */
  105. protected function _initViewConfigs()
  106. {
  107. $this->_viewConfig = $this->_design->getViewConfig(array(
  108. 'area' => Mage_Core_Model_Design_Package::DEFAULT_AREA,
  109. 'themeModel' => $this->_theme
  110. ));
  111. $this->_viewConfigParent = $this->_design->getViewConfig(array(
  112. 'area' => Mage_Core_Model_Design_Package::DEFAULT_AREA,
  113. 'themeModel' => $this->_theme->getParentTheme()
  114. ));
  115. return $this;
  116. }
  117. /**
  118. * Load all control values
  119. *
  120. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  121. */
  122. protected function _loadControlsData()
  123. {
  124. $this->_data = $this->_configuration->getAllControlsData();
  125. $this->_prepareControlList($this->_data);
  126. foreach ($this->_controlList as &$control) {
  127. $this->_loadControlData($control, 'value', $this->_viewConfig);
  128. $this->_loadControlData($control, 'default', $this->_viewConfigParent);
  129. }
  130. return $this;
  131. }
  132. /**
  133. * Prepare list of control links
  134. *
  135. * @param array $controls
  136. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  137. */
  138. protected function _prepareControlList(array &$controls)
  139. {
  140. foreach ($controls as $controlName => &$control) {
  141. if (!empty($control['components'])) {
  142. $this->_prepareControlList($control['components']);
  143. }
  144. $this->_controlList[$controlName] = &$control;
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Load data item values and default values from the view configuration
  150. *
  151. * @param array $control
  152. * @param string $paramName
  153. * @param Magento_Config_View $viewConfiguration
  154. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  155. */
  156. protected function _loadControlData(array &$control, $paramName, Magento_Config_View $viewConfiguration)
  157. {
  158. if (!empty($control['var'])) {
  159. list($module, $varKey) = $this->_extractModuleKey($control['var']);
  160. $control[$paramName] = $viewConfiguration->getVarValue($module, $varKey);
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Load control data
  166. *
  167. * @return array
  168. */
  169. public function getAllControlsData()
  170. {
  171. return $this->_data;
  172. }
  173. /**
  174. * Get control data
  175. *
  176. * @param string $controlName
  177. * @return array
  178. * @throws Mage_Core_Exception
  179. */
  180. public function getControlData($controlName)
  181. {
  182. if (!isset($this->_controlList[$controlName])) {
  183. throw new Mage_Core_Exception("Unknown control: \"{$controlName}\"");
  184. }
  185. return $this->_controlList[$controlName];
  186. }
  187. /**
  188. * Extract module and key name
  189. *
  190. * @param string $value
  191. * @return array
  192. */
  193. protected function _extractModuleKey($value)
  194. {
  195. return explode(self::SEPARATOR_MODULE, $value);
  196. }
  197. /**
  198. * Extract var data keys for current controls configuration
  199. * array(module => array(varKey => array(controlName, controlValue)))
  200. *
  201. * @param array $controlsData
  202. * @param array $controls
  203. * @return array
  204. */
  205. protected function _prepareVarData(array $controlsData, array $controls)
  206. {
  207. $result = array();
  208. foreach ($controlsData as $controlName => $controlValue) {
  209. if (isset($controls[$controlName])) {
  210. list($module, $varKey) = $this->_extractModuleKey($controls[$controlName]['var']);
  211. $result[$module][$varKey] = array($controlName, $controlValue);
  212. }
  213. }
  214. return $result;
  215. }
  216. /**
  217. * Save control values data
  218. *
  219. * @param array $controlsData
  220. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  221. */
  222. public function saveData(array $controlsData)
  223. {
  224. $configDom = $this->_viewConfig->getDomConfigCopy()->getDom();
  225. $varData = $this->_prepareVarData($controlsData, $this->_controlList);
  226. /** @var $varsNode DOMElement */
  227. foreach ($configDom->childNodes->item(0)->childNodes as $varsNode) {
  228. $moduleName = $varsNode->getAttribute('module');
  229. if (!isset($varData[$moduleName])) {
  230. continue;
  231. }
  232. /** @var $varNode DOMElement */
  233. foreach ($varsNode->getElementsByTagName('var') as $varNode) {
  234. $varName = $varNode->getAttribute('name');
  235. if (isset($varData[$moduleName][$varName])) {
  236. list($controlName, $controlValue) = $varData[$moduleName][$varName];
  237. $varNode->nodeValue = $controlValue;
  238. $this->_controlList[$controlName]['value'] = $controlValue;
  239. }
  240. }
  241. }
  242. $this->_saveViewConfiguration($configDom);
  243. $this->_eventDispatcher->dispatch('save_xml_configuration', array('configuration' => $this));
  244. return $this;
  245. }
  246. /**
  247. * Get control configuration
  248. *
  249. * @return Mage_DesignEditor_Model_Config_Control_Abstract
  250. */
  251. public function getControlConfig()
  252. {
  253. return $this->_configuration;
  254. }
  255. /**
  256. * Get theme
  257. *
  258. * @return Mage_Core_Model_Theme
  259. */
  260. public function getTheme()
  261. {
  262. return $this->_theme;
  263. }
  264. /**
  265. * Save customized DOM of view configuration
  266. *
  267. * @param DOMDocument $config
  268. * @return Mage_DesignEditor_Model_Editor_Tools_Controls_Configuration
  269. */
  270. protected function _saveViewConfiguration(DOMDocument $config)
  271. {
  272. $targetPath = $this->_theme->getCustomViewConfigPath();
  273. $this->_filesystem->setIsAllowCreateDirectories(true)->write($targetPath, $config->saveXML());
  274. return $this;
  275. }
  276. }