/** * Override Save Action for Bronto Templates * * @return $this|void */ public function brontoSaveAction() { if (!Mage::helper('bronto_email')->isEnabledForAny()) { return parent::saveAction(); } $request = $this->getRequest(); $id = $this->getRequest()->getParam('id'); $template = $this->_initTemplate('id'); if (!$template->getId() && $id) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('This Email template no longer exists.')); $this->_redirect('*/*/'); return $this; } try { // Get Send Type $sendType = $request->getParam('template_send_type'); // Get Store ID $storeId = Mage::app()->isSingleStoreMode() ? Mage::app()->getDefaultStoreView()->getId() : $request->getParam('store_id'); // Core Template Details $templateText = $sendType == 'magento' ? $request->getParam('template_text') : $request->getParam('template_text_hidden'); $templateSubject = $sendType == 'magento' ? $request->getParam('template_subject') : $request->getParam('template_subject_hidden'); $templateStyles = $sendType == 'magento' ? $request->getParam('template_styles') : $request->getParam('template_styles_hidden'); // Add Template Settings $template->setTemplateCode($request->getParam('template_code'))->setModifiedAt(Mage::getSingleton('core/date')->gmtDate())->setTemplateText($templateText)->setTemplateSubject($templateSubject)->setTemplateStyles($templateStyles)->setOrigTemplateCode($request->getParam('orig_template_code'))->setOrigTemplateVariables($request->getParam('orig_template_variables')); // Handle Template Type Settings if (!$template->getId()) { $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_HTML); } if ($request->getParam('_change_type_flag')) { $template->setTemplateType(Mage_Core_Model_Email_Template::TYPE_TEXT); $template->setTemplateStyles(''); } // Bronto Template Details $brontoMessageId = $sendType == 'magento' ? $request->getParam('bronto_message_id_hidden') : $request->getParam('bronto_message_id'); $brontoMessageName = $this->_getMessageName($brontoMessageId); $origTemplateText = $sendType == 'magento' ? $request->getParam('template_text') : $request->getParam('orig_template_text', null); // If Original Template Text value is empty, pull from the hidden field that should have a value if ('' == $origTemplateText || is_null($origTemplateText)) { $origTemplateText = $request->getParam('template_text_hidden'); } // If Bronto Message ID == '_new_' the importTemplate function will create the message // in Bronto and add the details to the template entry if ('_new_' == $brontoMessageId && 'magento' != $sendType) { $template->save(); $importModel = Mage::getModel('bronto_email/template_import'); $importModel->importTemplate($template->getId(), $storeId); $brontoTemplate = Mage::getModel('bronto_email/message')->load($template->getId()); } else { // Get Bronto Template and Add Template Settings $brontoTemplate = Mage::getModel('bronto_email/message')->setStoreId($storeId)->setBrontoMessageId($brontoMessageId)->setBrontoMessageName($brontoMessageName)->setOrigTemplateText($origTemplateText); } $brontoTemplate->setTemplateSendType($sendType)->setSalesRule($request->getParam('sales_rule', null)); // If Template doesn't exist or added_at is not set, set it if (!$template->getId() || !$template->getAddedAt()) { $template->setAddedAt(Mage::getSingleton('core/date')->gmtDate()); } // Save Template $template->save(); // Set Bronto Template ID to match newly saved Template and then save if ($brontoTemplate) { $brontoTemplate->setId($template->getId()); $brontoTemplate->save(); } Mage::getSingleton('adminhtml/session')->setFormData(false); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The email template has been saved.')); $this->_redirect('*/*'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->setData('email_template_form_data', $this->getRequest()->getParams()); Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_forward('brontoNew'); } return $this; }