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: 34:
35: class Mage_Newsletter_Model_Resource_Template extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('newsletter/template', 'template_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function loadByCode(Mage_Newsletter_Model_Template $object, $templateCode)
54: {
55: $read = $this->_getReadAdapter();
56: if ($read && !is_null($templateCode)) {
57: $select = $this->_getLoadSelect('template_code', $templateCode, $object)
58: ->where('template_actual = :template_actual');
59: $data = $read->fetchRow($select, array('template_actual'=>1));
60:
61: if ($data) {
62: $object->setData($data);
63: }
64: }
65:
66: $this->_afterLoad($object);
67:
68: return $this;
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function checkUsageInQueue(Mage_Newsletter_Model_Template $template)
78: {
79: if ($template->getTemplateActual() !== 0 && !$template->getIsSystem()) {
80: $select = $this->_getReadAdapter()->select()
81: ->from($this->getTable('newsletter/queue'), new Zend_Db_Expr('COUNT(queue_id)'))
82: ->where('template_id = :template_id');
83:
84: $countOfQueue = $this->_getReadAdapter()->fetchOne($select, array('template_id'=>$template->getId()));
85:
86: return $countOfQueue > 0;
87: } elseif ($template->getIsSystem()) {
88: return false;
89: } else {
90: return true;
91: }
92: }
93:
94: 95: 96: 97: 98: 99:
100: public function checkCodeUsage(Mage_Newsletter_Model_Template $template)
101: {
102: if ($template->getTemplateActual() != 0 || is_null($template->getTemplateActual())) {
103: $bind = array(
104: 'template_id' => $template->getId(),
105: 'template_code' => $template->getTemplateCode(),
106: 'template_actual' => 1
107: );
108: $select = $this->_getReadAdapter()->select()
109: ->from($this->getMainTable(), new Zend_Db_Expr('COUNT(template_id)'))
110: ->where('template_id != :template_id')
111: ->where('template_code = :template_code')
112: ->where('template_actual = :template_actual');
113:
114: $countOfCodes = $this->_getReadAdapter()->fetchOne($select, $bind);
115:
116: return $countOfCodes > 0;
117: } else {
118: return false;
119: }
120: }
121:
122: 123: 124: 125: 126: 127:
128: protected function _beforeSave(Mage_Core_Model_Abstract $object)
129: {
130: if ($this->checkCodeUsage($object)) {
131: Mage::throwException(Mage::helper('newsletter')->__('Duplicate template code.'));
132: }
133:
134: if (!$object->hasTemplateActual()) {
135: $object->setTemplateActual(1);
136: }
137: if (!$object->hasAddedAt()) {
138: $object->setAddedAt(Mage::getSingleton('core/date')->gmtDate());
139: }
140: $object->setModifiedAt(Mage::getSingleton('core/date')->gmtDate());
141:
142: return parent::_beforeSave($object);
143: }
144: }
145: