<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Customize\Event\EccubeEvents;
use Customize\Form\Type\ContactType;
use Eccube\Controller\AbstractController;
use Eccube\Event\EventArgs;
use Eccube\Service\MailService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class TopController extends AbstractController
{
/**
* @var MailService
*/
protected $mailService;
/**
* ContactController constructor.
*
* @param MailService $mailService
*/
public function __construct(MailService $mailService)
{
$this->mailService = $mailService;
}
/**
* @Route("/", name="homepage", methods={"GET","POST"})
*/
public function index(Request $request)
{
if ($this->isGranted('ROLE_USER')) {
$socketHostUrl = getenv('SOCKET_HOST_URL');
$Customer = $this->getUser();
$customerId = $Customer->getId();
$randomBytes = random_bytes(32);
$token = hash('sha256', $randomBytes);
$hashedToken = hash('sha256', $token);
$Customer->setToken($hashedToken);
$this->entityManager->persist($Customer);
$this->entityManager->flush();
return $this->render('index.twig', [
'socketHostUrl' => $socketHostUrl,
'customerId' => $customerId,
'token' => $token,
]);
} else {
// 未ログインの場合、LPを表示
$builder = $this->formFactory->createNamedBuilder('', ContactType::class);
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_TOP_INDEX_INITIALIZE);
$form = $builder->getForm();
$form->handleRequest($request);
$formResult = "";
// フォームサブミット
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$event = new EventArgs(
[
'form' => $form,
'data' => $data,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
$data = $event->getArgument('data');
// メール送信
$this->mailService->sendContactMail($data);
$formResult = "お問い合わせを送信しました。";
}
return $this->render('landing_page.twig', [
'form' => $form->createView(),
'formResult' => $formResult
]);
}
}
}