app/Plugin/PiaContact/Controller/SellGoldBarsController.php line 42

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\SellGoldBarsType;
  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. class SellGoldBarsController extends AbstractController
  15. {
  16.     protected $piaContactMailService;
  17.     protected $piaContactSpreadsheetService;
  18.     protected $authorizationChecker;
  19.     protected $eventDispatcher;
  20.     public function __construct(
  21.         PiaContactMailService $piaContactMailService,
  22.         PiaContactSpreadsheetService $piaContactSpreadsheetService,
  23.         AuthorizationCheckerInterface $authorizationChecker,
  24.         EventDispatcherInterface $eventDispatcher
  25.     ) {
  26.         $this->piaContactMailService $piaContactMailService;
  27.         $this->piaContactSpreadsheetService $piaContactSpreadsheetService;
  28.         $this->authorizationChecker $authorizationChecker;
  29.         $this->eventDispatcher $eventDispatcher;
  30.     }
  31.     /**
  32.      * ゴールドバーお売りくださいフォーム
  33.      *
  34.      * @Route("/sell_gold_bars", name="plugin_pia_sell_gold_bars")
  35.      * @Template("@PiaContact/default/sell_gold_bars/index.twig")
  36.      */
  37.     public function index(Request $request)
  38.     {
  39.         $builder $this->formFactory->createBuilder(SellGoldBarsType::class);
  40.         // ログインユーザーの情報を設定
  41.         if ($this->authorizationChecker->isGranted('ROLE_USER')) {
  42.             /** @var \Eccube\Entity\Customer $user */
  43.             $user $this->getUser();
  44.             $builder->setData([
  45.                 'name' => [
  46.                     'name01' => $user->getName01(),
  47.                     'name02' => $user->getName02(),
  48.                 ],
  49.                 'tel' => $user->getPhoneNumber(),
  50.                 'email' => $user->getEmail(),
  51.             ]);
  52.         }
  53.         // FRONT_CONTACT_INDEX_INITIALIZE
  54.         $event = new EventArgs(
  55.             [
  56.                 'builder' => $builder,
  57.             ],
  58.             $request
  59.         );
  60.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  61.         $form $builder->getForm();
  62.         $form->handleRequest($request);
  63.         if ($form->isSubmitted() && $form->isValid()) {
  64.             switch ($request->get('mode')) {
  65.                 case 'confirm':
  66.                     // 確認画面
  67.                     return $this->render('@PiaContact/default/sell_gold_bars/confirm.twig', [
  68.                         'form' => $form->createView(),
  69.                     ]);
  70.                 case 'complete':
  71.                     // 完了処理
  72.                     $data $form->getData();
  73.                     $data["create_date"] = date("Y-m-d H:i:s");
  74.                     // イベントを実行してデータを保存
  75.                     $event = new EventArgs(
  76.                         [
  77.                             'form' => $form,
  78.                             'data' => $data,
  79.                         ],
  80.                         $request
  81.                     );
  82.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  83.                     $data $event->getArgument('data');
  84.                     try {
  85.                         // スプレッドシートに保存
  86.                         $this->piaContactSpreadsheetService->saveSellGoldBars($data);
  87.                         // メール送信
  88.                         $this->piaContactMailService->sendPiaSellGoldBarsMail($data);
  89.                         $this->addSuccess('ゴールドバーお売りくださいのお問い合わせを受け付けました。''front');
  90.                     } catch (\Exception $e) {
  91.                         $this->addError('申込処理中にエラーが発生しました: ' $e->getMessage(), 'front');
  92.                         error_log('SellGoldBars Process Error: ' $e->getMessage());
  93.                         return [
  94.                             'form' => $form->createView(),
  95.                         ];
  96.                     }
  97.                     return $this->redirectToRoute('plugin_pia_sell_gold_bars_complete');
  98.             }
  99.         }
  100.         return [
  101.             'form' => $form->createView(),
  102.         ];
  103.     }
  104.     /**
  105.      * ゴールドバーお売りください完了画面
  106.      *
  107.      * @Route("/sell_gold_bars/complete", name="plugin_pia_sell_gold_bars_complete")
  108.      */
  109.     public function complete()
  110.     {
  111.         return $this->render('@PiaContact/default/sell_gold_bars/complete.twig');
  112.     }
  113. }