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_Subscriber_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37:
38: 39: 40: 41: 42:
43: protected $_queueLinkTable;
44:
45: 46: 47: 48: 49:
50: protected $_storeTable;
51:
52: 53: 54: 55: 56:
57: protected $_queueJoinedFlag = false;
58:
59: 60: 61: 62: 63:
64: protected $_showCustomersInfo = false;
65:
66: 67: 68: 69: 70:
71: protected $_countFilterPart = array();
72:
73: 74: 75: 76: 77:
78: protected function _construct()
79: {
80: parent::_construct();
81: $this->_init('newsletter/subscriber');
82: $this->_queueLinkTable = $this->getTable('newsletter/queue_link');
83: $this->_storeTable = $this->getTable('core/store');
84:
85:
86:
87: $this->_map['fields']['customer_lastname'] = 'customer_lastname_table.value';
88: $this->_map['fields']['customer_firstname'] = 'customer_firstname_table.value';
89: $this->_map['fields']['type'] = $this->getResource()->getReadConnection()
90: ->getCheckSql('main_table.customer_id = 0', 1, 2);
91: $this->_map['fields']['website_id'] = 'store.website_id';
92: $this->_map['fields']['group_id'] = 'store.group_id';
93: $this->_map['fields']['store_id'] = 'main_table.store_id';
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function useQueue(Mage_Newsletter_Model_Queue $queue)
103: {
104: $this->getSelect()
105: ->join(array('link'=>$this->_queueLinkTable), "link.subscriber_id = main_table.subscriber_id", array())
106: ->where("link.queue_id = ? ", $queue->getId());
107: $this->_queueJoinedFlag = true;
108: return $this;
109: }
110:
111: 112: 113: 114: 115:
116: public function useOnlyUnsent()
117: {
118: if ($this->_queueJoinedFlag) {
119: $this->addFieldToFilter('link.letter_sent_at', array('null' => 1));
120: }
121:
122: return $this;
123: }
124:
125: 126: 127: 128: 129:
130: public function showCustomerInfo()
131: {
132: $adapter = $this->getConnection();
133: $customer = Mage::getModel('customer/customer');
134: $firstname = $customer->getAttribute('firstname');
135: $lastname = $customer->getAttribute('lastname');
136:
137: $this->getSelect()
138: ->joinLeft(
139: array('customer_lastname_table'=>$lastname->getBackend()->getTable()),
140: $adapter->quoteInto('customer_lastname_table.entity_id=main_table.customer_id
141: AND customer_lastname_table.attribute_id = ?', (int)$lastname->getAttributeId()),
142: array('customer_lastname'=>'value')
143: )
144: ->joinLeft(
145: array('customer_firstname_table'=>$firstname->getBackend()->getTable()),
146: $adapter->quoteInto('customer_firstname_table.entity_id=main_table.customer_id
147: AND customer_firstname_table.attribute_id = ?', (int)$firstname->getAttributeId()),
148: array('customer_firstname'=>'value')
149: );
150:
151: return $this;
152: }
153:
154: 155: 156: 157: 158:
159: public function addSubscriberTypeField()
160: {
161: $this->getSelect()
162: ->columns(array('type'=>new Zend_Db_Expr($this->_getMappedField('type'))));
163: return $this;
164: }
165:
166: 167: 168: 169: 170:
171: public function showStoreInfo()
172: {
173: $this->getSelect()->join(
174: array('store' => $this->_storeTable),
175: 'store.store_id = main_table.store_id',
176: array('group_id', 'website_id')
177: );
178:
179: return $this;
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function _getFieldTableAlias($field)
191: {
192: if (strpos($field, 'customer') === 0) {
193: return $field .'_table.value';
194: }
195:
196: if ($field == 'type') {
197: return $this->getConnection()->getCheckSql('main_table.customer_id = 0', 1, 2);
198: }
199:
200: if (in_array($field, array('website_id', 'group_id'))) {
201: return 'store.' . $field;
202: }
203:
204: return 'main_table.' . $field;
205: }
206:
207: 208: 209: 210: 211:
212: public function getSelectCountSql()
213: {
214:
215: $select = parent::getSelectCountSql();
216: $countSelect = clone $this->getSelect();
217:
218: $countSelect->reset(Zend_Db_Select::HAVING);
219:
220: return $select;
221: }
222:
223: 224: 225: 226: 227:
228: public function useOnlyCustomers()
229: {
230: $this->addFieldToFilter('main_table.customer_id', array('gt' => 0));
231:
232: return $this;
233: }
234:
235: 236: 237: 238: 239:
240: public function useOnlySubscribed()
241: {
242: $this->addFieldToFilter('main_table.subscriber_status', Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
243:
244: return $this;
245: }
246:
247: 248: 249: 250: 251: 252:
253: public function addStoreFilter($storeIds)
254: {
255: $this->addFieldToFilter('main_table.store_id', array('in'=>$storeIds));
256: return $this;
257: }
258: }
259: