custom/plugins/ProxaAskQuestion/src/Storefront/Controller/AskQuestionController.php line 73

Open in your IDE?
  1. <?php
  2. namespace ProxaAskQuestion\Storefront\Controller;
  3. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  4. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Controller\StorefrontController;
  13. use Shopware\Storefront\Page\Product\ProductPageLoader;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @RouteScope(scopes={"storefront"})
  19.  */
  20. class AskQuestionController extends StorefrontController
  21. {
  22.     /**
  23.      * @var \Shopware\Core\System\SystemConfig\SystemConfigService
  24.      */
  25.     private SystemConfigService $systemConfigService;
  26.     /**
  27.      * @var \Shopware\Core\Content\Mail\Service\AbstractMailService
  28.      */
  29.     private AbstractMailService $mailService;
  30.     /**
  31.      * @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
  32.      */
  33.     private EntityRepositoryInterface $repositoryMailTemplate;
  34.     /**
  35.      * @var \Shopware\Storefront\Page\Product\ProductPageLoader
  36.      */
  37.     private ProductPageLoader $productPageLoader;
  38.     /**
  39.      * @param \Shopware\Core\System\SystemConfig\SystemConfigService $systemConfigService
  40.      * @param \Shopware\Core\Content\Mail\Service\AbstractMailService $mailService
  41.      * @param \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface $repositoryMailTemplate
  42.      * @param \Shopware\Storefront\Page\Product\ProductPageLoader $productPageLoader
  43.      */
  44.     public function __construct(
  45.         SystemConfigService       $systemConfigService,
  46.         AbstractMailService       $mailService,
  47.         EntityRepositoryInterface $repositoryMailTemplate,
  48.         ProductPageLoader         $productPageLoader
  49.     )
  50.     {
  51.         $this->systemConfigService $systemConfigService;
  52.         $this->mailService $mailService;
  53.         $this->repositoryMailTemplate $repositoryMailTemplate;
  54.         $this->productPageLoader $productPageLoader;
  55.     }
  56.     /**
  57.      * @Route("/ask-question/{productId}", name="frontend.proxa.AskQuestion", methods={"GET","POST"})
  58.      */
  59.     public function askQuestion(Request $requestSalesChannelContext $context): Response
  60.     {
  61.         $success null;
  62.         $page $this->productPageLoader->load($request$context);
  63.         if ($request->getMethod() == 'POST') {
  64.             $success $this->submitAction($request$context$page->getProduct());
  65.         }
  66.         return $this->renderStorefront('@ProxaAskQuestion/storefront/page/ask-question.html.twig', [
  67.             'page' => $page,
  68.             'success' => $success
  69.         ]);
  70.     }
  71.     private function submitAction(Request $requestSalesChannelContext $context, ?ProductEntity $product): bool
  72.     {
  73.         $data $request->request->all();
  74.         $data['product'] = $product->getVars();
  75.         unset($data['_csrf_token']);
  76.         if (!$mailTemplate $this->getMailTemplate($context)) {
  77.             return false;
  78.         }
  79.         try {
  80.             $this->sendMail($context$mailTemplate$data);
  81.             return true;
  82.         } catch (\Exception $exception) {
  83.             //TODO log
  84.         }
  85.         return false;
  86.     }
  87.     private function getMailTemplate(SalesChannelContext $context): ?MailTemplateEntity
  88.     {
  89.         $mailTemplateId $this->systemConfigService->getDomain('ProxaAskQuestion')['ProxaAskQuestion.config.mailTemplateID'] ?? null;
  90.         if (!$mailTemplateId) return null;
  91.         return $this->repositoryMailTemplate->search(new Criteria([$mailTemplateId]), $context->getContext())->first();
  92.     }
  93.     private function sendMail(SalesChannelContext $contextMailTemplateEntity $mailTemplate, array $templateData)
  94.     {
  95.         $data = new DataBag();
  96.         $data->set('recipients', [$this->systemConfigService->get('core.basicInformation.email'$context->getSalesChannel()->getId())]);
  97.         $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  98.         $data->set('salesChannelId'$context->getSalesChannelId());
  99.         $data->set('templateId'$mailTemplate->getId());
  100.         $data->set('customFields'$mailTemplate->getCustomFields());
  101.         $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  102.         $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  103.         $data->set('subject'$mailTemplate->getTranslation('subject'));
  104.         $data->set('mediaIds', []);
  105.         $this->mailService->send($data->all(), $context->getContext(), $templateData);
  106.     }
  107. }