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