00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Core_Model_Mysql4_Config extends Mage_Core_Model_Mysql4_Abstract
00029 {
00030 protected function _construct()
00031 {
00032 $this->_init('core/config_data', 'config_id');
00033 }
00034
00035
00036
00037
00038
00039
00040
00041 public function getChecksum($tables)
00042 {
00043 if (is_string($tables)) {
00044 $tablesArr = explode(',', $tables);
00045 $tables = array();
00046 foreach ($tablesArr as $table) {
00047 $table = $this->getTable(trim($table));
00048 if (!empty($table)) {
00049 $tables[] = $table;
00050 }
00051 }
00052 }
00053 if (empty($tables) || !$this->_getReadAdapter()) {
00054 return false;
00055 }
00056 $checksumArr = $this->_getReadAdapter()->fetchAll('checksum table '.join(',', $tables));
00057 $checksum = 0;
00058 foreach ($checksumArr as $r) {
00059 $checksum += $r['Checksum'];
00060 }
00061 return $checksum;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071 public function loadToXml(Mage_Core_Model_Config $xmlConfig, $cond=null)
00072 {
00073 $read = $this->_getReadAdapter();
00074 if (!$read) {
00075 return $this;
00076 }
00077
00078 $websites = array();
00079 $rows = $read->fetchAssoc("select website_id, code, name from ".$this->getTable('website'));
00080 foreach ($rows as $w) {
00081 $xmlConfig->setNode('websites/'.$w['code'].'/system/website/id', $w['website_id']);
00082 $xmlConfig->setNode('websites/'.$w['code'].'/system/website/name', $w['name']);
00083 $websites[$w['website_id']] = array('code'=>$w['code']);
00084 }
00085
00086 $stores = array();
00087 $rows = $read->fetchAssoc("select store_id, code, name, website_id from ".$this->getTable('store')." order by sort_order asc");
00088 foreach ($rows as $s) {
00089 $xmlConfig->setNode('stores/'.$s['code'].'/system/store/id', $s['store_id']);
00090 $xmlConfig->setNode('stores/'.$s['code'].'/system/store/name', $s['name']);
00091 $xmlConfig->setNode('stores/'.$s['code'].'/system/website/id', $s['website_id']);
00092 $xmlConfig->setNode('websites/'.$websites[$s['website_id']]['code'].'/system/stores/'.$s['code'], $s['store_id']);
00093 $stores[$s['store_id']] = array('code'=>$s['code']);
00094 $websites[$s['website_id']]['stores'][$s['store_id']] = $s['code'];
00095 }
00096
00097 $subst_from = array();
00098 $subst_to = array();
00099
00100
00101 $rows = $read->fetchAll("select scope, scope_id, path, value from ".$this->getMainTable().($cond ? ' where '.$cond : ''));
00102
00103
00104 foreach ($rows as $r) {
00105 if ($r['scope']!=='default') {
00106 continue;
00107 }
00108 $value = str_replace($subst_from, $subst_to, $r['value']);
00109 $xmlConfig->setNode('default/'.$r['path'], $value);
00110 }
00111
00112 $extendSource = $xmlConfig->getNode('default');
00113 foreach ($websites as $id=>$w) {
00114 $websiteNode = $xmlConfig->getNode('websites/'.$w['code']);
00115 $websiteNode->extend($extendSource);
00116 }
00117
00118 $deleteWebsites = array();
00119
00120 foreach ($rows as $r) {
00121 if ($r['scope']!=='websites') {
00122 continue;
00123 }
00124 $value = str_replace($subst_from, $subst_to, $r['value']);
00125 if (isset($websites[$r['scope_id']])) {
00126 $xmlConfig->setNode('websites/'.$websites[$r['scope_id']]['code'].'/'.$r['path'], $value);
00127 }
00128 else {
00129 $deleteWebsites[$r['scope_id']] = $r['scope_id'];
00130 }
00131 }
00132
00133
00134 foreach ($websites as $website) {
00135 $extendSource = $xmlConfig->getNode('websites/'.$website['code']);
00136 if (isset($website['stores'])) {
00137 foreach ($website['stores'] as $sCode) {
00138 $storeNode = $xmlConfig->getNode('stores/'.$sCode);
00139
00140
00141
00142 $storeNode->extend($extendSource, false);
00143 }
00144 }
00145 }
00146
00147 $deleteStores = array();
00148
00149 foreach ($rows as $r) {
00150 if ($r['scope']!=='stores') {
00151 continue;
00152 }
00153 $value = str_replace($subst_from, $subst_to, $r['value']);
00154 if (isset($stores[$r['scope_id']])) {
00155 $xmlConfig->setNode('stores/'.$stores[$r['scope_id']]['code'].'/'.$r['path'], $value);
00156 }
00157 else {
00158 $deleteStores[$r['scope_id']] = $r['scope_id'];
00159 }
00160 }
00161
00162 if ($deleteWebsites) {
00163 $this->_getWriteAdapter()->delete($this->getMainTable(), array(
00164 $this->_getWriteAdapter()->quoteInto('scope=?', 'websites'),
00165 $this->_getWriteAdapter()->quoteInto('scope_id IN(?)', $deleteWebsites),
00166 ));
00167 }
00168
00169 if ($deleteStores) {
00170 $this->_getWriteAdapter()->delete($this->getMainTable(), array(
00171 $this->_getWriteAdapter()->quoteInto('scope=?', 'stores'),
00172 $this->_getWriteAdapter()->quoteInto('scope_id IN(?)', $deleteStores),
00173 ));
00174 }
00175
00176 return $this;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 public function saveConfig($path, $value, $scope, $scopeId)
00189 {
00190 $writeAdapter = $this->_getWriteAdapter();
00191 $select = $writeAdapter->select()
00192 ->from($this->getMainTable())
00193 ->where('path=?', $path)
00194 ->where('scope=?', $scope)
00195 ->where('scope_id=?', $scopeId);
00196 $row = $writeAdapter->fetchRow($select);
00197
00198 $newData = array(
00199 'scope' => $scope,
00200 'scope_id' => $scopeId,
00201 'path' => $path,
00202 'value' => $value
00203 );
00204
00205 if ($row) {
00206 $whereCondition = $writeAdapter->quoteInto($this->getIdFieldName() . '=?', $row[$this->getIdFieldName()]);
00207 $writeAdapter->update($this->getMainTable(), $newData, $whereCondition);
00208 }
00209 else {
00210 $writeAdapter->insert($this->getMainTable(), $newData);
00211 }
00212 return $this;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 public function deleteConfig($path, $scope, $scopeId)
00224 {
00225 $writeAdapter = $this->_getWriteAdapter();
00226 $writeAdapter->delete($this->getMainTable(), array(
00227 $writeAdapter->quoteInto('path=?', $path),
00228 $writeAdapter->quoteInto('scope=?', $scope),
00229 $writeAdapter->quoteInto('scope_id=?', $scopeId)
00230 ));
00231 return $this;
00232 }
00233 }