PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Adminhtml/controllers/System/Email/TemplateController.php

https://bitbucket.org/jit_bec/shopifine
PHP | 225 lines | 136 code | 28 blank | 61 comment | 11 complexity | 1d8273321c6eb996d5700797c018bfb9 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 * System Template admin controller
 29 *
 30 * @category   Mage
 31 * @package    Mage_Adminhtml
 32 * @author      Magento Core Team <core@magentocommerce.com>
 33 */
 34class Mage_Adminhtml_System_Email_TemplateController extends Mage_Adminhtml_Controller_Action
 35{
 36    public function indexAction()
 37    {
 38        $this->_title($this->__('System'))->_title($this->__('Transactional Emails'));
 39
 40        if ($this->getRequest()->getQuery('ajax')) {
 41            $this->_forward('grid');
 42            return;
 43        }
 44
 45        $this->loadLayout();
 46        $this->_setActiveMenu('system/email_template');
 47        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Transactional Emails'), Mage::helper('adminhtml')->__('Transactional Emails'));
 48
 49        $this->_addContent($this->getLayout()->createBlock('adminhtml/system_email_template', 'template'));
 50        $this->renderLayout();
 51    }
 52
 53    public function gridAction()
 54    {
 55        $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/system_email_template_grid')->toHtml());
 56    }
 57
 58
 59    /**
 60     * New transactional email action
 61     *
 62     */
 63    public function newAction()
 64    {
 65        $this->_forward('edit');
 66    }
 67
 68    /**
 69     * Edit transactioanl email action
 70     *
 71     */
 72    public function editAction()
 73    {
 74        $this->loadLayout();
 75        $template = $this->_initTemplate('id');
 76        $this->_setActiveMenu('system/email_template');
 77        $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Transactional Emails'), Mage::helper('adminhtml')->__('Transactional Emails'), $this->getUrl('*/*'));
 78
 79        if ($this->getRequest()->getParam('id')) {
 80            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Edit Template'), Mage::helper('adminhtml')->__('Edit System Template'));
 81        } else {
 82            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('New Template'), Mage::helper('adminhtml')->__('New System Template'));
 83        }
 84
 85        $this->_title($template->getId() ? $template->getTemplateCode() : $this->__('New Template'));
 86
 87        $this->_addContent($this->getLayout()->createBlock('adminhtml/system_email_template_edit', 'template_edit')
 88                                                            ->setEditMode((bool)$this->getRequest()->getParam('id')));
 89        $this->renderLayout();
 90    }
 91
 92    public function saveAction()
 93    {
 94        $request = $this->getRequest();
 95        $id = $this->getRequest()->getParam('id');
 96
 97        $template = $this->_initTemplate('id');
 98        if (!$template->getId() && $id) {
 99            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('This Email template no longer exists.'));
100            $this->_redirect('*/*/');
101            return;
102        }
103
104        try {
105            $template->setTemplateSubject($request->getParam('template_subject'))
106                ->setTemplateCode($request->getParam('template_code'))
107/*
108                ->setTemplateSenderEmail($request->getParam('sender_email'))
109                ->setTemplateSenderName($request->getParam('sender_name'))
110*/
111                ->setTemplateText($request->getParam('template_text'))
112                ->setTemplateStyles($request->getParam('template_styles'))
113                ->setModifiedAt(Mage::getSingleton('core/date')->gmtDate())
114                ->setOrigTemplateCode($request->getParam('orig_template_code'))
115                ->setOrigTemplateVariables($request->getParam('orig_template_variables'));
116
117            if (!$template->getId()) {
118                //$type = constant(Mage::getConfig()->getModelClassName('core/email_template') . "::TYPE_HTML");
119                $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML);
120            }
121
122            if($request->getParam('_change_type_flag')) {
123                //$type = constant(Mage::getConfig()->getModelClassName('core/email_template') . "::TYPE_TEXT");
124                $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_TEXT);
125                $template->setTemplateStyles('');
126            }
127
128            $template->save();
129            Mage::getSingleton('adminhtml/session')->setFormData(false);
130            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The email template has been saved.'));
131            $this->_redirect('*/*');
132        }
133        catch (Exception $e) {
134            Mage::getSingleton('adminhtml/session')->setData('email_template_form_data', $this->getRequest()->getParams());
135            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
136            $this->_forward('new');
137        }
138
139    }
140
141    public function deleteAction() {
142
143        $template = $this->_initTemplate('id');
144        if($template->getId()) {
145            try {
146                $template->delete();
147                 // display success message
148                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The email template has been deleted.'));
149                // go to grid
150                $this->_redirect('*/*/');
151                return;
152            }
153            catch (Mage_Core_Exception $e) {
154                $this->_getSession()->addError($e->getMessage());
155            }
156            catch (Exception $e) {
157                $this->_getSession()->addError(Mage::helper('adminhtml')->__('An error occurred while deleting email template data. Please review log and try again.'));
158                Mage::logException($e);
159                // save data in session
160                Mage::getSingleton('adminhtml/session')->setFormData($data);
161                // redirect to edit form
162                $this->_redirect('*/*/edit', array('id' => $id));
163                return;
164            }
165        }
166        // display error message
167        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find a Email Template to delete.'));
168        // go to grid
169        $this->_redirect('*/*/');
170    }
171
172    public function previewAction()
173    {
174        $this->loadLayout('systemPreview');
175        $this->renderLayout();
176    }
177
178    /**
179     * Set template data to retrieve it in template info form
180     *
181     */
182    public function defaultTemplateAction()
183    {
184        $template = $this->_initTemplate('id');
185        $templateCode = $this->getRequest()->getParam('code');
186
187        $template->loadDefault($templateCode, $this->getRequest()->getParam('locale'));
188        $template->setData('orig_template_code', $templateCode);
189        $template->setData('template_variables', Zend_Json::encode($template->getVariablesOptionArray(true)));
190
191        $templateBlock = $this->getLayout()->createBlock('adminhtml/system_email_template_edit');
192        $template->setData('orig_template_used_default_for', $templateBlock->getUsedDefaultForPaths(false));
193
194        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($template->getData()));
195    }
196
197    /**
198     * Load email template from request
199     *
200     * @param string $idFieldName
201     * @return Mage_Adminhtml_Model_Email_Template $model
202     */
203    protected function _initTemplate($idFieldName = 'template_id')
204    {
205        $this->_title($this->__('System'))->_title($this->__('Transactional Emails'));
206
207        $id = (int)$this->getRequest()->getParam($idFieldName);
208        $model = Mage::getModel('adminhtml/email_template');
209        if ($id) {
210            $model->load($id);
211        }
212        if (!Mage::registry('email_template')) {
213            Mage::register('email_template', $model);
214        }
215        if (!Mage::registry('current_email_template')) {
216            Mage::register('current_email_template', $model);
217        }
218        return $model;
219    }
220
221    protected function _isAllowed()
222    {
223        return Mage::getSingleton('admin/session')->isAllowed('system/email_template');
224    }
225}