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 Product Collection
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Review
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Review_Model_Resource_Review_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
 36: {
 37:     /**
 38:      * Entities alias
 39:      *
 40:      * @var array
 41:      */
 42:     protected $_entitiesAlias        = array();
 43: 
 44:     /**
 45:      * Review store table
 46:      *
 47:      * @var string
 48:      */
 49:     protected $_reviewStoreTable;
 50: 
 51:     /**
 52:      * Add store data flag
 53:      *
 54:      * @var boolean
 55:      */
 56:     protected $_addStoreDataFlag     = false;
 57: 
 58: 
 59:     /**
 60:      * Filter by stores for the collection
 61:      *
 62:      * @var array
 63:      */
 64:     protected $_storesIds           = array();
 65: 
 66:     /**
 67:      * Define module
 68:      *
 69:      */
 70:     protected function _construct()
 71:     {
 72:         $this->_init('catalog/product');
 73:         $this->setRowIdFieldName('review_id');
 74:         $this->_reviewStoreTable = Mage::getSingleton('core/resource')->getTableName('review/review_store');
 75:         $this->_initTables();
 76:     }
 77: 
 78:     /**
 79:      * init select
 80:      *
 81:      * @return Mage_Review_Model_Resource_Review_Product_Collection
 82:      */
 83:     protected function _initSelect()
 84:     {
 85:         parent::_initSelect();
 86:         $this->_joinFields();
 87:         return $this;
 88:     }
 89: 
 90:     /**
 91:      * Adds store filter into array
 92:      *
 93:      * @param mixed $storeId
 94:      * @return Mage_Review_Model_Resource_Review_Product_Collection
 95:      */
 96:     public function addStoreFilter($storeId = null)
 97:     {
 98:         if (is_null($storeId)) {
 99:             $storeId = $this->getStoreId();
100:         }
101: 
102:         parent::addStoreFilter($storeId);
103: 
104:         if (!is_array($storeId)) {
105:             $storeId = array($storeId);
106:         }
107: 
108:         if (!empty($this->_storesIds)) {
109:             $this->_storesIds = array_intersect($this->_storesIds, $storeId);
110:         } else {
111:             $this->_storesIds = $storeId;
112:         }
113: 
114:         return $this;
115:     }
116: 
117:     /**
118:      * Adds specific store id into array
119:      *
120:      * @param array $storeId
121:      * @return Mage_Review_Model_Resource_Review_Product_Collection
122:      */
123:     public function setStoreFilter($storeId)
124:     {
125:         if (is_array($storeId) && isset($storeId['eq'])) {
126:             $storeId = array_shift($storeId);
127:         }
128: 
129:         if (!is_array($storeId)){
130:             $storeId = array($storeId);
131:         }
132: 
133:         if (!empty($this->_storesIds)){
134:             $this->_storesIds = array_intersect($this->_storesIds, $storeId);
135:         } else {
136:             $this->_storesIds = $storeId;
137:         }
138: 
139:         return $this;
140:     }
141: 
142:     /**
143:      * Applies all store filters in one place to prevent multiple joins in select
144:      *
145:      * @param null|Zend_Db_Select $select
146:      * @return Mage_Review_Model_Resource_Review_Product_Collection
147:      */
148:     protected function _applyStoresFilterToSelect(Zend_Db_Select $select = null)
149:     {
150:         $adapter = $this->getConnection();
151:         $storesIds = $this->_storesIds;
152:         if (is_null($select)){
153:             $select = $this->getSelect();
154:         }
155: 
156:         if (is_array($storesIds) && (count($storesIds) == 1)) {
157:             $storesIds = array_shift($storesIds);
158:         }
159: 
160:         if (is_array($storesIds) && !empty($storesIds)) {
161:             $inCond = $adapter->prepareSqlCondition('store.store_id', array('in' => $storesIds));
162:             $select->join(array('store' => $this->_reviewStoreTable),
163:                 'rt.review_id=store.review_id AND ' . $inCond,
164:                 array())
165:             ->group('rt.review_id');
166: 
167:             $this->_useAnalyticFunction = true;
168:         } else {
169:             $select->join(array('store' => $this->_reviewStoreTable),
170:                 $adapter->quoteInto('rt.review_id=store.review_id AND store.store_id = ?', (int)$storesIds),
171:                 array());
172:         }
173: 
174:         return $this;
175:     }
176: 
177:     /**
178:      * Add stores data
179:      *
180:      * @return Mage_Review_Model_Resource_Review_Product_Collection
181:      */
182:     public function addStoreData()
183:     {
184:         $this->_addStoreDataFlag = true;
185:         return $this;
186:     }
187: 
188:     /**
189:      * Add customer filter
190:      *
191:      * @param int $customerId
192:      * @return Mage_Review_Model_Resource_Review_Product_Collection
193:      */
194:     public function addCustomerFilter($customerId)
195:     {
196:         $this->getSelect()
197:             ->where('rdt.customer_id = ?', $customerId);
198:         return $this;
199:     }
200: 
201:     /**
202:      * Add entity filter
203:      *
204:      * @param int $entityId
205:      * @return Mage_Review_Model_Resource_Review_Product_Collection
206:      */
207:     public function addEntityFilter($entityId)
208:     {
209:         $this->getSelect()
210:             ->where('rt.entity_pk_value = ?', $entityId);
211:         return $this;
212:     }
213: 
214:     /**
215:      * Add status filter
216:      *
217:      * @param mixed $status
218:      * @return Mage_Review_Model_Resource_Review_Product_Collection
219:      */
220:     public function addStatusFilter($status)
221:     {
222:         $this->getSelect()
223:             ->where('rt.status_id = ?', $status);
224:         return $this;
225:     }
226: 
227:     /**
228:      * Set date order
229:      *
230:      * @param string $dir
231:      * @return Mage_Review_Model_Resource_Review_Product_Collection
232:      */
233:     public function setDateOrder($dir = 'DESC')
234:     {
235:         $this->setOrder('rt.created_at', $dir);
236:         return $this;
237:     }
238: 
239:     /**
240:      * Add review summary
241:      *
242:      * @return Mage_Review_Model_Resource_Review_Product_Collection
243:      */
244:     public function addReviewSummary()
245:     {
246:         foreach ($this->getItems() as $item) {
247:             $model = Mage::getModel('rating/rating');
248:             $model->getReviewSummary($item->getReviewId());
249:             $item->addData($model->getData());
250:         }
251:         return $this;
252:     }
253: 
254:     /**
255:      * Add rote votes
256:      *
257:      * @return Mage_Review_Model_Resource_Review_Product_Collection
258:      */
259:     public function addRateVotes()
260:     {
261:         foreach ($this->getItems() as $item) {
262:             $votesCollection = Mage::getModel('rating/rating_option_vote')
263:                 ->getResourceCollection()
264:                 ->setEntityPkFilter($item->getEntityId())
265:                 ->setStoreFilter(Mage::app()->getStore()->getId())
266:                 ->load();
267:             $item->setRatingVotes($votesCollection);
268:         }
269:         return $this;
270:     }
271: 
272:     /**
273:      * join fields to entity
274:      *
275:      * @return Mage_Review_Model_Resource_Review_Product_Collection
276:      */
277:     protected function _joinFields()
278:     {
279:         $reviewTable = Mage::getSingleton('core/resource')->getTableName('review/review');
280:         $reviewDetailTable = Mage::getSingleton('core/resource')->getTableName('review/review_detail');
281: 
282:         $this->addAttributeToSelect('name')
283:             ->addAttributeToSelect('sku');
284: 
285:         $this->getSelect()
286:             ->join(array('rt' => $reviewTable),
287:                 'rt.entity_pk_value = e.entity_id',
288:                 array('rt.review_id', 'review_created_at'=> 'rt.created_at', 'rt.entity_pk_value', 'rt.status_id'))
289:             ->join(array('rdt' => $reviewDetailTable),
290:                 'rdt.review_id = rt.review_id',
291:                 array('rdt.title','rdt.nickname', 'rdt.detail', 'rdt.customer_id', 'rdt.store_id'));
292:         return $this;
293:     }
294: 
295:     /**
296:      * Retrive all ids for collection
297:      *
298:      * @param unknown_type $limit
299:      * @param unknown_type $offset
300:      * @return array
301:      */
302:     public function getAllIds($limit = null, $offset = null)
303:     {
304:         $idsSelect = clone $this->getSelect();
305:         $idsSelect->reset(Zend_Db_Select::ORDER);
306:         $idsSelect->reset(Zend_Db_Select::LIMIT_COUNT);
307:         $idsSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
308:         $idsSelect->reset(Zend_Db_Select::COLUMNS);
309:         $idsSelect->columns('rt.review_id');
310:         return $this->getConnection()->fetchCol($idsSelect);
311:     }
312: 
313:     /**
314:      * Render SQL for retrieve product count
315:      *
316:      * @return string
317:      */
318:     public function getSelectCountSql()
319:     {
320:         $select = parent::getSelectCountSql();
321:         $select->reset(Zend_Db_Select::COLUMNS)
322:             ->columns('COUNT(e.entity_id)')
323:             ->reset(Zend_Db_Select::HAVING);
324: 
325:         return $select;
326:     }
327: 
328:     /**
329:      * Set order to attribute
330:      *
331:      * @param string $attribute
332:      * @param string $dir
333:      * @return Mage_Review_Model_Resource_Review_Product_Collection
334:      */
335:     public function setOrder($attribute, $dir = 'DESC')
336:     {
337:         switch($attribute) {
338:             case 'rt.review_id':
339:             case 'rt.created_at':
340:             case 'rt.status_id':
341:             case 'rdt.title':
342:             case 'rdt.nickname':
343:             case 'rdt.detail':
344:                 $this->getSelect()->order($attribute . ' ' . $dir);
345:                 break;
346:             case 'stores':
347:                 // No way to sort
348:                 break;
349:             case 'type':
350:                 $this->getSelect()->order('rdt.customer_id ' . $dir);
351:                 break;
352:             default:
353:                 parent::setOrder($attribute, $dir);
354:         }
355:         return $this;
356:     }
357: 
358:     /**
359:      * Add attribute to filter
360:      *
361:      * @param Mage_Eav_Model_Entity_Attribute_Abstract|string $attribute
362:      * @param array $condition
363:      * @param string $joinType
364:      * @return Mage_Review_Model_Resource_Review_Product_Collection
365:      */
366:     public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')
367:     {
368:         switch($attribute) {
369:             case 'rt.review_id':
370:             case 'rt.created_at':
371:             case 'rt.status_id':
372:             case 'rdt.title':
373:             case 'rdt.nickname':
374:             case 'rdt.detail':
375:                 $conditionSql = $this->_getConditionSql($attribute, $condition);
376:                 $this->getSelect()->where($conditionSql);
377:                 break;
378:             case 'stores':
379:                 $this->setStoreFilter($condition);
380:                 break;
381:             case 'type':
382:                 if ($condition == 1) {
383:                     $conditionParts = array(
384:                         $this->_getConditionSql('rdt.customer_id', array('is' => new Zend_Db_Expr('NULL'))),
385:                         $this->_getConditionSql('rdt.store_id', array('eq' => Mage_Core_Model_App::ADMIN_STORE_ID))
386:                     );
387:                     $conditionSql = implode(' AND ', $conditionParts);
388:                 } elseif ($condition == 2) {
389:                     $conditionSql = $this->_getConditionSql('rdt.customer_id', array('gt' => 0));
390:                 } else {
391:                     $conditionParts = array(
392:                         $this->_getConditionSql('rdt.customer_id', array('is' => new Zend_Db_Expr('NULL'))),
393:                         $this->_getConditionSql('rdt.store_id', array('neq' => Mage_Core_Model_App::ADMIN_STORE_ID))
394:                     );
395:                     $conditionSql = implode(' AND ', $conditionParts);
396:                 }
397:                 $this->getSelect()->where($conditionSql);
398:                 break;
399: 
400:             default:
401:                 parent::addAttributeToFilter($attribute, $condition, $joinType);
402:                 break;
403:         }
404:         return $this;
405:     }
406: 
407:     /**
408:      * Retrieves column values
409:      *
410:      * @param string $colName
411:      * @return array
412:      */
413:     public function getColumnValues($colName)
414:     {
415:         $col = array();
416:         foreach ($this->getItems() as $item) {
417:             $col[] = $item->getData($colName);
418:         }
419:         return $col;
420:     }
421: 
422:     /**
423:      * Action after load
424:      *
425:      * @return Mage_Review_Model_Resource_Review_Product_Collection
426:      */
427:     protected function _afterLoad()
428:     {
429:         parent::_afterLoad();
430:         if ($this->_addStoreDataFlag) {
431:             $this->_addStoreData();
432:         }
433:         return $this;
434:     }
435: 
436:     /**
437:      * Add store data
438:      *
439:      */
440:     protected function _addStoreData()
441:     {
442:         $adapter = $this->getConnection();
443:         //$this->_getConditionSql('rdt.customer_id', array('null' => null));
444:         $reviewsIds = $this->getColumnValues('review_id');
445:         $storesToReviews = array();
446:         if (count($reviewsIds)>0) {
447:             $reviewIdCondition = $this->_getConditionSql('review_id', array('in' => $reviewsIds));
448:             $storeIdCondition = $this->_getConditionSql('store_id', array('gt' => 0));
449:             $select = $adapter->select()
450:                 ->from($this->_reviewStoreTable)
451:                 ->where($reviewIdCondition)
452:                 ->where($storeIdCondition);
453:             $result = $adapter->fetchAll($select);
454:             foreach ($result as $row) {
455:                 if (!isset($storesToReviews[$row['review_id']])) {
456:                     $storesToReviews[$row['review_id']] = array();
457:                 }
458:                 $storesToReviews[$row['review_id']][] = $row['store_id'];
459:             }
460:         }
461: 
462:         foreach ($this as $item) {
463:             if (isset($storesToReviews[$item->getReviewId()])) {
464:                 $item->setData('stores', $storesToReviews[$item->getReviewId()]);
465:             } else {
466:                 $item->setData('stores', array());
467:             }
468: 
469:         }
470:     }
471: 
472:     /**
473:      * Redeclare parent method for store filters applying
474:      *
475:      * @return Mage_Review_Model_Resource_Review_Product_Collection
476:      */
477:     protected function _beforeLoad()
478:     {
479:         parent::_beforeLoad();
480:         $this->_applyStoresFilterToSelect();
481: 
482:         return $this;
483:     }
484: }
485: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0