Send.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SendFriend\Block;
  7. use Magento\Captcha\Block\Captcha;
  8. use Magento\Customer\Model\Context;
  9. /**
  10. * Email to a Friend Block
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Send extends \Magento\Framework\View\Element\Template
  17. {
  18. /**
  19. * SendFriend data
  20. *
  21. * @var \Magento\SendFriend\Helper\Data
  22. */
  23. protected $_sendfriendData = null;
  24. /**
  25. * Core registry
  26. *
  27. * @var \Magento\Framework\Registry
  28. */
  29. protected $_coreRegistry = null;
  30. /**
  31. * @var \Magento\Customer\Model\Session
  32. */
  33. protected $_customerSession;
  34. /**
  35. * @var \Magento\Framework\App\Http\Context
  36. */
  37. protected $httpContext;
  38. /**
  39. * @var \Magento\Customer\Helper\View
  40. */
  41. protected $_customerViewHelper;
  42. /**
  43. * @var \Magento\SendFriend\Model\SendFriend
  44. */
  45. protected $sendfriend;
  46. /**
  47. * @param \Magento\Framework\View\Element\Template\Context $context
  48. * @param \Magento\Customer\Model\Session $customerSession
  49. * @param \Magento\SendFriend\Helper\Data $sendfriendData
  50. * @param \Magento\Framework\Registry $registry
  51. * @param \Magento\Customer\Helper\View $customerViewHelper
  52. * @param \Magento\Framework\App\Http\Context $httpContext
  53. * @param \Magento\SendFriend\Model\SendFriend $sendfriend
  54. * @param array $data
  55. */
  56. public function __construct(
  57. \Magento\Framework\View\Element\Template\Context $context,
  58. \Magento\Customer\Model\Session $customerSession,
  59. \Magento\SendFriend\Helper\Data $sendfriendData,
  60. \Magento\Framework\Registry $registry,
  61. \Magento\Customer\Helper\View $customerViewHelper,
  62. \Magento\Framework\App\Http\Context $httpContext,
  63. \Magento\SendFriend\Model\SendFriend $sendfriend,
  64. array $data = []
  65. ) {
  66. $this->_customerSession = $customerSession;
  67. $this->_coreRegistry = $registry;
  68. $this->_sendfriendData = $sendfriendData;
  69. $this->sendfriend = $sendfriend;
  70. parent::__construct($context, $data);
  71. $this->_isScopePrivate = true;
  72. $this->httpContext = $httpContext;
  73. $this->_customerViewHelper = $customerViewHelper;
  74. }
  75. /**
  76. * Retrieve username for form field
  77. *
  78. * @return string
  79. */
  80. public function getUserName()
  81. {
  82. $name = $this->getFormData()->getData('sender/name');
  83. if (!empty($name)) {
  84. return trim($name);
  85. }
  86. /* @var $session \Magento\Customer\Model\Session */
  87. $session = $this->_customerSession;
  88. if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
  89. return $this->_customerViewHelper->getCustomerName(
  90. $session->getCustomerDataObject()
  91. );
  92. }
  93. return '';
  94. }
  95. /**
  96. * Retrieve sender email address
  97. *
  98. * @return string
  99. */
  100. public function getEmail()
  101. {
  102. $email = $this->getFormData()->getData('sender/email');
  103. if (!empty($email)) {
  104. return trim($email);
  105. }
  106. /* @var $session \Magento\Customer\Model\Session */
  107. $session = $this->_customerSession;
  108. if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
  109. return $session->getCustomerDataObject()->getEmail();
  110. }
  111. return '';
  112. }
  113. /**
  114. * Retrieve Message text
  115. *
  116. * @return string
  117. */
  118. public function getMessage()
  119. {
  120. return $this->getFormData()->getData('sender/message');
  121. }
  122. /**
  123. * Retrieve Form data or empty \Magento\Framework\DataObject
  124. *
  125. * @return \Magento\Framework\DataObject
  126. */
  127. public function getFormData()
  128. {
  129. $data = $this->getData('form_data');
  130. if (!$data instanceof \Magento\Framework\DataObject) {
  131. $data = new \Magento\Framework\DataObject();
  132. $this->setData('form_data', $data);
  133. }
  134. return $data;
  135. }
  136. /**
  137. * Set Form data array
  138. *
  139. * @param array $data
  140. * @return $this
  141. */
  142. public function setFormData($data)
  143. {
  144. if (is_array($data)) {
  145. $this->setData('form_data', new \Magento\Framework\DataObject($data));
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Retrieve Current Product Id
  151. *
  152. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  153. * @return int
  154. */
  155. public function getProductId()
  156. {
  157. return $this->getRequest()->getParam('id', null);
  158. }
  159. /**
  160. * Retrieve current category id for product
  161. *
  162. * @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
  163. * @return int
  164. */
  165. public function getCategoryId()
  166. {
  167. return $this->getRequest()->getParam('cat_id', null);
  168. }
  169. /**
  170. * Retrieve Max Recipients
  171. *
  172. * @return int
  173. */
  174. public function getMaxRecipients()
  175. {
  176. return $this->_sendfriendData->getMaxRecipients();
  177. }
  178. /**
  179. * Retrieve Send URL for Form Action
  180. *
  181. * @return string
  182. */
  183. public function getSendUrl()
  184. {
  185. return $this->getUrl(
  186. 'sendfriend/product/sendmail',
  187. [
  188. 'id' => $this->getProductId(),
  189. 'cat_id' => $this->getCategoryId(),
  190. ]
  191. );
  192. }
  193. /**
  194. * Check if user is allowed to send
  195. *
  196. * @return boolean
  197. */
  198. public function canSend()
  199. {
  200. return !$this->sendfriend->isExceedLimit();
  201. }
  202. /**
  203. * @inheritdoc
  204. * @since 100.3.1
  205. */
  206. protected function _prepareLayout()
  207. {
  208. if (!$this->getChildBlock('captcha')) {
  209. $this->addChild(
  210. 'captcha',
  211. Captcha::class,
  212. [
  213. 'cacheable' => false,
  214. 'after' => '-',
  215. 'form_id' => 'product_sendtofriend_form',
  216. 'image_width' => 230,
  217. 'image_height' => 230
  218. ]
  219. );
  220. }
  221. }
  222. }