PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Adminhtml/controllers/System/StoreController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 504 lines | 318 code | 22 blank | 164 comment | 9 complexity | 5810a89a6e8df566883b5f0a6510dbf8 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Store controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_System_StoreController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Init actions
  37. *
  38. * @return Mage_Adminhtml_Cms_PageController
  39. */
  40. protected function _initAction()
  41. {
  42. // load layout, set active menu and breadcrumbs
  43. $this->loadLayout()
  44. ->_setActiveMenu('system/store')
  45. ->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'))
  46. ->_addBreadcrumb(Mage::helper('adminhtml')->__('Manage Stores'), Mage::helper('adminhtml')->__('Manage Stores'))
  47. ;
  48. return $this;
  49. }
  50. public function indexAction()
  51. {
  52. $this->_title($this->__('System'))
  53. ->_title($this->__('Stores'));
  54. $this->_initAction()
  55. ->_addContent($this->getLayout()->createBlock('adminhtml/system_store_store'))
  56. ->renderLayout();
  57. }
  58. public function newWebsiteAction()
  59. {
  60. Mage::register('store_type', 'website');
  61. $this->_forward('newStore');
  62. }
  63. public function newGroupAction()
  64. {
  65. Mage::register('store_type', 'group');
  66. $this->_forward('newStore');
  67. }
  68. public function newStoreAction()
  69. {
  70. if (!Mage::registry('store_type')) {
  71. Mage::register('store_type', 'store');
  72. }
  73. Mage::register('store_action', 'add');
  74. $this->_forward('editStore');
  75. }
  76. public function editWebsiteAction()
  77. {
  78. Mage::register('store_type', 'website');
  79. $this->_forward('editStore');
  80. }
  81. public function editGroupAction()
  82. {
  83. Mage::register('store_type', 'group');
  84. $this->_forward('editStore');
  85. }
  86. public function editStoreAction()
  87. {
  88. $this->_title($this->__('System'))
  89. ->_title($this->__('Stores'));
  90. $session = $this->_getSession();
  91. if ($session->getPostData()) {
  92. Mage::register('store_post_data', $session->getPostData());
  93. $session->unsPostData();
  94. }
  95. if (!Mage::registry('store_type')) {
  96. Mage::register('store_type', 'store');
  97. }
  98. if (!Mage::registry('store_action')) {
  99. Mage::register('store_action', 'edit');
  100. }
  101. switch (Mage::registry('store_type')) {
  102. case 'website':
  103. $itemId = $this->getRequest()->getParam('website_id', null);
  104. $model = Mage::getModel('core/website');
  105. $title = Mage::helper('core')->__("Website");
  106. $notExists = Mage::helper('core')->__("The website does not exist.");
  107. $codeBase = Mage::helper('core')->__('Before modifying the website code please make sure that it is not used in index.php.');
  108. break;
  109. case 'group':
  110. $itemId = $this->getRequest()->getParam('group_id', null);
  111. $model = Mage::getModel('core/store_group');
  112. $title = Mage::helper('core')->__("Store");
  113. $notExists = Mage::helper('core')->__("The store does not exist");
  114. $codeBase = false;
  115. break;
  116. case 'store':
  117. $itemId = $this->getRequest()->getParam('store_id', null);
  118. $model = Mage::getModel('core/store');
  119. $title = Mage::helper('core')->__("Store View");
  120. $notExists = Mage::helper('core')->__("Store view doesn't exist");
  121. $codeBase = Mage::helper('core')->__('Before modifying the store view code please make sure that it is not used in index.php.');
  122. break;
  123. }
  124. if (null !== $itemId) {
  125. $model->load($itemId);
  126. }
  127. if ($model->getId() || Mage::registry('store_action') == 'add') {
  128. Mage::register('store_data', $model);
  129. if (Mage::registry('store_action') == 'add') {
  130. $this->_title($this->__('New ') . $title);
  131. }
  132. else {
  133. $this->_title($model->getName());
  134. }
  135. if (Mage::registry('store_action') == 'edit' && $codeBase && !$model->isReadOnly()) {
  136. $this->_getSession()->addNotice($codeBase);
  137. }
  138. $this->_initAction()
  139. ->_addContent($this->getLayout()->createBlock('adminhtml/system_store_edit'))
  140. ->renderLayout();
  141. }
  142. else {
  143. $session->addError($notExists);
  144. $this->_redirect('*/*/');
  145. }
  146. }
  147. public function saveAction()
  148. {
  149. if ($this->getRequest()->isPost() && $postData = $this->getRequest()->getPost()) {
  150. if (empty($postData['store_type']) || empty($postData['store_action'])) {
  151. $this->_redirect('*/*/');
  152. return;
  153. }
  154. $session = $this->_getSession();
  155. try {
  156. switch ($postData['store_type']) {
  157. case 'website':
  158. $postData['website']['name'] = $this->_getHelper()->removeTags($postData['website']['name']);
  159. $websiteModel = Mage::getModel('core/website');
  160. if ($postData['website']['website_id']) {
  161. $websiteModel->load($postData['website']['website_id']);
  162. }
  163. $websiteModel->setData($postData['website']);
  164. if ($postData['website']['website_id'] == '') {
  165. $websiteModel->setId(null);
  166. }
  167. $websiteModel->save();
  168. $session->addSuccess(Mage::helper('core')->__('The website has been saved.'));
  169. break;
  170. case 'group':
  171. $postData['group']['name'] = $this->_getHelper()->removeTags($postData['group']['name']);
  172. $groupModel = Mage::getModel('core/store_group');
  173. if ($postData['group']['group_id']) {
  174. $groupModel->load($postData['group']['group_id']);
  175. }
  176. $groupModel->setData($postData['group']);
  177. if ($postData['group']['group_id'] == '') {
  178. $groupModel->setId(null);
  179. }
  180. $groupModel->save();
  181. Mage::dispatchEvent('store_group_save', array('group' => $groupModel));
  182. $session->addSuccess(Mage::helper('core')->__('The store has been saved.'));
  183. break;
  184. case 'store':
  185. $eventName = 'store_edit';
  186. $storeModel = Mage::getModel('core/store');
  187. $postData['store']['name'] = $this->_getHelper()->removeTags($postData['store']['name']);
  188. if ($postData['store']['store_id']) {
  189. $storeModel->load($postData['store']['store_id']);
  190. }
  191. $storeModel->setData($postData['store']);
  192. if ($postData['store']['store_id'] == '') {
  193. $storeModel->setId(null);
  194. $eventName = 'store_add';
  195. }
  196. $groupModel = Mage::getModel('core/store_group')->load($storeModel->getGroupId());
  197. $storeModel->setWebsiteId($groupModel->getWebsiteId());
  198. $storeModel->save();
  199. Mage::app()->reinitStores();
  200. Mage::dispatchEvent($eventName, array('store'=>$storeModel));
  201. $session->addSuccess(Mage::helper('core')->__('The store view has been saved'));
  202. break;
  203. default:
  204. $this->_redirect('*/*/');
  205. return;
  206. }
  207. $this->_redirect('*/*/');
  208. return;
  209. }
  210. catch (Mage_Core_Exception $e) {
  211. $session->addError($e->getMessage());
  212. $session->setPostData($postData);
  213. }
  214. catch (Exception $e) {
  215. $session->addException($e, Mage::helper('core')->__('An error occurred while saving. Please review the error log.'));
  216. $session->setPostData($postData);
  217. }
  218. $this->_redirectReferer();
  219. return;
  220. }
  221. $this->_redirect('*/*/');
  222. }
  223. public function deleteWebsiteAction()
  224. {
  225. $this->_title($this->__('System'))
  226. ->_title($this->__('Stores'))
  227. ->_title($this->__('Delete Website'));
  228. $session = $this->_getSession();
  229. $itemId = $this->getRequest()->getParam('item_id', null);
  230. if (!$model = Mage::getModel('core/website')->load($itemId)) {
  231. $session->addError(Mage::helper('core')->__('Unable to proceed. Please, try again.'));
  232. $this->_redirect('*/*/');
  233. return ;
  234. }
  235. if (!$model->isCanDelete()) {
  236. $session->addError(Mage::helper('core')->__('This website cannot be deleted.'));
  237. $this->_redirect('*/*/editWebsite', array('website_id' => $itemId));
  238. return ;
  239. }
  240. $this->_addDeletionNotice('website');
  241. $this->_initAction()
  242. ->_addBreadcrumb(Mage::helper('core')->__('Delete Website'), Mage::helper('core')->__('Delete Website'))
  243. ->_addContent($this->getLayout()->createBlock('adminhtml/system_store_delete')
  244. ->setFormActionUrl($this->getUrl('*/*/deleteWebsitePost'))
  245. ->setBackUrl($this->getUrl('*/*/editWebsite', array('website_id' => $itemId)))
  246. ->setStoreTypeTitle(Mage::helper('core')->__('Website'))
  247. ->setDataObject($model)
  248. )
  249. ->renderLayout();
  250. }
  251. public function deleteGroupAction()
  252. {
  253. $this->_title($this->__('System'))
  254. ->_title($this->__('Stores'))
  255. ->_title($this->__('Delete Store'));
  256. $session = $this->_getSession();
  257. $itemId = $this->getRequest()->getParam('item_id', null);
  258. if (!$model = Mage::getModel('core/store_group')->load($itemId)) {
  259. $session->addError(Mage::helper('core')->__('Unable to proceed. Please, try again.'));
  260. $this->_redirect('*/*/');
  261. return ;
  262. }
  263. if (!$model->isCanDelete()) {
  264. $session->addError(Mage::helper('core')->__('This store cannot be deleted.'));
  265. $this->_redirect('*/*/editGroup', array('group_id' => $itemId));
  266. return ;
  267. }
  268. $this->_addDeletionNotice('store');
  269. $this->_initAction()
  270. ->_addBreadcrumb(Mage::helper('core')->__('Delete Store'), Mage::helper('core')->__('Delete Store'))
  271. ->_addContent($this->getLayout()->createBlock('adminhtml/system_store_delete')
  272. ->setFormActionUrl($this->getUrl('*/*/deleteGroupPost'))
  273. ->setBackUrl($this->getUrl('*/*/editGroup', array('group_id' => $itemId)))
  274. ->setStoreTypeTitle(Mage::helper('core')->__('Store'))
  275. ->setDataObject($model)
  276. )
  277. ->renderLayout();
  278. }
  279. public function deleteStoreAction()
  280. {
  281. $this->_title($this->__('System'))
  282. ->_title($this->__('Stores'))
  283. ->_title($this->__('Delete Store View'));
  284. $session = $this->_getSession();
  285. $itemId = $this->getRequest()->getParam('item_id', null);
  286. if (!$model = Mage::getModel('core/store')->load($itemId)) {
  287. $session->addError(Mage::helper('core')->__('Unable to proceed. Please, try again.'));
  288. $this->_redirect('*/*/');
  289. return ;
  290. }
  291. if (!$model->isCanDelete()) {
  292. $session->addError(Mage::helper('core')->__('This store view cannot be deleted.'));
  293. $this->_redirect('*/*/editStore', array('store_id' => $itemId));
  294. return ;
  295. }
  296. $this->_addDeletionNotice('store view');;
  297. $this->_initAction()
  298. ->_addBreadcrumb(Mage::helper('core')->__('Delete Store View'), Mage::helper('core')->__('Delete Store View'))
  299. ->_addContent($this->getLayout()->createBlock('adminhtml/system_store_delete')
  300. ->setFormActionUrl($this->getUrl('*/*/deleteStorePost'))
  301. ->setBackUrl($this->getUrl('*/*/editStore', array('store_id' => $itemId)))
  302. ->setStoreTypeTitle(Mage::helper('core')->__('Store View'))
  303. ->setDataObject($model)
  304. )
  305. ->renderLayout();
  306. }
  307. public function deleteWebsitePostAction()
  308. {
  309. $itemId = $this->getRequest()->getParam('item_id');
  310. if (!$model = Mage::getModel('core/website')->load($itemId)) {
  311. $this->_getSession()->addError(Mage::helper('core')->__('Unable to proceed. Please, try again'));
  312. $this->_redirect('*/*/');
  313. return ;
  314. }
  315. if (!$model->isCanDelete()) {
  316. $this->_getSession()->addError(Mage::helper('core')->__('This website cannot be deleted.'));
  317. $this->_redirect('*/*/editWebsite', array('website_id' => $model->getId()));
  318. return ;
  319. }
  320. $this->_backupDatabase('*/*/editWebsite', array('website_id' => $itemId));
  321. try {
  322. $model->delete();
  323. $this->_getSession()->addSuccess(Mage::helper('core')->__('The website has been deleted.'));
  324. $this->_redirect('*/*/');
  325. return ;
  326. }
  327. catch (Mage_Core_Exception $e) {
  328. $this->_getSession()->addError($e->getMessage());
  329. }
  330. catch (Exception $e) {
  331. $this->_getSession()->addException($e, Mage::helper('core')->__('Unable to delete website. Please, try again later.'));
  332. }
  333. $this->_redirect('*/*/editWebsite', array('website_id' => $itemId));
  334. }
  335. public function deleteGroupPostAction()
  336. {
  337. $itemId = $this->getRequest()->getParam('item_id');
  338. if (!$model = Mage::getModel('core/store_group')->load($itemId)) {
  339. $this->_getSession()->addError(Mage::helper('core')->__('Unable to proceed. Please, try again.'));
  340. $this->_redirect('*/*/');
  341. return ;
  342. }
  343. if (!$model->isCanDelete()) {
  344. $this->_getSession()->addError(Mage::helper('core')->__('This store cannot be deleted.'));
  345. $this->_redirect('*/*/editGroup', array('group_id' => $model->getId()));
  346. return ;
  347. }
  348. $this->_backupDatabase('*/*/editGroup', array('group_id' => $itemId));
  349. try {
  350. $model->delete();
  351. $this->_getSession()->addSuccess(Mage::helper('core')->__('The store has been deleted.'));
  352. $this->_redirect('*/*/');
  353. return ;
  354. }
  355. catch (Mage_Core_Exception $e) {
  356. $this->_getSession()->addError($e->getMessage());
  357. }
  358. catch (Exception $e) {
  359. $this->_getSession()->addException($e, Mage::helper('core')->__('Unable to delete store. Please, try again later.'));
  360. }
  361. $this->_redirect('*/*/editGroup', array('group_id' => $itemId));
  362. }
  363. /**
  364. * Delete store view post action
  365. *
  366. */
  367. public function deleteStorePostAction()
  368. {
  369. $itemId = $this->getRequest()->getParam('item_id');
  370. if (!$model = Mage::getModel('core/store')->load($itemId)) {
  371. $this->_getSession()->addError(Mage::helper('core')->__('Unable to proceed. Please, try again'));
  372. $this->_redirect('*/*/');
  373. return ;
  374. }
  375. if (!$model->isCanDelete()) {
  376. $this->_getSession()->addError(Mage::helper('core')->__('This store view cannot be deleted.'));
  377. $this->_redirect('*/*/editStore', array('store_id' => $model->getId()));
  378. return ;
  379. }
  380. $this->_backupDatabase('*/*/editStore', array('store_id' => $itemId));
  381. try {
  382. $model->delete();
  383. Mage::dispatchEvent('store_delete', array('store' => $model));
  384. $this->_getSession()->addSuccess(Mage::helper('core')->__('The store view has been deleted.'));
  385. $this->_redirect('*/*/');
  386. return ;
  387. }
  388. catch (Mage_Core_Exception $e) {
  389. $this->_getSession()->addError($e->getMessage());
  390. }
  391. catch (Exception $e) {
  392. $this->_getSession()->addException($e, Mage::helper('core')->__('Unable to delete store view. Please, try again later.'));
  393. }
  394. $this->_redirect('*/*/editStore', array('store_id' => $itemId));
  395. }
  396. protected function _isAllowed()
  397. {
  398. return Mage::getSingleton('admin/session')->isAllowed('system/store');
  399. }
  400. /**
  401. * Backup database
  402. *
  403. * @param string $failPath redirect path if backup failed
  404. * @param array $arguments
  405. * @return Mage_Adminhtml_System_StoreController
  406. */
  407. protected function _backupDatabase($failPath, $arguments=array())
  408. {
  409. if (! $this->getRequest()->getParam('create_backup')) {
  410. return $this;
  411. }
  412. try {
  413. $backupDb = Mage::getModel('backup/db');
  414. $backup = Mage::getModel('backup/backup')
  415. ->setTime(time())
  416. ->setType('db')
  417. ->setPath(Mage::getBaseDir('var') . DS . 'backups');
  418. $backupDb->createBackup($backup);
  419. $this->_getSession()->addSuccess(Mage::helper('backup')->__('Database was successfuly backed up.'));
  420. }
  421. catch (Mage_Core_Exception $e) {
  422. $this->_getSession()->addError($e->getMessage());
  423. $this->_redirect($failPath, $arguments);
  424. return ;
  425. }
  426. catch (Exception $e) {
  427. $this->_getSession()->addException($e, Mage::helper('backup')->__('Unable to create backup. Please, try again later.'));
  428. $this->_redirect($failPath, $arguments);
  429. return ;
  430. }
  431. return $this;
  432. }
  433. /**
  434. * Add notification on deleting store / store view / website
  435. *
  436. * @param string $typeTitle
  437. * @return Mage_Adminhtml_System_StoreController
  438. */
  439. protected function _addDeletionNotice($typeTitle)
  440. {
  441. $this->_getSession()->addNotice(
  442. Mage::helper('core')->__('Deleting a %1$s will not delete the information associated with the %1$s (e.g. categories, products, etc.), but the %1$s will not be able to be restored. It is suggested that you create a database backup before deleting the %1$s.', $typeTitle)
  443. );
  444. return $this;
  445. }
  446. }