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_Queue_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_addSubscribersFlag = false;
43:
44: 45: 46: 47: 48:
49: protected $_isStoreFilter = false;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_map['fields']['queue_id'] = 'main_table.queue_id';
58: $this->_init('newsletter/queue');
59: }
60:
61: 62: 63: 64: 65: 66: 67:
68: public function addTemplateInfo()
69: {
70: $this->getSelect()->joinLeft(array('template'=>$this->getTable('template')),
71: 'template.template_id=main_table.template_id',
72: array('template_subject','template_sender_name','template_sender_email')
73: );
74: $this->_joinedTables['template'] = true;
75: return $this;
76: }
77:
78: 79: 80: 81: 82:
83: protected function _addSubscriberInfoToSelect()
84: {
85:
86: $select = $this->getConnection()->select()
87: ->from(array('qlt' => $this->getTable('newsletter/queue_link')), 'COUNT(qlt.queue_link_id)')
88: ->where('qlt.queue_id = main_table.queue_id');
89: $totalExpr = new Zend_Db_Expr(sprintf('(%s)', $select->assemble()));
90: $select = $this->getConnection()->select()
91: ->from(array('qls' => $this->getTable('newsletter/queue_link')), 'COUNT(qls.queue_link_id)')
92: ->where('qls.queue_id = main_table.queue_id')
93: ->where('qls.letter_sent_at IS NOT NULL');
94: $sentExpr = new Zend_Db_Expr(sprintf('(%s)', $select->assemble()));
95:
96: $this->getSelect()->columns(array(
97: 'subscribers_sent' => $sentExpr,
98: 'subscribers_total' => $totalExpr
99: ));
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: public function load($printQuery = false, $logQuery = false)
111: {
112: if ($this->_addSubscribersFlag && !$this->isLoaded()) {
113: $this->_addSubscriberInfoToSelect();
114: }
115: return parent::load($printQuery, $logQuery);
116: }
117:
118: 119: 120: 121: 122:
123: public function addSubscribersInfo()
124: {
125: $this->_addSubscribersFlag = true;
126: return $this;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136:
137: public function addFieldToFilter($field, $condition = null)
138: {
139: if (in_array($field, array('subscribers_total', 'subscribers_sent'))) {
140: $this->addFieldToFilter('main_table.queue_id', array('in'=>$this->_getIdsFromLink($field, $condition)));
141: return $this;
142: } else {
143: return parent::addFieldToFilter($field, $condition);
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: protected function _getIdsFromLink($field, $condition)
155: {
156: $select = $this->getConnection()->select()
157: ->from(
158: $this->getTable('newsletter/queue_link'),
159: array('queue_id', 'total' => new Zend_Db_Expr('COUNT(queue_link_id)'))
160: )
161: ->group('queue_id')
162: ->having($this->_getConditionSql('total', $condition));
163:
164: if ($field == 'subscribers_sent') {
165: $select->where('letter_sent_at IS NOT NULL');
166: }
167:
168: $idList = $this->getConnection()->fetchCol($select);
169:
170: if (count($idList)) {
171: return $idList;
172: }
173:
174: return array(0);
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function addSubscriberFilter($subscriberId)
184: {
185: $this->getSelect()->join(array('link'=>$this->getTable('newsletter/queue_link')),
186: 'main_table.queue_id=link.queue_id',
187: array('letter_sent_at')
188: )
189: ->where('link.subscriber_id = ?', $subscriberId);
190:
191: return $this;
192: }
193:
194: 195: 196: 197: 198:
199: public function addOnlyForSendingFilter()
200: {
201: $this->getSelect()
202: ->where('main_table.queue_status in (?)', array(Mage_Newsletter_Model_Queue::STATUS_SENDING,
203: Mage_Newsletter_Model_Queue::STATUS_NEVER))
204: ->where('main_table.queue_start_at < ?', Mage::getSingleton('core/date')->gmtdate())
205: ->where('main_table.queue_start_at IS NOT NULL');
206:
207: return $this;
208: }
209:
210: 211: 212: 213: 214:
215: public function addOnlyUnsentFilter()
216: {
217: $this->addFieldToFilter('main_table.queue_status', Mage_Newsletter_Model_Queue::STATUS_NEVER);
218:
219: return $this;
220: }
221:
222: 223: 224: 225: 226:
227: public function toOptionArray()
228: {
229: return $this->_toOptionArray('queue_id', 'template_subject');
230: }
231:
232: 233: 234: 235: 236: 237:
238: public function addStoreFilter($storeIds)
239: {
240: if (!$this->_isStoreFilter) {
241: $this->getSelect()->joinInner(array('store_link' => $this->getTable('newsletter/queue_store_link')),
242: 'main_table.queue_id = store_link.queue_id', array()
243: )
244: ->where('store_link.store_id IN (?)', $storeIds)
245: ->group('main_table.queue_id');
246: $this->_isStoreFilter = true;
247: }
248: return $this;
249: }
250: }
251: