app/Plugin/PiaContact/Controller/AppraisalController.php line 45

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\FormAppraisalType;
  7. use Plugin\PiaContact\Service\PiaContactMailService;
  8. use Plugin\PiaContact\Service\PiaContactSpreadsheetService;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. /**
  15.  * 査定申込Controller
  16.  */
  17. class AppraisalController extends AbstractController
  18. {
  19.     protected $piaContactMailService;
  20.     protected $piaContactSpreadsheetService;
  21.     protected $authorizationChecker;
  22.     protected $eventDispatcher;
  23.     public function __construct(
  24.         PiaContactMailService $piaContactMailService,
  25.         PiaContactSpreadsheetService $piaContactSpreadsheetService,
  26.         AuthorizationCheckerInterface $authorizationChecker,
  27.         EventDispatcherInterface $eventDispatcher
  28.     ) {
  29.         $this->piaContactMailService $piaContactMailService;
  30.         $this->piaContactSpreadsheetService $piaContactSpreadsheetService;
  31.         $this->authorizationChecker $authorizationChecker;
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     /**
  35.      * 査定申込フォーム表示
  36.      *
  37.      * @Route("/form_appraisal", name="plugin_pia_form_appraisal")
  38.      * @Template("@PiaContact/default/appraisal/index.twig")
  39.      */
  40.     public function index(Request $request)
  41.     {
  42.         $builder $this->formFactory->createBuilder(FormAppraisalType::class);
  43.         
  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.                 'kana' => [
  54.                     'kana01' => $user->getKana01(),
  55.                     'kana02' => $user->getKana02(),
  56.                 ],
  57.                 'zip' => $user->getPostalCode(),
  58.                 'address' => [
  59.                     'pref' => $user->getPref(),
  60.                     'addr01' => $user->getAddr01(),
  61.                     'addr02' => $user->getAddr02(),
  62.                 ],
  63.                 'tel' => $user->getPhoneNumber(),
  64.                 'email' => $user->getEmail(),
  65.             ]);
  66.         }
  67.         // FRONT_CONTACT_INDEX_INITIALIZE
  68.         $event = new EventArgs(
  69.             [
  70.                 'builder' => $builder,
  71.             ],
  72.             $request
  73.         );
  74.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  75.         $form $builder->getForm();
  76.         $form->handleRequest($request);
  77.         if ($form->isSubmitted() && $form->isValid()) {
  78.             switch ($request->get('mode')) {
  79.                 case 'confirm':
  80.                     // 確認画面
  81.                     $data $form->getData();
  82.                     $files = [];
  83.                     
  84.                     // 画像データを一時保存
  85.                     for ($i 1$i <= 5$i++) {
  86.                         $fieldName "item0{$i}_img";
  87.                         if (isset($_FILES["form_appraisal"]["name"][$fieldName]) && $_FILES["form_appraisal"]["name"][$fieldName]) {
  88.                             $file_name $_FILES["form_appraisal"]["name"][$fieldName];
  89.                             $originalName $file_name;
  90.                             $extension substr($file_namestrrpos($file_name'.') + 1);
  91.                             $fileName date("Ymd_His") . "_" sprintf("%03d"rand(0999)) . "_img0{$i}." $extension;
  92.                             // 一時ディレクトリに保存(EC-CUBE4標準パス)
  93.                             $tempDir $this->getParameter('kernel.project_dir') . '/html/upload/temp_image/';
  94.                             if (!is_dir($tempDir)) {
  95.                                 mkdir($tempDir0777true);
  96.                             }
  97.                             rename($_FILES["form_appraisal"]["tmp_name"][$fieldName], $tempDir.$fileName);
  98.                             $files[$fieldName] = [
  99.                                 'name' => $originalName,
  100.                                 'file_name' => $fileName
  101.                             ];
  102.                             $data[$fieldName] = $originalName;
  103.                         } else {
  104.                             $data[$fieldName] = '';
  105.                         }
  106.                     }
  107.                     // セッションに画像情報を保存
  108.                     $request->getSession()->set('plugin.form.appraisal.img'serialize($files));
  109.                     
  110.                     return $this->render('@PiaContact/default/appraisal/confirm.twig', [
  111.                         'form' => $form->createView(),
  112.                     ]);
  113.                 case 'complete':
  114.                     // 完了処理
  115.                     $data $form->getData();
  116.                     // 連絡方法・受取方法の表示名を設定
  117.                     $htContact = ["1" => "TEL""2" => "メール"];
  118.                     $htReceiving = ["1" => "ご配送""2" => "ご来店"];
  119.                     $data["contact_nm"] = $htContact[$data["contact"]] ?? "";
  120.                     $data["receiving_nm"] = $htReceiving[$data["receiving"]] ?? "";
  121.                     // 画像ファイルの処理
  122.                     $this->processImageFiles($data$request);
  123.                     // イベントを実行してデータを保存
  124.                     $event = new EventArgs(
  125.                         [
  126.                             'form' => $form,
  127.                             'data' => $data,
  128.                         ],
  129.                         $request
  130.                     );
  131.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  132.                     $data $event->getArgument('data');
  133.                     try {
  134.                         // スプレッドシートに保存
  135.                         $this->piaContactSpreadsheetService->saveAppraisal($data);
  136.                         // メール送信
  137.                         $this->piaContactMailService->sendPiaAppraisalMail($data);
  138.                         $this->addSuccess('査定申込を受け付けました。''front');
  139.                     } catch (\Exception $e) {
  140.                         $this->addError('申込処理中にエラーが発生しました: ' $e->getMessage(), 'front');
  141.                         error_log('Appraisal Process Error: ' $e->getMessage());
  142.                         return [
  143.                             'form' => $form->createView(),
  144.                         ];
  145.                     }
  146.                     return $this->redirectToRoute('plugin_pia_form_appraisal_complete');
  147.             }
  148.         }
  149.         return [
  150.             'form' => $form->createView(),
  151.         ];
  152.     }
  153.     /**
  154.      * 査定申込完了画面
  155.      *
  156.      * @Route("/appraisal/complete", name="plugin_pia_form_appraisal_complete")
  157.      */
  158.     public function complete()
  159.     {
  160.         return $this->render('@PiaContact/default/appraisal/complete.twig');
  161.     }
  162.     /**
  163.      * 画像ファイルの処理
  164.      *
  165.      * @param array $data
  166.      * @param Request $request
  167.      */
  168.     private function processImageFiles(&$dataRequest $request)
  169.     {
  170.         $dir_tmp $this->getParameter('kernel.project_dir') . '/html/upload/temp_image/';
  171.         $dir_up $this->getParameter('kernel.project_dir') . '/html/upload/appraisal/';
  172.         // アップロードディレクトリが存在しない場合は作成
  173.         if (!is_dir($dir_up)) {
  174.             mkdir($dir_up0777true);
  175.         }
  176.         // セッションから画像情報を取得
  177.         $htImg unserialize($request->getSession()->get('plugin.form.appraisal.img'''));
  178.         for ($i 1$i <= 5$i++) {
  179.             $key "item0{$i}_img";
  180.             $data[$key] = empty($htImg[$key]["file_name"]) ? "" $htImg[$key]["file_name"];
  181.             if (!empty($data[$key])) {
  182.                 $tempFile $dir_tmp $data[$key];
  183.                 $finalFile $dir_up $data[$key];
  184.                 if (file_exists($tempFile)) {
  185.                     rename($tempFile$finalFile);
  186.                 }
  187.             }
  188.         }
  189.     }
  190. }