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_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_reviewTable;
43:
44: 45: 46: 47: 48:
49: protected $_reviewDetailTable;
50:
51: 52: 53: 54: 55:
56: protected $_reviewStatusTable;
57:
58: 59: 60: 61: 62:
63: protected $_reviewEntityTable;
64:
65: 66: 67: 68: 69:
70: protected $_reviewStoreTable;
71:
72: 73: 74: 75:
76: protected $_addStoreDataFlag = false;
77:
78: 79: 80: 81:
82: protected function _construct()
83: {
84: $this->_init('review/review');
85: $this->_reviewTable = $this->getTable('review/review');
86: $this->_reviewDetailTable = $this->getTable('review/review_detail');
87: $this->_reviewStatusTable = $this->getTable('review/review_status');
88: $this->_reviewEntityTable = $this->getTable('review/review_entity');
89: $this->_reviewStoreTable = $this->getTable('review/review_store');
90:
91: }
92:
93: 94: 95: 96: 97:
98: protected function _initSelect()
99: {
100: parent::_initSelect();
101: $this->getSelect()
102: ->join(array('detail' => $this->_reviewDetailTable),
103: 'main_table.review_id = detail.review_id',
104: array('detail_id', 'title', 'detail', 'nickname', 'customer_id'));
105: return $this;
106: }
107:
108: 109: 110: 111: 112: 113:
114: public function addCustomerFilter($customerId)
115: {
116: $this->addFilter('customer',
117: $this->getConnection()->quoteInto('detail.customer_id=?', $customerId),
118: 'string');
119: return $this;
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function addStoreFilter($storeId)
129: {
130: $inCond = $this->getConnection()->prepareSqlCondition('store.store_id', array('in' => $storeId));
131: $this->getSelect()->join(array('store'=>$this->_reviewStoreTable),
132: 'main_table.review_id=store.review_id',
133: array());
134: $this->getSelect()->where($inCond);
135: return $this;
136: }
137:
138: 139: 140: 141: 142:
143: public function addStoreData()
144: {
145: $this->_addStoreDataFlag = true;
146: return $this;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function addEntityFilter($entity, $pkValue)
157: {
158: if (is_numeric($entity)) {
159: $this->addFilter('entity',
160: $this->getConnection()->quoteInto('main_table.entity_id=?', $entity),
161: 'string');
162: } elseif (is_string($entity)) {
163: $this->_select->join($this->_reviewEntityTable,
164: 'main_table.entity_id='.$this->_reviewEntityTable.'.entity_id',
165: array('entity_code'));
166:
167: $this->addFilter('entity',
168: $this->getConnection()->quoteInto($this->_reviewEntityTable.'.entity_code=?', $entity),
169: 'string');
170: }
171:
172: $this->addFilter('entity_pk_value',
173: $this->getConnection()->quoteInto('main_table.entity_pk_value=?', $pkValue),
174: 'string');
175:
176: return $this;
177: }
178:
179: 180: 181: 182: 183: 184:
185: public function addStatusFilter($status)
186: {
187: if (is_numeric($status)) {
188: $this->addFilter('status',
189: $this->getConnection()->quoteInto('main_table.status_id=?', $status),
190: 'string');
191: } elseif (is_string($status)) {
192: $this->_select->join($this->_reviewStatusTable,
193: 'main_table.status_id='.$this->_reviewStatusTable.'.status_id',
194: array('status_code'));
195:
196: $this->addFilter('status',
197: $this->getConnection()->quoteInto($this->_reviewStatusTable.'.status_code=?', $status),
198: 'string');
199: }
200: return $this;
201: }
202:
203: 204: 205: 206: 207: 208:
209: public function setDateOrder($dir = 'DESC')
210: {
211: $this->setOrder('main_table.created_at', $dir);
212: return $this;
213: }
214:
215: 216: 217: 218: 219:
220: public function addRateVotes()
221: {
222: foreach ($this->getItems() as $item) {
223: $votesCollection = Mage::getModel('rating/rating_option_vote')
224: ->getResourceCollection()
225: ->setReviewFilter($item->getId())
226: ->setStoreFilter(Mage::app()->getStore()->getId())
227: ->addRatingInfo(Mage::app()->getStore()->getId())
228: ->load();
229: $item->setRatingVotes($votesCollection);
230: }
231:
232: return $this;
233: }
234:
235: 236: 237: 238: 239:
240: public function addReviewsTotalCount()
241: {
242: $this->_select->joinLeft(
243: array('r' => $this->_reviewTable),
244: 'main_table.entity_pk_value = r.entity_pk_value',
245: array('total_reviews' => new Zend_Db_Expr('COUNT(r.review_id)'))
246: )
247: ->group('main_table.review_id');
248:
249: 250: 251:
252: $this->_useAnalyticFunction = true;
253:
254: return $this;
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public function load($printQuery = false, $logQuery = false)
265: {
266: if ($this->isLoaded()) {
267: return $this;
268: }
269: Mage::dispatchEvent('review_review_collection_load_before', array('collection' => $this));
270: parent::load($printQuery, $logQuery);
271: if ($this->_addStoreDataFlag) {
272: $this->_addStoreData();
273: }
274: return $this;
275: }
276:
277: 278: 279: 280:
281: protected function _addStoreData()
282: {
283: $adapter = $this->getConnection();
284:
285: $reviewsIds = $this->getColumnValues('review_id');
286: $storesToReviews = array();
287: if (count($reviewsIds)>0) {
288: $inCond = $adapter->prepareSqlCondition('review_id', array('in' => $reviewsIds));
289: $select = $adapter->select()
290: ->from($this->_reviewStoreTable)
291: ->where($inCond);
292: $result = $adapter->fetchAll($select);
293: foreach ($result as $row) {
294: if (!isset($storesToReviews[$row['review_id']])) {
295: $storesToReviews[$row['review_id']] = array();
296: }
297: $storesToReviews[$row['review_id']][] = $row['store_id'];
298: }
299: }
300:
301: foreach ($this as $item) {
302: if (isset($storesToReviews[$item->getId()])) {
303: $item->setStores($storesToReviews[$item->getId()]);
304: } else {
305: $item->setStores(array());
306: }
307: }
308: }
309: }
310: