1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_Adminhtml_Model_Config_Data extends Varien_Object
36: {
37: 38: 39: 40: 41:
42: protected $_configData;
43:
44: 45: 46: 47: 48:
49: protected $_configRoot;
50:
51: 52: 53: 54: 55: 56:
57: public function save()
58: {
59: $this->_validate();
60: $this->_getScope();
61:
62: Mage::dispatchEvent('model_config_data_save_before', array('object' => $this));
63:
64: $section = $this->getSection();
65: $website = $this->getWebsite();
66: $store = $this->getStore();
67: $groups = $this->getGroups();
68: $scope = $this->getScope();
69: $scopeId = $this->getScopeId();
70:
71: if (empty($groups)) {
72: return $this;
73: }
74:
75: $sections = Mage::getModel('adminhtml/config')->getSections();
76:
77:
78: $oldConfig = $this->_getConfig(true);
79:
80: $deleteTransaction = Mage::getModel('core/resource_transaction');
81:
82: $saveTransaction = Mage::getModel('core/resource_transaction');
83:
84:
85:
86: $oldConfigAdditionalGroups = array();
87:
88: foreach ($groups as $group => $groupData) {
89: 90: 91:
92: $groupConfig = $sections->descend($section.'/groups/'.$group);
93:
94: if ($clonedFields = !empty($groupConfig->clone_fields)) {
95: if ($groupConfig->clone_model) {
96: $cloneModel = Mage::getModel((string)$groupConfig->clone_model);
97: } else {
98: Mage::throwException('Config form fieldset clone model required to be able to clone fields');
99: }
100: $mappedFields = array();
101: $fieldsConfig = $sections->descend($section.'/groups/'.$group.'/fields');
102:
103: if ($fieldsConfig->hasChildren()) {
104: foreach ($fieldsConfig->children() as $field => $node) {
105: foreach ($cloneModel->getPrefixes() as $prefix) {
106: $mappedFields[$prefix['field'].(string)$field] = (string)$field;
107: }
108: }
109: }
110: }
111:
112:
113: $fieldsetData = array();
114: foreach ($groupData['fields'] as $field => $fieldData) {
115: $fieldsetData[$field] = (is_array($fieldData) && isset($fieldData['value']))
116: ? $fieldData['value'] : null;
117: }
118:
119: foreach ($groupData['fields'] as $field => $fieldData) {
120: $fieldConfig = $sections->descend($section . '/groups/' . $group . '/fields/' . $field);
121: if (!$fieldConfig && $clonedFields && isset($mappedFields[$field])) {
122: $fieldConfig = $sections->descend($section . '/groups/' . $group . '/fields/'
123: . $mappedFields[$field]);
124: }
125: if (!$fieldConfig) {
126: $node = $sections->xpath($section .'//' . $group . '[@type="group"]/fields/' . $field);
127: if ($node) {
128: $fieldConfig = $node[0];
129: }
130: }
131:
132: 133: 134:
135: $backendClass = $fieldConfig->backend_model;
136: if (!$backendClass) {
137: $backendClass = 'core/config_data';
138: }
139:
140:
141: $dataObject = Mage::getModel($backendClass);
142: if (!$dataObject instanceof Mage_Core_Model_Config_Data) {
143: Mage::throwException('Invalid config field backend model: '.$backendClass);
144: }
145:
146: $dataObject
147: ->setField($field)
148: ->setGroups($groups)
149: ->setGroupId($group)
150: ->setStoreCode($store)
151: ->setWebsiteCode($website)
152: ->setScope($scope)
153: ->setScopeId($scopeId)
154: ->setFieldConfig($fieldConfig)
155: ->setFieldsetData($fieldsetData)
156: ;
157:
158: if (!isset($fieldData['value'])) {
159: $fieldData['value'] = null;
160: }
161:
162: $path = $section . '/' . $group . '/' . $field;
163:
164: 165: 166:
167: if (is_object($fieldConfig)) {
168: $configPath = (string)$fieldConfig->config_path;
169: if (!empty($configPath) && strrpos($configPath, '/') > 0) {
170:
171: $groupPath = substr($configPath, 0, strrpos($configPath, '/'));
172: if (!isset($oldConfigAdditionalGroups[$groupPath])) {
173: $oldConfig = $this->extendConfig($groupPath, true, $oldConfig);
174: $oldConfigAdditionalGroups[$groupPath] = true;
175: }
176: $path = $configPath;
177: }
178: }
179:
180: $inherit = !empty($fieldData['inherit']);
181:
182: $dataObject->setPath($path)
183: ->setValue($fieldData['value']);
184:
185: if (isset($oldConfig[$path])) {
186: $dataObject->setConfigId($oldConfig[$path]['config_id']);
187:
188: 189: 190:
191: if (!$inherit) {
192: $saveTransaction->addObject($dataObject);
193: }
194: else {
195: $deleteTransaction->addObject($dataObject);
196: }
197: }
198: elseif (!$inherit) {
199: $dataObject->unsConfigId();
200: $saveTransaction->addObject($dataObject);
201: }
202: }
203: }
204:
205: $deleteTransaction->delete();
206: $saveTransaction->save();
207:
208: return $this;
209: }
210:
211: 212: 213: 214: 215:
216: public function load()
217: {
218: if (is_null($this->_configData)) {
219: $this->_validate();
220: $this->_getScope();
221: $this->_configData = $this->_getConfig(false);
222: }
223: return $this->_configData;
224: }
225:
226: 227: 228: 229: 230: 231: 232: 233:
234: public function extendConfig($path, $full = true, $oldConfig = array())
235: {
236: $extended = $this->_getPathConfig($path, $full);
237: if (is_array($oldConfig) && !empty($oldConfig)) {
238: return $oldConfig + $extended;
239: }
240: return $extended;
241: }
242:
243: 244: 245: 246:
247: protected function _validate()
248: {
249: if (is_null($this->getSection())) {
250: $this->setSection('');
251: }
252: if (is_null($this->getWebsite())) {
253: $this->setWebsite('');
254: }
255: if (is_null($this->getStore())) {
256: $this->setStore('');
257: }
258: }
259:
260: 261: 262: 263:
264: protected function _getScope()
265: {
266: if ($this->getStore()) {
267: $scope = 'stores';
268: $scopeId = (int)Mage::getConfig()->getNode('stores/' . $this->getStore() . '/system/store/id');
269: $scopeCode = $this->getStore();
270: } elseif ($this->getWebsite()) {
271: $scope = 'websites';
272: $scopeId = (int)Mage::getConfig()->getNode('websites/' . $this->getWebsite() . '/system/website/id');
273: $scopeCode = $this->getWebsite();
274: } else {
275: $scope = 'default';
276: $scopeId = 0;
277: $scopeCode = '';
278: }
279: $this->setScope($scope);
280: $this->setScopeId($scopeId);
281: $this->setScopeCode($scopeCode);
282: }
283:
284: 285: 286: 287: 288: 289:
290: protected function _getConfig($full = true)
291: {
292: return $this->_getPathConfig($this->getSection(), $full);
293: }
294:
295: 296: 297: 298: 299: 300: 301:
302: protected function _getPathConfig($path, $full = true)
303: {
304: $configDataCollection = Mage::getModel('core/config_data')
305: ->getCollection()
306: ->addScopeFilter($this->getScope(), $this->getScopeId(), $path);
307:
308: $config = array();
309: foreach ($configDataCollection as $data) {
310: if ($full) {
311: $config[$data->getPath()] = array(
312: 'path' => $data->getPath(),
313: 'value' => $data->getValue(),
314: 'config_id' => $data->getConfigId()
315: );
316: }
317: else {
318: $config[$data->getPath()] = $data->getValue();
319: }
320: }
321: return $config;
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331:
332: public function getConfigDataValue($path, &$inherit = null, $configData = null)
333: {
334: $this->load();
335: if (is_null($configData)) {
336: $configData = $this->_configData;
337: }
338: if (isset($configData[$path])) {
339: $data = $configData[$path];
340: $inherit = false;
341: } else {
342: $data = $this->getConfigRoot()->descend($path);
343: $inherit = true;
344: }
345:
346: return $data;
347: }
348:
349: 350: 351: 352: 353:
354: public function getConfigRoot()
355: {
356: if (is_null($this->_configRoot)) {
357: $this->load();
358: $this->_configRoot = Mage::getConfig()->getNode(null, $this->getScope(), $this->getScopeCode());
359: }
360: return $this->_configRoot;
361: }
362: }
363: