app/Plugin/ApplyForSampleMagazine42/Controller/ApplyController.php line 47

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApplyForSampleMagazine42\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\Customer;
  5. use Plugin\ApplyForSampleMagazine42\Form\Type\ApplyType;
  6. use Eccube\Repository\PageRepository;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Plugin\ApplyForSampleMagazine42\Service\ApplyMailService;
  11. class ApplyController extends AbstractController
  12. {
  13.     /**
  14.      * @var PageRepository
  15.      */
  16.     private $pageRepository;
  17.     /**
  18.      * @var ApplyMailService
  19.      */
  20.     private $applyMailService;
  21.     /**
  22.      * ContactController constructor.
  23.      *
  24.      * @param PageRepository $pageRepository
  25.      * @param ApplyMailService $applyMailService
  26.      */
  27.     public function __construct(
  28.         PageRepository $pageRepository,
  29.         ApplyMailService $applyMailService)
  30.     {
  31.         $this->pageRepository $pageRepository;
  32.         $this->applyMailService $applyMailService;
  33.     }
  34.     /**
  35.      * 見本誌申し込み画面.
  36.      *
  37.      * @Route("/sample_magazine", name="apply_for_sample_magazine", methods={"GET", "POST"})
  38.      * @Route("/sample_magazine", name="apply_for_sample_magazine_confirm", methods={"GET", "POST"})
  39.      * @Template("SampleMagazine/index.twig")
  40.      */
  41.     public function index(Request $request)
  42.     {
  43.         $builder $this->formFactory->createBuilder(ApplyType::class);
  44.         if ($this->isGranted('ROLE_USER')) {
  45.             /** @var Customer $user */
  46.             $user $this->getUser();
  47.             $builder->setData(
  48.                 [
  49.                     'name01' => $user->getName01(),
  50.                     'name02' => $user->getName02(),
  51.                     'kana01' => $user->getKana01(),
  52.                     'kana02' => $user->getKana02(),
  53.                     'postal_code' => $user->getPostalCode(),
  54.                     'pref' => $user->getPref(),
  55.                     'addr01' => $user->getAddr01(),
  56.                     'addr02' => $user->getAddr02(),
  57.                     'phone_number' => $user->getPhoneNumber(),
  58.                     'email' => $user->getEmail(),
  59.                 ]
  60.             );
  61.         }
  62.         $form $builder->getForm();
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted() && $form->isValid()) {
  65.             switch ($request->get('mode')) {
  66.                 case 'confirm':
  67.                     return $this->render('SampleMagazine/confirm.twig', [
  68.                         'form' => $form->createView(),
  69.                         'Page' => $this->pageRepository->getPageByRoute('apply_for_sample_magazine_confirm'),
  70.                     ]);
  71.                 case 'complete':
  72.                     $data $form->getData();
  73.                     // メール送信
  74.                     $this->applyMailService->sendApplyMail($data);
  75.                     return $this->redirect($this->generateUrl('apply_for_sample_magazine_complete'));
  76.             }
  77.         }
  78.         return [
  79.             'form' => $form->createView(),
  80.         ];
  81.     }
  82.     /**
  83.      * お問い合わせ完了画面.
  84.      *
  85.      * @Route("/sample_magazine/complete", name="apply_for_sample_magazine_complete", methods={"GET"})
  86.      * @Template("SampleMagazine/complete.twig")
  87.      */
  88.     public function complete()
  89.     {
  90.         return [];
  91.     }
  92. }