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: class Mage_Rating_Model_Resource_Rating_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
35: {
36: 37: 38:
39: protected $_isStoreJoined = false;
40:
41: 42: 43: 44:
45: protected function _construct()
46: {
47: $this->_init('rating/rating');
48: }
49:
50: 51: 52: 53: 54: 55:
56: public function addEntityFilter($entity)
57: {
58: $adapter = $this->getConnection();
59:
60: $this->getSelect()
61: ->join($this->getTable('rating_entity'),
62: 'main_table.entity_id=' . $this->getTable('rating_entity') . '.entity_id',
63: array('entity_code'));
64:
65: if (is_numeric($entity)) {
66: $this->addFilter('entity',
67: $adapter->quoteInto($this->getTable('rating_entity') . '.entity_id=?', $entity),
68: 'string');
69: } elseif (is_string($entity)) {
70: $this->addFilter('entity',
71: $adapter->quoteInto($this->getTable('rating_entity') . '.entity_code=?', $entity),
72: 'string');
73: }
74: return $this;
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function setPositionOrder($dir='ASC')
84: {
85: $this->setOrder('main_table.position', $dir);
86: return $this;
87: }
88:
89: 90: 91: 92: 93: 94:
95: public function setStoreFilter($storeId)
96: {
97: $adapter = $this->getConnection();
98: if (!is_array($storeId)) {
99: $storeId = array($storeId === null ? -1 : $storeId);
100: }
101: if (empty($storeId)) {
102: return $this;
103: }
104: if (!$this->_isStoreJoined) {
105: $this->getSelect()
106: ->distinct(true)
107: ->join(
108: array('store'=>$this->getTable('rating_store')),
109: 'main_table.rating_id = store.rating_id',
110: array())
111:
112: ;
113: $this->_isStoreJoined = true;
114: }
115: $inCond = $adapter->prepareSqlCondition('store.store_id', array(
116: 'in' => $storeId
117: ));
118: $this->getSelect()
119: ->where($inCond);
120: $this->setPositionOrder();
121: return $this;
122: }
123:
124: 125: 126: 127: 128:
129: public function addOptionToItems()
130: {
131: $arrRatingId = $this->getColumnValues('rating_id');
132:
133: if (!empty($arrRatingId)) {
134: $collection = Mage::getResourceModel('rating/rating_option_collection')
135: ->addRatingFilter($arrRatingId)
136: ->setPositionOrder()
137: ->load();
138:
139: foreach ($this as $rating) {
140: $rating->setOptions($collection->getItemsByColumnValue('rating_id', $rating->getId()));
141: }
142: }
143:
144: return $this;
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: public function addEntitySummaryToItem($entityPkValue, $storeId)
155: {
156: $arrRatingId = $this->getColumnValues('rating_id');
157: if (count($arrRatingId) == 0) {
158: return $this;
159: }
160:
161: $adapter = $this->getConnection();
162:
163: $inCond = $adapter->prepareSqlCondition('rating_option_vote.rating_id', array(
164: 'in' => $arrRatingId
165: ));
166: $sumCond = new Zend_Db_Expr("SUM(rating_option_vote.{$adapter->quoteIdentifier('percent')})");
167: $countCond = new Zend_Db_Expr('COUNT(*)');
168: $select = $adapter->select()
169: ->from(array('rating_option_vote' => $this->getTable('rating/rating_option_vote')),
170: array(
171: 'rating_id' => 'rating_option_vote.rating_id',
172: 'sum' => $sumCond,
173: 'count' => $countCond
174: ))
175: ->join(
176: array('review_store' => $this->getTable('review/review_store')),
177: 'rating_option_vote.review_id=review_store.review_id AND review_store.store_id = :store_id',
178: array())
179: ->join(
180: array('rst' => $this->getTable('rating/rating_store')),
181: 'rst.rating_id = rating_option_vote.rating_id AND rst.store_id = :rst_store_id',
182: array())
183: ->join(array('review' => $this->getTable('review/review')),
184: 'review_store.review_id=review.review_id AND review.status_id=1',
185: array())
186: ->where($inCond)
187: ->where('rating_option_vote.entity_pk_value=:pk_value')
188: ->group('rating_option_vote.rating_id');
189: $bind = array(
190: ':store_id' => (int)$storeId,
191: ':rst_store_id' => (int)$storeId,
192: ':pk_value' => $entityPkValue
193: );
194: $data = $this->getConnection()->fetchAll($select, $bind);
195:
196: foreach ($data as $item) {
197: $rating = $this->getItemById($item['rating_id']);
198: if ($rating && $item['count']>0) {
199: $rating->setSummary($item['sum']/$item['count']);
200: }
201: }
202: return $this;
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function addRatingPerStoreName($storeId)
212: {
213: $adapter = $this->getConnection();
214: $ratingCodeCond = $adapter->getIfNullSql('title.value', 'main_table.rating_code');
215: $this->getSelect()
216: ->joinLeft(array('title' => $this->getTable('rating_title')),
217: $adapter->quoteInto('main_table.rating_id=title.rating_id AND title.store_id = ?', (int) $storeId),
218: array('rating_code' => $ratingCodeCond));
219: return $this;
220: }
221:
222: 223: 224: 225: 226:
227: public function addStoresToCollection()
228: {
229: if (!$this->_isCollectionLoaded) {
230: return $this;
231: }
232: $ratingIds = array();
233: foreach ($this as $item) {
234: $ratingIds[] = $item->getId();
235: $item->setStores(array());
236: }
237: if (!$ratingIds) {
238: return $this;
239: }
240: $adapter = $this->getConnection();
241:
242: $inCond = $adapter->prepareSqlCondition('rating_id', array(
243: 'in' => $ratingIds
244: ));
245:
246: $this->_select = $adapter
247: ->select()
248: ->from($this->getTable('rating_store'))
249: ->where($inCond);
250:
251: $data = $adapter->fetchAll($this->_select);
252: if (is_array($data) && count($data) > 0) {
253: foreach ($data as $row) {
254: $item = $this->getItemById($row['rating_id']);
255: $item->setStores(array_merge($item->getStores(), array($row['store_id'])));
256: }
257: }
258: return $this;
259: }
260: }
261: