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_Queue extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037 protected function _construct()
00038 {
00039 $this->_init('newsletter/queue', 'queue_id');
00040 }
00041
00042
00043
00044
00045
00046
00047
00048 public function addSubscribersToQueue(Mage_Newsletter_Model_Queue $queue, array $subscriberIds)
00049 {
00050 if (count($subscriberIds)==0) {
00051 Mage::throwException(Mage::helper('newsletter')->__('No subscribers selected'));
00052 }
00053
00054 if (!$queue->getId() && $queue->getQueueStatus()!=Mage_Newsletter_Model_Queue::STATUS_NEVER) {
00055 Mage::throwException(Mage::helper('newsletter')->__('Invalid queue selected'));
00056 }
00057
00058 $select = $this->_getWriteAdapter()->select();
00059 $select->from($this->getTable('queue_link'),'subscriber_id')
00060 ->where('queue_id = ?', $queue->getId())
00061 ->where('subscriber_id in (?)', $subscriberIds);
00062
00063 $usedIds = $this->_getWriteAdapter()->fetchCol($select);
00064 $this->_getWriteAdapter()->beginTransaction();
00065 try {
00066 foreach($subscriberIds as $subscriberId) {
00067 if(in_array($subscriberId, $usedIds)) {
00068 continue;
00069 }
00070 $data = array();
00071 $data['queue_id'] = $queue->getId();
00072 $data['subscriber_id'] = $subscriberId;
00073 $this->_getWriteAdapter()->insert($this->getTable('queue_link'), $data);
00074 }
00075 $this->_getWriteAdapter()->commit();
00076 }
00077 catch (Exception $e) {
00078 $this->_getWriteAdapter()->rollBack();
00079 }
00080
00081 }
00082
00083 public function removeSubscribersFromQueue(Mage_Newsletter_Model_Queue $queue)
00084 {
00085 try {
00086 $this->_getWriteAdapter()->delete(
00087 $this->getTable('queue_link'),
00088 array(
00089 $this->_getWriteAdapter()->quoteInto('queue_id = ?', $queue->getId()),
00090 'letter_sent_at IS NULL'
00091 )
00092 );
00093
00094 $this->_getWriteAdapter()->commit();
00095 }
00096 catch (Exception $e) {
00097 $this->_getWriteAdapter()->rollBack();
00098 }
00099
00100 }
00101
00102 public function setStores(Mage_Newsletter_Model_Queue $queue)
00103 {
00104 $this->_getWriteAdapter()
00105 ->delete(
00106 $this->getTable('queue_store_link'),
00107 $this->_getWriteAdapter()->quoteInto('queue_id = ?', $queue->getId())
00108 );
00109
00110 if (!is_array($queue->getStores())) {
00111 $stores = array();
00112 } else {
00113 $stores = $queue->getStores();
00114 }
00115
00116 foreach ($stores as $storeId) {
00117 $data = array();
00118 $data['store_id'] = $storeId;
00119 $data['queue_id'] = $queue->getId();
00120 $this->_getWriteAdapter()->insert($this->getTable('queue_store_link'), $data);
00121 }
00122
00123 $this->removeSubscribersFromQueue($queue);
00124
00125 if(count($stores)==0) {
00126 return $this;
00127 }
00128 $subscribers = Mage::getResourceSingleton('newsletter/subscriber_collection')
00129 ->addFieldToFilter('store_id', array('in'=>$stores))
00130 ->useOnlySubscribed()
00131 ->load();
00132
00133 $subscriberIds = array();
00134
00135 foreach ($subscribers as $subscriber) {
00136 $subscriberIds[] = $subscriber->getId();
00137 }
00138
00139 if (count($subscriberIds) > 0) {
00140 $this->addSubscribersToQueue($queue, $subscriberIds);
00141 }
00142
00143 return $this;
00144 }
00145
00146 public function getStores(Mage_Newsletter_Model_Queue $queue)
00147 {
00148 $select = $this->_getReadAdapter()->select()
00149 ->from($this->getTable('queue_store_link'), 'store_id')
00150 ->where('queue_id = ?', $queue->getId());
00151
00152 if(!($result = $this->_getReadAdapter()->fetchCol($select))) {
00153 $result = array();
00154 }
00155
00156 return $result;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 protected function _afterSave(Mage_Core_Model_Abstract $queue)
00166 {
00167 if($queue->getSaveTemplateFlag()) {
00168 $queue->getTemplate()->save();
00169 }
00170
00171 if($queue->getSaveStoresFlag()) {
00172 $this->setStores($queue);
00173 }
00174
00175 return $this;
00176 }
00177
00178 }