app/Plugin/PiaContact/Controller/ProductContactController.php line 60

Open in your IDE?
  1. <?php
  2. namespace Plugin\PiaContact\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Event\EccubeEvents;
  5. use Eccube\Event\EventArgs;
  6. use Plugin\PiaContact\Form\Type\ProductContactType;
  7. use Plugin\PiaContact\Repository\PiaProductContactRepository;
  8. use Plugin\PiaContact\Service\PiaContactMailService;
  9. use Plugin\PiaContact\Service\PiaContactSpreadsheetService;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use Plugin\PiaContact\Service\ReCaptchaService;
  15. class ProductContactController extends AbstractController
  16. {
  17.     /**
  18.      * @var PiaProductContactRepository
  19.      */
  20.     protected $piaProductContactRepository;
  21.     /**
  22.      * @var PiaContactMailService
  23.      */
  24.     protected $piaContactMailService;
  25.     /**
  26.      * @var PiaContactSpreadsheetService
  27.      */
  28.     protected $piaContactSpreadsheetService;
  29.     /**
  30.      * @var AuthorizationCheckerInterface
  31.      */
  32.     protected $authorizationChecker;
  33.     protected $reCaptchaService;
  34.     public function __construct(
  35.         PiaProductContactRepository $piaProductContactRepository,
  36.         PiaContactMailService $piaContactMailService,
  37.         AuthorizationCheckerInterface $authorizationChecker,
  38.         ReCaptchaService $reCaptchaService
  39.     ) {
  40.         $this->piaProductContactRepository $piaProductContactRepository;
  41.         $this->piaContactMailService $piaContactMailService;
  42.         $this->authorizationChecker $authorizationChecker;
  43.         $this->piaContactSpreadsheetService = new PiaContactSpreadsheetService();
  44.         $this->reCaptchaService $reCaptchaService;
  45.     }
  46.     /**
  47.      * 商品お問い合わせフォーム
  48.      *
  49.      * @Route("/product_contact", name="plugin_pia_contact")
  50.      * @Template("@PiaContact/default/product_contact/index.twig")
  51.      */
  52.     public function index(Request $request)
  53.     {
  54.         $productId $request->query->get('id') ?: $request->request->get('id');
  55.         
  56.         if (empty($productId)) {
  57.             throw new \Exception("商品IDを指定してください。");
  58.         }
  59.         $Product $this->entityManager->getRepository('Eccube\Entity\Product')->find($productId);
  60.         if (empty($Product)) {
  61.             throw new \Exception("商品情報を取得出来ませんでした。");
  62.         }
  63.         $builder $this->formFactory->createBuilder(ProductContactType::class);
  64.         $builder->setData(['product_id' => $productId]);
  65.         // ログインユーザーの情報を設定
  66.         if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  67.             $user $this->getUser();
  68.             $builder->setData([
  69.                 'product_id' => $productId,
  70.                 'name' => [
  71.                     'name01' => $user->getName01(),
  72.                     'name02' => $user->getName02(),
  73.                 ],
  74.                 'email' => $user->getEmail(),
  75.             ]);
  76.         }
  77.         $form $builder->getForm();
  78.         $form->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             switch ($request->get('mode')) {
  81.                 case 'confirm':
  82.                     // reCAPTCHA検証
  83.                     $recaptchaToken $request->request->get('g-recaptcha-response''');
  84.                     if (!$this->reCaptchaService->verify($recaptchaToken)) {
  85.                         $this->addError('reCAPTCHAの認証に失敗しました。もう一度お試しください。''front');
  86.                         break;
  87.                     }
  88.                     // 確認画面
  89.                     return $this->render('@PiaContact/default/product_contact/confirm.twig', [
  90.                         'form' => $form->createView(),
  91.                         'Product' => $Product,
  92.                     ]);
  93.                 case 'back':
  94.                     // 入力画面に戻る
  95.                     return [
  96.                         'form' => $form->createView(),
  97.                         'recaptcha_site_key' => $this->getParameter('recaptcha_site_key'),
  98.                         'Product' => $Product,
  99.                     ];
  100.                 case 'complete':
  101.                     // 完了処理
  102.                     $data $form->getData();
  103.                     $data['product_id'] = $productId;
  104.                     $data['phone_number'] = "";
  105.                     $data['Product'] = $Product;
  106.                     $data['contents'] =$data['message'];
  107.                     // イベントを実行(ContactManagement42でDBに保存される)
  108.                     $event = new EventArgs(
  109.                         [
  110.                             'form' => $form,
  111.                             'data' => $data,
  112.                         ],
  113.                         $request
  114.                     );
  115.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  116.                     $data $event->getArgument('data');
  117.                     // DBに登録されたIDを受付番号として使用
  118.                     $contactReplyId = isset($_SESSION["eccube.contact.contact_reply_id"]) ? $_SESSION["eccube.contact.contact_reply_id"] : date('YmdHis');
  119.                     $data['id'] = $contactReplyId;
  120.                     // メール送信
  121.                     try {
  122.                         $result $this->piaContactMailService->sendPiaContactMail($data$Product);
  123.                         if (!$result) {
  124.                             $this->addError('メール送信に失敗しました。');
  125.                         }
  126.                     } catch (\Exception $e) {
  127.                         $this->addError('メール送信エラー: ' $e->getMessage());
  128.                         return [
  129.                             'form' => $form->createView(),
  130.                             'recaptcha_site_key' => $this->getParameter('recaptcha_site_key'),
  131.                             'Product' => $Product,
  132.                         ];
  133.                     }
  134.                     // スプレッドシート保存
  135.                     try {
  136.                         $result $this->piaContactSpreadsheetService->saveProductContact($data$Product$contactReplyId);
  137.                         if (!$result) {
  138.                             $this->addError('スプレッドシート保存に失敗しました。');
  139.                         }
  140.                     } catch (\Exception $e) {
  141.                       dump($e->getMessage());exit;
  142.                         $this->addError('スプレッドシート保存エラー: ' $e->getMessage());
  143.                         return [
  144.                             'form' => $form->createView(),
  145.                             'recaptcha_site_key' => $this->getParameter('recaptcha_site_key'),
  146.                             'Product' => $Product,
  147.                         ];
  148.                     }
  149.                     return $this->redirectToRoute('plugin_pia_contact_complete');
  150.             }
  151.         }
  152.         return [
  153.             'form' => $form->createView(),
  154.             'recaptcha_site_key' => $this->getParameter('recaptcha_site_key'),
  155.             'Product' => $Product,
  156.         ];
  157.     }
  158.     /**
  159.      * 商品お問い合わせ完了画面
  160.      *
  161.      * @Route("/product_contact/complete", name="plugin_pia_contact_complete")
  162.      */
  163.     public function complete()
  164.     {
  165.         return $this->render('@PiaContact/default/product_contact/complete.twig');
  166.     }
  167. }