app/Plugin/PiaContact/Controller/BulkPurchaseController.php line 46

Open in your IDE?
  1. <?php
  2. namespace Plugin\PiaContact\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Event\EventArgs;
  5. use Eccube\Event\EccubeEvents;
  6. use Plugin\PiaContact\Form\Type\BulkPurchaseType;
  7. use Plugin\PiaContact\Service\PiaContactMailService;
  8. use Plugin\PiaContact\Service\PiaContactSpreadsheetService;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. use Plugin\PiaContact\Service\ReCaptchaService;
  15. class BulkPurchaseController extends AbstractController
  16. {
  17.     protected $piaContactMailService;
  18.     protected $piaContactSpreadsheetService;
  19.     protected $authorizationChecker;
  20.     protected $eventDispatcher;
  21.     protected $reCaptchaService;
  22.     public function __construct(
  23.         PiaContactMailService $piaContactMailService,
  24.         PiaContactSpreadsheetService $piaContactSpreadsheetService,
  25.         AuthorizationCheckerInterface $authorizationChecker,
  26.         EventDispatcherInterface $eventDispatcher,
  27.         ReCaptchaService $reCaptchaService
  28.     ) {
  29.         $this->piaContactMailService $piaContactMailService;
  30.         $this->piaContactSpreadsheetService $piaContactSpreadsheetService;
  31.         $this->authorizationChecker $authorizationChecker;
  32.         $this->eventDispatcher $eventDispatcher;
  33.         $this->reCaptchaService $reCaptchaService;
  34.     }
  35.     /**
  36.      * 大量購入フォーム
  37.      *
  38.      * @Route("/form_bulk_purchase", name="plugin_pia_form_bulk_purchase")
  39.      * @Template("@PiaContact/default/bulk_purchase/index.twig")
  40.      */
  41.     public function index(Request $request)
  42.     {
  43.         $builder $this->formFactory->createBuilder(BulkPurchaseType::class);
  44.         // ログインユーザーの情報を設定
  45.         if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  46.             /** @var \Eccube\Entity\Customer $user */
  47.             $user $this->getUser();
  48.             $builder->setData([
  49.                 'name' => [
  50.                     'name01' => $user->getName01(),
  51.                     'name02' => $user->getName02(),
  52.                 ],
  53.                 'tel' => $user->getPhoneNumber(),
  54.                 'email' => $user->getEmail(),
  55.             ]);
  56.         }
  57.         // FRONT_CONTACT_INDEX_INITIALIZE
  58.         $event = new EventArgs(
  59.             [
  60.                 'builder' => $builder,
  61.             ],
  62.             $request
  63.         );
  64.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  65.         $form $builder->getForm();
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             switch ($request->get('mode')) {
  69.                 case 'confirm':
  70.                     // reCAPTCHA検証
  71.                     $recaptchaToken $request->request->get('g-recaptcha-response''');
  72.                     if (!$this->reCaptchaService->verify($recaptchaToken)) {
  73.                         $this->addError('reCAPTCHAの認証に失敗しました。もう一度お試しください。''front');
  74.                         break;
  75.                     }
  76.                     // 確認画面
  77.                     return $this->render('@PiaContact/default/bulk_purchase/confirm.twig', [
  78.                         'form' => $form->createView(),
  79.                     ]);
  80.                 case 'complete':
  81.                     // 完了処理
  82.                     $data $form->getData();
  83.                     // イベントを実行してデータを保存
  84.                     $event = new EventArgs(
  85.                         [
  86.                             'form' => $form,
  87.                             'data' => $data,
  88.                         ],
  89.                         $request
  90.                     );
  91.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  92.                     $data $event->getArgument('data');
  93.                     try {
  94.                         // スプレッドシートに保存
  95.                         //$this->piaContactSpreadsheetService->saveBulkPurchase($data);
  96.                         // メール送信
  97.                         $this->piaContactMailService->sendPiaBulkPurchaseMail($data);
  98.                     } catch (\Exception $e) {
  99.                         $this->addError('申込処理中にエラーが発生しました: ' $e->getMessage(), 'front');
  100.                         error_log('BulkPurchase Process Error: ' $e->getMessage());
  101.                         return $this->redirectToRoute('plugin_pia_form_bulk_purchase');
  102.                     }
  103.                     return $this->redirectToRoute('plugin_pia_form_bulk_purchase_complete');
  104.             }
  105.         }
  106.         return [
  107.             'form' => $form->createView(),
  108.             'recaptcha_site_key' => $this->getParameter('recaptcha_site_key'),
  109.         ];
  110.     }
  111.     /**
  112.      * 大量購入完了画面
  113.      *
  114.      * @Route("/form_bulk_purchase/complete", name="plugin_pia_form_bulk_purchase_complete")
  115.      */
  116.     public function complete()
  117.     {
  118.         return $this->render('@PiaContact/default/bulk_purchase/complete.twig');
  119.     }
  120. }