当前位置: 首页>>代码示例>>PHP>>正文


PHP Generic::setSharingForm方法代码示例

本文整理汇总了PHP中Magento\Framework\Session\Generic::setSharingForm方法的典型用法代码示例。如果您正苦于以下问题:PHP Generic::setSharingForm方法的具体用法?PHP Generic::setSharingForm怎么用?PHP Generic::setSharingForm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Magento\Framework\Session\Generic的用法示例。


在下文中一共展示了Generic::setSharingForm方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: execute

 /**
  * Share wishlist
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  * @throws NotFoundException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function execute()
 {
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     if (!$this->_formKeyValidator->validate($this->getRequest())) {
         $resultRedirect->setPath('*/*/');
         return $resultRedirect;
     }
     $wishlist = $this->wishlistProvider->getWishlist();
     if (!$wishlist) {
         throw new NotFoundException(__('Page not found.'));
     }
     $sharingLimit = $this->_wishlistConfig->getSharingEmailLimit();
     $textLimit = $this->_wishlistConfig->getSharingTextLimit();
     $emailsLeft = $sharingLimit - $wishlist->getShared();
     $emails = $this->getRequest()->getPost('emails');
     $emails = empty($emails) ? $emails : explode(',', $emails);
     $error = false;
     $message = (string) $this->getRequest()->getPost('message');
     if (strlen($message) > $textLimit) {
         $error = __('Message length must not exceed %1 symbols', $textLimit);
     } else {
         $message = nl2br(htmlspecialchars($message));
         if (empty($emails)) {
             $error = __('Please enter an email address.');
         } else {
             if (count($emails) > $emailsLeft) {
                 $error = __('This wish list can be shared %1 more times.', $emailsLeft);
             } else {
                 foreach ($emails as $index => $email) {
                     $email = trim($email);
                     if (!\Zend_Validate::is($email, 'EmailAddress')) {
                         $error = __('Please input a valid email address.');
                         break;
                     }
                     $emails[$index] = $email;
                 }
             }
         }
     }
     if ($error) {
         $this->messageManager->addError($error);
         $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
         $resultRedirect->setPath('*/*/share');
         return $resultRedirect;
     }
     /** @var \Magento\Framework\View\Result\Layout $resultLayout */
     $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
     $this->addLayoutHandles($resultLayout);
     $this->inlineTranslation->suspend();
     $sent = 0;
     try {
         $customer = $this->_customerSession->getCustomerDataObject();
         $customerName = $this->_customerHelperView->getCustomerName($customer);
         $message .= $this->getRssLink($wishlist->getId(), $resultLayout);
         $emails = array_unique($emails);
         $sharingCode = $wishlist->getSharingCode();
         try {
             foreach ($emails as $email) {
                 $transport = $this->_transportBuilder->setTemplateIdentifier($this->scopeConfig->getValue('wishlist/email/email_template', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getStoreId()])->setTemplateVars(['customer' => $customer, 'customerName' => $customerName, 'salable' => $wishlist->isSalable() ? 'yes' : '', 'items' => $this->getWishlistItems($resultLayout), 'viewOnSiteLink' => $this->_url->getUrl('*/shared/index', ['code' => $sharingCode]), 'message' => $message, 'store' => $this->storeManager->getStore()])->setFrom($this->scopeConfig->getValue('wishlist/email/email_identity', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))->addTo($email)->getTransport();
                 $transport->sendMessage();
                 $sent++;
             }
         } catch (\Exception $e) {
             $wishlist->setShared($wishlist->getShared() + $sent);
             $wishlist->save();
             throw $e;
         }
         $wishlist->setShared($wishlist->getShared() + $sent);
         $wishlist->save();
         $this->inlineTranslation->resume();
         $this->_eventManager->dispatch('wishlist_share', ['wishlist' => $wishlist]);
         $this->messageManager->addSuccess(__('Your wish list has been shared.'));
         $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
         return $resultRedirect;
     } catch (\Exception $e) {
         $this->inlineTranslation->resume();
         $this->messageManager->addError($e->getMessage());
         $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
         $resultRedirect->setPath('*/*/share');
         return $resultRedirect;
     }
 }
开发者ID:kid17,项目名称:magento2,代码行数:92,代码来源:Send.php


注:本文中的Magento\Framework\Session\Generic::setSharingForm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。