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_Tag_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_isStoreFilter = false;
43:
44: 45: 46: 47: 48:
49: protected $_joinFlags = array();
50:
51: 52: 53: 54: 55:
56: public $_map = array(
57: 'fields' => array(
58: 'tag_id' => 'main_table.tag_id'
59: ),
60: );
61:
62: 63: 64: 65:
66: protected function _construct()
67: {
68: $this->_init('tag/tag');
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: public function load($printQuery = false, $logQuery = false)
79: {
80: if ($this->isLoaded()) {
81: return $this;
82: }
83: parent::load($printQuery, $logQuery);
84: if ($this->getFlag('add_stores_after')) {
85: $this->_addStoresVisibility();
86: }
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public function setJoinFlag($table)
100: {
101: $this->setFlag($table, true);
102: return $this;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function getJoinFlag($table)
115: {
116: return $this->getFlag($table);
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function unsetJoinFlag($table = null)
129: {
130: $this->setFlag($table, false);
131: return $this;
132: }
133:
134: 135: 136: 137: 138: 139:
140: public function limit($limit)
141: {
142: $this->getSelect()->limit($limit);
143: return $this;
144: }
145:
146: 147: 148: 149: 150: 151:
152: public function addPopularity($limit = null)
153: {
154: if (!$this->getFlag('popularity')) {
155: $this->getSelect()
156: ->joinLeft(
157: array('relation' => $this->getTable('tag/relation')),
158: 'main_table.tag_id = relation.tag_id',
159: array()
160: )
161: ->joinLeft(
162: array('summary' => $this->getTable('tag/summary')),
163: 'relation.tag_id = summary.tag_id AND relation.store_id = summary.store_id',
164: array('popularity')
165: )
166: ->group('main_table.tag_id');
167:
168: 169: 170:
171: $this->_useAnalyticFunction = true;
172:
173: if (!is_null($limit)) {
174: $this->getSelect()->limit($limit);
175: }
176:
177: $this->setFlag('popularity');
178: }
179: return $this;
180: }
181:
182: 183: 184: 185: 186: 187:
188: public function addSummary($storeId)
189: {
190: if (!$this->getFlag('summary')) {
191: $tableAlias = 'summary';
192: $joinCondition = $this->getConnection()
193: ->quoteInto(' AND ' . $tableAlias . '.store_id IN(?)', $storeId);
194:
195: $this->getSelect()
196: ->joinLeft(
197: array($tableAlias => $this->getTable('tag/summary')),
198: 'main_table.tag_id = ' . $tableAlias . '.tag_id' . $joinCondition,
199: array('store_id','popularity', 'customers', 'products'
200: ));
201:
202: $this->addFilterToMap('store_id', $tableAlias . '.store_id');
203: $this->addFilterToMap('popularity', $tableAlias . '.popularity');
204: $this->addFilterToMap('customers', $tableAlias . '.customers');
205: $this->addFilterToMap('products', $tableAlias . '.products');
206:
207: $this->setFlag('summary', true);
208: }
209: return $this;
210: }
211:
212: 213: 214: 215: 216:
217: public function addStoresVisibility()
218: {
219: $this->setFlag('add_stores_after', true);
220: return $this;
221: }
222:
223: 224: 225: 226: 227:
228: protected function _addStoresVisibility()
229: {
230: $tagIds = $this->getColumnValues('tag_id');
231:
232: $tagsStores = array();
233: if (sizeof($tagIds) > 0) {
234: $select = $this->getConnection()->select()
235: ->from($this->getTable('tag/summary'), array('store_id', 'tag_id'))
236: ->where('tag_id IN(?)', $tagIds);
237: $tagsRaw = $this->getConnection()->fetchAll($select);
238:
239: foreach ($tagsRaw as $tag) {
240: if (!isset($tagsStores[$tag['tag_id']])) {
241: $tagsStores[$tag['tag_id']] = array();
242: }
243:
244: $tagsStores[$tag['tag_id']][] = $tag['store_id'];
245: }
246: }
247:
248: foreach ($this as $item) {
249: if (isset($tagsStores[$item->getId()])) {
250: $item->setStores($tagsStores[$item->getId()]);
251: } else {
252: $item->setStores(array());
253: }
254: }
255:
256: return $this;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public function addFieldToFilter($field, $condition = null)
267: {
268: if ($this->getFlag('relation') && 'popularity' == $field) {
269:
270: $this->getSelect()->having(
271: $this->_getConditionSql('COUNT(relation.tag_relation_id)', $condition)
272: );
273: } elseif ($this->getFlag('summary') && in_array(
274: $field, array('customers', 'products', 'uses', 'historical_uses', 'popularity')
275: )) {
276: $this->getSelect()->where($this->_getConditionSql('summary.' . $field, $condition));
277: } else {
278: parent::addFieldToFilter($field, $condition);
279: }
280: return $this;
281: }
282:
283: 284: 285: 286: 287:
288: public function getSelectCountSql()
289: {
290: $select = parent::getSelectCountSql();
291:
292: $select->reset(Zend_Db_Select::COLUMNS);
293: $select->reset(Zend_Db_Select::GROUP);
294: $select->reset(Zend_Db_Select::HAVING);
295: $select->columns('COUNT(DISTINCT main_table.tag_id)');
296: return $select;
297: }
298:
299: 300: 301: 302: 303: 304: 305:
306: public function addStoreFilter($storeId, $allFilter = true)
307: {
308: if (!$this->getFlag('store_filter')) {
309:
310: $this->getSelect()->joinLeft(
311: array('summary_store' => $this->getTable('tag/summary')),
312: 'main_table.tag_id = summary_store.tag_id'
313: );
314:
315: $this->getSelect()->where('summary_store.store_id IN (?)', $storeId);
316:
317: $this->getSelect()->group('main_table.tag_id');
318:
319: if ($this->getFlag('relation') && $allFilter) {
320: $this->getSelect()->where('relation.store_id IN (?)', $storeId);
321: }
322: if ($this->getFlag('prelation') && $allFilter) {
323: $this->getSelect()->where('prelation.store_id IN (?)', $storeId);
324: }
325:
326: 327: 328:
329:
330: $this->_useAnalyticFunction = true;
331:
332: $this->setFlag('store_filter', true);
333: }
334:
335: return $this;
336: }
337:
338: 339: 340: 341: 342:
343: public function setActiveFilter()
344: {
345: $statusActive = Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE;
346: $this->getSelect()->where('relation.active = ?', $statusActive);
347: if ($this->getFlag('prelation')) {
348: $this->getSelect()->where('prelation.active = ?', $statusActive);
349: }
350: return $this;
351: }
352:
353: 354: 355: 356: 357: 358:
359: public function addStatusFilter($status)
360: {
361: $this->getSelect()->where('main_table.status = ?', $status);
362: return $this;
363: }
364:
365: 366: 367: 368: 369: 370:
371: public function addProductFilter($productId)
372: {
373: $this->addFieldToFilter('relation.product_id', $productId);
374: if ($this->getFlag('prelation')) {
375: $this->addFieldToFilter('prelation.product_id', $productId);
376: }
377: return $this;
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function addCustomerFilter($customerId)
387: {
388: $this->getSelect()
389: ->where('relation.customer_id = ?', $customerId);
390: if ($this->getFlag('prelation')) {
391: $this->getSelect()
392: ->where('prelation.customer_id = ?', $customerId);
393: }
394: return $this;
395: }
396:
397: 398: 399: 400: 401:
402: public function addTagGroup()
403: {
404: $this->getSelect()->group('main_table.tag_id');
405: $this->_useAnalyticFunction = true;
406: return $this;
407: }
408:
409: 410: 411: 412: 413:
414: public function joinRel()
415: {
416: $this->setFlag('relation', true);
417: $this->getSelect()->joinLeft(
418: array('relation' => $this->getTable('tag/relation')),
419: 'main_table.tag_id=relation.tag_id'
420: );
421: return $this;
422: }
423: }
424: