/** * Check usage of template code in other templates * * @param Mage_Newsletter_Model_Template $template * @return boolean */ public function checkCodeUsage(Mage_Newsletter_Model_Template $template) { if ($template->getTemplateActual() != 0 || is_null($template->getTemplateActual())) { $bind = array('template_id' => $template->getId(), 'template_code' => $template->getTemplateCode(), 'template_actual' => 1); $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), new Zend_Db_Expr('COUNT(template_id)'))->where('template_id != :template_id')->where('template_code = :template_code')->where('template_actual = :template_actual'); $countOfCodes = $this->_getReadAdapter()->fetchOne($select, $bind); return $countOfCodes > 0; } else { return false; } }
/** * Prepares template for saving, validates input data * * @param Mage_Newsletter_Model_Template $template * @return array */ protected function _prepareSave(Mage_Newsletter_Model_Template $template) { $data = array(); $data['template_code'] = $template->getTemplateCode(); $data['template_text'] = $template->getTemplateText(); $data['template_text_preprocessed'] = $template->getTemplateTextPreprocessed(); $data['template_type'] = (int) $template->getTemplateType(); $data['template_subject'] = $template->getTemplateSubject(); $data['template_sender_name'] = $template->getTemplateSenderName(); $data['template_sender_email'] = $template->getTemplateSenderEmail(); $data['template_actual'] = !is_null($template->getTemplateActual()) && $template->getTemplateActual() == 0 ? 0 : 1; if (!$template->getAddedAt()) { $template->setAddedAt(Mage::getSingleton('core/date')->gmtDate()); $template->setModifiedAt(Mage::getSingleton('core/date')->gmtDate()); } $data['modified_at'] = $template->getModifiedAt(); $data['added_at'] = $template->getAddedAt(); if ($this->checkCodeUsage($template)) { Mage::throwException(Mage::helper('newsletter')->__('Duplicate of template code')); } $validators = array('template_code' => array(Zend_Filter_Input::ALLOW_EMPTY => false), 'template_type' => 'Alnum', 'template_sender_email' => 'EmailAddress', 'template_sender_name' => array(Zend_Filter_Input::ALLOW_EMPTY => false)); $validateInput = new Zend_Filter_Input(array(), $validators, $data); if (!$validateInput->isValid()) { $errorString = ''; foreach ($validateInput->getMessages() as $message) { if (is_array($message)) { foreach ($message as $str) { $errorString .= $str . "\n"; } } else { $errorString .= $message . "\n"; } } Mage::throwException($errorString); } return $data; }