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

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