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
00035 class Mage_Rating_Model_Mysql4_Rating extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 public function _construct()
00038 {
00039 $this->_init('rating/rating', 'rating_id');
00040 }
00041
00042
00043
00044
00045
00046
00047 protected function _initUniqueFields()
00048 {
00049 $this->_uniqueFields = array(array(
00050 'field' => 'rating_code',
00051 'title' => ''
00052 ));
00053 return $this;
00054 }
00055
00056 protected function _getLoadSelect($field, $value, $object)
00057 {
00058 $read = $this->_getReadAdapter();
00059
00060 $select = $read->select()
00061 ->from(array('main'=>$this->getMainTable()), array('rating_id', 'entity_id', 'position'))
00062 ->joinLeft(array('title'=>$this->getTable('rating_title')),
00063 'main.rating_id=title.rating_id AND title.store_id = '. (int) Mage::getSingleton('core/store')->getId(),
00064 array('IF(title.value IS NULL, main.rating_code, title.value) AS rating_code'))
00065 ->where('main.'.$field.'=?', $value);
00066 return $select;
00067 }
00068
00069 protected function _afterLoad(Mage_Core_Model_Abstract $object) {
00070 parent::_afterLoad($object);
00071 $select = $this->_getReadAdapter()->select()
00072 ->from($this->getTable('rating_title'))
00073 ->where('rating_id=?', $object->getId());
00074
00075 $data = $this->_getReadAdapter()->fetchAll($select);
00076 $storeCodes = array();
00077 foreach ($data as $row) {
00078 $storeCodes[$row['store_id']] = $row['value'];
00079 }
00080 if(sizeof($storeCodes)>0) {
00081 $object->setRatingCodes($storeCodes);
00082 }
00083
00084 $storesSelect = $this->_getReadAdapter()->select()
00085 ->from($this->getTable('rating_store'))
00086 ->where('rating_id=?', $object->getId());
00087
00088 $stores = $this->_getReadAdapter()->fetchAll($storesSelect);
00089
00090 $putStores = array();
00091 foreach ($stores as $store) {
00092 $putStores[] = $store['store_id'];
00093 }
00094
00095 $object->setStores($putStores);
00096
00097 return $this;
00098 }
00099
00100 protected function _afterSave(Mage_Core_Model_Abstract $object) {
00101 parent::_afterSave($object);
00102
00103 if($object->hasRatingCodes()) {
00104 try {
00105 $this->_getWriteAdapter()->beginTransaction();
00106 $condition = $this->_getWriteAdapter()->quoteInto('rating_id = ?', $object->getId());
00107 $this->_getWriteAdapter()->delete($this->getTable('rating_title'), $condition);
00108 if ($ratingCodes = $object->getRatingCodes()) {
00109 foreach ($ratingCodes as $storeId=>$value) {
00110 if(trim($value)=='') {
00111 continue;
00112 }
00113 $data = new Varien_Object();
00114 $data->setRatingId($object->getId())
00115 ->setStoreId($storeId)
00116 ->setValue($value);
00117 $this->_getWriteAdapter()->insert($this->getTable('rating_title'), $data->getData());
00118 }
00119 }
00120 $this->_getWriteAdapter()->commit();
00121 }
00122 catch (Exception $e) {
00123 $this->_getWriteAdapter()->rollBack();
00124 }
00125 }
00126
00127 if($object->hasStores()) {
00128 try {
00129 $condition = $this->_getWriteAdapter()->quoteInto('rating_id = ?', $object->getId());
00130 $this->_getWriteAdapter()->delete($this->getTable('rating_store'), $condition);
00131 foreach ($object->getStores() as $storeId) {
00132 $storeInsert = new Varien_Object();
00133 $storeInsert->setStoreId($storeId);
00134 $storeInsert->setRatingId($object->getId());
00135 $this->_getWriteAdapter()->insert($this->getTable('rating_store'), $storeInsert->getData());
00136 }
00137 }
00138 catch (Exception $e) {
00139 $this->_getWriteAdapter()->rollBack();
00140 }
00141 }
00142
00143 return $this;
00144 }
00145
00146 public function getEntitySummary($object, $onlyForCurrentStore = true)
00147 {
00148 $read = $this->_getReadAdapter();
00149 $sql = "SELECT
00150 SUM({$this->getTable('rating_vote')}.percent) as sum,
00151 COUNT(*) as count,
00152 {$this->getTable('review/review_store')}.store_id
00153 FROM
00154 {$this->getTable('rating_vote')}
00155 INNER JOIN
00156 {$this->getTable('review/review')}
00157 ON {$this->getTable('rating_vote')}.review_id={$this->getTable('review/review')}.review_id
00158 LEFT JOIN
00159 {$this->getTable('review/review_store')}
00160 ON {$this->getTable('rating_vote')}.review_id={$this->getTable('review/review_store')}.review_id
00161 INNER JOIN
00162 {$this->getTable('rating/rating_store')} AS rst
00163 ON rst.rating_id = {$this->getTable('rating_vote')}.rating_id AND rst.store_id = {$this->getTable('review/review_store')}.store_id
00164 INNER JOIN
00165 {$this->getTable('review/review_status')} AS review_status
00166 ON {$this->getTable('review/review')}.status_id = review_status.status_id
00167 WHERE
00168 {$read->quoteInto($this->getTable('rating_vote').'.entity_pk_value=?', $object->getEntityPkValue())}
00169 AND review_status.status_code = 'approved'
00170 GROUP BY
00171 {$this->getTable('rating_vote')}.entity_pk_value, {$this->getTable('review/review_store')}.store_id";
00172
00173 $data = $read->fetchAll($sql);
00174
00175
00176 if($onlyForCurrentStore) {
00177 foreach ($data as $row) {
00178 if($row['store_id']==Mage::app()->getStore()->getId()) {
00179 $object->addData( $row );
00180 }
00181 }
00182 return $object;
00183 }
00184
00185 $result = array();
00186
00187
00188 $stores = Mage::getModel('core/store')->getResourceCollection()->load();
00189
00190 foreach ($data as $row) {
00191 $clone = clone $object;
00192 $clone->addData( $row );
00193 $result[$clone->getStoreId()] = $clone;
00194 }
00195
00196 $usedStoresId = array_keys($result);
00197
00198 foreach ($stores as $store) {
00199 if (!in_array($store->getId(), $usedStoresId)) {
00200 $clone = clone $object;
00201 $clone->setCount(0);
00202 $clone->setSum(0);
00203 $clone->setStoreId($store->getId());
00204 $result[$store->getId()] = $clone;
00205 }
00206 }
00207
00208
00209
00210 return array_values($result);
00211 }
00212
00213 public function getReviewSummary($object, $onlyForCurrentStore = true)
00214 {
00215 $read = $this->_getReadAdapter();
00216 $sql = "SELECT
00217 SUM({$this->getTable('rating_vote')}.percent) as sum,
00218 COUNT(*) as count,
00219 {$this->getTable('review/review_store')}.store_id
00220 FROM
00221 {$this->getTable('rating_vote')}
00222 LEFT JOIN
00223 {$this->getTable('review/review_store')}
00224 ON {$this->getTable('rating_vote')}.review_id={$this->getTable('review/review_store')}.review_id
00225 INNER JOIN
00226 {$this->getTable('rating/rating_store')} AS rst
00227 ON rst.rating_id = {$this->getTable('rating_vote')}.rating_id AND rst.store_id = {$this->getTable('review/review_store')}.store_id
00228 WHERE
00229 {$read->quoteInto($this->getTable('rating_vote').'.review_id=?', $object->getReviewId())}
00230 GROUP BY
00231 {$this->getTable('rating_vote')}.review_id, {$this->getTable('review/review_store')}.store_id";
00232
00233 $data = $read->fetchAll($sql);
00234
00235 if($onlyForCurrentStore) {
00236 foreach ($data as $row) {
00237 if($row['store_id']==Mage::app()->getStore()->getId()) {
00238 $object->addData( $row );
00239 }
00240 }
00241 return $object;
00242 }
00243
00244 $result = array();
00245
00246 $stores = Mage::app()->getStore()->getResourceCollection()->load();
00247
00248 foreach ($data as $row) {
00249 $clone = clone $object;
00250 $clone->addData( $row );
00251 $result[$clone->getStoreId()] = $clone;
00252 }
00253
00254 $usedStoresId = array_keys($result);
00255
00256 foreach ($stores as $store) {
00257 if (!in_array($store->getId(), $usedStoresId)) {
00258 $clone = clone $object;
00259 $clone->setCount(0);
00260 $clone->setSum(0);
00261 $clone->setStoreId($store->getId());
00262 $result[$store->getId()] = $clone;
00263
00264 }
00265 }
00266
00267 return array_values($result);
00268 }
00269 }