app/Customize/Controller/ContactController.php line 117

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Customize\Form\Type\Front\Contact\ContactTypeType;
  4. use Customize\Service\MailService;
  5. use Eccube\Controller\AbstractController;
  6. use Eccube\Entity\Customer;
  7. use Eccube\Event\EccubeEvents;
  8. use Eccube\Event\EventArgs;
  9. use Eccube\Form\Type\Front\ContactType;
  10. use Eccube\Repository\PageRepository;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ContactController extends AbstractController
  15. {
  16.     /**
  17.      * @var MailService
  18.      */
  19.     protected $mailService;
  20.     /**
  21.      * @var PageRepository
  22.      */
  23.     private $pageRepository;
  24.     public function __construct(
  25.         MailService $mailService,
  26.         PageRepository $pageRepository
  27.     ) {
  28.         $this->mailService $mailService;
  29.         $this->pageRepository $pageRepository;
  30.     }
  31.     /**
  32.      * お問い合わせトップ画面.
  33.      *
  34.      * @Route("/contact", name="contact_top", methods={"GET"})
  35.      */
  36.     public function contact(Request $request)
  37.     {
  38.         return $this->redirectToRoute('contact_type', [], 301);
  39.     }
  40.     /**
  41.      * お問い合わせ種別選択画面.
  42.      *
  43.      * @Route("/contact/type", name="contact_type", methods={"GET", "POST"})
  44.      * @Template("Contact/type.twig")
  45.      */
  46.     public function type(Request $request)
  47.     {
  48.         // FIXME:必要な時に非表示戻す
  49.         return $this->redirectToRoute('contact', ['type' => 'other']);
  50.         $builder $this->formFactory->createBuilder(ContactTypeType::class);
  51.         $form $builder->getForm();
  52.         return [
  53.             'form' => $form->createView(),
  54.         ];
  55.     }
  56.     /**
  57.      * お問い合わせ画面.
  58.      *
  59.      * @Route(
  60.      *     path="/contact/{type}",
  61.      *     name="contact",
  62.      *     methods={"GET", "POST"},
  63.      *     requirements={
  64.      *         "type": "catalog|other"
  65.      *     }
  66.      * )
  67.      * @Template("Contact/index.twig")
  68.      */
  69.     public function index(Request $request$type)
  70.     {
  71.         // FIXME:必要な時に非表示戻す
  72.         if ($type === 'catalog') {
  73.             return $this->redirectToRoute('contact', ['type' => 'other']);
  74.         }
  75.         $builder $this->formFactory->createBuilder(ContactType::class, null, [
  76.             'type' => $type,
  77.         ]);
  78.         if ($this->isGranted('ROLE_USER')) {
  79.             /** @var Customer $user */
  80.             $user $this->getUser();
  81.             $builder->setData(
  82.                 [
  83.                     'name01' => $user->getName01(),
  84.                     'name02' => $user->getName02(),
  85.                     'kana01' => $user->getKana01(),
  86.                     'kana02' => $user->getKana02(),
  87.                     'postal_code' => $user->getPostalCode(),
  88.                     'pref' => $user->getPref(),
  89.                     'addr01' => $user->getAddr01(),
  90.                     'addr02' => $user->getAddr02(),
  91.                     'phone_number' => $user->getPhoneNumber(),
  92.                     'email' => $user->getEmail(),
  93.                 ]
  94.             );
  95.         }
  96.         // FRONT_CONTACT_INDEX_INITIALIZE
  97.         $event = new EventArgs(
  98.             [
  99.                 'builder' => $builder,
  100.             ],
  101.             $request
  102.         );
  103.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  104.         $form $builder->getForm();
  105.         $form->handleRequest($request);
  106.         if ($form->isSubmitted() && $form->isValid()) {
  107.             switch ($request->get('mode')) {
  108.                 case 'confirm':
  109.                     return $this->render('Contact/confirm.twig', [
  110.                         'form' => $form->createView(),
  111.                         'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
  112.                         'type' => $type,
  113.                     ]);
  114.                 case 'complete':
  115.                     $data $form->getData();
  116.                     $event = new EventArgs(
  117.                         [
  118.                             'form' => $form,
  119.                             'data' => $data,
  120.                             'type' => $type,
  121.                         ],
  122.                         $request
  123.                     );
  124.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  125.                     $data $event->getArgument('data');
  126.                     // メール送信
  127.                     $this->mailService->sendContactMail($data);
  128.                     return $this->redirect($this->generateUrl('contact_complete'));
  129.             }
  130.         }
  131.         return [
  132.             'form' => $form->createView(),
  133.             'type' => $type,
  134.         ];
  135.     }
  136. }