00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Rating_Model_Mysql4_Rating_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00035 {
00036
00037
00038
00039 protected $_isStoreJoined = false;
00040
00041 public function _construct()
00042 {
00043 $this->_init('rating/rating');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 public function addEntityFilter($entity)
00055 {
00056 $this->_select->join($this->getTable('rating_entity'),
00057 'main_table.entity_id='.$this->getTable('rating_entity').'.entity_id');
00058
00059 if (is_numeric($entity)) {
00060 $this->addFilter('entity',
00061 $this->getConnection()->quoteInto($this->getTable('rating_entity').'.entity_id=?', $entity),
00062 'string');
00063 }
00064 elseif (is_string($entity)) {
00065 $this->addFilter('entity',
00066 $this->getConnection()->quoteInto($this->getTable('rating_entity').'.entity_code=?', $entity),
00067 'string');
00068 }
00069 return $this;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 public function setPositionOrder($dir='ASC')
00079 {
00080 $this->setOrder('main_table.position', $dir);
00081 return $this;
00082 }
00083
00084 public function setStoreFilter($storeId)
00085 {
00086 if(!is_array($storeId)) {
00087 $storeId = array($storeId === null ? -1 : $storeId);
00088 }
00089 if (empty($storeId)) {
00090 return $this;
00091 }
00092 if (!$this->_isStoreJoined) {
00093 $this->getSelect()->join(array('store'=>$this->getTable('rating_store')), 'main_table.rating_id = store.rating_id')
00094 ->group('main_table.rating_id');
00095 $this->_isStoreJoined = true;
00096 }
00097 $this->getSelect()->where($this->getConnection()->quoteInto('store.store_id IN(?)', $storeId));
00098 $this->setPositionOrder();
00099 return $this;
00100 }
00101
00102
00103
00104
00105
00106
00107 public function addOptionToItems()
00108 {
00109 $arrRatingId = $this->getColumnValues('rating_id');
00110
00111 if (!empty($arrRatingId)) {
00112 $collection = Mage::getResourceModel('rating/rating_option_collection')
00113 ->addRatingFilter($arrRatingId)
00114 ->setPositionOrder()
00115 ->load();
00116
00117 foreach ($this as $rating) {
00118 $rating->setOptions($collection->getItemsByColumnValue('rating_id', $rating->getId()));
00119 }
00120 }
00121
00122 return $this;
00123 }
00124
00125 public function addEntitySummaryToItem($entityPkValue, $storeId)
00126 {
00127 $arrRatingId = $this->getColumnValues('rating_id');
00128
00129 if( count($arrRatingId) == 0 ) {
00130 return;
00131 }
00132
00133 $sql = "SELECT
00134 {$this->getTable('rating_vote')}.rating_id as rating_id,
00135 SUM({$this->getTable('rating_vote')}.percent) as sum,
00136 COUNT(*) as count
00137 FROM
00138 {$this->getTable('rating_vote')}
00139 INNER JOIN
00140 {$this->getTable('review/review_store')}
00141 ON {$this->getTable('rating_vote')}.review_id={$this->getTable('review/review_store')}.review_id AND {$this->getTable('review/review_store')}.store_id = ". (int) $storeId . "
00142 INNER JOIN
00143 {$this->getTable('rating/rating_store')} AS rst
00144 ON rst.rating_id = {$this->getTable('rating_vote')}.rating_id AND rst.store_id = ". (int) $storeId . "
00145 INNER JOIN
00146 {$this->getTable('review/review')} ON {$this->getTable('review/review_store')}.review_id={$this->getTable('review/review')}.review_id AND {$this->getTable('review/review')}.status_id=1
00147 WHERE
00148 {$this->getConnection()->quoteInto($this->getTable('rating_vote').'.rating_id IN (?)', $arrRatingId)}
00149 AND {$this->getConnection()->quoteInto($this->getTable('rating_vote').'.entity_pk_value=?', $entityPkValue)}
00150 GROUP BY
00151 {$this->getTable('rating_vote')}.rating_id";
00152
00153 $data = $this->getConnection()->fetchAll($sql);
00154
00155 foreach ($data as $item) {
00156 $rating = $this->getItemById($item['rating_id']);
00157 if ($rating && $item['count']>0) {
00158 $rating->setSummary($item['sum']/$item['count']);
00159 }
00160 }
00161 return $this;
00162 }
00163
00164 public function addRatingPerStoreName($storeId) {
00165 $this->getSelect()->joinLeft(array('title'=>$this->getTable('rating_title')),
00166 'main_table.rating_id=title.rating_id AND title.store_id = '. (int) $storeId,
00167 array('IF(title.value IS NULL, main_table.rating_code, title.value) AS rating_code'));
00168 return $this;
00169 }
00170
00171 public function addStoresToCollection()
00172 {
00173 if (!$this->_isCollectionLoaded) {
00174 return $this;
00175 }
00176 $ratingIds = array();
00177 foreach ($this as $item) {
00178 $ratingIds[] = $item->getId();
00179 $item->setStores(array());
00180 }
00181 if (!$ratingIds) {
00182 return $this;
00183 }
00184
00185 $this->_select = $this->getConnection()
00186 ->select()
00187 ->from($this->getTable('rating_store'))
00188 ->where('rating_id IN(?)', $ratingIds);
00189 foreach ($this->getConnection()->fetchAll($this->_select) as $row) {
00190 $item = $this->getItemById($row['rating_id']);
00191 $item->setStores(array_merge($item->getStores(), array($row['store_id'])));
00192 }
00193
00194 return $this;
00195 }
00196 }