Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Send.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\Framework\Session\Generic as WishlistSession;
15 use Magento\Framework\View\Result\Layout as ResultLayout;
16 
21 {
26 
30  protected $inlineTranslation;
31 
35  protected $_transportBuilder;
36 
40  protected $_wishlistConfig;
41 
45  protected $wishlistProvider;
46 
50  protected $_customerSession;
51 
55  protected $_formKeyValidator;
56 
60  protected $wishlistSession;
61 
65  protected $scopeConfig;
66 
70  protected $storeManager;
71 
86  public function __construct(
87  Action\Context $context,
88  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
89  \Magento\Customer\Model\Session $customerSession,
91  \Magento\Wishlist\Model\Config $wishlistConfig,
92  \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
93  \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
94  \Magento\Customer\Helper\View $customerHelperView,
95  WishlistSession $wishlistSession,
98  ) {
99  $this->_formKeyValidator = $formKeyValidator;
100  $this->_customerSession = $customerSession;
101  $this->wishlistProvider = $wishlistProvider;
102  $this->_wishlistConfig = $wishlistConfig;
103  $this->_transportBuilder = $transportBuilder;
104  $this->inlineTranslation = $inlineTranslation;
105  $this->_customerHelperView = $customerHelperView;
106  $this->wishlistSession = $wishlistSession;
107  $this->scopeConfig = $scopeConfig;
108  $this->storeManager = $storeManager;
109  parent::__construct($context);
110  }
111 
121  public function execute()
122  {
124  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
125  if (!$this->_formKeyValidator->validate($this->getRequest())) {
126  $resultRedirect->setPath('*/*/');
127  return $resultRedirect;
128  }
129 
130  $wishlist = $this->wishlistProvider->getWishlist();
131  if (!$wishlist) {
132  throw new NotFoundException(__('Page not found.'));
133  }
134 
135  $sharingLimit = $this->_wishlistConfig->getSharingEmailLimit();
136  $textLimit = $this->_wishlistConfig->getSharingTextLimit();
137  $emailsLeft = $sharingLimit - $wishlist->getShared();
138 
139  $emails = $this->getRequest()->getPost('emails');
140  $emails = empty($emails) ? $emails : explode(',', $emails);
141 
142  $error = false;
143  $message = (string)$this->getRequest()->getPost('message');
144  if (strlen($message) > $textLimit) {
145  $error = __('Message length must not exceed %1 symbols', $textLimit);
146  } else {
147  $message = nl2br(htmlspecialchars($message));
148  if (empty($emails)) {
149  $error = __('Please enter an email address.');
150  } else {
151  if (count($emails) > $emailsLeft) {
152  $error = __('This wish list can be shared %1 more times.', $emailsLeft);
153  } else {
154  foreach ($emails as $index => $email) {
155  $email = trim($email);
156  if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
157  $error = __('Please enter a valid email address.');
158  break;
159  }
160  $emails[$index] = $email;
161  }
162  }
163  }
164  }
165 
166  if ($error) {
167  $this->messageManager->addError($error);
168  $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
169  $resultRedirect->setPath('*/*/share');
170  return $resultRedirect;
171  }
173  $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
174  $this->addLayoutHandles($resultLayout);
175  $this->inlineTranslation->suspend();
176 
177  $sent = 0;
178 
179  try {
180  $customer = $this->_customerSession->getCustomerDataObject();
181  $customerName = $this->_customerHelperView->getCustomerName($customer);
182 
183  $message .= $this->getRssLink($wishlist->getId(), $resultLayout);
184  $emails = array_unique($emails);
185  $sharingCode = $wishlist->getSharingCode();
186 
187  try {
188  foreach ($emails as $email) {
189  $transport = $this->_transportBuilder->setTemplateIdentifier(
190  $this->scopeConfig->getValue(
191  'wishlist/email/email_template',
193  )
194  )->setTemplateOptions(
195  [
196  'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
197  'store' => $this->storeManager->getStore()->getStoreId(),
198  ]
199  )->setTemplateVars(
200  [
201  'customer' => $customer,
202  'customerName' => $customerName,
203  'salable' => $wishlist->isSalable() ? 'yes' : '',
204  'items' => $this->getWishlistItems($resultLayout),
205  'viewOnSiteLink' => $this->_url->getUrl('*/shared/index', ['code' => $sharingCode]),
206  'message' => $message,
207  'store' => $this->storeManager->getStore(),
208  ]
209  )->setFrom(
210  $this->scopeConfig->getValue(
211  'wishlist/email/email_identity',
213  )
214  )->addTo(
215  $email
216  )->getTransport();
217 
218  $transport->sendMessage();
219 
220  $sent++;
221  }
222  } catch (\Exception $e) {
223  $wishlist->setShared($wishlist->getShared() + $sent);
224  $wishlist->save();
225  throw $e;
226  }
227  $wishlist->setShared($wishlist->getShared() + $sent);
228  $wishlist->save();
229 
230  $this->inlineTranslation->resume();
231 
232  $this->_eventManager->dispatch('wishlist_share', ['wishlist' => $wishlist]);
233  $this->messageManager->addSuccess(__('Your wish list has been shared.'));
234  $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
235  return $resultRedirect;
236  } catch (\Exception $e) {
237  $this->inlineTranslation->resume();
238  $this->messageManager->addError($e->getMessage());
239  $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
240  $resultRedirect->setPath('*/*/share');
241  return $resultRedirect;
242  }
243  }
244 
254  protected function addLayoutHandles(ResultLayout $resultLayout)
255  {
256  if ($this->getRequest()->getParam('rss_url')) {
257  $resultLayout->addHandle('wishlist_email_rss');
258  }
259  $resultLayout->addHandle('wishlist_email_items');
260  }
261 
269  protected function getRssLink($wishlistId, ResultLayout $resultLayout)
270  {
271  if ($this->getRequest()->getParam('rss_url')) {
272  return $resultLayout->getLayout()
273  ->getBlock('wishlist.email.rss')
274  ->setWishlistId($wishlistId)
275  ->toHtml();
276  }
277  }
278 
285  protected function getWishlistItems(ResultLayout $resultLayout)
286  {
287  return $resultLayout->getLayout()
288  ->getBlock('wishlist.email.items')
289  ->toHtml();
290  }
291 }
__construct(Action\Context $context, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Wishlist\Model\Config $wishlistConfig, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Customer\Helper\View $customerHelperView, WishlistSession $wishlistSession, ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager)
Definition: Send.php:86
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
__()
Definition: __.php:13
$message
getRssLink($wishlistId, ResultLayout $resultLayout)
Definition: Send.php:269
$wishlist
Definition: wishlist.php:10
addLayoutHandles(ResultLayout $resultLayout)
Definition: Send.php:254
static is($value, $classBaseName, array $args=array(), $namespaces=array())
Definition: Validate.php:195
$index
Definition: list.phtml:44
getWishlistItems(ResultLayout $resultLayout)
Definition: Send.php:285