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_Review_Model_Resource_Review_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
36: {
37: 38: 39: 40: 41:
42: protected $_entitiesAlias = array();
43:
44: 45: 46: 47: 48:
49: protected $_reviewStoreTable;
50:
51: 52: 53: 54: 55:
56: protected $_addStoreDataFlag = false;
57:
58:
59: 60: 61: 62: 63:
64: protected $_storesIds = array();
65:
66: 67: 68: 69:
70: protected function _construct()
71: {
72: $this->_init('catalog/product');
73: $this->setRowIdFieldName('review_id');
74: $this->_reviewStoreTable = Mage::getSingleton('core/resource')->getTableName('review/review_store');
75: $this->_initTables();
76: }
77:
78: 79: 80: 81: 82:
83: protected function _initSelect()
84: {
85: parent::_initSelect();
86: $this->_joinFields();
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function addStoreFilter($storeId = null)
97: {
98: if (is_null($storeId)) {
99: $storeId = $this->getStoreId();
100: }
101:
102: parent::addStoreFilter($storeId);
103:
104: if (!is_array($storeId)) {
105: $storeId = array($storeId);
106: }
107:
108: if (!empty($this->_storesIds)) {
109: $this->_storesIds = array_intersect($this->_storesIds, $storeId);
110: } else {
111: $this->_storesIds = $storeId;
112: }
113:
114: return $this;
115: }
116:
117: 118: 119: 120: 121: 122:
123: public function setStoreFilter($storeId)
124: {
125: if (is_array($storeId) && isset($storeId['eq'])) {
126: $storeId = array_shift($storeId);
127: }
128:
129: if (!is_array($storeId)){
130: $storeId = array($storeId);
131: }
132:
133: if (!empty($this->_storesIds)){
134: $this->_storesIds = array_intersect($this->_storesIds, $storeId);
135: } else {
136: $this->_storesIds = $storeId;
137: }
138:
139: return $this;
140: }
141:
142: 143: 144: 145: 146: 147:
148: protected function _applyStoresFilterToSelect(Zend_Db_Select $select = null)
149: {
150: $adapter = $this->getConnection();
151: $storesIds = $this->_storesIds;
152: if (is_null($select)){
153: $select = $this->getSelect();
154: }
155:
156: if (is_array($storesIds) && (count($storesIds) == 1)) {
157: $storesIds = array_shift($storesIds);
158: }
159:
160: if (is_array($storesIds) && !empty($storesIds)) {
161: $inCond = $adapter->prepareSqlCondition('store.store_id', array('in' => $storesIds));
162: $select->join(array('store' => $this->_reviewStoreTable),
163: 'rt.review_id=store.review_id AND ' . $inCond,
164: array())
165: ->group('rt.review_id');
166:
167: $this->_useAnalyticFunction = true;
168: } else {
169: $select->join(array('store' => $this->_reviewStoreTable),
170: $adapter->quoteInto('rt.review_id=store.review_id AND store.store_id = ?', (int)$storesIds),
171: array());
172: }
173:
174: return $this;
175: }
176:
177: 178: 179: 180: 181:
182: public function addStoreData()
183: {
184: $this->_addStoreDataFlag = true;
185: return $this;
186: }
187:
188: 189: 190: 191: 192: 193:
194: public function addCustomerFilter($customerId)
195: {
196: $this->getSelect()
197: ->where('rdt.customer_id = ?', $customerId);
198: return $this;
199: }
200:
201: 202: 203: 204: 205: 206:
207: public function addEntityFilter($entityId)
208: {
209: $this->getSelect()
210: ->where('rt.entity_pk_value = ?', $entityId);
211: return $this;
212: }
213:
214: 215: 216: 217: 218: 219:
220: public function addStatusFilter($status)
221: {
222: $this->getSelect()
223: ->where('rt.status_id = ?', $status);
224: return $this;
225: }
226:
227: 228: 229: 230: 231: 232:
233: public function setDateOrder($dir = 'DESC')
234: {
235: $this->setOrder('rt.created_at', $dir);
236: return $this;
237: }
238:
239: 240: 241: 242: 243:
244: public function addReviewSummary()
245: {
246: foreach ($this->getItems() as $item) {
247: $model = Mage::getModel('rating/rating');
248: $model->getReviewSummary($item->getReviewId());
249: $item->addData($model->getData());
250: }
251: return $this;
252: }
253:
254: 255: 256: 257: 258:
259: public function addRateVotes()
260: {
261: foreach ($this->getItems() as $item) {
262: $votesCollection = Mage::getModel('rating/rating_option_vote')
263: ->getResourceCollection()
264: ->setEntityPkFilter($item->getEntityId())
265: ->setStoreFilter(Mage::app()->getStore()->getId())
266: ->load();
267: $item->setRatingVotes($votesCollection);
268: }
269: return $this;
270: }
271:
272: 273: 274: 275: 276:
277: protected function _joinFields()
278: {
279: $reviewTable = Mage::getSingleton('core/resource')->getTableName('review/review');
280: $reviewDetailTable = Mage::getSingleton('core/resource')->getTableName('review/review_detail');
281:
282: $this->addAttributeToSelect('name')
283: ->addAttributeToSelect('sku');
284:
285: $this->getSelect()
286: ->join(array('rt' => $reviewTable),
287: 'rt.entity_pk_value = e.entity_id',
288: array('rt.review_id', 'review_created_at'=> 'rt.created_at', 'rt.entity_pk_value', 'rt.status_id'))
289: ->join(array('rdt' => $reviewDetailTable),
290: 'rdt.review_id = rt.review_id',
291: array('rdt.title','rdt.nickname', 'rdt.detail', 'rdt.customer_id', 'rdt.store_id'));
292: return $this;
293: }
294:
295: 296: 297: 298: 299: 300: 301:
302: public function getAllIds($limit = null, $offset = null)
303: {
304: $idsSelect = clone $this->getSelect();
305: $idsSelect->reset(Zend_Db_Select::ORDER);
306: $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
307: $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
308: $idsSelect->reset(Zend_Db_Select::COLUMNS);
309: $idsSelect->columns('rt.review_id');
310: return $this->getConnection()->fetchCol($idsSelect);
311: }
312:
313: 314: 315: 316: 317:
318: public function getSelectCountSql()
319: {
320: $select = parent::getSelectCountSql();
321: $select->reset(Zend_Db_Select::COLUMNS)
322: ->columns('COUNT(e.entity_id)')
323: ->reset(Zend_Db_Select::HAVING);
324:
325: return $select;
326: }
327:
328: 329: 330: 331: 332: 333: 334:
335: public function setOrder($attribute, $dir = 'DESC')
336: {
337: switch($attribute) {
338: case 'rt.review_id':
339: case 'rt.created_at':
340: case 'rt.status_id':
341: case 'rdt.title':
342: case 'rdt.nickname':
343: case 'rdt.detail':
344: $this->getSelect()->order($attribute . ' ' . $dir);
345: break;
346: case 'stores':
347:
348: break;
349: case 'type':
350: $this->getSelect()->order('rdt.customer_id ' . $dir);
351: break;
352: default:
353: parent::setOrder($attribute, $dir);
354: }
355: return $this;
356: }
357:
358: 359: 360: 361: 362: 363: 364: 365:
366: public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')
367: {
368: switch($attribute) {
369: case 'rt.review_id':
370: case 'rt.created_at':
371: case 'rt.status_id':
372: case 'rdt.title':
373: case 'rdt.nickname':
374: case 'rdt.detail':
375: $conditionSql = $this->_getConditionSql($attribute, $condition);
376: $this->getSelect()->where($conditionSql);
377: break;
378: case 'stores':
379: $this->setStoreFilter($condition);
380: break;
381: case 'type':
382: if ($condition == 1) {
383: $conditionParts = array(
384: $this->_getConditionSql('rdt.customer_id', array('is' => new Zend_Db_Expr('NULL'))),
385: $this->_getConditionSql('rdt.store_id', array('eq' => Mage_Core_Model_App::ADMIN_STORE_ID))
386: );
387: $conditionSql = implode(' AND ', $conditionParts);
388: } elseif ($condition == 2) {
389: $conditionSql = $this->_getConditionSql('rdt.customer_id', array('gt' => 0));
390: } else {
391: $conditionParts = array(
392: $this->_getConditionSql('rdt.customer_id', array('is' => new Zend_Db_Expr('NULL'))),
393: $this->_getConditionSql('rdt.store_id', array('neq' => Mage_Core_Model_App::ADMIN_STORE_ID))
394: );
395: $conditionSql = implode(' AND ', $conditionParts);
396: }
397: $this->getSelect()->where($conditionSql);
398: break;
399:
400: default:
401: parent::addAttributeToFilter($attribute, $condition, $joinType);
402: break;
403: }
404: return $this;
405: }
406:
407: 408: 409: 410: 411: 412:
413: public function getColumnValues($colName)
414: {
415: $col = array();
416: foreach ($this->getItems() as $item) {
417: $col[] = $item->getData($colName);
418: }
419: return $col;
420: }
421:
422: 423: 424: 425: 426:
427: protected function _afterLoad()
428: {
429: parent::_afterLoad();
430: if ($this->_addStoreDataFlag) {
431: $this->_addStoreData();
432: }
433: return $this;
434: }
435:
436: 437: 438: 439:
440: protected function _addStoreData()
441: {
442: $adapter = $this->getConnection();
443:
444: $reviewsIds = $this->getColumnValues('review_id');
445: $storesToReviews = array();
446: if (count($reviewsIds)>0) {
447: $reviewIdCondition = $this->_getConditionSql('review_id', array('in' => $reviewsIds));
448: $storeIdCondition = $this->_getConditionSql('store_id', array('gt' => 0));
449: $select = $adapter->select()
450: ->from($this->_reviewStoreTable)
451: ->where($reviewIdCondition)
452: ->where($storeIdCondition);
453: $result = $adapter->fetchAll($select);
454: foreach ($result as $row) {
455: if (!isset($storesToReviews[$row['review_id']])) {
456: $storesToReviews[$row['review_id']] = array();
457: }
458: $storesToReviews[$row['review_id']][] = $row['store_id'];
459: }
460: }
461:
462: foreach ($this as $item) {
463: if (isset($storesToReviews[$item->getReviewId()])) {
464: $item->setData('stores', $storesToReviews[$item->getReviewId()]);
465: } else {
466: $item->setData('stores', array());
467: }
468:
469: }
470: }
471:
472: 473: 474: 475: 476:
477: protected function _beforeLoad()
478: {
479: parent::_beforeLoad();
480: $this->_applyStoresFilterToSelect();
481:
482: return $this;
483: }
484: }
485: