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_Option
00035 {
00036 protected $_reviewTable;
00037 protected $_ratingOptionTable;
00038 protected $_ratingVoteTable;
00039 protected $_aggregateTable;
00040 protected $_reviewStoreTable;
00041 protected $_ratingStoreTable;
00042
00043 protected $_read;
00044 protected $_write;
00045
00046 protected $_optionData;
00047 protected $_optionId;
00048
00049 public function __construct()
00050 {
00051 $this->_reviewTable = Mage::getSingleton('core/resource')->getTableName('review/review');
00052 $this->_ratingOptionTable = Mage::getSingleton('core/resource')->getTableName('rating/rating_option');
00053 $this->_ratingVoteTable = Mage::getSingleton('core/resource')->getTableName('rating/rating_vote');
00054 $this->_aggregateTable = Mage::getSingleton('core/resource')->getTableName('rating/rating_vote_aggregated');
00055 $this->_reviewStoreTable = Mage::getSingleton('core/resource')->getTableName('review/review_store');
00056 $this->_ratingStoreTable = Mage::getSingleton('core/resource')->getTableName('rating/rating_store');
00057
00058 $this->_read = Mage::getSingleton('core/resource')->getConnection('rating_read');
00059 $this->_write = Mage::getSingleton('core/resource')->getConnection('rating_write');
00060 }
00061
00062 public function save($object)
00063 {
00064 if( $object->getId() ) {
00065 $condition = $this->_write->quoteInto('option_id = ?', $object->getId());
00066 $object->unsetData('option_id');
00067 $this->_write->update($this->_ratingOptionTable, $object->getData(), $condition);
00068 } else {
00069 $this->_write->insert($this->_ratingOptionTable, $object->getData());
00070 }
00071 return $object;
00072 }
00073
00074 public function delete($object)
00075 {
00076 $condition = $this->_write->quoteInto('option_id = ?', $object->getId());
00077 $this->_write->delete($this->_ratingOptionTable, $condition);
00078 }
00079
00080 public function addVote($option)
00081 {
00082 $action = Mage::app()->getFrontController()->getAction();
00083
00084 if ($action instanceof Mage_Core_Controller_Front_Action || $action instanceof Mage_Adminhtml_Controller_Action) {
00085 $optionData = $this->load($option->getId());
00086 $data = array(
00087 'option_id' => $option->getId(),
00088 'review_id' => $option->getReviewId(),
00089 'percent' => (($optionData['value'] / 5) * 100),
00090 'value' => $optionData['value']
00091 );
00092
00093 if( !$option->getDoUpdate() ) {
00094 $data['remote_ip'] = $action->getRequest()->getServer('REMOTE_ADDR');
00095 $data['remote_ip_long'] = ip2long($action->getRequest()->getServer('REMOTE_ADDR'));
00096 $data['customer_id'] = Mage::getSingleton('customer/session')->getCustomerId();
00097 $data['entity_pk_value'] = $option->getEntityPkValue();
00098 $data['rating_id'] = $option->getRatingId();
00099 }
00100
00101 $this->_write->beginTransaction();
00102 try {
00103 if( $option->getDoUpdate() ) {
00104 $condition = "vote_id = '{$option->getVoteId()}' AND review_id = '{$option->getReviewId()}'";
00105 $this->_write->update($this->_ratingVoteTable, $data, $condition);
00106 $this->aggregate($option);
00107 } else {
00108 $this->_write->insert($this->_ratingVoteTable, $data);
00109 $option->setVoteId($this->_write->lastInsertId());
00110 $this->aggregate($option);
00111 }
00112 $this->_write->commit();
00113 }
00114 catch (Exception $e){
00115 $this->_write->rollback();
00116 throw new Exception($e->getMessage());
00117 }
00118 }
00119 return $this;
00120 }
00121
00122 public function aggregate($option)
00123 {
00124 $optionData = $this->load($option->getId());
00125 $vote = Mage::getModel('rating/rating_option_vote')->load($option->getVoteId());
00126 $this->aggregateEntityByRatingId($vote->getRatingId(), $vote->getEntityPkValue());
00127 }
00128
00129 public function aggregateEntityByRatingId($ratingId, $entityPkValue)
00130 {
00131 $select = $this->_read->select()
00132 ->from($this->_aggregateTable)
00133 ->where('rating_id = ?', $ratingId)
00134 ->where('entity_pk_value = ?', $entityPkValue);
00135
00136 $data = $this->_read->fetchAll($select);
00137
00138 $oldData = array();
00139 foreach($data as $row) {
00140 $oldData[$row['store_id']] = $row['primary_id'];
00141 }
00142
00143 $select = $this->_read->select()
00144 ->from(array('vote'=>$this->_ratingVoteTable),
00145 array('COUNT(vote.vote_id) AS vote_count',
00146 'SUM(vote.value) AS vote_value_sum',
00147 'COUNT(CASE WHEN review.status_id=1 THEN vote.vote_id ELSE NULL END) AS app_vote_count',
00148 'SUM(CASE WHEN review.status_id=1 THEN vote.value ELSE 0 END) AS app_vote_value_sum',
00149 ))
00150 ->join(array('review'=>$this->_reviewTable), 'vote.review_id=review.review_id', array())
00151 ->joinLeft(array('store'=>$this->_reviewStoreTable), 'vote.review_id=store.review_id', 'store_id')
00152 ->join(array('rstore'=>$this->_ratingStoreTable), 'vote.rating_id=rstore.rating_id AND rstore.store_id=store.store_id', array())
00153 ->where('vote.rating_id = ?', $ratingId)
00154 ->where('vote.entity_pk_value = ?', $entityPkValue)
00155 ->group('vote.rating_id')
00156 ->group('vote.entity_pk_value')
00157 ->group('store.store_id');
00158
00159 $perStoreInfo = $this->_read->fetchAll($select);
00160
00161 $usedStores = array();
00162 foreach($perStoreInfo as $row) {
00163 $saveData = new Varien_Object(array(
00164 'rating_id' => $ratingId,
00165 'entity_pk_value' => $entityPkValue,
00166 'vote_count' => $row['vote_count'],
00167 'vote_value_sum' => $row['vote_value_sum'],
00168 'percent' => (($row['vote_value_sum']/$row['vote_count'])/5) * 100,
00169 'percent_approved' => ($row['app_vote_count'] ? ((($row['app_vote_value_sum']/$row['app_vote_count'])/5) * 100) : 0),
00170 'store_id' => $row['store_id'],
00171 ));
00172
00173 if(isset($oldData[$row['store_id']])) {
00174 $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$row['store_id']]);
00175 $this->_write->update($this->_aggregateTable, $saveData->getData(), $condition);
00176 } else {
00177 $this->_write->insert($this->_aggregateTable, $saveData->getData());
00178 }
00179
00180 $usedStores[] = $row['store_id'];
00181 }
00182
00183 $toDelete = array_diff(array_keys($oldData), $usedStores);
00184
00185 foreach ($toDelete as $storeId) {
00186 $condition = $this->_write->quoteInto("primary_id = ?", $oldData[$storeId]);
00187 $this->_write->delete($this->_aggregateTable, $condition);
00188 }
00189 }
00190
00191 public function load($optionId)
00192 {
00193 if( !$this->_optionData || $this->_optionId != $optionId ) {
00194 $select = $this->_read->select();
00195 $select->from($this->_ratingOptionTable)
00196 ->where('option_id = ?', $optionId);
00197
00198 $data = $this->_read->fetchRow($select);
00199
00200 $this->_optionData = $data;
00201 $this->_optionId = $optionId;
00202 return $data;
00203 }
00204
00205 return $this->_optionData;
00206 }
00207 }