PageRenderTime 67ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Model/Config/Data.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 362 lines | 211 code | 37 blank | 114 comment | 35 complexity | 729519b5f702f8799acadd715286bf4a 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Adminhtml config data model
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Model_Config_Data extends Varien_Object
  34. {
  35. /**
  36. * Config data for sections
  37. *
  38. * @var array
  39. */
  40. protected $_configData;
  41. /**
  42. * Root config node
  43. *
  44. * @var Mage_Core_Model_Config_Element
  45. */
  46. protected $_configRoot;
  47. /**
  48. * Save config section
  49. * Require set: section, website, store and groups
  50. *
  51. * @return Mage_Adminhtml_Model_Config_Data
  52. */
  53. public function save()
  54. {
  55. $this->_validate();
  56. $this->_getScope();
  57. Mage::dispatchEvent('model_config_data_save_before', array('object' => $this));
  58. $section = $this->getSection();
  59. $website = $this->getWebsite();
  60. $store = $this->getStore();
  61. $groups = $this->getGroups();
  62. $scope = $this->getScope();
  63. $scopeId = $this->getScopeId();
  64. if (empty($groups)) {
  65. return $this;
  66. }
  67. $sections = Mage::getModel('adminhtml/config')->getSections();
  68. /* @var $sections Mage_Core_Model_Config_Element */
  69. $oldConfig = $this->_getConfig(true);
  70. $deleteTransaction = Mage::getModel('core/resource_transaction');
  71. /* @var $deleteTransaction Mage_Core_Model_Resource_Transaction */
  72. $saveTransaction = Mage::getModel('core/resource_transaction');
  73. /* @var $saveTransaction Mage_Core_Model_Resource_Transaction */
  74. // Extends for old config data
  75. $oldConfigAdditionalGroups = array();
  76. foreach ($groups as $group => $groupData) {
  77. /**
  78. * Map field names if they were cloned
  79. */
  80. $groupConfig = $sections->descend($section.'/groups/'.$group);
  81. if ($clonedFields = !empty($groupConfig->clone_fields)) {
  82. if ($groupConfig->clone_model) {
  83. $cloneModel = Mage::getModel((string)$groupConfig->clone_model);
  84. } else {
  85. Mage::throwException('Config form fieldset clone model required to be able to clone fields');
  86. }
  87. $mappedFields = array();
  88. $fieldsConfig = $sections->descend($section.'/groups/'.$group.'/fields');
  89. if ($fieldsConfig->hasChildren()) {
  90. foreach ($fieldsConfig->children() as $field => $node) {
  91. foreach ($cloneModel->getPrefixes() as $prefix) {
  92. $mappedFields[$prefix['field'].(string)$field] = (string)$field;
  93. }
  94. }
  95. }
  96. }
  97. // set value for group field entry by fieldname
  98. // use extra memory
  99. $fieldsetData = array();
  100. foreach ($groupData['fields'] as $field => $fieldData) {
  101. $fieldsetData[$field] = (is_array($fieldData) && isset($fieldData['value']))
  102. ? $fieldData['value'] : null;
  103. }
  104. foreach ($groupData['fields'] as $field => $fieldData) {
  105. $fieldConfig = $sections->descend($section . '/groups/' . $group . '/fields/' . $field);
  106. if (!$fieldConfig && $clonedFields && isset($mappedFields[$field])) {
  107. $fieldConfig = $sections->descend($section . '/groups/' . $group . '/fields/'
  108. . $mappedFields[$field]);
  109. }
  110. if (!$fieldConfig) {
  111. $node = $sections->xpath($section .'//' . $group . '[@type="group"]/fields/' . $field);
  112. if ($node) {
  113. $fieldConfig = $node[0];
  114. }
  115. }
  116. /**
  117. * Get field backend model
  118. */
  119. $backendClass = (isset($fieldConfig->backend_model))? $fieldConfig->backend_model : false;
  120. if (!$backendClass) {
  121. $backendClass = 'core/config_data';
  122. }
  123. /** @var $dataObject Mage_Core_Model_Config_Data */
  124. $dataObject = Mage::getModel($backendClass);
  125. if (!$dataObject instanceof Mage_Core_Model_Config_Data) {
  126. Mage::throwException('Invalid config field backend model: '.$backendClass);
  127. }
  128. $dataObject
  129. ->setField($field)
  130. ->setGroups($groups)
  131. ->setGroupId($group)
  132. ->setStoreCode($store)
  133. ->setWebsiteCode($website)
  134. ->setScope($scope)
  135. ->setScopeId($scopeId)
  136. ->setFieldConfig($fieldConfig)
  137. ->setFieldsetData($fieldsetData)
  138. ;
  139. if (!isset($fieldData['value'])) {
  140. $fieldData['value'] = null;
  141. }
  142. $path = $section . '/' . $group . '/' . $field;
  143. /**
  144. * Look for custom defined field path
  145. */
  146. if (is_object($fieldConfig)) {
  147. $configPath = (string)$fieldConfig->config_path;
  148. if (!empty($configPath) && strrpos($configPath, '/') > 0) {
  149. // Extend old data with specified section group
  150. $groupPath = substr($configPath, 0, strrpos($configPath, '/'));
  151. if (!isset($oldConfigAdditionalGroups[$groupPath])) {
  152. $oldConfig = $this->extendConfig($groupPath, true, $oldConfig);
  153. $oldConfigAdditionalGroups[$groupPath] = true;
  154. }
  155. $path = $configPath;
  156. }
  157. }
  158. $inherit = !empty($fieldData['inherit']);
  159. $dataObject->setPath($path)
  160. ->setValue($fieldData['value']);
  161. if (isset($oldConfig[$path])) {
  162. $dataObject->setConfigId($oldConfig[$path]['config_id']);
  163. /**
  164. * Delete config data if inherit
  165. */
  166. if (!$inherit) {
  167. $saveTransaction->addObject($dataObject);
  168. }
  169. else {
  170. $deleteTransaction->addObject($dataObject);
  171. }
  172. }
  173. elseif (!$inherit) {
  174. $dataObject->unsConfigId();
  175. $saveTransaction->addObject($dataObject);
  176. }
  177. }
  178. }
  179. $deleteTransaction->delete();
  180. $saveTransaction->save();
  181. return $this;
  182. }
  183. /**
  184. * Load config data for section
  185. *
  186. * @return array
  187. */
  188. public function load()
  189. {
  190. if (is_null($this->_configData)) {
  191. $this->_validate();
  192. $this->_getScope();
  193. $this->_configData = $this->_getConfig(false);
  194. }
  195. return $this->_configData;
  196. }
  197. /**
  198. * Extend config data with additional config data by specified path
  199. *
  200. * @param string $path Config path prefix
  201. * @param bool $full Simple config structure or not
  202. * @param array $oldConfig Config data to extend
  203. * @return array
  204. */
  205. public function extendConfig($path, $full = true, $oldConfig = array())
  206. {
  207. $extended = $this->_getPathConfig($path, $full);
  208. if (is_array($oldConfig) && !empty($oldConfig)) {
  209. return $oldConfig + $extended;
  210. }
  211. return $extended;
  212. }
  213. /**
  214. * Validate isset required parametrs
  215. *
  216. */
  217. protected function _validate()
  218. {
  219. if (is_null($this->getSection())) {
  220. $this->setSection('');
  221. }
  222. if (is_null($this->getWebsite())) {
  223. $this->setWebsite('');
  224. }
  225. if (is_null($this->getStore())) {
  226. $this->setStore('');
  227. }
  228. }
  229. /**
  230. * Get scope name and scopeId
  231. *
  232. */
  233. protected function _getScope()
  234. {
  235. if ($this->getStore()) {
  236. $scope = 'stores';
  237. $scopeId = (int)Mage::getConfig()->getNode('stores/' . $this->getStore() . '/system/store/id');
  238. $scopeCode = $this->getStore();
  239. } elseif ($this->getWebsite()) {
  240. $scope = 'websites';
  241. $scopeId = (int)Mage::getConfig()->getNode('websites/' . $this->getWebsite() . '/system/website/id');
  242. $scopeCode = $this->getWebsite();
  243. } else {
  244. $scope = 'default';
  245. $scopeId = 0;
  246. $scopeCode = '';
  247. }
  248. $this->setScope($scope);
  249. $this->setScopeId($scopeId);
  250. $this->setScopeCode($scopeCode);
  251. }
  252. /**
  253. * Return formatted config data for current section
  254. *
  255. * @param bool $full Simple config structure or not
  256. * @return array
  257. */
  258. protected function _getConfig($full = true)
  259. {
  260. return $this->_getPathConfig($this->getSection(), $full);
  261. }
  262. /**
  263. * Return formatted config data for specified path prefix
  264. *
  265. * @param string $path Config path prefix
  266. * @param bool $full Simple config structure or not
  267. * @return array
  268. */
  269. protected function _getPathConfig($path, $full = true)
  270. {
  271. $configDataCollection = Mage::getModel('core/config_data')
  272. ->getCollection()
  273. ->addScopeFilter($this->getScope(), $this->getScopeId(), $path);
  274. $config = array();
  275. foreach ($configDataCollection as $data) {
  276. if ($full) {
  277. $config[$data->getPath()] = array(
  278. 'path' => $data->getPath(),
  279. 'value' => $data->getValue(),
  280. 'config_id' => $data->getConfigId()
  281. );
  282. }
  283. else {
  284. $config[$data->getPath()] = $data->getValue();
  285. }
  286. }
  287. return $config;
  288. }
  289. /**
  290. * Get config data value
  291. *
  292. * @param string $path
  293. * @param null|bool $inherit
  294. * @param null|array $configData
  295. * @return Varien_Simplexml_Element
  296. */
  297. public function getConfigDataValue($path, &$inherit = null, $configData = null)
  298. {
  299. $this->load();
  300. if (is_null($configData)) {
  301. $configData = $this->_configData;
  302. }
  303. if (array_key_exists($path, $configData)) {
  304. $data = $configData[$path];
  305. $inherit = false;
  306. } else {
  307. $data = $this->getConfigRoot()->descend($path);
  308. $inherit = true;
  309. }
  310. return $data;
  311. }
  312. /**
  313. * Get config root node for current scope
  314. *
  315. * @return Mage_Core_Model_Config_Element
  316. */
  317. public function getConfigRoot()
  318. {
  319. if (is_null($this->_configRoot)) {
  320. $this->load();
  321. $this->_configRoot = Mage::getConfig()->getNode(null, $this->getScope(), $this->getScopeCode());
  322. }
  323. return $this->_configRoot;
  324. }
  325. }