<?php
namespace Customize\Controller;
use Customize\Form\Type\Front\Contact\ContactTypeType;
use Customize\Service\MailService;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Front\ContactType;
use Eccube\Repository\PageRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
/**
* @var MailService
*/
protected $mailService;
/**
* @var PageRepository
*/
private $pageRepository;
public function __construct(
MailService $mailService,
PageRepository $pageRepository
) {
$this->mailService = $mailService;
$this->pageRepository = $pageRepository;
}
/**
* お問い合わせトップ画面.
*
* @Route("/contact", name="contact_top", methods={"GET"})
*/
public function contact(Request $request)
{
return $this->redirectToRoute('contact_type', [], 301);
}
/**
* お問い合わせ種別選択画面.
*
* @Route("/contact/type", name="contact_type", methods={"GET", "POST"})
* @Template("Contact/type.twig")
*/
public function type(Request $request)
{
// FIXME:必要な時に非表示戻す
return $this->redirectToRoute('contact', ['type' => 'other']);
$builder = $this->formFactory->createBuilder(ContactTypeType::class);
$form = $builder->getForm();
return [
'form' => $form->createView(),
];
}
/**
* お問い合わせ画面.
*
* @Route(
* path="/contact/{type}",
* name="contact",
* methods={"GET", "POST"},
* requirements={
* "type": "catalog|other"
* }
* )
* @Template("Contact/index.twig")
*/
public function index(Request $request, $type)
{
// FIXME:必要な時に非表示戻す
if ($type === 'catalog') {
return $this->redirectToRoute('contact', ['type' => 'other']);
}
$builder = $this->formFactory->createBuilder(ContactType::class, null, [
'type' => $type,
]);
if ($this->isGranted('ROLE_USER')) {
/** @var Customer $user */
$user = $this->getUser();
$builder->setData(
[
'name01' => $user->getName01(),
'name02' => $user->getName02(),
'kana01' => $user->getKana01(),
'kana02' => $user->getKana02(),
'postal_code' => $user->getPostalCode(),
'pref' => $user->getPref(),
'addr01' => $user->getAddr01(),
'addr02' => $user->getAddr02(),
'phone_number' => $user->getPhoneNumber(),
'email' => $user->getEmail(),
]
);
}
// FRONT_CONTACT_INDEX_INITIALIZE
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
return $this->render('Contact/confirm.twig', [
'form' => $form->createView(),
'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
'type' => $type,
]);
case 'complete':
$data = $form->getData();
$event = new EventArgs(
[
'form' => $form,
'data' => $data,
'type' => $type,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
$data = $event->getArgument('data');
// メール送信
$this->mailService->sendContactMail($data);
return $this->redirect($this->generateUrl('contact_complete'));
}
}
return [
'form' => $form->createView(),
'type' => $type,
];
}
}