Review.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Block\Product;
  7. use Magento\Framework\DataObject\IdentityInterface;
  8. use Magento\Framework\View\Element\Template;
  9. /**
  10. * Product Review Tab
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Review extends Template implements IdentityInterface
  17. {
  18. /**
  19. * Core registry
  20. *
  21. * @var \Magento\Framework\Registry
  22. */
  23. protected $_coreRegistry;
  24. /**
  25. * Review resource model
  26. *
  27. * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
  28. */
  29. protected $_reviewsColFactory;
  30. /**
  31. * @param \Magento\Framework\View\Element\Template\Context $context
  32. * @param \Magento\Framework\Registry $registry
  33. * @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Framework\View\Element\Template\Context $context,
  38. \Magento\Framework\Registry $registry,
  39. \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
  40. array $data = []
  41. ) {
  42. $this->_coreRegistry = $registry;
  43. $this->_reviewsColFactory = $collectionFactory;
  44. parent::__construct($context, $data);
  45. $this->setTabTitle();
  46. }
  47. /**
  48. * Get current product id
  49. *
  50. * @return null|int
  51. */
  52. public function getProductId()
  53. {
  54. $product = $this->_coreRegistry->registry('product');
  55. return $product ? $product->getId() : null;
  56. }
  57. /**
  58. * Get URL for ajax call
  59. *
  60. * @return string
  61. */
  62. public function getProductReviewUrl()
  63. {
  64. return $this->getUrl(
  65. 'review/product/listAjax',
  66. [
  67. '_secure' => $this->getRequest()->isSecure(),
  68. 'id' => $this->getProductId(),
  69. ]
  70. );
  71. }
  72. /**
  73. * Set tab title
  74. *
  75. * @return void
  76. */
  77. public function setTabTitle()
  78. {
  79. $title = $this->getCollectionSize()
  80. ? __('Reviews %1', '<span class="counter">' . $this->getCollectionSize() . '</span>')
  81. : __('Reviews');
  82. $this->setTitle($title);
  83. }
  84. /**
  85. * Get size of reviews collection
  86. *
  87. * @return int
  88. */
  89. public function getCollectionSize()
  90. {
  91. $collection = $this->_reviewsColFactory->create()->addStoreFilter(
  92. $this->_storeManager->getStore()->getId()
  93. )->addStatusFilter(
  94. \Magento\Review\Model\Review::STATUS_APPROVED
  95. )->addEntityFilter(
  96. 'product',
  97. $this->getProductId()
  98. );
  99. return $collection->getSize();
  100. }
  101. /**
  102. * Return unique ID(s) for each object in system
  103. *
  104. * @return array
  105. */
  106. public function getIdentities()
  107. {
  108. return [\Magento\Review\Model\Review::CACHE_TAG];
  109. }
  110. }