PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Rating/Model/Resource/Rating/Collection.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 260 lines | 160 code | 20 blank | 80 comment | 13 complexity | c534ff40ef290d3c56f0af910c449e92 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Rating
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Rating collection resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Rating
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Rating_Model_Resource_Rating_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * @var bool
  37. */
  38. protected $_isStoreJoined = false;
  39. /**
  40. * Resource initialization
  41. *
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init('rating/rating');
  46. }
  47. /**
  48. * Add entity filter
  49. *
  50. * @param int|string $entity
  51. * @return Mage_Rating_Model_Resource_Rating_Collection
  52. */
  53. public function addEntityFilter($entity)
  54. {
  55. $adapter = $this->getConnection();
  56. $this->getSelect()
  57. ->join($this->getTable('rating_entity'),
  58. 'main_table.entity_id=' . $this->getTable('rating_entity') . '.entity_id',
  59. array('entity_code'));
  60. if (is_numeric($entity)) {
  61. $this->addFilter('entity',
  62. $adapter->quoteInto($this->getTable('rating_entity') . '.entity_id=?', $entity),
  63. 'string');
  64. } elseif (is_string($entity)) {
  65. $this->addFilter('entity',
  66. $adapter->quoteInto($this->getTable('rating_entity') . '.entity_code=?', $entity),
  67. 'string');
  68. }
  69. return $this;
  70. }
  71. /**
  72. * set order by position field
  73. *
  74. * @param string $dir
  75. * @return Mage_Rating_Model_Resource_Rating_Collection
  76. */
  77. public function setPositionOrder($dir='ASC')
  78. {
  79. $this->setOrder('main_table.position', $dir);
  80. return $this;
  81. }
  82. /**
  83. * Set store filter
  84. *
  85. * @param int_type $storeId
  86. * @return Mage_Rating_Model_Resource_Rating_Collection
  87. */
  88. public function setStoreFilter($storeId)
  89. {
  90. $adapter = $this->getConnection();
  91. if (!is_array($storeId)) {
  92. $storeId = array($storeId === null ? -1 : $storeId);
  93. }
  94. if (empty($storeId)) {
  95. return $this;
  96. }
  97. if (!$this->_isStoreJoined) {
  98. $this->getSelect()
  99. ->distinct(true)
  100. ->join(
  101. array('store'=>$this->getTable('rating_store')),
  102. 'main_table.rating_id = store.rating_id',
  103. array())
  104. // ->group('main_table.rating_id')
  105. ;
  106. $this->_isStoreJoined = true;
  107. }
  108. $inCond = $adapter->prepareSqlCondition('store.store_id', array(
  109. 'in' => $storeId
  110. ));
  111. $this->getSelect()
  112. ->where($inCond);
  113. $this->setPositionOrder();
  114. return $this;
  115. }
  116. /**
  117. * Add options to ratings in collection
  118. *
  119. * @return Mage_Rating_Model_Resource_Rating_Collection
  120. */
  121. public function addOptionToItems()
  122. {
  123. $arrRatingId = $this->getColumnValues('rating_id');
  124. if (!empty($arrRatingId)) {
  125. $collection = Mage::getResourceModel('rating/rating_option_collection')
  126. ->addRatingFilter($arrRatingId)
  127. ->setPositionOrder()
  128. ->load();
  129. foreach ($this as $rating) {
  130. $rating->setOptions($collection->getItemsByColumnValue('rating_id', $rating->getId()));
  131. }
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Add entity summary to item
  137. *
  138. * @param int $entityPkValue
  139. * @param int $storeId
  140. * @return Mage_Rating_Model_Resource_Rating_Collection
  141. */
  142. public function addEntitySummaryToItem($entityPkValue, $storeId)
  143. {
  144. $arrRatingId = $this->getColumnValues('rating_id');
  145. if (count($arrRatingId) == 0) {
  146. return $this;
  147. }
  148. $adapter = $this->getConnection();
  149. $inCond = $adapter->prepareSqlCondition('rating_option_vote.rating_id', array(
  150. 'in' => $arrRatingId
  151. ));
  152. $sumCond = new Zend_Db_Expr("SUM(rating_option_vote.{$adapter->quoteIdentifier('percent')})");
  153. $countCond = new Zend_Db_Expr('COUNT(*)');
  154. $select = $adapter->select()
  155. ->from(array('rating_option_vote' => $this->getTable('rating/rating_option_vote')),
  156. array(
  157. 'rating_id' => 'rating_option_vote.rating_id',
  158. 'sum' => $sumCond,
  159. 'count' => $countCond
  160. ))
  161. ->join(
  162. array('review_store' => $this->getTable('review/review_store')),
  163. 'rating_option_vote.review_id=review_store.review_id AND review_store.store_id = :store_id',
  164. array())
  165. ->join(
  166. array('rst' => $this->getTable('rating/rating_store')),
  167. 'rst.rating_id = rating_option_vote.rating_id AND rst.store_id = :rst_store_id',
  168. array())
  169. ->join(array('review' => $this->getTable('review/review')),
  170. 'review_store.review_id=review.review_id AND review.status_id=1',
  171. array())
  172. ->where($inCond)
  173. ->where('rating_option_vote.entity_pk_value=:pk_value')
  174. ->group('rating_option_vote.rating_id');
  175. $bind = array(
  176. ':store_id' => (int)$storeId,
  177. ':rst_store_id' => (int)$storeId,
  178. ':pk_value' => $entityPkValue
  179. );
  180. $data = $this->getConnection()->fetchAll($select, $bind);
  181. foreach ($data as $item) {
  182. $rating = $this->getItemById($item['rating_id']);
  183. if ($rating && $item['count']>0) {
  184. $rating->setSummary($item['sum']/$item['count']);
  185. }
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Add rating store name
  191. *
  192. * @param int $storeId
  193. * @return Mage_Rating_Model_Resource_Rating_Collection
  194. */
  195. public function addRatingPerStoreName($storeId)
  196. {
  197. $adapter = $this->getConnection();
  198. $ratingCodeCond = $adapter->getIfNullSql('title.value', 'main_table.rating_code');
  199. $this->getSelect()
  200. ->joinLeft(array('title' => $this->getTable('rating_title')),
  201. $adapter->quoteInto('main_table.rating_id=title.rating_id AND title.store_id = ?', (int) $storeId),
  202. array('rating_code' => $ratingCodeCond));
  203. return $this;
  204. }
  205. /**
  206. * Add stores to collection
  207. *
  208. * @return Mage_Rating_Model_Resource_Rating_Collection
  209. */
  210. public function addStoresToCollection()
  211. {
  212. if (!$this->_isCollectionLoaded) {
  213. return $this;
  214. }
  215. $ratingIds = array();
  216. foreach ($this as $item) {
  217. $ratingIds[] = $item->getId();
  218. $item->setStores(array());
  219. }
  220. if (!$ratingIds) {
  221. return $this;
  222. }
  223. $adapter = $this->getConnection();
  224. $inCond = $adapter->prepareSqlCondition('rating_id', array(
  225. 'in' => $ratingIds
  226. ));
  227. $this->_select = $adapter
  228. ->select()
  229. ->from($this->getTable('rating_store'))
  230. ->where($inCond);
  231. $data = $adapter->fetchAll($this->_select);
  232. if (is_array($data) && count($data) > 0) {
  233. foreach ($data as $row) {
  234. $item = $this->getItemById($row['rating_id']);
  235. $item->setStores(array_merge($item->getStores(), array($row['store_id'])));
  236. }
  237. }
  238. return $this;
  239. }
  240. }