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 extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('newsletter/queue', 'queue_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function addSubscribersToQueue(Mage_Newsletter_Model_Queue $queue, array $subscriberIds)
53: {
54: if (count($subscriberIds)==0) {
55: Mage::throwException(Mage::helper('newsletter')->__('No subscribers selected.'));
56: }
57:
58: if (!$queue->getId() && $queue->getQueueStatus()!=Mage_Newsletter_Model_Queue::STATUS_NEVER) {
59: Mage::throwException(Mage::helper('newsletter')->__('Invalid queue selected.'));
60: }
61:
62: $adapter = $this->_getWriteAdapter();
63:
64: $select = $adapter->select();
65: $select->from($this->getTable('newsletter/queue_link'), 'subscriber_id')
66: ->where('queue_id = ?', $queue->getId())
67: ->where('subscriber_id in (?)', $subscriberIds);
68:
69: $usedIds = $adapter->fetchCol($select);
70: $adapter->beginTransaction();
71: try {
72: foreach ($subscriberIds as $subscriberId) {
73: if (in_array($subscriberId, $usedIds)) {
74: continue;
75: }
76: $data = array();
77: $data['queue_id'] = $queue->getId();
78: $data['subscriber_id'] = $subscriberId;
79: $adapter->insert($this->getTable('newsletter/queue_link'), $data);
80: }
81: $adapter->commit();
82: }
83: catch (Exception $e) {
84: $adapter->rollBack();
85: }
86: }
87:
88: 89: 90: 91: 92:
93: public function removeSubscribersFromQueue(Mage_Newsletter_Model_Queue $queue)
94: {
95: $adapter = $this->_getWriteAdapter();
96: try {
97: $adapter->delete(
98: $this->getTable('newsletter/queue_link'),
99: array(
100: 'queue_id = ?' => $queue->getId(),
101: 'letter_sent_at IS NULL'
102: )
103: );
104:
105: $adapter->commit();
106: }
107: catch (Exception $e) {
108: $adapter->rollBack();
109: }
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function setStores(Mage_Newsletter_Model_Queue $queue)
119: {
120: $adapter = $this->_getWriteAdapter();
121: $adapter->delete(
122: $this->getTable('newsletter/queue_store_link'),
123: array('queue_id = ?' => $queue->getId())
124: );
125:
126: $stores = $queue->getStores();
127: if (!is_array($stores)) {
128: $stores = array();
129: }
130:
131: foreach ($stores as $storeId) {
132: $data = array();
133: $data['store_id'] = $storeId;
134: $data['queue_id'] = $queue->getId();
135: $adapter->insert($this->getTable('newsletter/queue_store_link'), $data);
136: }
137: $this->removeSubscribersFromQueue($queue);
138:
139: if (count($stores) == 0) {
140: return $this;
141: }
142:
143: $subscribers = Mage::getResourceSingleton('newsletter/subscriber_collection')
144: ->addFieldToFilter('store_id', array('in'=>$stores))
145: ->useOnlySubscribed()
146: ->load();
147:
148: $subscriberIds = array();
149:
150: foreach ($subscribers as $subscriber) {
151: $subscriberIds[] = $subscriber->getId();
152: }
153:
154: if (count($subscriberIds) > 0) {
155: $this->addSubscribersToQueue($queue, $subscriberIds);
156: }
157:
158: return $this;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function getStores(Mage_Newsletter_Model_Queue $queue)
168: {
169: $adapter = $this->_getReadAdapter();
170: $select = $adapter->select()->from($this->getTable('newsletter/queue_store_link'), 'store_id')
171: ->where('queue_id = :queue_id');
172:
173: if (!($result = $adapter->fetchCol($select, array('queue_id'=>$queue->getId())))) {
174: $result = array();
175: }
176:
177: return $result;
178: }
179:
180: 181: 182: 183: 184: 185:
186: protected function _afterSave(Mage_Core_Model_Abstract $queue)
187: {
188: if ($queue->getSaveStoresFlag()) {
189: $this->setStores($queue);
190: }
191: return $this;
192: }
193: }
194: