PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php

https://bitbucket.org/kdms/sh-magento
PHP | 327 lines | 196 code | 35 blank | 96 comment | 23 complexity | 7e995de63b1316f45b28edc384dc9612 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Configuration controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_System_ConfigController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Whether current section is allowed
  37. *
  38. * @var bool
  39. */
  40. protected $_isSectionAllowedFlag = true;
  41. /**
  42. * Controller predispatch method
  43. * Check if current section is found and is allowed
  44. *
  45. * @return Mage_Adminhtml_System_ConfigController
  46. */
  47. public function preDispatch()
  48. {
  49. parent::preDispatch();
  50. if ($this->getRequest()->getParam('section')) {
  51. $this->_isSectionAllowedFlag = $this->_isSectionAllowed($this->getRequest()->getParam('section'));
  52. }
  53. return $this;
  54. }
  55. /**
  56. * Index action
  57. *
  58. */
  59. public function indexAction()
  60. {
  61. $this->_forward('edit');
  62. }
  63. /**
  64. * Edit configuration section
  65. *
  66. */
  67. public function editAction()
  68. {
  69. $this->_title($this->__('System'))->_title($this->__('Configuration'));
  70. $current = $this->getRequest()->getParam('section');
  71. $website = $this->getRequest()->getParam('website');
  72. $store = $this->getRequest()->getParam('store');
  73. Mage::getSingleton('adminhtml/config_data')
  74. ->setSection($current)
  75. ->setWebsite($website)
  76. ->setStore($store);
  77. $configFields = Mage::getSingleton('adminhtml/config');
  78. $sections = $configFields->getSections($current);
  79. $section = $sections->$current;
  80. $hasChildren = $configFields->hasChildren($section, $website, $store);
  81. if (!$hasChildren && $current) {
  82. $this->_redirect('*/*/', array('website'=>$website, 'store'=>$store));
  83. }
  84. $this->loadLayout();
  85. $this->_setActiveMenu('system/config');
  86. $this->getLayout()->getBlock('menu')->setAdditionalCacheKeyInfo(array($current));
  87. $this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'),
  88. $this->getUrl('*/system'));
  89. $this->getLayout()->getBlock('left')
  90. ->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
  91. if ($this->_isSectionAllowedFlag) {
  92. $this->_addContent($this->getLayout()->createBlock('adminhtml/system_config_edit')->initForm());
  93. $this->_addJs($this->getLayout()
  94. ->createBlock('adminhtml/template')
  95. ->setTemplate('system/shipping/ups.phtml'));
  96. $this->_addJs($this->getLayout()
  97. ->createBlock('adminhtml/template')
  98. ->setTemplate('system/config/js.phtml'));
  99. $this->_addJs($this->getLayout()
  100. ->createBlock('adminhtml/template')
  101. ->setTemplate('system/shipping/applicable_country.phtml'));
  102. $this->renderLayout();
  103. }
  104. }
  105. /**
  106. * Save configuration
  107. *
  108. */
  109. public function saveAction()
  110. {
  111. $session = Mage::getSingleton('adminhtml/session');
  112. /* @var $session Mage_Adminhtml_Model_Session */
  113. $groups = $this->getRequest()->getPost('groups');
  114. if (isset($_FILES['groups']['name']) && is_array($_FILES['groups']['name'])) {
  115. /**
  116. * Carefully merge $_FILES and $_POST information
  117. * None of '+=' or 'array_merge_recursive' can do this correct
  118. */
  119. foreach($_FILES['groups']['name'] as $groupName => $group) {
  120. if (is_array($group)) {
  121. foreach ($group['fields'] as $fieldName => $field) {
  122. if (!empty($field['value'])) {
  123. $groups[$groupName]['fields'][$fieldName] = array('value' => $field['value']);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. try {
  130. if (!$this->_isSectionAllowed($this->getRequest()->getParam('section'))) {
  131. throw new Exception(Mage::helper('adminhtml')->__('This section is not allowed.'));
  132. }
  133. // custom save logic
  134. $this->_saveSection();
  135. $section = $this->getRequest()->getParam('section');
  136. $website = $this->getRequest()->getParam('website');
  137. $store = $this->getRequest()->getParam('store');
  138. Mage::getSingleton('adminhtml/config_data')
  139. ->setSection($section)
  140. ->setWebsite($website)
  141. ->setStore($store)
  142. ->setGroups($groups)
  143. ->save();
  144. // reinit configuration
  145. Mage::getConfig()->reinit();
  146. Mage::dispatchEvent('admin_system_config_section_save_after', array(
  147. 'website' => $website,
  148. 'store' => $store,
  149. 'section' => $section
  150. ));
  151. Mage::app()->reinitStores();
  152. // website and store codes can be used in event implementation, so set them as well
  153. Mage::dispatchEvent("admin_system_config_changed_section_{$section}",
  154. array('website' => $website, 'store' => $store)
  155. );
  156. $session->addSuccess(Mage::helper('adminhtml')->__('The configuration has been saved.'));
  157. }
  158. catch (Mage_Core_Exception $e) {
  159. foreach(explode("\n", $e->getMessage()) as $message) {
  160. $session->addError($message);
  161. }
  162. }
  163. catch (Exception $e) {
  164. $session->addException($e,
  165. Mage::helper('adminhtml')->__('An error occurred while saving this configuration:') . ' '
  166. . $e->getMessage());
  167. }
  168. $this->_saveState($this->getRequest()->getPost('config_state'));
  169. $this->_redirect('*/*/edit', array('_current' => array('section', 'website', 'store')));
  170. }
  171. /**
  172. * Custom save logic for section
  173. */
  174. protected function _saveSection ()
  175. {
  176. $method = '_save' . uc_words($this->getRequest()->getParam('section'), '');
  177. if (method_exists($this, $method)) {
  178. $this->$method();
  179. }
  180. }
  181. /**
  182. * Advanced save procedure
  183. */
  184. protected function _saveAdvanced()
  185. {
  186. Mage::app()->cleanCache(
  187. array(
  188. 'layout',
  189. Mage_Core_Model_Layout_Update::LAYOUT_GENERAL_CACHE_TAG
  190. ));
  191. }
  192. /**
  193. * Save fieldset state through AJAX
  194. *
  195. */
  196. public function stateAction()
  197. {
  198. if ($this->getRequest()->getParam('isAjax') == 1
  199. && $this->getRequest()->getParam('container') != ''
  200. && $this->getRequest()->getParam('value') != '') {
  201. $configState = array(
  202. $this->getRequest()->getParam('container') => $this->getRequest()->getParam('value')
  203. );
  204. $this->_saveState($configState);
  205. $this->getResponse()->setBody('success');
  206. }
  207. }
  208. /**
  209. * Export shipping table rates in csv format
  210. *
  211. */
  212. public function exportTableratesAction()
  213. {
  214. $fileName = 'tablerates.csv';
  215. /** @var $gridBlock Mage_Adminhtml_Block_Shipping_Carrier_Tablerate_Grid */
  216. $gridBlock = $this->getLayout()->createBlock('adminhtml/shipping_carrier_tablerate_grid');
  217. $website = Mage::app()->getWebsite($this->getRequest()->getParam('website'));
  218. if ($this->getRequest()->getParam('conditionName')) {
  219. $conditionName = $this->getRequest()->getParam('conditionName');
  220. } else {
  221. $conditionName = $website->getConfig('carriers/tablerate/condition_name');
  222. }
  223. $gridBlock->setWebsiteId($website->getId())->setConditionName($conditionName);
  224. $content = $gridBlock->getCsvFile();
  225. $this->_prepareDownloadResponse($fileName, $content);
  226. }
  227. /**
  228. * Check is allow modify system configuration
  229. *
  230. * @return bool
  231. */
  232. protected function _isAllowed()
  233. {
  234. return Mage::getSingleton('admin/session')->isAllowed('system/config');
  235. }
  236. /**
  237. * Check if specified section allowed in ACL
  238. *
  239. * Will forward to deniedAction(), if not allowed.
  240. *
  241. * @param string $section
  242. * @return bool
  243. */
  244. protected function _isSectionAllowed($section)
  245. {
  246. try {
  247. $session = Mage::getSingleton('admin/session');
  248. $resourceLookup = "admin/system/config/{$section}";
  249. if ($session->getData('acl') instanceof Mage_Admin_Model_Acl) {
  250. $resourceId = $session->getData('acl')->get($resourceLookup)->getResourceId();
  251. if (!$session->isAllowed($resourceId)) {
  252. throw new Exception('');
  253. }
  254. return true;
  255. }
  256. }
  257. catch (Zend_Acl_Exception $e) {
  258. $this->norouteAction();
  259. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  260. return false;
  261. }
  262. catch (Exception $e) {
  263. $this->deniedAction();
  264. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  265. return false;
  266. }
  267. }
  268. /**
  269. * Save state of configuration field sets
  270. *
  271. * @param array $configState
  272. * @return bool
  273. */
  274. protected function _saveState($configState = array())
  275. {
  276. $adminUser = Mage::getSingleton('admin/session')->getUser();
  277. if (is_array($configState)) {
  278. $extra = $adminUser->getExtra();
  279. if (!is_array($extra)) {
  280. $extra = array();
  281. }
  282. if (!isset($extra['configState'])) {
  283. $extra['configState'] = array();
  284. }
  285. foreach ($configState as $fieldset => $state) {
  286. $extra['configState'][$fieldset] = $state;
  287. }
  288. $adminUser->saveExtra($extra);
  289. }
  290. return true;
  291. }
  292. }