Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Review\Block;
7 
12 
22 {
28  protected $_reviewData = null;
29 
35  protected $productRepository;
36 
42  protected $_ratingFactory;
43 
47  protected $urlEncoder;
48 
54  protected $messageManager;
55 
59  protected $httpContext;
60 
64  protected $customerUrl;
65 
69  protected $jsLayout;
70 
74  private $serializer;
75 
92  public function __construct(
93  \Magento\Framework\View\Element\Template\Context $context,
94  \Magento\Framework\Url\EncoderInterface $urlEncoder,
95  \Magento\Review\Helper\Data $reviewData,
97  \Magento\Review\Model\RatingFactory $ratingFactory,
98  \Magento\Framework\Message\ManagerInterface $messageManager,
99  \Magento\Framework\App\Http\Context $httpContext,
100  \Magento\Customer\Model\Url $customerUrl,
101  array $data = [],
102  \Magento\Framework\Serialize\Serializer\Json $serializer = null
103  ) {
104  $this->urlEncoder = $urlEncoder;
105  $this->_reviewData = $reviewData;
106  $this->productRepository = $productRepository;
107  $this->_ratingFactory = $ratingFactory;
108  $this->messageManager = $messageManager;
109  $this->httpContext = $httpContext;
110  $this->customerUrl = $customerUrl;
111  parent::__construct($context, $data);
112  $this->jsLayout = isset($data['jsLayout']) ? $data['jsLayout'] : [];
113  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
114  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
115  }
116 
122  protected function _construct()
123  {
124  parent::_construct();
125 
126  $this->setAllowWriteReviewFlag(
127  $this->httpContext->getValue(Context::CONTEXT_AUTH)
128  || $this->_reviewData->getIsGuestAllowToWrite()
129  );
130  if (!$this->getAllowWriteReviewFlag()) {
131  $queryParam = $this->urlEncoder->encode(
132  $this->getUrl('*/*/*', ['_current' => true]) . '#review-form'
133  );
134  $this->setLoginLink(
135  $this->getUrl(
136  'customer/account/login/',
137  [Url::REFERER_QUERY_PARAM_NAME => $queryParam]
138  )
139  );
140  }
141 
142  $this->setTemplate('Magento_Review::form.phtml');
143  }
144 
148  public function getJsLayout()
149  {
150  return $this->serializer->serialize($this->jsLayout);
151  }
152 
159  public function getProductInfo()
160  {
161  return $this->productRepository->getById(
162  $this->getProductId(),
163  false,
164  $this->_storeManager->getStore()->getId()
165  );
166  }
167 
173  public function getAction()
174  {
175  return $this->getUrl(
176  'review/product/post',
177  [
178  '_secure' => $this->getRequest()->isSecure(),
179  'id' => $this->getProductId(),
180  ]
181  );
182  }
183 
190  public function getRatings()
191  {
192  return $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter(
193  'product'
194  )->setPositionOrder()->addRatingPerStoreName(
195  $this->_storeManager->getStore()->getId()
196  )->setStoreFilter(
197  $this->_storeManager->getStore()->getId()
198  )->setActiveFilter(
199  true
200  )->load()->addOptionToItems();
201  }
202 
208  public function getRegisterUrl()
209  {
210  return $this->customerUrl->getRegisterUrl();
211  }
212 
218  protected function getProductId()
219  {
220  return $this->getRequest()->getParam('id', false);
221  }
222 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Review\Helper\Data $reviewData, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\Http\Context $httpContext, \Magento\Customer\Model\Url $customerUrl, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Form.php:92
const REFERER_QUERY_PARAM_NAME
Definition: Url.php:32