app/Plugin/ProductContactB/Controller/ProductContactController.php line 46

Open in your IDE?
  1. <?php
  2. namespace Plugin\ProductContactB\Controller;
  3. use Plugin\ProductContactB\Form\Type\ProductContactType;
  4. use Plugin\ProductContactB\Service\ProductContactMailService;
  5. use Eccube\Controller\AbstractController;
  6. use Eccube\Entity\Customer;
  7. use Eccube\Repository\ProductRepository;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class ProductContactController extends AbstractController
  12. {
  13.     /**
  14.      * @var ProductRepository
  15.      */
  16.     protected $productRepository;
  17.     /**
  18.      * @var ProductContactMailService
  19.      */
  20.     protected $mailService;
  21.     /**
  22.      * ContactController constructor.
  23.      *
  24.      * @param ProductRepository $productRepository
  25.      * @param ProductContactMailService $mailService
  26.      */
  27.     public function __construct(
  28.         ProductRepository $productRepository,
  29.         ProductContactMailService $mailService)
  30.     {
  31.         $this->productRepository $productRepository;
  32.         $this->mailService $mailService;
  33.     }
  34.     /**
  35.      * お問い合わせ画面.
  36.      *
  37.      * @Route("/products/contact", name="product_contact")
  38.      * @Template("@ProductContactB/default/index.twig")
  39.      */
  40.     public function index(Request $request)
  41.     {
  42.         $builder $this->formFactory->createBuilder(ProductContactType::class);
  43.         if ($this->isGranted('ROLE_USER')) {
  44.             /** @var Customer $user */
  45.             $user $this->getUser();
  46.             $builder->setData(
  47.                 [
  48.                     'name01' => $user->getName01(),
  49.                     'name02' => $user->getName02(),
  50.                     'kana01' => $user->getKana01(),
  51.                     'kana02' => $user->getKana02(),
  52.                     'postal_code' => $user->getPostalCode(),
  53.                     'pref' => $user->getPref(),
  54.                     'addr01' => $user->getAddr01(),
  55.                     'addr02' => $user->getAddr02(),
  56.                     'phone_number' => $user->getPhoneNumber(),
  57.                     'email' => $user->getEmail(),
  58.                 ]
  59.             );
  60.         }
  61.         // confirmのときはpostのほうがよいかも
  62.         $id $request->query->get('product_id');
  63.         $builder->setData(['product_id' => $id]);
  64.         $Product $this->productRepository->find($id);
  65.         $form $builder->getForm();
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             switch ($request->get('mode')) {
  69.                 case 'confirm':
  70.                     $form $builder->getForm();
  71.                     $form->handleRequest($request);
  72.                     return $this->render('@ProductContactB/default/confirm.twig', [
  73.                         'form' => $form->createView(),
  74.                         'Product' => $Product,
  75.                     ]);
  76.                 case 'complete':
  77.                     $data $form->getData();
  78.                     // メール送信
  79.                     $this->mailService->sendProductContactMail($Product$data);
  80.                     return $this->redirect($this->generateUrl('product_contact_complete'));
  81.             }
  82.         }
  83.         return [
  84.             'form' => $form->createView(),
  85.             'Product' => $Product,
  86.         ];
  87.     }
  88.     /**
  89.      * お問い合わせ完了画面.
  90.      *
  91.      * @Route("/products/contact/complete", name="product_contact_complete")
  92.      * @Template("@ProductContactB/default/complete.twig")
  93.      */
  94.     public function complete()
  95.     {
  96.         return [];
  97.     }
  98. }