<?php
namespace ProxaAskQuestion\Storefront\Controller;
use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\Product\ProductPageLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class AskQuestionController extends StorefrontController
{
/**
* @var \Shopware\Core\System\SystemConfig\SystemConfigService
*/
private SystemConfigService $systemConfigService;
/**
* @var \Shopware\Core\Content\Mail\Service\AbstractMailService
*/
private AbstractMailService $mailService;
/**
* @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface
*/
private EntityRepositoryInterface $repositoryMailTemplate;
/**
* @var \Shopware\Storefront\Page\Product\ProductPageLoader
*/
private ProductPageLoader $productPageLoader;
/**
* @param \Shopware\Core\System\SystemConfig\SystemConfigService $systemConfigService
* @param \Shopware\Core\Content\Mail\Service\AbstractMailService $mailService
* @param \Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface $repositoryMailTemplate
* @param \Shopware\Storefront\Page\Product\ProductPageLoader $productPageLoader
*/
public function __construct(
SystemConfigService $systemConfigService,
AbstractMailService $mailService,
EntityRepositoryInterface $repositoryMailTemplate,
ProductPageLoader $productPageLoader
)
{
$this->systemConfigService = $systemConfigService;
$this->mailService = $mailService;
$this->repositoryMailTemplate = $repositoryMailTemplate;
$this->productPageLoader = $productPageLoader;
}
/**
* @Route("/ask-question/{productId}", name="frontend.proxa.AskQuestion", methods={"GET","POST"})
*/
public function askQuestion(Request $request, SalesChannelContext $context): Response
{
$success = null;
$page = $this->productPageLoader->load($request, $context);
if ($request->getMethod() == 'POST') {
$success = $this->submitAction($request, $context, $page->getProduct());
}
return $this->renderStorefront('@ProxaAskQuestion/storefront/page/ask-question.html.twig', [
'page' => $page,
'success' => $success
]);
}
private function submitAction(Request $request, SalesChannelContext $context, ?ProductEntity $product): bool
{
$data = $request->request->all();
$data['product'] = $product->getVars();
unset($data['_csrf_token']);
if (!$mailTemplate = $this->getMailTemplate($context)) {
return false;
}
try {
$this->sendMail($context, $mailTemplate, $data);
return true;
} catch (\Exception $exception) {
//TODO log
}
return false;
}
private function getMailTemplate(SalesChannelContext $context): ?MailTemplateEntity
{
$mailTemplateId = $this->systemConfigService->getDomain('ProxaAskQuestion')['ProxaAskQuestion.config.mailTemplateID'] ?? null;
if (!$mailTemplateId) return null;
return $this->repositoryMailTemplate->search(new Criteria([$mailTemplateId]), $context->getContext())->first();
}
private function sendMail(SalesChannelContext $context, MailTemplateEntity $mailTemplate, array $templateData)
{
$data = new DataBag();
$data->set('recipients', [$this->systemConfigService->get('core.basicInformation.email', $context->getSalesChannel()->getId())]);
$data->set('senderName', $mailTemplate->getTranslation('senderName'));
$data->set('salesChannelId', $context->getSalesChannelId());
$data->set('templateId', $mailTemplate->getId());
$data->set('customFields', $mailTemplate->getCustomFields());
$data->set('contentHtml', $mailTemplate->getTranslation('contentHtml'));
$data->set('contentPlain', $mailTemplate->getTranslation('contentPlain'));
$data->set('subject', $mailTemplate->getTranslation('subject'));
$data->set('mediaIds', []);
$this->mailService->send($data->all(), $context->getContext(), $templateData);
}
}