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_Tag_Model_Resource_Customer_Collection extends Mage_Customer_Model_Resource_Customer_Collection
36: {
37: 38: 39: 40: 41:
42: protected $_allowDisableGrouping = true;
43:
44: 45: 46: 47: 48:
49: protected $_countAttribute = 'tr.tag_id';
50:
51: 52: 53: 54: 55:
56: protected $_joinFlags = array();
57:
58: 59: 60: 61: 62:
63: public function _initSelect()
64: {
65: parent::_initSelect();
66: $this->_joinFields();
67: $this->_setIdFieldName('tag_relation_id');
68: return $this;
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79:
80: public function setJoinFlag($table)
81: {
82: $this->setFlag($table, true);
83: return $this;
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93: 94:
95: public function getJoinFlag($table)
96: {
97: return $this->getFlag($table);
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function unsetJoinFlag($table = null)
110: {
111: $this->setFlag($table, false);
112: return $this;
113: }
114:
115: 116: 117: 118: 119: 120:
121: public function addTagFilter($tagId)
122: {
123: $this->getSelect()
124: ->where('tr.tag_id = ?', $tagId);
125: return $this;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function addProductFilter($productId)
135: {
136: $this->getSelect()
137: ->where('tr.product_id = ?', $productId);
138: return $this;
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function addStoreFilter($storeId)
148: {
149: $this->getSelect()->where('tr.store_id IN (?)', $storeId);
150: return $this;
151: }
152:
153: 154: 155: 156: 157: 158:
159: public function addStatusFilter($status)
160: {
161: $this->getSelect()
162: ->where('t.status = ?', $status);
163: return $this;
164: }
165:
166: 167: 168: 169: 170:
171: public function addDescOrder()
172: {
173: $this->getSelect()
174: ->order('tr.tag_relation_id desc');
175: return $this;
176: }
177:
178: 179: 180: 181: 182:
183: public function addGroupByTag()
184: {
185: $this->getSelect()
186: ->group('tr.tag_id');
187:
188: 189: 190:
191: $this->_useAnalyticFunction = true;
192:
193: $this->_allowDisableGrouping = true;
194: return $this;
195: }
196:
197: 198: 199: 200: 201:
202: public function addGroupByCustomer()
203: {
204: $this->getSelect()
205: ->group('tr.customer_id');
206:
207: $this->_allowDisableGrouping = false;
208: return $this;
209: }
210:
211: 212: 213: 214: 215:
216: public function addGroupByCustomerProduct()
217: {
218:
219: $this->_allowDisableGrouping = false;
220: return $this;
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function addCustomerFilter($customerId)
230: {
231: $this->getSelect()->where('tr.customer_id = ?', $customerId);
232: return $this;
233: }
234:
235: 236: 237: 238:
239: protected function _joinFields()
240: {
241: $tagRelationTable = $this->getTable('tag/relation');
242: $tagTable = $this->getTable('tag/tag');
243:
244:
245: $this->addAttributeToSelect('firstname')
246: ->addAttributeToSelect('lastname')
247: ->addAttributeToSelect('email');
248:
249: $this->getSelect()
250: ->join(
251: array('tr' => $tagRelationTable),
252: 'tr.customer_id = e.entity_id',
253: array('tag_relation_id', 'product_id', 'active')
254: )
255: ->join(array('t' => $tagTable), 't.tag_id = tr.tag_id', array('*'));
256: }
257:
258: 259: 260: 261: 262:
263: public function getSelectCountSql()
264: {
265: $countSelect = parent::getSelectCountSql();
266:
267: if ($this->_allowDisableGrouping) {
268: $countSelect->reset(Zend_Db_Select::COLUMNS);
269: $countSelect->reset(Zend_Db_Select::GROUP);
270: $countSelect->columns('COUNT(DISTINCT ' . $this->getCountAttribute() . ')');
271: }
272: return $countSelect;
273: }
274:
275: 276: 277: 278: 279:
280: public function addProductName()
281: {
282: $productsId = array();
283: $productsData = array();
284:
285: foreach ($this->getItems() as $item) {
286: $productsId[] = $item->getProductId();
287: }
288:
289: $productsId = array_unique($productsId);
290:
291:
292: if ( sizeof($productsId) == 0 ) {
293: return;
294: }
295:
296: $collection = Mage::getModel('catalog/product')->getCollection()
297: ->addAttributeToSelect('name')
298: ->addAttributeToSelect('sku')
299: ->addIdFilter($productsId);
300:
301: $collection->load();
302:
303: foreach ($collection->getItems() as $item) {
304: $productsData[$item->getId()] = $item->getName();
305: $productsSku[$item->getId()] = $item->getSku();
306: }
307:
308: foreach ($this->getItems() as $item) {
309: $item->setProduct($productsData[$item->getProductId()]);
310: $item->setProductSku($productsSku[$item->getProductId()]);
311: }
312: return $this;
313: }
314:
315: 316: 317: 318: 319: 320: 321:
322: public function setOrder($attribute, $dir = 'desc')
323: {
324: switch( $attribute ) {
325: case 'name':
326: case 'status':
327: $this->getSelect()->order($attribute . ' ' . $dir);
328: break;
329:
330: default:
331: parent::setOrder($attribute, $dir);
332: }
333: return $this;
334: }
335:
336: 337: 338: 339: 340: 341:
342: public function setCountAttribute($value)
343: {
344: $this->_countAttribute = $value;
345: return $this;
346: }
347:
348: 349: 350: 351: 352:
353: public function getCountAttribute()
354: {
355: return $this->_countAttribute;
356: }
357:
358: 359: 360: 361: 362: 363: 364:
365: public function addFieldToFilter($attribute, $condition = null)
366: {
367: if ($attribute == 'name') {
368: $where = $this->_getConditionSql('t.name', $condition);
369: $this->getSelect()->where($where, null, Varien_Db_Select::TYPE_CONDITION);
370: return $this;
371: } else {
372: return parent::addFieldToFilter($attribute, $condition);
373: }
374: }
375:
376: }
377: