Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Review_Block_Customer_List
  • Mage_Review_Block_Customer_Recent
  • Mage_Review_Block_Customer_View
  • Mage_Review_Block_Form
  • Mage_Review_Block_Helper
  • Mage_Review_Block_Product_View
  • Mage_Review_Block_Product_View_List
  • Mage_Review_Block_View
  • Mage_Review_CustomerController
  • Mage_Review_Helper_Data
  • Mage_Review_Model_Mysql4_Review
  • Mage_Review_Model_Mysql4_Review_Collection
  • Mage_Review_Model_Mysql4_Review_Product_Collection
  • Mage_Review_Model_Mysql4_Review_Status_Collection
  • Mage_Review_Model_Mysql4_Review_Summary
  • Mage_Review_Model_Mysql4_Review_Summary_Collection
  • Mage_Review_Model_Observer
  • Mage_Review_Model_Resource_Review
  • Mage_Review_Model_Resource_Review_Collection
  • Mage_Review_Model_Resource_Review_Product_Collection
  • Mage_Review_Model_Resource_Review_Status
  • Mage_Review_Model_Resource_Review_Status_Collection
  • Mage_Review_Model_Resource_Review_Summary
  • Mage_Review_Model_Resource_Review_Summary_Collection
  • Mage_Review_Model_Review
  • Mage_Review_Model_Review_Status
  • Mage_Review_Model_Review_Summary
  • Mage_Review_Model_Session
  • Mage_Review_ProductController
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Review
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Review resource model
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Review
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Review_Model_Resource_Review extends Mage_Core_Model_Resource_Db_Abstract
 36: {
 37:     /**
 38:      * Review table
 39:      *
 40:      * @var string
 41:      */
 42:     protected $_reviewTable;
 43: 
 44:     /**
 45:      * Review Detail table
 46:      *
 47:      * @var string
 48:      */
 49:     protected $_reviewDetailTable;
 50: 
 51:     /**
 52:      * Review status table
 53:      *
 54:      * @var string
 55:      */
 56:     protected $_reviewStatusTable;
 57: 
 58:     /**
 59:      * Review entity table
 60:      *
 61:      * @var string
 62:      */
 63:     protected $_reviewEntityTable;
 64: 
 65:     /**
 66:      * Review store table
 67:      *
 68:      * @var string
 69:      */
 70:     protected $_reviewStoreTable;
 71: 
 72:     /**
 73:      * Review aggregate table
 74:      *
 75:      * @var string
 76:      */
 77:     protected $_aggregateTable;
 78: 
 79:     /**
 80:      * Cache of deleted rating data
 81:      *
 82:      * @var array
 83:      */
 84:     private $_deleteCache   = array();
 85: 
 86:     /**
 87:      * Define main table. Define other tables name
 88:      *
 89:      */
 90:     protected function _construct()
 91:     {
 92:         $this->_init('review/review', 'review_id');
 93:         $this->_reviewTable         = $this->getTable('review/review');
 94:         $this->_reviewDetailTable   = $this->getTable('review/review_detail');
 95:         $this->_reviewStatusTable   = $this->getTable('review/review_status');
 96:         $this->_reviewEntityTable   = $this->getTable('review/review_entity');
 97:         $this->_reviewStoreTable    = $this->getTable('review/review_store');
 98:         $this->_aggregateTable      = $this->getTable('review/review_aggregate');
 99:     }
100: 
101:     /**
102:      * Retrieve select object for load object data
103:      *
104:      * @param string $field
105:      * @param mixed $value
106:      * @param unknown_type $object
107:      * @return Zend_Db_Select
108:      */
109:     protected function _getLoadSelect($field, $value, $object)
110:     {
111:         $select = parent::_getLoadSelect($field, $value, $object);
112:         $select->join(
113:             $this->_reviewDetailTable,
114:             $this->getMainTable().".review_id = {$this->_reviewDetailTable}.review_id"
115:         );
116:         return $select;
117:     }
118: 
119:     /**
120:      * Perform actions before object save
121:      *
122:      * @param Varien_Object $object
123:      * @return Mage_Review_Model_Resource_Review
124:      */
125:     protected function _beforeSave(Mage_Core_Model_Abstract $object)
126:     {
127:         if (!$object->getId()) {
128:             $object->setCreatedAt(Mage::getSingleton('core/date')->gmtDate());
129:         }
130:         if ($object->hasData('stores') && is_array($object->getStores())) {
131:             $stores = $object->getStores();
132:             $stores[] = 0;
133:             $object->setStores($stores);
134:         } elseif ($object->hasData('stores')) {
135:             $object->setStores(array($object->getStores(), 0));
136:         }
137:         return $this;
138:     }
139: 
140:     /**
141:      * Perform actions after object save
142:      *
143:      * @param Varien_Object $object
144:      * @return Mage_Review_Model_Resource_Review
145:      */
146:     protected function _afterSave(Mage_Core_Model_Abstract $object)
147:     {
148:         $adapter = $this->_getWriteAdapter();
149:         /**
150:          * save detail
151:          */
152:         $detail = array(
153:             'title'     => $object->getTitle(),
154:             'detail'    => $object->getDetail(),
155:             'nickname'  => $object->getNickname(),
156:         );
157:         $select = $adapter->select()
158:             ->from($this->_reviewDetailTable, 'detail_id')
159:             ->where('review_id = :review_id');
160:         $detailId = $adapter->fetchOne($select, array(':review_id' => $object->getId()));
161: 
162:         if ($detailId) {
163:             $condition = array("detail_id = ?" => $detailId);
164:             $adapter->update($this->_reviewDetailTable, $detail, $condition);
165:         } else {
166:             $detail['store_id']   = $object->getStoreId();
167:             $detail['customer_id']= $object->getCustomerId();
168:             $detail['review_id']  = $object->getId();
169:             $adapter->insert($this->_reviewDetailTable, $detail);
170:         }
171: 
172: 
173:         /**
174:          * save stores
175:          */
176:         $stores = $object->getStores();
177:         if (!empty($stores)) {
178:             $condition = array('review_id = ?' => $object->getId());
179:             $adapter->delete($this->_reviewStoreTable, $condition);
180: 
181:             $insertedStoreIds = array();
182:             foreach ($stores as $storeId) {
183:                 if (in_array($storeId, $insertedStoreIds)) {
184:                     continue;
185:                 }
186: 
187:                 $insertedStoreIds[] = $storeId;
188:                 $storeInsert = array(
189:                     'store_id' => $storeId,
190:                     'review_id'=> $object->getId()
191:                 );
192:                 $adapter->insert($this->_reviewStoreTable, $storeInsert);
193:             }
194:         }
195: 
196:         // reaggregate ratings, that depend on this review
197:         $this->_aggregateRatings(
198:             $this->_loadVotedRatingIds($object->getId()),
199:             $object->getEntityPkValue()
200:         );
201: 
202:         return $this;
203:     }
204: 
205:     /**
206:      * Perform actions after object load
207:      *
208:      * @param Varien_Object $object
209:      * @return Mage_Review_Model_Resource_Review
210:      */
211:     protected function _afterLoad(Mage_Core_Model_Abstract $object)
212:     {
213:         $adapter = $this->_getReadAdapter();
214:         $select = $adapter->select()
215:             ->from($this->_reviewStoreTable, array('store_id'))
216:             ->where('review_id = :review_id');
217:         $stores = $adapter->fetchCol($select, array(':review_id' => $object->getId()));
218:         if (empty($stores) && Mage::app()->isSingleStoreMode()) {
219:             $object->setStores(array(Mage::app()->getStore(true)->getId()));
220:         } else {
221:             $object->setStores($stores);
222:         }
223:         return $this;
224:     }
225: 
226:     /**
227:      * Action before delete
228:      *
229:      * @param Mage_Core_Model_Abstract $object
230:      * @return Mage_Review_Model_Resource_Review
231:      */
232:     protected function _beforeDelete(Mage_Core_Model_Abstract $object)
233:     {
234:         // prepare rating ids, that depend on review
235:         $this->_deleteCache = array(
236:             'ratingIds'     => $this->_loadVotedRatingIds($object->getId()),
237:             'entityPkValue' => $object->getEntityPkValue()
238:         );
239:         return $this;
240:     }
241: 
242:     /**
243:      * Perform actions after object delete
244:      *
245:      * @param Mage_Core_Model_Abstract $object
246:      * @return Mage_Review_Model_Resource_Review
247:      */
248:     public function afterDeleteCommit(Mage_Core_Model_Abstract $object)
249:     {
250:         $this->aggregate($object);
251: 
252:         // reaggregate ratings, that depended on this review
253:         $this->_aggregateRatings(
254:             $this->_deleteCache['ratingIds'],
255:             $this->_deleteCache['entityPkValue']
256:         );
257:         $this->_deleteCache = array();
258: 
259:         return $this;
260:     }
261: 
262:     /**
263:      * Retrieves total reviews
264:      *
265:      * @param int $entityPkValue
266:      * @param bool $approvedOnly
267:      * @param int $storeId
268:      * @return int
269:      */
270:     public function getTotalReviews($entityPkValue, $approvedOnly = false, $storeId = 0)
271:     {
272:         $adapter = $this->_getReadAdapter();
273:         $select = $adapter->select()
274:             ->from($this->_reviewTable,
275:                 array(
276:                     'review_count' => new Zend_Db_Expr('COUNT(*)')
277:                 ))
278:             ->where("{$this->_reviewTable}.entity_pk_value = :pk_value");
279:         $bind = array(':pk_value' => $entityPkValue);
280:         if ($storeId > 0) {
281:             $select->join(array('store'=>$this->_reviewStoreTable),
282:                 $this->_reviewTable.'.review_id=store.review_id AND store.store_id = :store_id',
283:                 array());
284:             $bind[':store_id'] = (int)$storeId;
285:         }
286:         if ($approvedOnly) {
287:             $select->where("{$this->_reviewTable}.status_id = :status_id");
288:             $bind[':status_id'] = 1;
289:         }
290:         return $adapter->fetchOne($select, $bind);
291:     }
292: 
293:     /**
294:      * Aggregate
295:      *
296:      * @param Mage_Core_Model_Abstract $object
297:      */
298:     public function aggregate($object)
299:     {
300:         $readAdapter    = $this->_getReadAdapter();
301:         $writeAdapter   = $this->_getWriteAdapter();
302:         if (!$object->getEntityPkValue() && $object->getId()) {
303:             $object->load($object->getReviewId());
304:         }
305: 
306:         $ratingModel    = Mage::getModel('rating/rating');
307:         $ratingSummaries= $ratingModel->getEntitySummary($object->getEntityPkValue(), false);
308: 
309:         foreach ($ratingSummaries as $ratingSummaryObject) {
310:             if ($ratingSummaryObject->getCount()) {
311:                 $ratingSummary = round($ratingSummaryObject->getSum() / $ratingSummaryObject->getCount());
312:             } else {
313:                 $ratingSummary = $ratingSummaryObject->getSum();
314:             }
315: 
316:             $reviewsCount = $this->getTotalReviews(
317:                 $object->getEntityPkValue(),
318:                 true,
319:                 $ratingSummaryObject->getStoreId()
320:             );
321:             $select = $readAdapter->select()
322:                 ->from($this->_aggregateTable)
323:                 ->where('entity_pk_value = :pk_value')
324:                 ->where('entity_type = :entity_type')
325:                 ->where('store_id = :store_id');
326:             $bind = array(
327:                 ':pk_value'    => $object->getEntityPkValue(),
328:                 ':entity_type' => $object->getEntityId(),
329:                 ':store_id'    =>$ratingSummaryObject->getStoreId()
330:             );
331:             $oldData = $readAdapter->fetchRow($select, $bind);
332: 
333:             $data = new Varien_Object();
334: 
335:             $data->setReviewsCount($reviewsCount)
336:                 ->setEntityPkValue($object->getEntityPkValue())
337:                 ->setEntityType($object->getEntityId())
338:                 ->setRatingSummary(($ratingSummary > 0) ? $ratingSummary : 0)
339:                 ->setStoreId($ratingSummaryObject->getStoreId());
340: 
341:            $writeAdapter->beginTransaction();
342:             try {
343:                 if ($oldData['primary_id'] > 0) {
344:                     $condition = array("{$this->_aggregateTable}.primary_id = ?" => $oldData['primary_id']);
345:                     $writeAdapter->update($this->_aggregateTable, $data->getData(), $condition);
346:                 } else {
347:                     $writeAdapter->insert($this->_aggregateTable, $data->getData());
348:                 }
349:                 $writeAdapter->commit();
350:             } catch (Exception $e) {
351:                 $writeAdapter->rollBack();
352:             }
353:         }
354:     }
355: 
356:     /**
357:      * Get rating IDs from review votes
358:      *
359:      * @param int $reviewId
360:      * @return array
361:      */
362:     protected function _loadVotedRatingIds($reviewId)
363:     {
364:         $adapter = $this->_getReadAdapter();
365:         if (empty($reviewId)) {
366:             return array();
367:         }
368:         $select = $adapter->select()
369:             ->from(array('v' => $this->getTable('rating/rating_option_vote')), 'r.rating_id')
370:             ->joinInner(array('r' => $this->getTable('rating/rating')), 'v.rating_id=r.rating_id')
371:             ->where('v.review_id = :revire_id');
372:         return $adapter->fetchCol($select, array(':revire_id' => $reviewId));
373:     }
374: 
375:     /**
376:      * Aggregate this review's ratings.
377:      * Useful, when changing the review.
378:      *
379:      * @param array $ratingIds
380:      * @param int $entityPkValue
381:      * @return Mage_Review_Model_Resource_Review
382:      */
383:     protected function _aggregateRatings($ratingIds, $entityPkValue)
384:     {
385:         if ($ratingIds && !is_array($ratingIds)) {
386:             $ratingIds = array((int)$ratingIds);
387:         }
388:         if ($ratingIds && $entityPkValue
389:             && ($resource = Mage::getResourceSingleton('rating/rating_option'))
390:             ) {
391:             foreach ($ratingIds as $ratingId) {
392:                 $resource->aggregateEntityByRatingId(
393:                     $ratingId, $entityPkValue
394:                 );
395:             }
396:         }
397:         return $this;
398:     }
399: 
400:     /**
401:      * Reaggregate this review's ratings.
402:      *
403:      * @param int $reviewId
404:      * @param int $entityPkValue
405:      */
406:     public function reAggregateReview($reviewId, $entityPkValue)
407:     {
408:         $this->_aggregateRatings($this->_loadVotedRatingIds($reviewId), $entityPkValue);
409:     }
410: 
411:     /**
412:      * Get review entity type id by code
413:      *
414:      * @param string $entityCode
415:      * @return int|bool
416:      */
417:     public function getEntityIdByCode($entityCode)
418:     {
419:         $adapter = $this->_getReadAdapter();
420:         $select = $adapter->select()
421:             ->from($this->_reviewEntityTable, array('entity_id'))
422:             ->where('entity_code = :entity_code');
423:         return $adapter->fetchOne($select, array(':entity_code' => $entityCode));
424:     }
425: 
426:     /**
427:      * Delete reviews by product id.
428:      * Better to call this method in transaction, because operation performed on two separated tables
429:      *
430:      * @param int $productId
431:      * @return Mage_Review_Model_Resource_Review
432:      */
433:     public function deleteReviewsByProductId($productId)
434:     {
435:         $this->_getWriteAdapter()->delete($this->_reviewTable, array(
436:             'entity_pk_value=?' => $productId,
437:             'entity_id=?' => $this->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE)
438:         ));
439:         $this->_getWriteAdapter()->delete($this->getTable('review/review_aggregate'), array(
440:             'entity_pk_value=?' => $productId,
441:             'entity_type=?' => $this->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE)
442:         ));
443:         return $this;
444:     }
445: }
446: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0