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_Core_Model_Resource_Email_Template extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/email_template', 'template_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function loadByCode($templateCode)
53: {
54: $select = $this->_getReadAdapter()->select()
55: ->from($this->getMainTable())
56: ->where('template_code = :template_code');
57: $result = $this->_getReadAdapter()->fetchRow($select, array('template_code' => $templateCode));
58:
59: if (!$result) {
60: return array();
61: }
62: return $result;
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function checkCodeUsage(Mage_Core_Model_Email_Template $template)
72: {
73: if ($template->getTemplateActual() != 0 || is_null($template->getTemplateActual())) {
74: $select = $this->_getReadAdapter()->select()
75: ->from($this->getMainTable(), 'COUNT(*)')
76: ->where('template_code = :template_code');
77: $bind = array(
78: 'template_code' => $template->getTemplateCode()
79: );
80:
81: $templateId = $template->getId();
82: if ($templateId) {
83: $select->where('template_id != :template_id');
84: $bind['template_id'] = $templateId;
85: }
86:
87: $result = $this->_getReadAdapter()->fetchOne($select, $bind);
88: if ($result) {
89: return true;
90: }
91: }
92: return false;
93: }
94:
95: 96: 97: 98: 99: 100:
101: protected function _beforeSave(Mage_Core_Model_Abstract $object)
102: {
103: if ($object->isObjectNew()) {
104: $object->setCreatedAt($this->formatDate(true));
105: }
106: $object->setModifiedAt($this->formatDate(true));
107: $object->setTemplateType((int)$object->getTemplateType());
108:
109: return parent::_beforeSave($object);
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function getSystemConfigByPathsAndTemplateId($paths, $templateId)
120: {
121: $orWhere = array();
122: $pathesCounter = 1;
123: $bind = array();
124: foreach ($paths as $path) {
125: $pathAlias = 'path_' . $pathesCounter;
126: $orWhere[] = 'path = :' . $pathAlias;
127: $bind[$pathAlias] = $path;
128: $pathesCounter++;
129: }
130: $bind['template_id'] = $templateId;
131: $select = $this->_getReadAdapter()->select()
132: ->from($this->getTable('core/config_data'), array('scope', 'scope_id', 'path'))
133: ->where('value LIKE :template_id')
134: ->where(join(' OR ', $orWhere));
135:
136: return $this->_getReadAdapter()->fetchAll($select, $bind);
137: }
138: }
139: