PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Review/Model/Resource/Review/Product/Collection.php

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