app/Customize/Controller/CustomContactController.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller;
  13. use Eccube\Controller\ContactController as BaseContactController;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\Customer;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Front\ContactType;
  19. use Eccube\Repository\PageRepository;
  20. use Eccube\Service\MailService;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. // ▼お問い合わせ項目プルダウン①/⑤ マスターデータ管理に登録されていない場合は、プルダウンを非表示にするため。
  25. use Customize\Entity\Master\ContactOption;
  26. use Customize\Repository\Master\ContactOptionRepository;
  27. // ▼商品お問い合わせ①/⑤
  28. use Eccube\Entity\Product;
  29. use Eccube\Repository\ProductRepository;
  30. class CustomContactController extends BaseContactController
  31. {
  32.     /**
  33.      * @var MailService
  34.      */
  35.     protected $mailService;
  36.     /**
  37.      * @var PageRepository
  38.      */
  39.     private $pageRepository;
  40.     /**
  41.      * ContactController constructor.
  42.      *
  43.      * @param MailService $mailService
  44.      * @param PageRepository $pageRepository
  45.      */
  46.     // ▼お問い合わせ項目プルダウン②/⑤ ▼商品お問い合わせ②/⑤
  47.     public function __construct(
  48.         MailService $mailService,
  49.         PageRepository $pageRepository,
  50.         ContactOptionRepository $contactOptionRepository
  51.         ProductRepository $productRepository)
  52.     {
  53.         $this->mailService $mailService;
  54.         $this->pageRepository $pageRepository;
  55.         $this->contactOptionRepository $contactOptionRepository;
  56.         $this->productRepository $productRepository;
  57.     }
  58.     /**
  59.      * お問い合わせ画面.
  60.      *
  61.      * @Route("/contact", name="contact", methods={"GET", "POST"})
  62.      * @Route("/contact", name="contact_confirm", methods={"GET", "POST"})
  63.      * @Template("Contact/index.twig")
  64.      */
  65.     public function index(Request $request)
  66.     {
  67.         $builder $this->formFactory->createBuilder(ContactType::class);
  68.         // ▼商品お問い合わせ③/⑤
  69.         $product_id $request->query->get('product_id');
  70.         $Product $this->productRepository
  71.             ->findOneBy(
  72.                 ['id' => $product_id],
  73.                 ['id' => 'DESC']
  74.             );
  75.         if ($this->isGranted('ROLE_USER') and $product_id) {
  76.             /** @var Customer $user */
  77.             $user $this->getUser();
  78.             $builder->setData(
  79.                 [
  80.                     'name01' => $user->getName01(),
  81.                     'name02' => $user->getName02(),
  82.                     'kana01' => $user->getKana01(),
  83.                     'kana02' => $user->getKana02(),
  84.                     'postal_code' => $user->getPostalCode(),
  85.                     'pref' => $user->getPref(),
  86.                     'addr01' => $user->getAddr01(),
  87.                     'addr02' => $user->getAddr02(),
  88.                     'phone_number' => $user->getPhoneNumber(),
  89.                     'email' => $user->getEmail(),
  90.                     'product' => '商品ID:'.$Product->getID().'「'.$Product->getName().'」',
  91.                 ]
  92.             );
  93.         } elseif ($this->isGranted('ROLE_USER')) {
  94.             $user $this->getUser();
  95.             $builder->setData(
  96.                 [
  97.                     'name01' => $user->getName01(),
  98.                     'name02' => $user->getName02(),
  99.                     'kana01' => $user->getKana01(),
  100.                     'kana02' => $user->getKana02(),
  101.                     'postal_code' => $user->getPostalCode(),
  102.                     'pref' => $user->getPref(),
  103.                     'addr01' => $user->getAddr01(),
  104.                     'addr02' => $user->getAddr02(),
  105.                     'phone_number' => $user->getPhoneNumber(),
  106.                     'email' => $user->getEmail(),
  107.                 ]
  108.             );
  109.         } elseif($product_id) {
  110.             $builder->setData(
  111.                 [
  112.                     'product' => '商品ID:'.$Product->getID().'「'.$Product->getName().'」',
  113.                 ]
  114.             );
  115.         }
  116.         // FRONT_CONTACT_INDEX_INITIALIZE
  117.         $event = new EventArgs(
  118.             [
  119.                 'builder' => $builder,
  120.             ],
  121.             $request
  122.         );
  123.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  124.         $form $builder->getForm();
  125.         $form->handleRequest($request);
  126.         // ▼お問い合わせ項目プルダウン③/⑤
  127.         $ContactOption $this->contactOptionRepository
  128.             ->findBy(
  129.                 [],
  130.                 ['id' => 'ASC']
  131.             );
  132.         if ($form->isSubmitted() && $form->isValid()) {
  133.             switch ($request->get('mode')) {
  134.                 case 'confirm':
  135.                     return $this->render('Contact/confirm.twig', [
  136.                         'form' => $form->createView(),
  137.                         'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
  138.                         'ContactOption' => $ContactOption//▼お問い合わせ項目プルダウン④/⑤
  139.                         'product_id' => $product_id//▼商品お問い合わせ④/⑤
  140.                         'Product' => $Product,
  141.                     ]);
  142.                 case 'complete':
  143.                     $data $form->getData();
  144.                     $event = new EventArgs(
  145.                         [
  146.                             'form' => $form,
  147.                             'data' => $data,
  148.                         ],
  149.                         $request
  150.                     );
  151.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  152.                     $data $event->getArgument('data');
  153.                     // メール送信
  154.                     $this->mailService->sendContactMail($data);
  155.                     return $this->redirect($this->generateUrl('contact_complete'));
  156.             }
  157.         }
  158.         return [
  159.             'form' => $form->createView(),
  160.             'ContactOption' => $ContactOption// ▼お問い合わせ項目プルダウン⑤/⑤
  161.             'product_id' => $product_id//▼商品お問い合わせ⑤/⑤
  162.             'Product' => $Product,
  163.         ];
  164.     }
  165. }