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: class Mage_Adminhtml_Newsletter_TemplateController extends Mage_Adminhtml_Controller_Action
34: {
35: 36: 37: 38: 39:
40: protected function _isAllowed ()
41: {
42: return Mage::getSingleton('admin/session')
43: ->isAllowed('newsletter/template');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _setTitle()
52: {
53: return $this->_title($this->__('Newsletter'))->_title($this->__('Newsletter Templates'));
54: }
55:
56: 57: 58: 59:
60: public function indexAction ()
61: {
62: $this->_setTitle();
63:
64: if ($this->getRequest()->getQuery('ajax')) {
65: $this->_forward('grid');
66: return;
67: }
68: $this->loadLayout();
69: $this->_setActiveMenu('newsletter/template');
70: $this->_addBreadcrumb(Mage::helper('newsletter')->__('Newsletter Templates'), Mage::helper('newsletter')->__('Newsletter Templates'));
71: $this->_addContent($this->getLayout()->createBlock('adminhtml/newsletter_template', 'template'));
72: $this->renderLayout();
73: }
74:
75: 76: 77: 78:
79: public function gridAction ()
80: {
81: $this->loadLayout();
82: $grid = $this->getLayout()->createBlock('adminhtml/newsletter_template_grid')
83: ->toHtml();
84: $this->getResponse()->setBody($grid);
85: }
86:
87: 88: 89: 90:
91: public function newAction ()
92: {
93: $this->_forward('edit');
94: }
95:
96: 97: 98: 99:
100: public function editAction ()
101: {
102: $this->_setTitle();
103:
104: $model = Mage::getModel('newsletter/template');
105: if ($id = $this->getRequest()->getParam('id')) {
106: $model->load($id);
107: }
108:
109: Mage::register('_current_template', $model);
110:
111: $this->loadLayout();
112: $this->_setActiveMenu('newsletter/template');
113:
114: if ($model->getId()) {
115: $breadcrumbTitle = Mage::helper('newsletter')->__('Edit Template');
116: $breadcrumbLabel = $breadcrumbTitle;
117: }
118: else {
119: $breadcrumbTitle = Mage::helper('newsletter')->__('New Template');
120: $breadcrumbLabel = Mage::helper('newsletter')->__('Create Newsletter Template');
121: }
122:
123: $this->_title($model->getId() ? $model->getTemplateCode() : $this->__('New Template'));
124:
125: $this->_addBreadcrumb($breadcrumbLabel, $breadcrumbTitle);
126:
127:
128: if ($values = $this->_getSession()->getData('newsletter_template_form_data', true)) {
129: $model->addData($values);
130: }
131:
132: if ($editBlock = $this->getLayout()->getBlock('template_edit')) {
133: $editBlock->setEditMode($model->getId() > 0);
134: }
135:
136: $this->renderLayout();
137: }
138:
139: 140: 141: 142:
143: public function dropAction ()
144: {
145: $this->loadLayout('newsletter_template_preview');
146: $this->renderLayout();
147: }
148:
149: 150: 151: 152:
153: public function saveAction ()
154: {
155: $request = $this->getRequest();
156: if (!$request->isPost()) {
157: $this->getResponse()->setRedirect($this->getUrl('*/newsletter_template'));
158: }
159: $template = Mage::getModel('newsletter/template');
160:
161: if ($id = (int)$request->getParam('id')) {
162: $template->load($id);
163: }
164:
165: try {
166: $template->addData($request->getParams())
167: ->setTemplateSubject($request->getParam('subject'))
168: ->setTemplateCode($request->getParam('code'))
169: ->setTemplateSenderEmail($request->getParam('sender_email'))
170: ->setTemplateSenderName($request->getParam('sender_name'))
171: ->setTemplateText($request->getParam('text'))
172: ->setTemplateStyles($request->getParam('styles'))
173: ->setModifiedAt(Mage::getSingleton('core/date')->gmtDate());
174:
175: if (!$template->getId()) {
176: $template->setTemplateType(Mage_Newsletter_Model_Template::TYPE_HTML);
177: }
178: if ($this->getRequest()->getParam('_change_type_flag')) {
179: $template->setTemplateType(Mage_Newsletter_Model_Template::TYPE_TEXT);
180: $template->setTemplateStyles('');
181: }
182: if ($this->getRequest()->getParam('_save_as_flag')) {
183: $template->setId(null);
184: }
185: $template->save();
186: $this->_redirect('*/*');
187: }
188: catch (Mage_Core_Exception $e) {
189: $this->_getSession()->addError(nl2br($e->getMessage()));
190: $this->_getSession()->setData('newsletter_template_form_data',
191: $this->getRequest()->getParams());
192: }
193: catch (Exception $e) {
194: $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while saving this template.'));
195: $this->_getSession()->setData('newsletter_template_form_data', $this->getRequest()->getParams());
196: }
197: $this->_forward('new');
198: }
199:
200: 201: 202: 203:
204: public function deleteAction ()
205: {
206: $template = Mage::getModel('newsletter/template')
207: ->load($this->getRequest()->getParam('id'));
208: if ($template->getId()) {
209: try {
210: $template->delete();
211: }
212: catch (Mage_Core_Exception $e) {
213: $this->_getSession()->addError($e->getMessage());
214: }
215: catch (Exception $e) {
216: $this->_getSession()->addException($e, Mage::helper('adminhtml')->__('An error occurred while deleting this template.'));
217: }
218: }
219: $this->_redirect('*/*');
220: }
221:
222: 223: 224: 225:
226: public function previewAction ()
227: {
228: $this->_setTitle();
229: $this->loadLayout();
230:
231: $data = $this->getRequest()->getParams();
232: if (empty($data) || !isset($data['id'])) {
233: $this->_forward('noRoute');
234: return $this;
235: }
236:
237:
238: $data['preview_store_id'] = Mage::app()->getDefaultStoreView()->getId();
239:
240: $this->getLayout()->getBlock('preview_form')->setFormData($data);
241: $this->renderLayout();
242: }
243: }
244: