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_System_ConfigController extends Mage_Adminhtml_Controller_Action
00036 {
00037
00038
00039
00040
00041
00042 protected function _construct()
00043 {
00044 $this->setFlag('index', 'no-preDispatch', true);
00045 return parent::_construct();
00046 }
00047
00048
00049
00050
00051
00052 public function indexAction()
00053 {
00054 $this->_forward('edit');
00055 }
00056
00057
00058
00059
00060
00061 public function editAction()
00062 {
00063 $current = $this->getRequest()->getParam('section');
00064 $website = $this->getRequest()->getParam('website');
00065 $store = $this->getRequest()->getParam('store');
00066
00067 $configFields = Mage::getSingleton('adminhtml/config');
00068
00069
00070 $sections = $configFields->getSections($current);
00071 $section = $sections->$current;
00072 $hasChildren = $configFields->hasChildren($section, $website, $store);
00073 if (!$hasChildren && $current) {
00074 $this->_redirect('*/*/', array('website'=>$website, 'store'=>$store));
00075 }
00076
00077 $this->loadLayout();
00078
00079 $this->_setActiveMenu('system/config');
00080
00081 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'), $this->getUrl('*/system'));
00082
00083 $this->getLayout()->getBlock('left')
00084 ->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
00085
00086 if ($this->_isSectionAllowed($this->getRequest()->getParam('section'))) {
00087 $this->_addContent($this->getLayout()->createBlock('adminhtml/system_config_edit')->initForm());
00088
00089 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('system/shipping/ups.phtml'));
00090 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('system/config/js.phtml'));
00091 $this->_addJs($this->getLayout()->createBlock('adminhtml/template')->setTemplate('system/shipping/applicable_country.phtml'));
00092
00093 $this->renderLayout();
00094 }
00095 }
00096
00097
00098
00099
00100
00101 public function saveAction()
00102 {
00103 $session = Mage::getSingleton('adminhtml/session');
00104
00105
00106 $groups = $this->getRequest()->getPost('groups');
00107
00108
00109 if (isset($_FILES['groups']['name']) && is_array($_FILES['groups']['name'])) {
00110
00111
00112
00113
00114 foreach($_FILES['groups']['name'] as $groupName => $group) {
00115 if (is_array($group)) {
00116 foreach ($group['fields'] as $fieldName => $field) {
00117 if (!empty($field['value'])) {
00118 $groups[$groupName]['fields'][$fieldName] = array('value' => $field['value']);
00119 }
00120 }
00121 }
00122 }
00123 }
00124
00125 try {
00126 if (!$this->_isSectionAllowed($this->getRequest()->getParam('section'))) {
00127 throw new Exception(Mage::helper('adminhtml')->__('This section is not allowed.'));
00128 }
00129
00130
00131 $this->_saveSection();
00132 $section = $this->getRequest()->getParam('section');
00133 $website = $this->getRequest()->getParam('website');
00134 $store = $this->getRequest()->getParam('store');
00135 Mage::getModel('adminhtml/config_data')
00136 ->setSection($section)
00137 ->setWebsite($website)
00138 ->setStore($store)
00139 ->setGroups($groups)
00140 ->save();
00141
00142
00143 Mage::getConfig()->reinit();
00144 Mage::app()->reinitStores();
00145
00146
00147
00148 Mage::dispatchEvent("admin_system_config_changed_section_{$section}",
00149 array('website' => $website, 'store' => $store)
00150 );
00151 $session->addSuccess(Mage::helper('adminhtml')->__('Configuration successfully saved'));
00152 }
00153 catch (Mage_Core_Exception $e) {
00154 foreach(split("\n", $e->getMessage()) as $message) {
00155 $session->addError($message);
00156 }
00157 }
00158 catch (Exception $e) {
00159 $session->addException($e, Mage::helper('adminhtml')->__('Error while saving this configuration: '.$e->getMessage()));
00160 }
00161
00162 $this->_saveState($this->getRequest()->getPost('config_state'));
00163
00164 $this->_redirect('*/*/edit', array('_current' => array('section', 'website', 'store')));
00165 }
00166
00167
00168
00169
00170 protected function _saveSection ()
00171 {
00172 $method = '_save' . uc_words($this->getRequest()->getParam('section'), '');
00173 if (method_exists($this, $method)) {
00174 $this->$method();
00175 }
00176 }
00177
00178
00179
00180
00181 protected function _saveAdvanced ()
00182 {
00183 Mage::app()->cleanCache(
00184 array(
00185 'layout',
00186 Mage_Core_Model_Layout_Update::LAYOUT_GENERAL_CACHE_TAG
00187 )
00188 );
00189 }
00190
00191
00192
00193
00194
00195 public function stateAction()
00196 {
00197 if ($this->getRequest()->getParam('isAjax') == 1
00198 && $this->getRequest()->getParam('container') != ''
00199 && $this->getRequest()->getParam('value') != '') {
00200
00201 $configState = array(
00202 $this->getRequest()->getParam('container') => $this->getRequest()->getParam('value')
00203 );
00204 $this->_saveState($configState);
00205 $this->getResponse()->setBody('success');
00206 }
00207 }
00208
00209
00210
00211
00212
00213 public function exportTableratesAction()
00214 {
00215 $websiteModel = Mage::app()->getWebsite($this->getRequest()->getParam('website'));
00216
00217 if ($this->getRequest()->getParam('conditionName')) {
00218 $conditionName = $this->getRequest()->getParam('conditionName');
00219 } else {
00220 $conditionName = $websiteModel->getConfig('carriers/tablerate/condition_name');
00221 }
00222
00223 $tableratesCollection = Mage::getResourceModel('shipping/carrier_tablerate_collection');
00224
00225 $tableratesCollection->setConditionFilter($conditionName);
00226 $tableratesCollection->setWebsiteFilter($websiteModel->getId());
00227 $tableratesCollection->load();
00228
00229 $csv = '';
00230
00231 $conditionName = Mage::getModel('shipping/carrier_tablerate')->getCode('condition_name_short', $conditionName);
00232
00233 $csvHeader = array('"'.Mage::helper('adminhtml')->__('Country').'"', '"'.Mage::helper('adminhtml')->__('Region/State').'"', '"'.Mage::helper('adminhtml')->__('Zip/Postal Code').'"', '"'.$conditionName.'"', '"'.Mage::helper('adminhtml')->__('Shipping Price').'"');
00234 $csv .= implode(',', $csvHeader)."\n";
00235
00236 foreach ($tableratesCollection->getItems() as $item) {
00237 if ($item->getData('dest_country') == '') {
00238 $country = '*';
00239 } else {
00240 $country = $item->getData('dest_country');
00241 }
00242 if ($item->getData('dest_region') == '') {
00243 $region = '*';
00244 } else {
00245 $region = $item->getData('dest_region');
00246 }
00247 if ($item->getData('dest_zip') == '') {
00248 $zip = '*';
00249 } else {
00250 $zip = $item->getData('dest_zip');
00251 }
00252
00253 $csvData = array($country, $region, $zip, $item->getData('condition_value'), $item->getData('price'));
00254 foreach ($csvData as $cell) {
00255 $cell = '"'.str_replace('"', '""', $cell).'"';
00256 }
00257 $csv .= implode(',', $csvData)."\n";
00258 }
00259
00260 header('Pragma: public');
00261 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
00262
00263 header("Content-type: application/octet-stream");
00264 header("Content-disposition: attachment; filename=tablerates.csv");
00265 echo $csv;
00266 exit;
00267 }
00268
00269
00270
00271
00272
00273 protected function _isAllowed()
00274 {
00275 return Mage::getSingleton('admin/session')->isAllowed('system/config');
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 protected function _isSectionAllowed($section)
00287 {
00288 try {
00289 $session = Mage::getSingleton('admin/session');
00290 $resourceLookup = "admin/system/config/{$section}";
00291 $resourceId = $session->getData('acl')->get($resourceLookup)->getResourceId();
00292 if (!$session->isAllowed($resourceId)) {
00293 throw new Exception('');
00294 }
00295 return true;
00296 }
00297 catch (Exception $e) {
00298 $this->_forward('denied');
00299 return false;
00300 }
00301 }
00302
00303
00304
00305
00306
00307
00308
00309 protected function _saveState($configState = array())
00310 {
00311 $adminUser = Mage::getSingleton('admin/session')->getUser();
00312 if (is_array($configState)) {
00313 $extra = $adminUser->getExtra();
00314 if (!is_array($extra)) {
00315 $extra = array();
00316 }
00317 if (!isset($extra['configState'])) {
00318 $extra['configState'] = array();
00319 }
00320 foreach ($configState as $fieldset => $state) {
00321 $extra['configState'][$fieldset] = $state;
00322 }
00323 $adminUser->saveExtra($extra);
00324 }
00325
00326 return true;
00327 }
00328 }