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: class Mage_Adminhtml_System_ConfigController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_isSectionAllowedFlag = true;
42:
43: 44: 45: 46: 47: 48:
49: public function preDispatch()
50: {
51: parent::preDispatch();
52:
53: if ($this->getRequest()->getParam('section')) {
54: $this->_isSectionAllowedFlag = $this->_isSectionAllowed($this->getRequest()->getParam('section'));
55: }
56:
57: return $this;
58: }
59:
60: 61: 62: 63:
64: public function indexAction()
65: {
66: $this->_forward('edit');
67: }
68:
69: 70: 71: 72:
73: public function editAction()
74: {
75: $this->_title($this->__('System'))->_title($this->__('Configuration'));
76:
77: $current = $this->getRequest()->getParam('section');
78: $website = $this->getRequest()->getParam('website');
79: $store = $this->getRequest()->getParam('store');
80:
81: Mage::getSingleton('adminhtml/config_data')
82: ->setSection($current)
83: ->setWebsite($website)
84: ->setStore($store);
85:
86: $configFields = Mage::getSingleton('adminhtml/config');
87:
88: $sections = $configFields->getSections($current);
89: $section = $sections->$current;
90: $hasChildren = $configFields->hasChildren($section, $website, $store);
91: if (!$hasChildren && $current) {
92: $this->_redirect('*/*/', array('website'=>$website, 'store'=>$store));
93: }
94:
95: $this->loadLayout();
96:
97: $this->_setActiveMenu('system/config');
98: $this->getLayout()->getBlock('menu')->setAdditionalCacheKeyInfo(array($current));
99:
100: $this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'),
101: $this->getUrl('*/system'));
102:
103: $this->getLayout()->getBlock('left')
104: ->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
105:
106: if ($this->_isSectionAllowedFlag) {
107: $this->_addContent($this->getLayout()->createBlock('adminhtml/system_config_edit')->initForm());
108:
109: $this->_addJs($this->getLayout()
110: ->createBlock('adminhtml/template')
111: ->setTemplate('system/shipping/ups.phtml'));
112: $this->_addJs($this->getLayout()
113: ->createBlock('adminhtml/template')
114: ->setTemplate('system/config/js.phtml'));
115: $this->_addJs($this->getLayout()
116: ->createBlock('adminhtml/template')
117: ->setTemplate('system/shipping/applicable_country.phtml'));
118:
119: $this->renderLayout();
120: }
121: }
122:
123: 124: 125: 126:
127: public function saveAction()
128: {
129: $session = Mage::getSingleton('adminhtml/session');
130:
131:
132: $groups = $this->getRequest()->getPost('groups');
133:
134: if (isset($_FILES['groups']['name']) && is_array($_FILES['groups']['name'])) {
135: 136: 137: 138:
139: foreach($_FILES['groups']['name'] as $groupName => $group) {
140: if (is_array($group)) {
141: foreach ($group['fields'] as $fieldName => $field) {
142: if (!empty($field['value'])) {
143: $groups[$groupName]['fields'][$fieldName] = array('value' => $field['value']);
144: }
145: }
146: }
147: }
148: }
149:
150: try {
151: if (!$this->_isSectionAllowed($this->getRequest()->getParam('section'))) {
152: throw new Exception(Mage::helper('adminhtml')->__('This section is not allowed.'));
153: }
154:
155:
156: $this->_saveSection();
157: $section = $this->getRequest()->getParam('section');
158: $website = $this->getRequest()->getParam('website');
159: $store = $this->getRequest()->getParam('store');
160: Mage::getSingleton('adminhtml/config_data')
161: ->setSection($section)
162: ->setWebsite($website)
163: ->setStore($store)
164: ->setGroups($groups)
165: ->save();
166:
167:
168: Mage::getConfig()->reinit();
169: Mage::dispatchEvent('admin_system_config_section_save_after', array(
170: 'website' => $website,
171: 'store' => $store,
172: 'section' => $section
173: ));
174: Mage::app()->reinitStores();
175:
176:
177: Mage::dispatchEvent("admin_system_config_changed_section_{$section}",
178: array('website' => $website, 'store' => $store)
179: );
180: $session->addSuccess(Mage::helper('adminhtml')->__('The configuration has been saved.'));
181: }
182: catch (Mage_Core_Exception $e) {
183: foreach(explode("\n", $e->getMessage()) as $message) {
184: $session->addError($message);
185: }
186: }
187: catch (Exception $e) {
188: $session->addException($e,
189: Mage::helper('adminhtml')->__('An error occurred while saving this configuration:') . ' '
190: . $e->getMessage());
191: }
192:
193: $this->_saveState($this->getRequest()->getPost('config_state'));
194:
195: $this->_redirect('*/*/edit', array('_current' => array('section', 'website', 'store')));
196: }
197:
198: 199: 200:
201: protected function _saveSection ()
202: {
203: $method = '_save' . uc_words($this->getRequest()->getParam('section'), '');
204: if (method_exists($this, $method)) {
205: $this->$method();
206: }
207: }
208:
209: 210: 211:
212: protected function _saveAdvanced()
213: {
214: Mage::app()->cleanCache(
215: array(
216: 'layout',
217: Mage_Core_Model_Layout_Update::LAYOUT_GENERAL_CACHE_TAG
218: ));
219: }
220:
221: 222: 223: 224:
225: public function stateAction()
226: {
227: if ($this->getRequest()->getParam('isAjax') == 1
228: && $this->getRequest()->getParam('container') != ''
229: && $this->getRequest()->getParam('value') != '') {
230:
231: $configState = array(
232: $this->getRequest()->getParam('container') => $this->getRequest()->getParam('value')
233: );
234: $this->_saveState($configState);
235: $this->getResponse()->setBody('success');
236: }
237: }
238:
239: 240: 241: 242:
243: public function exportTableratesAction()
244: {
245: $fileName = 'tablerates.csv';
246:
247: $gridBlock = $this->getLayout()->createBlock('adminhtml/shipping_carrier_tablerate_grid');
248: $website = Mage::app()->getWebsite($this->getRequest()->getParam('website'));
249: if ($this->getRequest()->getParam('conditionName')) {
250: $conditionName = $this->getRequest()->getParam('conditionName');
251: } else {
252: $conditionName = $website->getConfig('carriers/tablerate/condition_name');
253: }
254: $gridBlock->setWebsiteId($website->getId())->setConditionName($conditionName);
255: $content = $gridBlock->getCsvFile();
256: $this->_prepareDownloadResponse($fileName, $content);
257: }
258:
259: 260: 261: 262: 263:
264: protected function _isAllowed()
265: {
266: return Mage::getSingleton('admin/session')->isAllowed('system/config');
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276:
277: protected function _isSectionAllowed($section)
278: {
279: try {
280: $session = Mage::getSingleton('admin/session');
281: $resourceLookup = "admin/system/config/{$section}";
282: if ($session->getData('acl') instanceof Mage_Admin_Model_Acl) {
283: $resourceId = $session->getData('acl')->get($resourceLookup)->getResourceId();
284: if (!$session->isAllowed($resourceId)) {
285: throw new Exception('');
286: }
287: return true;
288: }
289: }
290: catch (Zend_Acl_Exception $e) {
291: $this->norouteAction();
292: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
293: return false;
294: }
295: catch (Exception $e) {
296: $this->deniedAction();
297: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
298: return false;
299: }
300: }
301:
302: 303: 304: 305: 306: 307:
308: protected function _saveState($configState = array())
309: {
310: $adminUser = Mage::getSingleton('admin/session')->getUser();
311: if (is_array($configState)) {
312: $extra = $adminUser->getExtra();
313: if (!is_array($extra)) {
314: $extra = array();
315: }
316: if (!isset($extra['configState'])) {
317: $extra['configState'] = array();
318: }
319: foreach ($configState as $fieldset => $state) {
320: $extra['configState'][$fieldset] = $state;
321: }
322: $adminUser->saveExtra($extra);
323: }
324:
325: return true;
326: }
327: }
328: