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_Review_Model_Mysql4_Review_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00036 {
00037 protected $_entitiesAlias = array();
00038 protected $_reviewStoreTable;
00039 protected $_addStoreDataFlag = false;
00040
00041 protected function _construct()
00042 {
00043 $this->_init('catalog/product');
00044 $this->setRowIdFieldName('review_id');
00045 $this->_reviewStoreTable = Mage::getSingleton('core/resource')->getTableName('review/review_store');
00046 }
00047
00048 protected function _initSelect()
00049 {
00050 parent::_initSelect();
00051 $this->_joinFields();
00052 return $this;
00053 }
00054
00055 public function addStoreFilter($storeId=null)
00056 {
00057 parent::addStoreFilter($storeId);
00058 $this->getSelect()
00059 ->join(array('store'=>$this->_reviewStoreTable),
00060 'rt.review_id=store.review_id AND store.store_id=' . (int)$storeId, array());
00061 return $this;
00062 }
00063
00064 public function setStoreFilter($storeId)
00065 {
00066 if( is_array($storeId) && isset($storeId['eq']) ) {
00067 $storeId = array_shift($storeId);
00068 }
00069
00070 if( is_array($storeId) ) {
00071 $this->getSelect()
00072 ->join(array('store'=>$this->_reviewStoreTable),
00073 $this->getConnection()->quoteInto('rt.review_id=store.review_id AND store.store_id IN(?)', $storeId), array())
00074 ->distinct(true)
00075 ;
00076 } else {
00077 $this->getSelect()
00078 ->join(array('store'=>$this->_reviewStoreTable),
00079 'rt.review_id=store.review_id AND store.store_id=' . (int)$storeId, array());
00080 }
00081
00082 return $this;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091 public function addStoreData()
00092 {
00093 $this->_addStoreDataFlag = true;
00094 return $this;
00095 }
00096 public function addCustomerFilter($customerId)
00097 {
00098 $this->getSelect()
00099 ->where('rdt.customer_id = ?', $customerId);
00100 return $this;
00101 }
00102
00103 public function addEntityFilter($entityId)
00104 {
00105 $this->getSelect()
00106 ->where('rt.entity_pk_value = ?', $entityId);
00107 return $this;
00108 }
00109
00110 public function addStatusFilter($status)
00111 {
00112 $this->getSelect()
00113 ->where('rt.status_id = ?', $status);
00114 return $this;
00115 }
00116
00117 public function setDateOrder($dir='DESC')
00118 {
00119 $this->setOrder('rt.created_at', $dir);
00120 return $this;
00121 }
00122
00123 public function addReviewSummary()
00124 {
00125 foreach( $this->getItems() as $item ) {
00126 $model = Mage::getModel('rating/rating');
00127 $model->getReviewSummary($item->getReviewId());
00128 $item->addData($model->getData());
00129 }
00130 return $this;
00131 }
00132
00133 public function addRateVotes()
00134 {
00135 foreach( $this->getItems() as $item ) {
00136 $votesCollection = Mage::getModel('rating/rating_option_vote')
00137 ->getResourceCollection()
00138 ->setEntityPkFilter($item->getEntityId())
00139 ->setStoreFilter(Mage::app()->getStore()->getId())
00140 ->load();
00141 $item->setRatingVotes( $votesCollection );
00142 }
00143 return $this;
00144 }
00145
00146 protected function _joinFields()
00147 {
00148 $reviewTable = Mage::getSingleton('core/resource')->getTableName('review/review');
00149 $reviewDetailTable = Mage::getSingleton('core/resource')->getTableName('review/review_detail');
00150
00151 $this->addAttributeToSelect('name')
00152 ->addAttributeToSelect('sku');
00153
00154 $this->getSelect()
00155 ->join(array('rt' => $reviewTable),
00156 'rt.entity_pk_value = e.entity_id',
00157 array('review_id', 'created_at', 'entity_pk_value', 'status_id'))
00158 ->join(array('rdt' => $reviewDetailTable), 'rdt.review_id = rt.review_id');
00159 return $this;
00160 }
00161
00162
00163
00164
00165
00166
00167 public function getAllIds($limit=null, $offset=null)
00168 {
00169 $idsSelect = clone $this->getSelect();
00170 $idsSelect->reset(Zend_Db_Select::ORDER);
00171 $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00172 $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00173 $idsSelect->reset(Zend_Db_Select::COLUMNS);
00174 $idsSelect->from(null, 'rt.review_id');
00175 return $this->getConnection()->fetchCol($idsSelect);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184 public function getSelectCountSql()
00185 {
00186 $countSelect = clone $this->getSelect();
00187 $countSelect->reset(Zend_Db_Select::ORDER);
00188 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00189 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00190
00191 $sql = $countSelect->__toString();
00192 $sql = preg_replace('/^select\s+.+?\s+from\s+/is', 'select count(e.entity_id) from ', $sql);
00193
00194 return $sql;
00195 }
00196
00197 public function setOrder($attribute, $dir='desc')
00198 {
00199 switch( $attribute ) {
00200 case 'rt.review_id':
00201 case 'rt.created_at':
00202 case 'rt.status_id':
00203 case 'rdt.title':
00204 case 'rdt.nickname':
00205 case 'rdt.detail':
00206 $this->getSelect()->order($attribute . ' ' . $dir);
00207 break;
00208 case 'stores':
00209
00210 break;
00211 case 'type':
00212 $this->getSelect()->order('rdt.customer_id ' . $dir);
00213 break;
00214 default:
00215 parent::setOrder($attribute, $dir);
00216 }
00217 return $this;
00218 }
00219
00220 public function addAttributeToFilter($attribute, $condition=null, $joinType='inner')
00221 {
00222 switch( $attribute ) {
00223 case 'rt.review_id':
00224 case 'rt.created_at':
00225 case 'rt.status_id':
00226 case 'rdt.title':
00227 case 'rdt.nickname':
00228 case 'rdt.detail':
00229 $conditionSql = $this->_getConditionSql($attribute, $condition);
00230 $this->getSelect()->where($conditionSql);
00231 return $this;
00232 break;
00233 case 'stores':
00234 $this->setStoreFilter($condition);
00235 return $this;
00236 break;
00237 case 'type':
00238 if($condition == 1) {
00239 $this->getSelect()->where('rdt.customer_id = 0');
00240 } elseif ($condition == 2) {
00241 $this->getSelect()->where('rdt.customer_id > 0');
00242 } else {
00243 $this->getSelect()->where('rdt.customer_id IS NULL');
00244 }
00245 return $this;
00246 break;
00247
00248 default:
00249 parent::addAttributeToFilter($attribute, $condition, $joinType);
00250 }
00251 return $this;
00252 }
00253
00254 public function getColumnValues($colName)
00255 {
00256 $col = array();
00257 foreach ($this->getItems() as $item) {
00258 $col[] = $item->getData($colName);
00259 }
00260 return $col;
00261 }
00262
00263 protected function _afterLoad()
00264 {
00265 parent::_afterLoad();
00266 if ($this->_addStoreDataFlag) {
00267 $this->_addStoreData();
00268 }
00269 return $this;
00270 }
00271
00272 protected function _addStoreData()
00273 {
00274 $reviewsIds = $this->getColumnValues('review_id');
00275 $storesToReviews = array();
00276 if (count($reviewsIds)>0) {
00277 $select = $this->getConnection()->select()
00278 ->from($this->_reviewStoreTable)
00279 ->where('review_id IN(?)', $reviewsIds)
00280 ->where('store_id > ?', 0);
00281 $result = $this->getConnection()->fetchAll($select);
00282 foreach ($result as $row) {
00283 if (!isset($storesToReviews[$row['review_id']])) {
00284 $storesToReviews[$row['review_id']] = array();
00285 }
00286 $storesToReviews[$row['review_id']][] = $row['store_id'];
00287 }
00288 }
00289
00290 foreach ($this as $item) {
00291 if(isset($storesToReviews[$item->getReviewId()])) {
00292 $item->setData('stores',$storesToReviews[$item->getReviewId()]);
00293 } else {
00294 $item->setData('stores', array());
00295 }
00296
00297 }
00298 }
00299 }