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
00036 class Mage_Newsletter_Model_Mysql4_Subscriber_Collection extends Varien_Data_Collection_Db
00037 {
00038
00039
00040
00041
00042
00043 protected $_subscriberTable;
00044
00045
00046
00047
00048
00049
00050 protected $_queueLinkTable;
00051
00052
00053
00054
00055
00056
00057 protected $_storeTable;
00058
00059
00060
00061
00062
00063
00064 protected $_queueJoinedFlag = false;
00065
00066
00067
00068
00069
00070
00071 protected $_showCustomersInfo = false;
00072
00073
00074
00075
00076
00077
00078 protected $_countFilterPart = array();
00079
00080
00081
00082
00083
00084
00085 public function __construct()
00086 {
00087 parent::__construct(Mage::getSingleton('core/resource')->getConnection('newsletter_read'));
00088 $this->_subscriberTable = Mage::getSingleton('core/resource')->getTableName('newsletter/subscriber');
00089 $this->_queueLinkTable = Mage::getSingleton('core/resource')->getTableName('newsletter/queue_link');
00090 $this->_storeTable = Mage::getSingleton('core/resource')->getTableName('core/store');
00091 $this->_select->from(array('main_table'=>$this->_subscriberTable));
00092 $this->setItemObjectClass(Mage::getConfig()->getModelClassName('newsletter/subscriber'));
00093 }
00094
00095
00096
00097
00098
00099
00100 public function useQueue(Mage_Newsletter_Model_Queue $queue)
00101 {
00102 $this->_select->join(array('link'=>$this->_queueLinkTable), "link.subscriber_id = main_table.subscriber_id", array())
00103 ->where("link.queue_id = ? ", $queue->getId());
00104 $this->_queueJoinedFlag = true;
00105 return $this;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 public function getAllIds()
00115 {
00116 $idsSelect = clone $this->getSelect();
00117 $idsSelect->reset(Zend_Db_Select::ORDER);
00118 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00119 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00120 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00121 $idsSelect->from(null,
00122 'main_table.subscriber_id'
00123 );
00124 return $this->getConnection()->fetchCol($idsSelect);
00125 }
00126
00127
00128
00129
00130 public function useOnlyUnsent( )
00131 {
00132 if($this->_queueJoinedFlag) {
00133 $this->_select->where("link.letter_sent_at IS NULL");
00134 }
00135
00136 return $this;
00137 }
00138
00139
00140
00141
00142
00143
00144 public function showCustomerInfo()
00145 {
00146 $customer = Mage::getModel('customer/customer');
00147
00148 $firstname = $customer->getAttribute('firstname');
00149 $lastname = $customer->getAttribute('lastname');
00150
00151
00152
00153
00154
00155
00156 $this->getSelect()
00157 ->joinLeft(
00158 array('customer_lastname_table'=>$lastname->getBackend()->getTable()),
00159 'customer_lastname_table.entity_id=main_table.customer_id
00160 AND customer_lastname_table.attribute_id = '.(int) $lastname->getAttributeId() . '
00161 ',
00162 array('customer_lastname'=>'value')
00163 )
00164 ->joinLeft(
00165 array('customer_firstname_table'=>$firstname->getBackend()->getTable()),
00166 'customer_firstname_table.entity_id=main_table.customer_id
00167 AND customer_firstname_table.attribute_id = '.(int) $firstname->getAttributeId() . '
00168 ',
00169 array('customer_firstname'=>'value')
00170 );
00171
00172 return $this;
00173 }
00174
00175 public function addSubscriberTypeField()
00176 {
00177 $this->getSelect()
00178 ->from(null, array('type'=>new Zend_Db_Expr('IF(main_table.customer_id = 0, 1, 2)')));
00179 return $this;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188 public function showStoreInfo()
00189 {
00190 $this->getSelect()->join(
00191 array('store' => $this->_storeTable),
00192 'store.store_id = main_table.store_id',
00193 array('group_id', 'website_id')
00194 );
00195
00196 return $this;
00197 }
00198
00199 public function addFieldToFilter($field, $condition=null)
00200 {
00201 if(!is_null($condition)) {
00202 $this->_select->where($this->_getConditionSql($this->_getFieldTableAlias($field), $condition));
00203 $this->_countFilterPart[] = $this->_getConditionSql($this->_getFieldTableAlias($field), $condition);
00204 }
00205 return $this;
00206 }
00207
00208 public function _getFieldTableAlias($field)
00209 {
00210 if (strpos($field,'customer') === 0) {
00211 return $field .'_table.value';
00212 }
00213
00214 if($field=='type') {
00215 return new Zend_Db_Expr('IF(main_table.customer_id = 0, 1, 2)');
00216 }
00217
00218 if(in_array($field, array('website_id','group_id'))) {
00219 return 'store.' . $field;
00220 }
00221
00222 return 'main_table.' . $field;
00223 }
00224
00225 public function getSelectCountSql()
00226 {
00227 $this->_renderFilters();
00228
00229 $countSelect = clone $this->_select;
00230
00231 $countSelect->reset(Zend_Db_Select::HAVING);
00232 $countSelect->reset(Zend_Db_Select::ORDER);
00233 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00234 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00235
00236 foreach ($this->_countFilterPart as $where) {
00237 $countSelect->where($where);
00238 }
00239
00240
00241
00242 $sql = $countSelect->__toString();
00243 $sql = preg_replace('/^select\s+.+?\s+from\s+/is', 'select count(*) from ', $sql);
00244
00245 return $sql;
00246 }
00247
00248
00249
00250
00251
00252 public function useOnlyCustomers()
00253 {
00254 $this->_select->where("main_table.customer_id > 0");
00255
00256 return $this;
00257 }
00258
00259
00260
00261
00262 public function useOnlySubscribed()
00263 {
00264 $this->_select->where("main_table.subscriber_status = ?", Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
00265
00266 return $this;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276 public function load($printQuery=false, $logQuery=false)
00277 {
00278 if ($this->isLoaded()) {
00279 return $this;
00280 }
00281 parent::load($printQuery, $logQuery);
00282 return $this;
00283 }
00284 }