00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Core_Model_Mysql4_Email_Template
00035 {
00036
00037
00038
00039
00040
00041 protected $_templateTable;
00042
00043
00044
00045
00046 protected $_write;
00047
00048
00049
00050
00051 protected $_read;
00052
00053
00054
00055
00056
00057
00058 public function __construct()
00059 {
00060 $this->_templateTable = Mage::getSingleton('core/resource')->getTableName('core/email_template');
00061 $this->_read = Mage::getSingleton('core/resource')->getConnection('core_read');
00062 $this->_write = Mage::getSingleton('core/resource')->getConnection('core_write');
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public function load($templateId)
00072 {
00073 $select = $this->_read->select()
00074 ->from($this->_templateTable)
00075 ->where('template_id=?', $templateId);
00076
00077 $result = $this->_read->fetchRow($select);
00078
00079 if (!$result) {
00080 return array();
00081 }
00082
00083 return $result;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 public function loadByCode($templateCode)
00096 {
00097 $select = $this->_read->select()
00098 ->from($this->_templateTable)
00099 ->where('template_code=?', $templateCode);
00100
00101
00102 $result = $this->_read->fetchRow($select);
00103
00104 if (!$result) {
00105 return array();
00106 }
00107
00108 return $result;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 public function checkCodeUsage(Mage_Core_Model_Email_Template $template)
00119 {
00120 if($template->getTemplateActual()!=0 || is_null($template->getTemplateActual())) {
00121
00122 $select = $this->_read->select()
00123 ->from($this->_templateTable, new Zend_Db_Expr('COUNT(template_id)'))
00124 ->where('template_id!=?',$template->getId())
00125 ->where('template_code=?',$template->getTemplateCode());
00126
00127 $countOfCodes = $this->_read->fetchOne($select);
00128
00129 return $countOfCodes > 0;
00130 } else {
00131 return false;
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function save(Mage_Core_Model_Email_Template $template)
00142 {
00143 $this->_write->beginTransaction();
00144 try {
00145 $data = $this->_prepareSave($template);
00146 if($template->getId()) {
00147 $this->_write->update($this->_templateTable, $data,
00148 $this->_write->quoteInto('template_id=?',$template->getId()));
00149 } else {
00150 $this->_write->insert($this->_templateTable, $data);
00151 $template->setId($this->_write->lastInsertId($this->_templateTable));
00152 }
00153
00154 $this->_write->commit();
00155 }
00156 catch (Exception $e) {
00157 $this->_write->rollBack();
00158 throw $e;
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 protected function _prepareSave(Mage_Core_Model_Email_Template $template)
00169 {
00170 $data = array();
00171 $data['template_code'] = $template->getTemplateCode();
00172 $data['template_text'] = $template->getTemplateText();
00173 $data['template_type'] = (int) $template->getTemplateType();
00174 $data['template_subject'] = $template->getTemplateSubject();
00175 $data['template_sender_name'] = $template->getTemplateSenderName();
00176 $data['template_sender_email'] = $template->getTemplateSenderEmail();
00177
00178 if(!$template->getAddedAt()) {
00179 $template->setAddedAt(Mage::getSingleton('core/date')->gmtDate());
00180 $template->setModifiedAt(Mage::getSingleton('core/date')->gmtDate());
00181 }
00182
00183 $data['modified_at'] = $template->getModifiedAt();
00184 $data['added_at'] = $template->getAddedAt();
00185
00186 if($this->checkCodeUsage($template)) {
00187 Mage::throwException(Mage::helper('core')->__('Duplicate Of Template Code'));
00188 }
00189
00190 $validators = array(
00191 'template_code' => array(Zend_Filter_Input::ALLOW_EMPTY => false),
00192 'template_type' => 'Alnum',
00193 #'template_sender_email' => 'EmailAddress',
00194 #'template_sender_name' => array(Zend_Filter_Input::ALLOW_EMPTY => false)
00195 );
00196
00197 $validateInput = new Zend_Filter_Input(array(), $validators, $data);
00198 if(!$validateInput->isValid()) {
00199 $errorString = '';
00200
00201 foreach($validateInput->getMessages() as $message) {
00202 if(is_array($message)) {
00203 foreach($message as $str) {
00204 $errorString.= $str . "\n";
00205 }
00206 } else {
00207 $errorString.= $message . "\n";
00208 }
00209
00210 }
00211
00212 Mage::throwException($errorString);
00213 }
00214
00215 return $data;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 public function delete($templateId)
00225 {
00226 $this->_write->beginTransaction();
00227 try {
00228 $this->_write->delete($this->_templateTable, $this->_write->quoteInto('template_id=?', $templateId));
00229 $this->_write->commit();
00230 }
00231 catch(Exception $e) {
00232 $this->_write->rollBack();
00233 Mage::throwException(Mage::helper('core')->__('Cannot Delete Email Template'));
00234 }
00235
00236 return $this;
00237 }
00238 }