app/Customize/Controller/MailController.php line 338

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\Controller;
  13. use Customize\Event\LmEvents;
  14. use Customize\Service\CustomerService;
  15. use Customize\Service\CommonService;
  16. use Customize\Service\Google\ReCaptchaService;
  17. use Lm\Service\Store\FileStoreService;
  18. use Customize\Converter\DynamicConverter;
  19. use Customize\Form\Type\Front\QuestionType;
  20. use Customize\Service\MailQuestionService;
  21. use Eccube\Controller\ContactController;
  22. use Customize\Form\Type\Front\MailType;
  23. use Eccube\Entity\Customer;
  24. use Eccube\Repository\PageRepository;
  25. use Customize\Service\MailService;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\RequestStack;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. class MailController extends ContactController
  31. {
  32.     /*
  33.     const S3_LM_DOC = 'lm-doc-dev';
  34.     const TMP_DIR = 'front-public/images/tmp_img/';
  35.     const S3_REGION = 'ap-northeast-1';
  36.     const S3_API_VERSION = 'latest';
  37.     */
  38.     /**
  39.      * @var MailService
  40.      */
  41.     protected $mailService;
  42.     /**
  43.      * @var PageRepository
  44.      */
  45.     private $pageRepository;
  46.     /**
  47.      * @var CustomerService
  48.      */
  49.     protected $CustomerService;
  50.     /**
  51.      * @var CommonService
  52.      */
  53.     protected $CommonService;
  54.     /**
  55.      * @var BreadCrumbs_MyPageTop
  56.      */
  57.     protected $BreadCrumbs_MyPageTop;
  58.     /**
  59.      * @var Request|null
  60.      */
  61.     protected $request;
  62.     /**
  63.      * @var MailQuestionService
  64.      */
  65.     protected $mailQuestionService;
  66.     /**
  67.      * @var DynamicConverter
  68.      */
  69.     protected $dynamicConverter;
  70.     /**
  71.      * @var ReCaptchaService
  72.      */
  73.     protected $reCaptchaService;
  74.     /**
  75.      * ContactController constructor.
  76.      *
  77.      * @param MailService $mailService
  78.      * @param PageRepository $pageRepository
  79.      */
  80.     public function __construct(
  81.         CustomerService $CustomerService,
  82.         CommonService   $CommonService,
  83.         MailService     $mailService,
  84.         MailQuestionService $mailQuestionService,
  85.         DynamicConverter $dynamicConverter,
  86.         PageRepository  $pageRepository,
  87.         RequestStack    $requestStack,
  88.         ReCaptchaService $reCaptchaService
  89.     )
  90.     {
  91.         $this->CustomerService $CustomerService;
  92.         $this->CommonService $CommonService;
  93.         $this->mailService $mailService;
  94.         $this->mailQuestionService $mailQuestionService;
  95.         $this->dynamicConverter $dynamicConverter;
  96.         $this->pageRepository $pageRepository;
  97.         $this->request $requestStack->getMasterRequest();
  98.         $this->reCaptchaService $reCaptchaService;
  99.     }
  100.     /**
  101.      * お問い合わせ画面.
  102.      *
  103.      * @Route("/mail/", name="contact", methods={"GET", "POST"})
  104.      * @Route("/mail/", name="contact_confirm", methods={"GET", "POST"})
  105.      * @Route("/mypage/contact/", name="mypage_contact", methods={"GET", "POST"})
  106.      * @Route("/mypage/contact/", name="mypage_contact_confirm", methods={"GET", "POST"})
  107.      * @Template("Contact/index.twig")
  108.      */
  109.     public function index(Request $request)
  110.     {
  111.         // 他画面との共有のための設定
  112.         // TODO: 完了画面の対応
  113.         $routeName $request->get('_route');
  114.         $routeBase null;
  115.         $BreadCrumbs = [];
  116.         $isShowLeftMenu false;
  117.         $reCaptchaSiteKey $this->reCaptchaService->getReCaptchaSiteKey();
  118.         if (in_array($routeName, ["mypage_contact""mypage_contact_confirm"])) {
  119.             $isShowLeftMenu true;
  120.         }
  121.         if (preg_match('/^((?:\S+_)?contact)/'$routeName$matches)) {
  122.             $routeBase $matches[1];
  123.             $BreadCrumbs = [
  124.                 => $this->__getBreadCrumbs_MyPageTop(),
  125.             ];
  126.         }
  127.         $confirmRouteName "{$routeBase}_confirm";
  128.         $completeRouteName "{$routeBase}_complete";
  129.         $builder $this->formFactory->createBuilder(MailType::class);
  130.         if ($this->isGranted('ROLE_USER')) {
  131.             /** @var Customer $user */
  132.             $Customer $this->getUser();
  133.             $LmCustomer $this->CustomerService->GetLandMarkCustomerId($Customer->getLmCustomerId());
  134.             if ($LmCustomer) {
  135.                 $builder->setData(
  136.                     [
  137.                         'inquiry_user_name' => $LmCustomer['customer_user_name'],
  138.                         'inquiry_tel' => $LmCustomer['customer_tel'],
  139.                         'inquiry_email' => $LmCustomer['customer_mail'],
  140.                     ]
  141.                 );
  142.             }
  143.         }
  144.         $contact $request->request->get('contact');
  145.         if( !empty($contact['inquiry_user_name']) ){
  146.             $contact['inquiry_user_name'] = $this->unicodeTrim($contact['inquiry_user_name']);
  147.         }
  148.         $request->request->set('contact'$contact);
  149.         $this->session->set(LmEvents::SESSION_FRONT_MYPAGE_EMAIL_INQUIRY$contact['inquiry_email'] ?? '');
  150.         $form $builder->getForm();
  151.         $form->handleRequest($request);
  152.         if ($form->isSubmitted() && $form->isValid()) {
  153.             $this->reCaptchaService->handleRequest($request);
  154.             /*
  155.             $s3 = new S3Client(array(
  156.                 'version' => self::S3_API_VERSION,
  157.                 'region' => self::S3_REGION,
  158.             ));
  159.             */
  160.             $s3 = new FileStoreService(env('S3_LM_DOC'), MailService::INQUIRY_TMP_DIR);
  161.             // 添付ファイルの処理
  162.             for ($i 1$i <= 3$i++) {
  163.                 if (isset($_FILES['contact']['tmp_name']['inquiry_file' $i]) and $_FILES['contact']['tmp_name']['inquiry_file' $i] != "") {
  164.                     $f_name[$i] = $_FILES['contact']['name']['inquiry_file' $i];
  165.                     $t_name[$i] = $_FILES['contact']['tmp_name']['inquiry_file' $i];
  166.                     $kaku[$i] = strtolower(substr($f_name[$i], strrpos($f_name[$i], ".") + 1strlen($f_name[$i]) - strrpos($f_name[$i], ".")));
  167.                     $contentType[$i] = $_FILES['contact']['type']['inquiry_file' $i];
  168.                 }
  169.             }
  170.             switch ($request->get('mode')) {
  171.                 case 'confirm':
  172.                     for ($i 1$i <= 3$i++) {
  173.                         // 保存
  174.                         if (isset($_FILES['contact']['tmp_name']['inquiry_file' $i]) and $_FILES['contact']['tmp_name']['inquiry_file' $i] != "") {
  175.                             if (isset($_SESSION['inquiry']['inquiry_file' $i]['tmp_name']) and $_SESSION['inquiry']['inquiry_file' $i]['tmp_name'] != "") {
  176.                                 if ($s3->isObjectExists($_SESSION['inquiry']['inquiry_file' $i]['tmp_name'])) {
  177.                                     $s3->deleteObject($_SESSION['inquiry']['inquiry_file' $i]['tmp_name']);
  178.                                 }
  179.                                 unset($_SESSION['inquiry']['inquiry_file' $i]);
  180.                             }
  181.                             // 拡張子を取得
  182.                             while (true) {
  183.                                 $new_name $this->getNewPass() . "." $kaku[$i];
  184.                                 if (!$s3->isObjectExists($new_name))
  185.                                     break;
  186.                             }
  187.                             /*
  188.                             $args = array(
  189.                                 'Bucket' => self::S3_LM_DOC,
  190.                                 'SourceFile' => $t_name[$i],
  191.                                 'Key' => self::TMP_DIR . $new_name,
  192.                             );
  193.                             // 添付ファイルを保存
  194.                             $s3->putObject($args);
  195.                             */
  196.                             // 添付ファイルを保存
  197.                             $s3->uploadFile($new_name$t_name[$i]);
  198.                             // dump(self::TMP_DIR . $new_name);exit;
  199.                             // セッション変数に登録
  200.                             $_SESSION['inquiry']['inquiry_file' $i]['tmp_name'] = $new_name;
  201.                             $_SESSION['inquiry']['inquiry_file' $i]['name'] = $f_name[$i];
  202.                             $_SESSION['inquiry']['inquiry_file' $i]['ext'] = $kaku[$i];
  203.                             $_SESSION['inquiry']['inquiry_file' $i]['contentType'] = $contentType[$i];
  204.                         }
  205.                     }
  206.                     //
  207.                     $tmpFiles $_SESSION['inquiry'] ?? []; // TODO: $_SESSIONの使用を廃止
  208.                     //
  209.                     $thumbnails array_map(function ($tmpFile) {
  210.                         //
  211.                         $bucket env('S3_LM_DOC');
  212.                         $prefix MailService::INQUIRY_TMP_DIR;
  213.                         $filename $tmpFile['tmp_name'];
  214.                         $url env('S3_LM_DOC_PATH') . "/{$prefix}{$filename}";
  215.                         //
  216.                         return [
  217.                             'name' => $tmpFile['name'],
  218.                             'url' => $url,
  219.                         ];
  220.                     }, $tmpFiles);
  221.                     return $this->render('Contact/confirm.twig', [
  222.                         'form' => $form->createView(),
  223.                         'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
  224.                         'Thumbnails' => $thumbnails,
  225.                         'BreadCrumbs' => $BreadCrumbs,
  226.                         'isShowLeftMenu' => $isShowLeftMenu,
  227.                         'reCaptchaSiteKey' => $reCaptchaSiteKey,
  228.                         'routeName' => $routeName,
  229.                         'path_info' => $request->getPathInfo()
  230.                     ]);
  231.                 case 'complete':
  232.                     //
  233.                     $inquiryData $form->getData();
  234.                     //
  235.                     $tmpFiles $_SESSION['inquiry'] ?? []; // TODO: $_SESSIONの使用を廃止
  236.                     $this->mailService->createInquiryMail($inquiryData$tmpFiles);
  237.                     // 後処理
  238.                     unset($_SESSION['inquiry']);
  239.                     return $this->redirect($this->generateUrl($completeRouteName));
  240.             }
  241.         }
  242.         $main_h1 null;
  243.         $category_h1 null;
  244.         $h1_comment null;
  245.         $common_header_contents_all '';
  246.         $common_header_contents null;
  247.         $recentviews = [];
  248.         $custom_menu null;
  249.         $custom_frequently_searched_word null;
  250.         $data=[];
  251.         return [
  252.             'form' => $form->createView(),
  253.             'confirmRouteName' => $confirmRouteName,
  254.             'data' => $data,
  255.             'main_h1' => $main_h1,
  256.             'category_h1' => $category_h1,
  257.             'h1_comment' => $h1_comment,
  258.             'common_header_contents_all' => $common_header_contents_all,
  259.             'common_header_contents' => $common_header_contents,
  260.             'recentviews' => $recentviews,
  261.             'custom_menu' => $custom_menu,
  262.             'custom_frequently_searched_word' => $custom_frequently_searched_word,
  263.             'BreadCrumbs' => $BreadCrumbs,
  264.             'isShowLeftMenu' => $isShowLeftMenu,
  265.             'reCaptchaSiteKey' => $reCaptchaSiteKey,
  266.             'routeName' => $routeName
  267.         ];
  268.     }
  269.     /**
  270.      * Function trim space (full-width | normal-space) from string
  271.      *
  272.      * @param string $str Input string
  273.      * @return string
  274.      */
  275.     private function unicodeTrim ($str) {
  276.         return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);
  277.     }
  278.     /**
  279.      * お問い合わせ完了画面.
  280.      *
  281.      * @Route("/mail/complete/", name="contact_complete", methods={"GET"})
  282.      * #Route("/mypage/contact_complete/", name="mypage_contact_complete", methods={"GET"})
  283.      * @Template("Contact/complete.twig")
  284.      */
  285.     public function complete()
  286.     {
  287.         //
  288.         $request $this->request;
  289.         //
  290.         $routeName $request->get('_route');
  291.         $BreadCrumbs = [];
  292.         if (preg_match('/^((?:\S+_)?contact)/'$routeName$matches)) {
  293.             $BreadCrumbs = [
  294.                 => $this->__getBreadCrumbs_MyPageTop(),
  295.             ];
  296.         }
  297.         return [
  298.             'BreadCrumbs' => $BreadCrumbs,
  299.             'isShowLeftMenu' => true,
  300.         ];
  301.     }
  302.     /**
  303.      * @Route("/mail/question", name="mail_question", methods={"GET", "POST"})
  304.      * @Template("Mail/question.twig")
  305.      */
  306.     public function question(Request $request)
  307.     {
  308.         $goodsId $request->get('goods_id');
  309.         $goods $this->loadGoods($goodsId);
  310.         if (empty($goods)) {
  311.             return $this->redirectToRoute('contact');
  312.         }
  313.         $builder $this->formFactory->createBuilder(QuestionType::class);
  314.         $form $builder->getForm();
  315.         $form->handleRequest($request);
  316.         if ($form->isSubmitted() && $form->isValid()) {
  317.             return $this->render('Mail/question_confirm.twig', [
  318.                 'goods' => $goods,
  319.                 'form' => $form->createView(),
  320.                 'reCaptchaSiteKey' => $this->reCaptchaService->getReCaptchaSiteKey(),
  321.                 'path_info' => $request->getPathInfo()
  322.             ]);
  323.         }
  324.         return [
  325.             'goods' => $goods,
  326.             'form' => $form->createView(),
  327.             'reCaptchaSiteKey' => $this->reCaptchaService->getReCaptchaSiteKey()
  328.         ];
  329.     }
  330.     /**
  331.      * @Route("/mail/question_confirm", name="question_confirm", methods={"POST"})
  332.      * @Template("Mail/question_confirm.twig")
  333.      */
  334.     public function questionConfirm(Request $request)
  335.     {
  336.         $goodsId $request->get('goods_id');
  337.         $goods $this->loadGoods($goodsId);
  338.         if (empty($goods)) {
  339.             return $this->redirectToRoute('contact');
  340.         }
  341.         $builder $this->formFactory->createBuilder(QuestionType::class);
  342.         $form $builder->getForm();
  343.         $form->handleRequest($request);
  344.         if ($form->isSubmitted() && $form->isValid()) {
  345.             $this->reCaptchaService->handleRequest($request);
  346.             $this->mailQuestionService->createQuestion($goodsId$form->getData());
  347.             return $this->redirectToRoute('question_complete', ['goods_id' => $goodsId]);
  348.         }
  349.         return [
  350.             'goods' => $goods,
  351.             'form' => $form->createView(),
  352.             'reCaptchaSiteKey' => $this->reCaptchaService->getReCaptchaSiteKey()
  353.         ];
  354.     }
  355.     /**
  356.      * @Route("/mail/question_complete/", name="question_complete", methods={"GET"})
  357.      * @Template("Mail/question_complete.twig")
  358.      */
  359.     public function questionComplete(Request $request)
  360.     {
  361.         $goodsId $request->get('goods_id');
  362.         $goods $this->loadGoods($goodsId);
  363.         if (empty($goods)) {
  364.             return $this->redirectToRoute('contact');
  365.         }
  366.         return [
  367.             'goods' => $goods,
  368.         ];
  369.     }
  370.     private function loadGoods($goodsId)
  371.     {
  372.         $goods $this->mailQuestionService->getGoodsById($goodsId);
  373.         if (empty($goods)) {
  374.             return null;
  375.         }
  376.         $goods['gp_katabans'] = $this->mailQuestionService->getGoodsKatabansById($goodsId);
  377.         $displayPrice $this->dynamicConverter->LM_displayPriceList3($goodsId);
  378.         if ($displayPrice['mi'] != $displayPrice['ma']) {
  379.             $goods['prices'] = number_format($displayPrice['mi']) . '円~' number_format($displayPrice['ma']) . '円(税込)';
  380.         } else {
  381.             $goods['prices'] = number_format($displayPrice['mi']) . '円(税込)';
  382.         }
  383.         return $goods;
  384.     }
  385.     private function getNewPass()
  386.     {
  387.         $str "";
  388.         for ($i 0$i 32$i++) {
  389.             list ($usec$sec) = explode(' 'microtime());
  390.             $rnd = (float)$sec + ((float)$usec 100000);
  391.             mt_srand($rnd);
  392.             $rand_val mt_rand(035);
  393.             $str .= substr("abcdefghijklmnopqrstuvwxyz1234567890"$rand_val1);
  394.         }
  395.         return $str;
  396.     }
  397.     /**
  398.      * マイページトップのパンくず定義
  399.      * @return Array マイページトップのパンくず href, label
  400.      */
  401.     private function __getBreadCrumbs_MyPageTop()
  402.     {
  403.         return [
  404.             'href' => $this->generateUrl('mypage'),
  405.             'label' => $this->CommonService->GetPagename('mypage'),
  406.         ];
  407.     }
  408. }