src/Controller/IndexController.php line 168

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Component\ApiTrait;
  4. use App\Component\CartService;
  5. use App\Component\OptionTrait;
  6. use App\Component\RefererTrait;
  7. use App\Entity\Category;
  8. use App\Entity\Menu;
  9. use App\Entity\Post;
  10. use App\Entity\Shop;
  11. use App\Entity\ShopPlan;
  12. use App\Entity\User;
  13. use App\Form\Type\ContactType;
  14. use App\Form\Type\RegisterType;
  15. use App\Form\Type\UserRegisterInputType;
  16. use App\Form\Type\UserRegisterType;
  17. use App\Form\Type\UserRegisterVerifyType;
  18. use App\Security\OTPService;
  19. use App\Service\GoogleRecaptcha;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use GuzzleHttp\Client;
  22. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  23. use Knp\Component\Pager\PaginatorInterface;
  24. use Mailgun\Exception\HttpClientException;
  25. use Mailgun\Mailgun;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. use Symfony\Component\Form\FormError;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
  31. use Symfony\Component\RateLimiter\RateLimiterFactory;
  32. use Symfony\Component\Routing\Annotation\Route;
  33. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  34. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  35. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  36. use Symfony\Component\Security\Core\Security;
  37. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  38. use Symfony\Component\Validator\ConstraintViolation;
  39. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  40. use Symfony\Contracts\HttpClient\HttpClientInterface;
  41. /**
  42.  * @author Kazuyuki Hayashi <kaz@noop.co.jp>
  43.  */
  44. class IndexController extends AbstractController
  45. {
  46.     use RefererTrait;
  47.     use OptionTrait;
  48.     use ApiTrait;
  49.     /**
  50.      * トップページ
  51.      * @Route("/", name="index")
  52.      *
  53.      * @param EntityManagerInterface $em
  54.      * @return Response
  55.      */
  56.     public function index(EntityManagerInterface $em)
  57.     {
  58.         // クーポン非表示に伴いトップを店舗一覧に変更
  59.         return $this->redirectToRoute('search');
  60.         $locations $em->getRepository('App:Location')
  61.             ->createQueryBuilder('l')
  62.             ->leftJoin('l.area''a')
  63.             ->join('l.shops''s''WITH''s.public = 1 and s.ready = 1')
  64.             ->orderBy('a.position''ASC')
  65.             ->addOrderBy('l.position''ASC')
  66.             ->getQuery()
  67.             ->getResult();
  68.         $areas $em->getRepository('App:Area')
  69.             ->createQueryBuilder('a')
  70.             ->join('a.locations''l')
  71.             ->join('l.shops''s''WITH''s.public = 1 and s.ready = 1')
  72.             ->orderBy('a.position''ASC')
  73.             ->getQuery()
  74.             ->getResult();
  75.         $categories $em->getRepository('App:Category')
  76.             ->createQueryBuilder('c')
  77.             ->join('c.shops''s''WITH''s.public = 1 and s.ready = 1')
  78.             ->orderBy('c.position''ASC')
  79.             ->getQuery()
  80.             ->getResult();
  81.         return $this->render('index/index.html.twig', [
  82.             'areas' => $areas,
  83.             'locations' => $locations,
  84.             'categories' => $categories
  85.         ]);
  86.     }
  87.     /**
  88.      * @Route("/search", name="search")
  89.      * @param EntityManagerInterface $em
  90.      * @param PaginatorInterface     $paginator
  91.      * @param Request                $request
  92.      * @return Response
  93.      */
  94.     public function search(EntityManagerInterface $emPaginatorInterface $paginatorRequest $request)
  95.     {
  96.         $seed date('Ymd');
  97.         $areas $em->getRepository('App:Area')->findBy([], ['position' => 'ASC']);
  98.         $categories $em->getRepository('App:Category')->findBy([], ['position' => 'ASC']);
  99.         $area null;
  100.         $location null;
  101.         $category null;
  102.         $nextParam = [];
  103.         if ($request->get('a')) {
  104.             $area $em->getRepository('App:Area')->find($request->get('a'));
  105.             $nextParam['a'] = $request->get('a');
  106.         }
  107.         if ($request->get('l')) {
  108.             $location $em->getRepository('App:Location')->find($request->get('l'));
  109.             $nextParam['l'] = $request->get('l');
  110.         }
  111.         if ($request->get('c')) {
  112.             $category $em->getRepository('App:Category')->find($request->get('c'));
  113.             $nextParam['c'] = $request->get('c');
  114.         }
  115.         $builder $this->createShopListBuilder($em$request->get('a'), $request->get('l'), $request->get('c'), $request->get('pp'), $request->get('d'), $request->get('sa'));
  116.         /* @var SlidingPagination $pagination */
  117.         $pagination $paginator->paginate($builder$request->get('page'1), 20);
  118.         $pageInfo $pagination->getPaginationData();
  119.         /* @var SlidingPagination $pagination */
  120.         $hasNext = ($pageInfo['pageCount'] + 0) > $request->get('page'1);
  121.         $nextParam['page'] = $pagination->getCurrentPageNumber() + 1;
  122.         $totalShopCount $pagination->getTotalItemCount();
  123.         $cart null;
  124.         $user $this->getUser();
  125.         if ($user) {
  126.             $cart $em->getRepository('App:Cart')->getAvailableCartByUser($user);
  127.         }
  128.         return $this->render('index/search.html.twig', [
  129.             'areas' => $areas,
  130.             'categories' => $categories,
  131.             'area' => $area,
  132.             'location' => $location,
  133.             'category' => $category,
  134.             'pagination' => $pagination,
  135.             'pageInfo' => $pageInfo,
  136.             'nextParam' => $nextParam,
  137.             'hasNext' => $hasNext,
  138.             'totalShopCount' => $totalShopCount,
  139.             'cart' => $cart,
  140.         ]);
  141.     }
  142.     /**
  143.      * @Route("/explore", name="explore")
  144.      * @return Response
  145.      */
  146.     public function explore(EntityManagerInterface $em)
  147.     {
  148.         $locations $em->getRepository('App:Location')
  149.             ->createQueryBuilder('l')
  150.             ->leftJoin('l.area''a')
  151.             ->orderBy('a.position''ASC')
  152.             ->addOrderBy('l.position''ASC')
  153.             ->getQuery()
  154.             ->getResult();
  155.         $categories $em->getRepository('App:Category')->findBy([], ['position' => 'ASC']);
  156.         return $this->render('index/explore.html.twig', [
  157.             'locations' => $locations,
  158.             'categories' => $categories
  159.         ]);
  160.     }
  161.     /**
  162.      * @Route("/navi", name="navi")
  163.      * @return Response
  164.      */
  165.     public function navi()
  166.     {
  167.         return $this->render('index/navi.html.twig', [
  168.         ]);
  169.     }
  170.     /**
  171.      * LP
  172.      * @Route("/lp", name="lp")
  173.      *
  174.      * @param EntityManagerInterface $em
  175.      * @return Response
  176.      */
  177.     public function lp(EntityManagerInterface $em)
  178.     {
  179.         return $this->render('index/lp/index.html.twig', [
  180.         ]);
  181.     }
  182.     /**
  183.      * @Route("/law", name="law")
  184.      * @return Response
  185.      */
  186.     public function law(Request $request)
  187.     {
  188.         return $this->render('index/law.html.twig', [
  189.             'fromApp' => $request->query->has('app'),
  190.         ]);
  191.     }
  192.     /**
  193.      * @Route("/shop/{id}", name="shop")
  194.      * @param Shop $shop
  195.      * @return Response
  196.      */
  197.     public function shop(Shop $shop)
  198.     {
  199.         return $this->render('index/shop.html.twig', [
  200.             'shop' => $shop
  201.         ]);
  202.     }
  203.     /**
  204.      * @Route("/shop/{id}/law", name="shop_law")
  205.      * @param Shop $shop
  206.      * @return Response
  207.      */
  208.     public function shopLaw(Request $requestShop $shop)
  209.     {
  210.         return $this->render('index/shop_law.html.twig', [
  211.             'fromApp' => $request->query->has('app'),
  212.             'shop' => $shop,
  213.             'lawOptions' => $this->getLawOptionValues(),
  214.         ]);
  215.     }
  216.     /**
  217.      * @Route("/shop/menu/{id}", name="shop_menu_show")
  218.      * @param Menu $menu
  219.      * @return Response
  220.      */
  221.     public function shopMenuShow(Menu $menu)
  222.     {
  223.         return $this->render('index/menu_show.html.twig', [
  224.             'shop' => $menu->getShop(),
  225.             'menu' => $menu,
  226.         ]);
  227.     }
  228.     /**
  229.      * @Route("/user-register", name="user_register")
  230.      * @return Response
  231.      */
  232.     public function userRegister(Request $requestGoogleRecaptcha $googleRecaptchaRateLimiterFactory $otpApiLimiterOTPService $OTPServiceSecurity $security)
  233.     {
  234.         if ($this->getUser()) {
  235.             $token $security->getToken();
  236.             $_user $token->getUser();
  237.             if ($_user instanceof User) {
  238.                 return $this->redirectToRoute('user_dashboard');
  239.             }
  240.         }
  241.         $params $this->getRefererParams();
  242.         $this->container->get('session')->set('user-register-referer'$params);
  243.         $form $this->createForm(UserRegisterType::class);
  244.         $form->handleRequest($request);
  245.         if ($form->isSubmitted()){
  246.             if (! $googleRecaptcha->siteVerify($request)) {
  247.                 $googleRecaptchaError = new FormError('reCAPTCHAの認証に失敗しました。再度送信をお願いいたします。''', [], null'grecaptcha');
  248.                 $form->addError($googleRecaptchaError);
  249. //                return $this->redirectToRoute('register');
  250.             }
  251.             if ($form->isValid()) {
  252.                 $data $form->getData();
  253.                 // create a limiter based on a unique identifier of the client
  254.                 // (e.g. the client's IP address, a username/email, an API key, etc.)
  255.                 $limiter $otpApiLimiter->create($request->getClientIp());
  256.                 // the argument of consume() is the number of tokens to consume
  257.                 // and returns an object of type Limit
  258.                 if (false === $limiter->consume(1)->isAccepted()) {
  259.                     throw new TooManyRequestsHttpException();
  260.                 }
  261.                 if ($this->getParameter('kernel.environment') !== 'dev') {
  262.                     $OTPService->generateOTP($data['tel']);
  263.                 }
  264.                 $this->container->get('session')->set('user_register_tel'$data['tel']);
  265.                 return $this->redirectToRoute('user_register_verify');
  266.             }
  267.         }
  268.         $this->container->get('session')->remove('user_register_tel');
  269.         return $this->render('index/user_register/index.html.twig', [
  270.             'form' => $form->createView(),
  271.         ]);
  272.     }
  273.     /**
  274.      * AJAXで呼び出される想定
  275.      *
  276.      * @Route("/user-register/re-send", name="user_register_re_send")
  277.      * @return Response
  278.      */
  279.     public function userRegisterReSend(Request $requestRateLimiterFactory $otpApiLimiterOTPService $OTPService)
  280.     {
  281.         // create a limiter based on a unique identifier of the client
  282.         // (e.g. the client's IP address, a username/email, an API key, etc.)
  283.         $limiter $otpApiLimiter->create($request->getClientIp());
  284.         // the argument of consume() is the number of tokens to consume
  285.         // and returns an object of type Limit
  286.         if (false === $limiter->consume(1)->isAccepted()) {
  287.             throw new TooManyRequestsHttpException();
  288.         }
  289.         $tel $this->container->get('session')->get('user_register_tel');
  290.         if (! $tel) {
  291.             return $this->redirectToRoute('user_register');
  292.         }
  293.         $OTPService->generateOTP($tel);
  294.         $this->container->get('session')->set('user_register_tel'$tel);
  295.         return $this->json([]);
  296.     }
  297.     /**
  298.      * @Route("/user-register/verify", name="user_register_verify")
  299.      * @return Response
  300.      */
  301.     public function userRegisterVerify(EventDispatcherInterface $eventDispatcherEntityManagerInterface $emRequest $requestOTPService $OTPServiceCartService $cartService)
  302.     {
  303.         $form $this->createForm(UserRegisterVerifyType::class);
  304.         $tel $this->container->get('session')->get('user_register_tel');
  305.         if (! $tel) {
  306.             return $this->redirectToRoute('user_register');
  307.         }
  308.         $form->handleRequest($request);
  309.         if ($form->isSubmitted() && $form->isValid()) {
  310.             $data $form->getData();
  311.             $isValid false;
  312.             if ($this->getParameter('kernel.environment') === 'dev') {
  313.                 $isValid true;
  314.             } else {
  315.                 $isValid $OTPService->isValidOTP($data['otp'], $tel);
  316.             }
  317.             if (! $isValid) {
  318.                 $form->get('otp')->addError(new FormError('PINが正しくありません。'));
  319.                 // 入力エラーがある場合はスルーしてverify.html.twigを表示
  320.             } else {
  321.                 $user $em->getRepository('App:User')->createQueryBuilder('u')
  322.                     ->andWhere('u.deletedAt is null')
  323.                     ->andWhere('u.tel = :tel')
  324.                     ->setParameter('tel'$tel)
  325.                     ->setMaxResults(1)
  326.                     ->getQuery()
  327.                     ->getOneOrNullResult()
  328.                 ;
  329.                 if ($user) {
  330.                     // 登録済みの場合、ログインして元のページにリダイレクト
  331.                     $token = new UsernamePasswordToken($user'user'$user->getRoles());
  332.                     $this->container->get('security.token_storage')->setToken($token);
  333.                     $this->container->get('session')->set('_security_main'serialize($token));
  334.                     $loginEvent = new InteractiveLoginEvent($request$token);
  335.                     $eventDispatcher->dispatch($loginEvent'security.interactive_login');
  336. //                    $cart = $em->getRepository('App:Cart')->getAvailableCartByUser($user);
  337. //                    if ($cart) {
  338. //                        // 有効なカートがある場合はセッションにカートIDを追加
  339. //                        $this->container->get('session')->set(CartSessionStorage::CART_KEY_NAME, $cart->getId());
  340. //                    }
  341. //                    dump($this->getUser());
  342.                     if (! $this->container->get('session')->has('auth_adding_item')) {
  343. //                        dd('auth_adding');
  344.                         // カート追加以外の登録またはログイン=元のページに戻す
  345.                         $pathInfo $this->container->get('session')->get('user-register-referer');
  346.                         $params = [];
  347.                         foreach ($pathInfo as $key => $value) {
  348.                             if ($key != '_route' && $key != '_controller') {
  349.                                 $params[$key] = $value;
  350.                             }
  351.                         }
  352.                         return $this->redirectToRoute($pathInfo['_route'], $params);
  353.                     } else {
  354. //                        dd('to cart_index');
  355.                         $cart $cartService->getOrCreateCart($user);
  356.                         if ($cart->getShop()) {
  357.                             return $this->redirectToRoute('shop', [
  358.                                 'id' => $cart->getShop()->getId(),
  359.                             ]);
  360.                         }
  361.                         return $this->redirectToRoute('cart_index');
  362.                     }
  363.                 } else {
  364.                     // 未登録の場合、入力画面へ
  365.                     return $this->redirectToRoute('user_register_input');
  366.                 }
  367.             }
  368.         }
  369.         return $this->render('index/user_register/verify.html.twig', [
  370.             'form' => $form->createView(),
  371.         ]);
  372.     }
  373.     /**
  374.      * @Route("/user-register/input", name="user_register_input")
  375.      * @param EntityManagerInterface $em
  376.      * @param Request $request
  377.      * @param UserPasswordEncoderInterface $passwordEncoder
  378.      * @param OTPService $OTPService
  379.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  380.      * @throws \Psr\Container\ContainerExceptionInterface
  381.      * @throws \Psr\Container\NotFoundExceptionInterface
  382.      */
  383.     public function userRegisterInput(EventDispatcherInterface $eventDispatcherEntityManagerInterface $emRequest $requestUserPasswordEncoderInterface $passwordEncoderOTPService $OTPService)
  384.     {
  385.         $user = new User();
  386.         $form $this->createForm(UserRegisterInputType::class, $user);
  387.         $tel $this->container->get('session')->get('user_register_tel');
  388.         if (! $tel) {
  389.             return $this->redirectToRoute('user_register');
  390.         }
  391.         $form->handleRequest($request);
  392.         if ($form->isSubmitted() && $form->isValid()) {
  393.             $data $form->getData();
  394.             $user->setTel($tel);
  395.             $user->setPassword($passwordEncoder->encodePassword($userrandom_bytes(32)));
  396.             $em->persist($user);
  397.             $em->flush();
  398.             $this->container->get('session')->remove('user_register_tel');
  399.             $token = new UsernamePasswordToken($user'user'$user->getRoles());
  400.             $this->container->get('security.token_storage')->setToken($token);
  401.             $this->container->get('session')->set('_security_main'serialize($token));
  402.             $loginEvent = new InteractiveLoginEvent($request$token);
  403.             $eventDispatcher->dispatch($loginEvent'security.interactive_login');
  404.             return $this->redirectToRoute('user_register_done');
  405.         }
  406.         return $this->render('index/user_register/input.html.twig', [
  407.             'form' => $form->createView(),
  408.             'tel' => $tel,
  409.         ]);
  410.     }
  411.     /**
  412.      * @Route("/user-register/done", name="user_register_done")
  413.      * @return Response
  414.      */
  415.     public function userRegisterDone(EntityManagerInterface $em)
  416.     {
  417.         $user $this->getUser();
  418.         $cart $em->getRepository('App:Cart')->getAvailableCartByUser($user);
  419.         $cartItemLength 0;
  420.         $shopId null;
  421.         if ($cart) {
  422.             $cartItemLength count($cart->getCartItems());
  423.             if ($cart->getShop()) {
  424.                 $shopId $cart->getShop()->getId();
  425.             }
  426.         }
  427.         return $this->render('index/user_register/done.html.twig', [
  428.             'cartItemLength' => $cartItemLength,
  429.             'shopId' => $shopId,
  430.         ]);
  431.     }
  432.     /**
  433.      * @Route("/area", name="area")
  434.      * @return Response
  435.      */
  436.     public function area()
  437.     {
  438.         return $this->render('index/area.html.twig', [
  439.         ]);
  440.     }
  441. //    /**
  442. //     * @Route("/coupon", name="coupon")
  443. //     * @return Response
  444. //     */
  445. //    public function coupon()
  446. //    {
  447. //        return $this->render('index/coupon.html.twig', [
  448. //        ]);
  449. //    }
  450.     /**
  451.      * @Route("/faq", name="faq")
  452.      * @return Response
  453.      */
  454.     public function faq(EntityManagerInterface $em)
  455.     {
  456.         $questionCategories $em->getRepository('App:QuestionCategory')
  457.             ->createQueryBuilder('qc')
  458.             ->andWhere('qc.public = 1')
  459.             ->andWhere('qc.toUser = 1')
  460.             ->orderBy('qc.position')
  461.             ->getQuery()
  462.             ->getResult();
  463.         return $this->render('index/faq.html.twig', [
  464.             'questionCategories' => $questionCategories,
  465.         ]);
  466.     }
  467.     /**
  468.      * @Route("/news", name="news")
  469.      * @return Response
  470.      */
  471.     public function news(EntityManagerInterface $emRequest $requestPaginatorInterface $paginator)
  472.     {
  473.         $builder $em->getRepository('App:Post')->createQueryBuilder('p')
  474.             ->andWhere('p.toUser = 1')
  475.             ->andWhere('p.publishedAt <= :now')
  476.             ->setParameter('now', new \DateTime())
  477.             ->orderBy('p.publishedAt''DESC');
  478.         ;
  479.         /* @var SlidingPagination $pagination */
  480.         $pagination $paginator->paginate($builder$request->get('page'1), 20);
  481.         $pageInfo $pagination->getPaginationData();
  482.         return $this->render('index/news/index.html.twig', [
  483.             'pagination' => $pagination,
  484.             'pageInfo' => $pageInfo,
  485.         ]);
  486.     }
  487.     /**
  488.      * @Route("/news/{id}", name="news_show")
  489.      * @return Response
  490.      */
  491.     public function newsShow(Post $postRequest $request)
  492.     {
  493.         return $this->render('index/news/show.html.twig', [
  494.             'post' => $post,
  495.             'fromApp' => $request->query->has('app'),
  496.         ]);
  497.     }
  498.     /**
  499.      * @Route("/map", name="map")
  500.      * @param EntityManagerInterface $em
  501.      * @return Response
  502.      */
  503.     public function map(EntityManagerInterface $em)
  504.     {
  505.         $shops $em->getRepository('App:Shop')->findBy(['public' => true'ready' => true]);
  506.         return $this->render('index/map.html.twig', [
  507.             'shops' => $shops
  508.         ]);
  509.     }
  510.     /**
  511.      * @Route("/pins", name="pins")
  512.      * @param Request                $request
  513.      * @param EntityManagerInterface $em
  514.      */
  515.     public function pins(Request $requestEntityManagerInterface $em)
  516.     {
  517.         $lat1 = (float) $request->get('lat1');
  518.         $lng1 = (float) $request->get('lng1');
  519.         $lat2 = (float) $request->get('lat2');
  520.         $lng2 = (float) $request->get('lng2');
  521.         /* @var Shop[] $shops */
  522.         $shops $em->getRepository('App:Shop')->createQueryBuilder('s')
  523.             ->andWhere('s.coordinate.latitude > :lat1')
  524.             ->andWhere('s.coordinate.longitude > :lng1')
  525.             ->andWhere('s.coordinate.latitude < :lat2')
  526.             ->andWhere('s.coordinate.longitude < :lng2')
  527.             ->andWhere('s.public = 1')
  528.             ->andWhere('s.ready = 1')
  529.             // 富山駅(初期値)を表示する場合はこの行を削除してください。
  530.             ->andWhere('(s.coordinate.latitude <> 36.7018268 and s.coordinate.longitude <> 137.2126258)')
  531.             ->setParameter('lat1'$lat1)
  532.             ->setParameter('lng1'$lng1)
  533.             ->setParameter('lat2'$lat2)
  534.             ->setParameter('lng2'$lng2)
  535.             ->getQuery()
  536.             ->getResult();
  537.         $json = [];
  538.         foreach ($shops as $shop) {
  539.             $json[$shop->getId()] = [
  540.                 'id' => $shop->getId(),
  541.                 'latitude' => $shop->getCoordinate()->getLatitude(),
  542.                 'longitude' => $shop->getCoordinate()->getLongitude(),
  543.                 'name' => $shop->getName(),
  544.                 'category' => implode(' / 'array_map(function (Category $category) {
  545.                     return $category->getName();
  546.                 }, $shop->getCategories()->toArray())),
  547.                 'path' => $this->generateUrl('shop', ['id' => $shop->getId()]),
  548.                 'minutes' => $shop->getEstimatedMinutes() ?: '-',
  549.                 'toGo' => $shop->getToGo(),
  550.                 'delivery' => $shop->getDelivery(),
  551.                 'menus' => array_map(function (Menu $menu) {
  552.                     return [
  553.                         'image' => $menu->getImage() ?: '/img/no-image.jpeg',
  554.                         'name' => $menu->getName(),
  555.                     ];
  556.                 }, $shop->getMenus()->toArray()),
  557.                 'html' => $this->renderView('index/map_panel.html.twig',  ['shop' => $shop])
  558.             ];
  559.         }
  560.         return $this->json($json);
  561.     }
  562.     /**
  563.      * @Route("/about", name="about")
  564.      * @return Response
  565.      */
  566.     public function about()
  567.     {
  568.         return $this->render('index/about.html.twig', []);
  569.     }
  570.     /**
  571.      * @Route("/campaign", name="campaign")
  572.      * @return Response
  573.      */
  574.     public function campaign()
  575.     {
  576.         return $this->render('index/campaign.html.twig', []);
  577.     }
  578.     /**
  579.      * @Route("/terms", name="terms")
  580.      * @return Response
  581.      */
  582.     public function terms(Request $request)
  583.     {
  584.         return $this->render('index/terms.html.twig', [
  585.             'fromApp' => $request->query->has('app'),
  586.         ]);
  587.     }
  588.     /**
  589.      * @Route("/privacy", name="privacy")
  590.      * @return Response
  591.      */
  592.     public function privacy(Request $request)
  593.     {
  594.         return $this->render('index/privacy.html.twig', [
  595.             'fromApp' => $request->query->has('app'),
  596.         ]);
  597.     }
  598.     /**
  599.      * @Route("/links", name="links")
  600.      * @return Response
  601.      */
  602.     public function links()
  603.     {
  604.         return $this->render('index/links.html.twig', []);
  605.     }
  606.     /**
  607.      * @Route("/register", name="register")
  608.      * @see \App\Controller\Api\ShopController::shopRegister()
  609.      * @param EntityManagerInterface       $em
  610.      * @param UserPasswordEncoderInterface $passwordEncoder
  611.      * @param Request                      $request
  612.      * @return Response
  613.      */
  614.     public function register(GoogleRecaptcha $googleRecaptchaEntityManagerInterface $emUserPasswordEncoderInterface $passwordEncoderRequest $request)
  615.     {
  616.         $shop = new Shop();
  617.         $shop->setReady(false);
  618.         $form $this->createForm(RegisterType::class, $shop);
  619.         $form->handleRequest($request);
  620.         if ($form->isSubmitted()) {
  621.             if (! $googleRecaptcha->siteVerify($request)) {
  622.                 $googleRecaptchaError = new FormError('reCAPTCHAの認証に失敗しました。再度送信をお願いいたします。''', [], null'grecaptcha');
  623.                 $form->addError($googleRecaptchaError);
  624. //                return $this->redirectToRoute('register');
  625.             }
  626.             if ($form->isValid()) {
  627.                 $shopPlan $em->getRepository('App:ShopPlan')->createQueryBuilder('sp')
  628.                     ->andWhere('sp.slug = :plan')
  629.                     ->setParameter('plan'ShopPlan::SLUG_STANDARD)
  630.                     ->setMaxResults(1)
  631.                     ->getQuery()
  632.                     ->getOneOrNullResult();
  633.                 $shop->setShopPlan($shopPlan);
  634.                 $shop->setPassword($passwordEncoder->encodePassword($shop$shop->getPlainPassword()));
  635.                 $em->persist($shop);
  636.                 $em->flush();
  637.                 try {
  638.                     $mailgun Mailgun::create($_ENV['MAILGUN_API_KEY']);
  639.                     $mailgun->messages()->send($_ENV['MAILGUN_DOMAIN'], array(
  640.                         'from'    => $_ENV['MAIL_FROM_ADDRESS'],
  641.                         'to'    => $shop->getEmail(),
  642.                         'bcc'  => $_ENV['MAIL_TO_ADDRESS'],
  643.                         'subject' => '掲載申し込み',
  644.                         //'text'    => $this->renderView('index/register.eml.twig', ['shop' => $shop])
  645.                         'template' => 'registration',
  646.                         'h:X-Mailgun-Variables' => json_encode([
  647.                             'url' => $this->generateUrl('shop_dashboard', [], UrlGeneratorInterface::ABSOLUTE_URL),
  648.                             'name' => $shop->getName(),
  649.                             'homepage' => 'https://uchideri.com'
  650.                         ])
  651.                     ));
  652.                 } catch (HttpClientException $e) {
  653.                 } catch (\Exception $e) {
  654.                 } catch (\Throwable $e) {
  655.                 }
  656.                 return $this->redirectToRoute('register_complete');
  657.             }
  658.         }
  659.         return $this->render('index/register.html.twig', [
  660.             'form' => $form->createView()
  661.         ]);
  662.     }
  663.     /**
  664.      * @Route("/register/complete", name="register_complete")
  665.      * @return Response
  666.      */
  667.     public function registerComplete()
  668.     {
  669.         return $this->render('index/register_complete.html.twig', []);
  670.     }
  671.     /**
  672.      * @Route("/contact", name="contact")
  673.      * @return Response
  674.      */
  675.     public function contact(Request $request)
  676.     {
  677.         $form $this->createForm(ContactType::class);
  678.         $form->handleRequest($request);
  679.         if ($form->isSubmitted() && $form->isValid()) {
  680.             $data $form->getData();
  681.             // to-do email
  682.             $mailgun Mailgun::create($_ENV['MAILGUN_API_KEY']);
  683.             try {
  684.                 $mailgun->messages()->send($_ENV['MAILGUN_DOMAIN'], array(
  685.                     'from'    => $_ENV['MAIL_FROM_ADDRESS'],
  686.                     'to'    => $_ENV['MAIL_TO_ADDRESS'],
  687. //                    'bcc'   => 'kaz@noop.co.jp',
  688.                     'subject' => 'ホームページからのお問い合わせ',
  689.                     //'text'    => $this->renderView('index/register.eml.twig', ['shop' => $shop])
  690.                     'template' => 'contact.notification',
  691.                     'h:X-Mailgun-Variables' => json_encode([
  692.                         'name' => $data['name'],
  693.                         'tel' => $data['tel'],
  694.                         'email' => $data['email'],
  695.                         'content' => $data['content'],
  696.                         'date' => date('Y/m/d H:i:s')
  697.                     ])
  698.                 ));
  699.             } catch (HttpClientException $e) {
  700.             } catch (\Exception $e) {
  701.             } catch (\Throwable $e) {
  702.             }
  703.             try {
  704.                 $mailgun->messages()->send($_ENV['MAILGUN_DOMAIN'], array(
  705.                     'from'    => $_ENV['MAIL_FROM_ADDRESS'],
  706.                     'to'    => $data['email'],
  707.                     'subject' => 'お問い合わせありがとうございます',
  708.                     //'text'    => $this->renderView('index/register.eml.twig', ['shop' => $shop])
  709.                     'template' => 'contact',
  710.                     'h:X-Mailgun-Variables' => json_encode([
  711.                         'url' => $this->generateUrl('index', [], UrlGeneratorInterface::ABSOLUTE_URL),
  712.                         'name' => $data['name']
  713.                     ])
  714.                 ));
  715.             } catch (HttpClientException $e) {
  716.             } catch (\Exception $e) {
  717.             } catch (\Throwable $e) {
  718.             }
  719.             return $this->redirectToRoute('contact_complete');
  720.         }
  721.         return $this->render('index/contact.html.twig', [
  722.             'form' => $form->createView()
  723.         ]);
  724.     }
  725.     /**
  726.      * @Route("/contact/complete", name="contact_complete")
  727.      * @return Response
  728.      */
  729.     public function contactComplete()
  730.     {
  731.         return $this->render('index/contact_complete.html.twig', []);
  732.     }
  733. }