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
00035 class Mage_Newsletter_Model_Mysql4_Queue_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00036 {
00037 protected $_addSubscribersFlag = false;
00038
00039
00040
00041 protected $_isStoreFilter = false;
00042
00043
00044
00045
00046 protected function _construct()
00047 {
00048 $this->_init('newsletter/queue');
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 public function addTemplateInfo() {
00058 $this->getSelect()->joinLeft(array('template'=>$this->getTable('template')),
00059 'template.template_id=main_table.template_id',
00060 array('template_subject','template_sender_name','template_sender_email'));
00061 $this->_joinedTables['template'] = true;
00062 return $this;
00063 }
00064
00065 protected function _addSubscriberInfoToSelect()
00066 {
00067 $this->_addSubscribersFlag = true;
00068 $this->getSize();
00069 $this->getSelect()
00070 ->joinLeft(array('link_total'=>$this->getTable('queue_link')),
00071 'main_table.queue_id=link_total.queue_id',
00072 array(
00073 new Zend_Db_Expr('COUNT(DISTINCT link_total.queue_link_id) AS subscribers_total')
00074 ))
00075 ->joinLeft(array('link_sent'=>$this->getTable('queue_link')),
00076 'main_table.queue_id=link_sent.queue_id and link_sent.letter_sent_at IS NOT NULL',
00077 array(
00078 new Zend_Db_Expr('COUNT(DISTINCT link_sent.queue_link_id) AS subscribers_sent')
00079 ))
00080 ->group('main_table.queue_id');
00081 return $this;
00082 }
00083
00084 public function load($printQuery=false, $logQuery=false) {
00085 if($this->_addSubscribersFlag && !$this->isLoaded()) {
00086 $this->_addSubscriberInfoToSelect();
00087 }
00088
00089 return parent::load($printQuery, $logQuery);
00090 }
00091
00092
00093
00094
00095
00096
00097 public function addSubscribersInfo()
00098 {
00099 $this->_addSubscribersFlag = true;
00100 return $this;
00101 }
00102
00103 public function addFieldToFilter($field, $condition=null)
00104 {
00105 if(in_array($field, array('subscribers_total', 'subscribers_sent'))) {
00106 $this->addFieldToFilter('main_table.queue_id', array('in'=>$this->_getIdsFromLink($field, $condition)));
00107 return $this;
00108 } else {
00109 return parent::addFieldToFilter($field, $condition);
00110 }
00111 }
00112
00113 protected function _getIdsFromLink($field, $condition) {
00114 $select = $this->getConnection()->select()
00115 ->from($this->getTable('queue_link'), array('queue_id', 'COUNT(queue_link_id) as total'))
00116 ->group('queue_id')
00117 ->having($this->_getConditionSql('total', $condition));
00118
00119 if($field == 'subscribers_sent') {
00120 $select->where('letter_sent_at IS NOT NULL');
00121 }
00122
00123 $idList = $this->getConnection()->fetchCol($select);
00124
00125 if(count($idList)) {
00126 return $idList;
00127 }
00128
00129 return array(0);
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 public function addSubscriberFilter($subscriberId)
00139 {
00140 $this->getSelect()
00141 ->join(array('link'=>$this->getTable('queue_link')),
00142 'main_table.queue_id=link.queue_id',
00143 array('letter_sent_at')
00144 )
00145 ->where('link.subscriber_id = ?', $subscriberId);
00146
00147 return $this;
00148 }
00149
00150
00151
00152
00153
00154
00155 public function addOnlyForSendingFilter()
00156 {
00157 $this->getSelect()
00158 ->where('main_table.queue_status in (?)', array(Mage_Newsletter_Model_Queue::STATUS_SENDING,
00159 Mage_Newsletter_Model_Queue::STATUS_NEVER))
00160 ->where('main_table.queue_start_at < ?', Mage::getSingleton('core/date')->gmtdate())
00161 ->where('main_table.queue_start_at IS NOT NULL');
00162
00163 return $this;
00164 }
00165
00166
00167
00168
00169
00170
00171 public function addOnlyUnsentFilter()
00172 {
00173 $this->getSelect()
00174 ->where('main_table.queue_status = ?', Mage_Newsletter_Model_Queue::STATUS_NEVER);
00175
00176 return $this;
00177 }
00178
00179 public function toOptionArray()
00180 {
00181 return $this->_toOptionArray('queue_id', 'template_subject');
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 public function addStoreFilter($storeIds)
00191 {
00192 if (!$this->_isStoreFilter) {
00193 $this->getSelect()->joinInner(array('store_link' => $this->getTable('queue_store_link')),
00194 'main_table.queue_id = store_link.queue_id', array()
00195 )->where('store_link.store_id IN (?)', $storeIds);
00196 $this->_isStoreFilter = true;
00197 }
00198 return $this;
00199 }
00200 }