PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 304 lines | 174 code | 36 blank | 94 comment | 31 complexity | 0817646b868680f35e35642413ff7fbe MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.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. * Save config section
  37. * Require set: section, website, store and groups
  38. *
  39. * @return Mage_Adminhtml_Model_Config_Data
  40. */
  41. public function save()
  42. {
  43. $this->_validate();
  44. $this->_getScope();
  45. $section = $this->getSection();
  46. $website = $this->getWebsite();
  47. $store = $this->getStore();
  48. $groups = $this->getGroups();
  49. $scope = $this->getScope();
  50. $scopeId = $this->getScopeId();
  51. if (empty($groups)) {
  52. return $this;
  53. }
  54. $sections = Mage::getModel('adminhtml/config')->getSections();
  55. /* @var $sections Mage_Core_Model_Config_Element */
  56. $oldConfig = $this->_getConfig(true);
  57. $deleteTransaction = Mage::getModel('core/resource_transaction');
  58. /* @var $deleteTransaction Mage_Core_Model_Resource_Transaction */
  59. $saveTransaction = Mage::getModel('core/resource_transaction');
  60. /* @var $saveTransaction Mage_Core_Model_Resource_Transaction */
  61. // Extends for old config data
  62. $oldConfigAdditionalGroups = array();
  63. foreach ($groups as $group => $groupData) {
  64. /**
  65. * Map field names if they were cloned
  66. */
  67. $groupConfig = $sections->descend($section.'/groups/'.$group);
  68. if ($clonedFields = !empty($groupConfig->clone_fields)) {
  69. if ($groupConfig->clone_model) {
  70. $cloneModel = Mage::getModel((string)$groupConfig->clone_model);
  71. } else {
  72. Mage::throwException('Config form fieldset clone model required to be able to clone fields');
  73. }
  74. $mappedFields = array();
  75. $fieldsConfig = $sections->descend($section.'/groups/'.$group.'/fields');
  76. if ($fieldsConfig->hasChildren()) {
  77. foreach ($fieldsConfig->children() as $field => $node) {
  78. foreach ($cloneModel->getPrefixes() as $prefix) {
  79. $mappedFields[$prefix['field'].(string)$field] = (string)$field;
  80. }
  81. }
  82. }
  83. }
  84. // set value for group field entry by fieldname
  85. // use extra memory
  86. $fieldsetData = array();
  87. foreach ($groupData['fields'] as $field => $fieldData) {
  88. $fieldsetData[$field] = (is_array($fieldData) && isset($fieldData['value']))
  89. ? $fieldData['value'] : null;
  90. }
  91. foreach ($groupData['fields'] as $field => $fieldData) {
  92. /**
  93. * Get field backend model
  94. */
  95. $backendClass = $sections->descend($section.'/groups/'.$group.'/fields/'.$field.'/backend_model');
  96. if (!$backendClass && $clonedFields && isset($mappedFields[$field])) {
  97. $backendClass = $sections->descend($section.'/groups/'.$group.'/fields/'.$mappedFields[$field].'/backend_model');
  98. }
  99. if (!$backendClass) {
  100. $backendClass = 'core/config_data';
  101. }
  102. $dataObject = Mage::getModel($backendClass);
  103. if (!$dataObject instanceof Mage_Core_Model_Config_Data) {
  104. Mage::throwException('Invalid config field backend model: '.$backendClass);
  105. }
  106. /* @var $dataObject Mage_Core_Model_Config_Data */
  107. $fieldConfig = $sections->descend($section.'/groups/'.$group.'/fields/'.$field);
  108. if (!$fieldConfig && $clonedFields && isset($mappedFields[$field])) {
  109. $fieldConfig = $sections->descend($section.'/groups/'.$group.'/fields/'.$mappedFields[$field]);
  110. }
  111. $dataObject
  112. ->setField($field)
  113. ->setGroups($groups)
  114. ->setGroupId($group)
  115. ->setStoreCode($store)
  116. ->setWebsiteCode($website)
  117. ->setScope($scope)
  118. ->setScopeId($scopeId)
  119. ->setFieldConfig($fieldConfig)
  120. ->setFieldsetData($fieldsetData)
  121. ;
  122. if (!isset($fieldData['value'])) {
  123. $fieldData['value'] = null;
  124. }
  125. /*if (is_array($fieldData['value'])) {
  126. $fieldData['value'] = join(',', $fieldData['value']);
  127. }*/
  128. $path = $section.'/'.$group.'/'.$field;
  129. /**
  130. * Look for custom defined field path
  131. */
  132. if (is_object($fieldConfig)) {
  133. $configPath = (string)$fieldConfig->config_path;
  134. if (!empty($configPath) && strrpos($configPath, '/') > 0) {
  135. // Extend old data with specified section group
  136. $groupPath = substr($configPath, 0, strrpos($configPath, '/'));
  137. if (!isset($oldConfigAdditionalGroups[$groupPath])) {
  138. $oldConfig = $this->extendConfig($groupPath, true, $oldConfig);
  139. $oldConfigAdditionalGroups[$groupPath] = true;
  140. }
  141. $path = $configPath;
  142. }
  143. }
  144. $inherit = !empty($fieldData['inherit']);
  145. $dataObject->setPath($path)
  146. ->setValue($fieldData['value']);
  147. if (isset($oldConfig[$path])) {
  148. $dataObject->setConfigId($oldConfig[$path]['config_id']);
  149. /**
  150. * Delete config data if inherit
  151. */
  152. if (!$inherit) {
  153. $saveTransaction->addObject($dataObject);
  154. }
  155. else {
  156. $deleteTransaction->addObject($dataObject);
  157. }
  158. }
  159. elseif (!$inherit) {
  160. $dataObject->unsConfigId();
  161. $saveTransaction->addObject($dataObject);
  162. }
  163. }
  164. }
  165. $deleteTransaction->delete();
  166. $saveTransaction->save();
  167. return $this;
  168. }
  169. /**
  170. * Load config data for section
  171. *
  172. * @return array
  173. */
  174. public function load()
  175. {
  176. $this->_validate();
  177. $this->_getScope();
  178. return $this->_getConfig(false);
  179. }
  180. /**
  181. * Extend config data with additional config data by specified path
  182. *
  183. * @param string $path Config path prefix
  184. * @param bool $full Simple config structure or not
  185. * @param array $oldConfig Config data to extend
  186. * @return array
  187. */
  188. public function extendConfig($path, $full = true, $oldConfig = array())
  189. {
  190. $extended = $this->_getPathConfig($path, $full);
  191. if (is_array($oldConfig) && !empty($oldConfig)) {
  192. return $oldConfig + $extended;
  193. }
  194. return $extended;
  195. }
  196. /**
  197. * Validate isset required parametrs
  198. *
  199. */
  200. protected function _validate()
  201. {
  202. if (is_null($this->getSection())) {
  203. $this->setSection('');
  204. }
  205. if (is_null($this->getWebsite())) {
  206. $this->setWebsite('');
  207. }
  208. if (is_null($this->getStore())) {
  209. $this->setStore('');
  210. }
  211. }
  212. /**
  213. * Get scope name and scopeId
  214. *
  215. */
  216. protected function _getScope()
  217. {
  218. if ($this->getStore()) {
  219. $scope = 'stores';
  220. $scopeId = (int)Mage::getConfig()->getNode('stores/' . $this->getStore() . '/system/store/id');
  221. } elseif ($this->getWebsite()) {
  222. $scope = 'websites';
  223. $scopeId = (int)Mage::getConfig()->getNode('websites/' . $this->getWebsite() . '/system/website/id');
  224. } else {
  225. $scope = 'default';
  226. $scopeId = 0;
  227. }
  228. $this->setScope($scope);
  229. $this->setScopeId($scopeId);
  230. }
  231. /**
  232. * Return formatted config data for current section
  233. *
  234. * @param bool $full Simple config structure or not
  235. * @return array
  236. */
  237. protected function _getConfig($full = true)
  238. {
  239. return $this->_getPathConfig($this->getSection(), $full);
  240. }
  241. /**
  242. * Return formatted config data for specified path prefix
  243. *
  244. * @param string $path Config path prefix
  245. * @param bool $full Simple config structure or not
  246. * @return array
  247. */
  248. protected function _getPathConfig($path, $full = true)
  249. {
  250. $configDataCollection = Mage::getModel('core/config_data')
  251. ->getCollection()
  252. ->addScopeFilter($this->getScope(), $this->getScopeId(), $path);
  253. $config = array();
  254. foreach ($configDataCollection as $data) {
  255. if ($full) {
  256. $config[$data->getPath()] = array(
  257. 'path' => $data->getPath(),
  258. 'value' => $data->getValue(),
  259. 'config_id' => $data->getConfigId()
  260. );
  261. }
  262. else {
  263. $config[$data->getPath()] = $data->getValue();
  264. }
  265. }
  266. return $config;
  267. }
  268. }