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_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: 61: 62:
63: public function newAction()
64: {
65: $this->_forward('edit');
66: }
67:
68: 69: 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: 109: 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:
119: $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML);
120: }
121:
122: if($request->getParam('_change_type_flag')) {
123:
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:
148: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The email template has been deleted.'));
149:
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:
160: Mage::getSingleton('adminhtml/session')->setFormData($data);
161:
162: $this->_redirect('*/*/edit', array('id' => $id));
163: return;
164: }
165: }
166:
167: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Unable to find a Email Template to delete.'));
168:
169: $this->_redirect('*/*/');
170: }
171:
172: public function previewAction()
173: {
174: $this->loadLayout('systemPreview');
175: $this->renderLayout();
176: }
177:
178: 179: 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: 199: 200: 201: 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: }
226: