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_Newsletter_Model_Mysql4_Template extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039
00040 protected function _construct()
00041 {
00042 $this->_init('newsletter/template', 'template_id');
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052 public function loadByCode(Mage_Newsletter_Model_Template $object, $templateCode)
00053 {
00054 $read = $this->_getReadAdapter();
00055 if ($read && !is_null($templateCode)) {
00056 $select = $this->_getLoadSelect('template_code', $templateCode, $object)
00057 ->where('template_actual=?', 1);
00058 $data = $read->fetchRow($select);
00059
00060 if ($data) {
00061 $object->setData($data);
00062 }
00063 }
00064
00065 $this->_afterLoad($object);
00066
00067 return $this;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public function checkUsageInQueue(Mage_Newsletter_Model_Template $template)
00077 {
00078 if ($template->getTemplateActual() !== 0 && !$template->getIsSystem()) {
00079 $select = $this->_getReadAdapter()->select()
00080 ->from($this->getTable('newsletter/queue'), new Zend_Db_Expr('COUNT(queue_id)'))
00081 ->where('template_id=?',$template->getId());
00082
00083 $countOfQueue = $this->_getReadAdapter()->fetchOne($select);
00084
00085 return $countOfQueue > 0;
00086 }
00087 elseif ($template->getIsSystem()) {
00088 return false;
00089 }
00090 else {
00091 return true;
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 public function checkCodeUsage(Mage_Newsletter_Model_Template $template)
00102 {
00103 if ($template->getTemplateActual() != 0 || is_null($template->getTemplateActual())) {
00104 $select = $this->_getReadAdapter()->select()
00105 ->from($this->getMainTable(), new Zend_Db_Expr('COUNT(template_id)'))
00106 ->where('template_id!=?',$template->getId())
00107 ->where('template_code=?',$template->getTemplateCode())
00108 ->where('template_actual=?',1);
00109
00110 $countOfCodes = $this->_getReadAdapter()->fetchOne($select);
00111
00112 return $countOfCodes > 0;
00113 } else {
00114 return false;
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00125 {
00126 if ($this->checkCodeUsage($object)) {
00127 Mage::throwException(Mage::helper('newsletter')->__('Duplicate of template code'));
00128 }
00129
00130 if (!$object->hasTemplateActual()) {
00131 $object->setTemplateActual(1);
00132 }
00133 if (!$object->hasAddedAt()) {
00134 $object->setAddedAt(Mage::getSingleton('core/date')->gmtDate());
00135 }
00136 $object->setModifiedAt(Mage::getSingleton('core/date')->gmtDate());
00137
00138 return parent::_beforeSave($object);
00139 }
00140 }