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_Core_Model_Resource_Config extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/config_data', 'config_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function loadToXml(Mage_Core_Model_Config $xmlConfig, $condition = null)
54: {
55: $read = $this->_getReadAdapter();
56: if (!$read) {
57: return $this;
58: }
59:
60: $websites = array();
61: $select = $read->select()
62: ->from($this->getTable('core/website'), array('website_id', 'code', 'name'));
63: $rowset = $read->fetchAssoc($select);
64: foreach ($rowset as $w) {
65: $xmlConfig->setNode('websites/'.$w['code'].'/system/website/id', $w['website_id']);
66: $xmlConfig->setNode('websites/'.$w['code'].'/system/website/name', $w['name']);
67: $websites[$w['website_id']] = array('code' => $w['code']);
68: }
69:
70: $stores = array();
71: $select = $read->select()
72: ->from($this->getTable('core/store'), array('store_id', 'code', 'name', 'website_id'))
73: ->order('sort_order ' . Varien_Db_Select::SQL_ASC);
74: $rowset = $read->fetchAssoc($select);
75: foreach ($rowset as $s) {
76: if (!isset($websites[$s['website_id']])) {
77: continue;
78: }
79: $xmlConfig->setNode('stores/'.$s['code'].'/system/store/id', $s['store_id']);
80: $xmlConfig->setNode('stores/'.$s['code'].'/system/store/name', $s['name']);
81: $xmlConfig->setNode('stores/'.$s['code'].'/system/website/id', $s['website_id']);
82: $xmlConfig->setNode('websites/'.$websites[$s['website_id']]['code'].'/system/stores/'.$s['code'], $s['store_id']);
83: $stores[$s['store_id']] = array('code'=>$s['code']);
84: $websites[$s['website_id']]['stores'][$s['store_id']] = $s['code'];
85: }
86:
87: $substFrom = array();
88: $substTo = array();
89:
90:
91: $select = $read->select()
92: ->from($this->getMainTable(), array('scope', 'scope_id', 'path', 'value'));
93: if (!is_null($condition)) {
94: $select->where($condition);
95: }
96: $rowset = $read->fetchAll($select);
97:
98:
99:
100: foreach ($rowset as $r) {
101: if ($r['scope'] !== 'default') {
102: continue;
103: }
104: $value = str_replace($substFrom, $substTo, $r['value']);
105: $xmlConfig->setNode('default/' . $r['path'], $value);
106: }
107:
108:
109: $extendSource = $xmlConfig->getNode('default');
110: foreach ($websites as $id=>$w) {
111: $websiteNode = $xmlConfig->getNode('websites/' . $w['code']);
112: $websiteNode->extend($extendSource);
113: }
114:
115: $deleteWebsites = array();
116:
117: foreach ($rowset as $r) {
118: if ($r['scope'] !== 'websites') {
119: continue;
120: }
121: $value = str_replace($substFrom, $substTo, $r['value']);
122: if (isset($websites[$r['scope_id']])) {
123: $nodePath = sprintf('websites/%s/%s', $websites[$r['scope_id']]['code'], $r['path']);
124: $xmlConfig->setNode($nodePath, $value);
125: } else {
126: $deleteWebsites[$r['scope_id']] = $r['scope_id'];
127: }
128: }
129:
130:
131: foreach ($websites as $website) {
132: $extendSource = $xmlConfig->getNode('websites/' . $website['code']);
133: if (isset($website['stores'])) {
134: foreach ($website['stores'] as $sCode) {
135: $storeNode = $xmlConfig->getNode('stores/'.$sCode);
136: 137: 138:
139: $storeNode->extend($extendSource, false);
140: }
141: }
142: }
143:
144: $deleteStores = array();
145:
146: foreach ($rowset as $r) {
147: if ($r['scope'] !== 'stores') {
148: continue;
149: }
150: $value = str_replace($substFrom, $substTo, $r['value']);
151: if (isset($stores[$r['scope_id']])) {
152: $nodePath = sprintf('stores/%s/%s', $stores[$r['scope_id']]['code'], $r['path']);
153: $xmlConfig->setNode($nodePath, $value);
154: } else {
155: $deleteStores[$r['scope_id']] = $r['scope_id'];
156: }
157: }
158:
159: if ($deleteWebsites) {
160: $this->_getWriteAdapter()->delete($this->getMainTable(), array(
161: 'scope = ?' => 'websites',
162: 'scope_id IN(?)' => $deleteWebsites,
163: ));
164: }
165:
166: if ($deleteStores) {
167: $this->_getWriteAdapter()->delete($this->getMainTable(), array(
168: 'scope=?' => 'stores',
169: 'scope_id IN(?)' => $deleteStores,
170: ));
171: }
172: return $this;
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182: 183:
184: public function saveConfig($path, $value, $scope, $scopeId)
185: {
186: $writeAdapter = $this->_getWriteAdapter();
187: $select = $writeAdapter->select()
188: ->from($this->getMainTable())
189: ->where('path = ?', $path)
190: ->where('scope = ?', $scope)
191: ->where('scope_id = ?', $scopeId);
192: $row = $writeAdapter->fetchRow($select);
193:
194: $newData = array(
195: 'scope' => $scope,
196: 'scope_id' => $scopeId,
197: 'path' => $path,
198: 'value' => $value
199: );
200:
201: if ($row) {
202: $whereCondition = array($this->getIdFieldName() . '=?' => $row[$this->getIdFieldName()]);
203: $writeAdapter->update($this->getMainTable(), $newData, $whereCondition);
204: } else {
205: $writeAdapter->insert($this->getMainTable(), $newData);
206: }
207: return $this;
208: }
209:
210: 211: 212: 213: 214: 215: 216: 217:
218: public function deleteConfig($path, $scope, $scopeId)
219: {
220: $adapter = $this->_getWriteAdapter();
221: $adapter->delete($this->getMainTable(), array(
222: $adapter->quoteInto('path = ?', $path),
223: $adapter->quoteInto('scope = ?', $scope),
224: $adapter->quoteInto('scope_id = ?', $scopeId)
225: ));
226: return $this;
227: }
228: }
229: