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
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Model_Config_Data extends Varien_Object
00036 {
00037
00038
00039
00040
00041
00042
00043 public function save()
00044 {
00045 $this->_validate();
00046 $this->_getScope();
00047
00048 $section = $this->getSection();
00049 $website = $this->getWebsite();
00050 $store = $this->getStore();
00051 $groups = $this->getGroups();
00052 $scope = $this->getScope();
00053 $scopeId = $this->getScopeId();
00054
00055 if (empty($groups)) {
00056 return $this;
00057 }
00058
00059 $sections = Mage::getModel('adminhtml/config')->getSections();
00060
00061
00062 $oldConfig = $this->_getConfig(true);
00063
00064 $deleteTransaction = Mage::getModel('core/resource_transaction');
00065
00066 $saveTransaction = Mage::getModel('core/resource_transaction');
00067
00068
00069 foreach ($groups as $group => $groupData) {
00070
00071
00072
00073
00074 $groupConfig = $sections->descend($section.'/groups/'.$group);
00075
00076 if ($clonedFields = !empty($groupConfig->clone_fields)) {
00077 if ($groupConfig->clone_model) {
00078 $cloneModel = Mage::getModel((string)$groupConfig->clone_model);
00079 } else {
00080 Mage::throwException('Config form fieldset clone model required to be able to clone fields');
00081 }
00082 $mappedFields = array();
00083 $fieldsConfig = $sections->descend($section.'/groups/'.$group.'/fields');
00084
00085 if ($fieldsConfig->hasChildren()) {
00086 foreach ($fieldsConfig->children() as $field => $node) {
00087 foreach ($cloneModel->getPrefixes() as $prefix) {
00088 $mappedFields[$prefix['field'].(string)$field] = (string)$field;
00089 }
00090 }
00091 }
00092 }
00093
00094 foreach ($groupData['fields'] as $field => $fieldData) {
00095
00096
00097
00098
00099 $backendClass = $sections->descend($section.'/groups/'.$group.'/fields/'.$field.'/backend_model');
00100 if (!$backendClass && $clonedFields && isset($mappedFields[$field])) {
00101 $backendClass = $sections->descend($section.'/groups/'.$group.'/fields/'.$mappedFields[$field].'/backend_model');
00102 }
00103 if (!$backendClass) {
00104 $backendClass = 'core/config_data';
00105 }
00106
00107 $dataObject = Mage::getModel($backendClass);
00108 if (!$dataObject instanceof Mage_Core_Model_Config_Data) {
00109 Mage::throwException('Invalid config field backend model: '.$backendClass);
00110 }
00111
00112
00113 $fieldConfig = $sections->descend($section.'/groups/'.$group.'/fields/'.$field);
00114 if (!$fieldConfig && $clonedFields && isset($mappedFields[$field])) {
00115 $fieldConfig = $sections->descend($section.'/groups/'.$group.'/fields/'.$mappedFields[$field]);
00116 }
00117
00118 $dataObject
00119 ->setField($field)
00120 ->setGroups($groups)
00121 ->setGroupId($group)
00122 ->setStoreCode($store)
00123 ->setWebsiteCode($website)
00124 ->setScope($scope)
00125 ->setScopeId($scopeId)
00126 ->setFieldConfig($fieldConfig)
00127 ;
00128
00129 if (!isset($fieldData['value'])) {
00130 $fieldData['value'] = null;
00131 }
00132
00133
00134
00135
00136
00137 $path = $section.'/'.$group.'/'.$field;
00138 $inherit = !empty($fieldData['inherit']);
00139
00140 $dataObject->setPath($path)
00141 ->setValue($fieldData['value']);
00142
00143 if (isset($oldConfig[$path])) {
00144 $dataObject->setConfigId($oldConfig[$path]['config_id']);
00145
00146
00147
00148
00149 if (!$inherit) {
00150 $saveTransaction->addObject($dataObject);
00151 }
00152 else {
00153 $deleteTransaction->addObject($dataObject);
00154 }
00155 }
00156 elseif (!$inherit) {
00157 $dataObject->unsConfigId();
00158 $saveTransaction->addObject($dataObject);
00159 }
00160 }
00161
00162 }
00163
00164 $deleteTransaction->delete();
00165 $saveTransaction->save();
00166
00167 return $this;
00168 }
00169
00170
00171
00172
00173
00174
00175 public function load()
00176 {
00177 $this->_validate();
00178 $this->_getScope();
00179
00180 return $this->_getConfig(false);
00181 }
00182
00183
00184
00185
00186
00187 protected function _validate()
00188 {
00189 if (is_null($this->getSection())) {
00190 $this->setSection('');
00191
00192 }
00193 if (is_null($this->getWebsite())) {
00194 $this->setWebsite('');
00195
00196 }
00197 if (is_null($this->getStore())) {
00198 $this->setStore('');
00199
00200 }
00201 }
00202
00203
00204
00205
00206
00207 protected function _getScope()
00208 {
00209 if ($this->getStore()) {
00210 $scope = 'stores';
00211 $scopeId = (int)Mage::getConfig()->getNode('stores/' . $this->getStore() . '/system/store/id');
00212 } elseif ($this->getWebsite()) {
00213 $scope = 'websites';
00214 $scopeId = (int)Mage::getConfig()->getNode('websites/' . $this->getWebsite() . '/system/website/id');
00215 } else {
00216 $scope = 'default';
00217 $scopeId = 0;
00218 }
00219 $this->setScope($scope);
00220 $this->setScopeId($scopeId);
00221 }
00222
00223
00224
00225
00226
00227
00228 protected function _getConfig($full = true)
00229 {
00230 $configDataCollection = Mage::getModel('core/config_data')
00231 ->getCollection()
00232 ->addScopeFilter($this->getScope(), $this->getScopeId(), $this->getSection());
00233
00234 $config = array();
00235 foreach ($configDataCollection as $data) {
00236 if ($full) {
00237 $config[$data->getPath()] = array(
00238 'path' => $data->getPath(),
00239 'value' => $data->getValue(),
00240 'config_id' => $data->getConfigId()
00241 );
00242 }
00243 else {
00244 $config[$data->getPath()] = $data->getValue();
00245 }
00246 }
00247 return $config;
00248 }
00249 }