app/Customize/Controller/ContactController.php line 101

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 Eccube\Controller\AbstractController;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Event\EccubeEvents;
  16. use Customize\Event\CustomEvents;
  17. use Customize\Form\Type\Front\OrderFormType;
  18. use Eccube\Form\Type\Admin\SearchProductType;
  19. use Eccube\Event\EventArgs;
  20. use Eccube\Form\Type\Front\ContactType;
  21. use Eccube\Repository\PageRepository;
  22. use Eccube\Repository\ProductRepository;
  23. use Eccube\Repository\LayoutRepository;
  24. use Customize\Service\MailService;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Eccube\Entity\Layout;
  29. use Eccube\Common\EccubeConfig;
  30. use Eccube\Repository\CategoryRepository;
  31. use Knp\Component\Pager\PaginatorInterface;
  32. use Eccube\Form\Type\AddCartType;
  33. class ContactController extends AbstractController
  34. {
  35.     /**
  36.      * @var EccubeConfig
  37.      */
  38.     protected $eccubeConfig;
  39.     /**
  40.      * @var ProductRepository
  41.      */
  42.     private $productRepository;
  43.     /**
  44.      * @var LayoutRepository
  45.      */
  46.     private $layoutRepository;
  47.     /**
  48.      * @var MailService
  49.      */
  50.     protected $mailService;
  51.     /**
  52.      * @var PageRepository
  53.      */
  54.     private $pageRepository;
  55.     /**
  56.      * @var CategoryRepository
  57.      */
  58.     protected $categoryRepository;
  59.     /**
  60.      * ContactController constructor.
  61.      *
  62.      * @param EccubeConfig $eccubeConfig
  63.      * @param MailService $mailService
  64.      * @param PageRepository $pageRepository
  65.      * @param CategoryRepository $categoryRepository
  66.      */
  67.     public function __construct(
  68.         MailService $mailService,
  69.         PageRepository $pageRepository,
  70.         ProductRepository $productRepository,
  71.         CategoryRepository $categoryRepository,
  72.         LayoutRepository $layoutRepository,
  73.         EccubeConfig $eccubeConfig
  74.     )
  75.     {
  76.         $this->eccubeConfig $eccubeConfig;
  77.         $this->mailService $mailService;
  78.         $this->pageRepository $pageRepository;
  79.         $this->productRepository $productRepository;
  80.         $this->layoutRepository $layoutRepository;
  81.         $this->categoryRepository $categoryRepository;
  82.     }
  83.     /**
  84.      * お問い合わせ画面.
  85.      *
  86.      * @Route("/contact", name="contact", methods={"GET", "POST"})
  87.      * @Route("/contact", name="contact_confirm", methods={"GET", "POST"})
  88.      * @Template("Contact/index.twig")
  89.      */
  90.     public function index(Request $request)
  91.     {
  92.         $Product null;
  93.         if ($product_id $request->query->get('product_id')) {
  94.             $Product $this->productRepository->find($product_id);
  95.         }
  96.         $builder $this->formFactory->createBuilder(ContactType::class);
  97.         if ($this->isGranted('ROLE_USER')) {
  98.             /** @var Customer $user */
  99.             $user $this->getUser();
  100.             $builder->setData(
  101.                 [
  102.                     'name01'        => $user->getName01(),
  103.                     'name02'        => $user->getName02(),
  104.                     'kana01'        => $user->getKana01(),
  105.                     'kana02'        => $user->getKana02(),
  106.                     'postal_code'   => $user->getPostalCode(),
  107.                     'pref'          => $user->getPref(),
  108.                     'addr01'        => $user->getAddr01(),
  109.                     'addr02'        => $user->getAddr02(),
  110.                     'phone_number'  => $user->getPhoneNumber(),
  111.                     'email'         => $user->getEmail(),
  112.                     'product_name'  => $Product $Product->getName() : null,
  113.                 ]
  114.             );
  115.         }
  116.         // FRONT_CONTACT_INDEX_INITIALIZE
  117.         $event = new EventArgs(
  118.             [
  119.                 'builder' => $builder,
  120.             ],
  121.             $request
  122.         );
  123.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
  124.         $form $builder->getForm();
  125.         $form['product_name']->setData($Product $Product->getName() : null);
  126.         $form['title']->setData('サンプル依頼');
  127.         $form->handleRequest($request);
  128.         if ($form->isSubmitted() && $form->isValid()) {
  129.             switch ($request->get('mode')) {
  130.                 case 'confirm':
  131.                     return $this->render('Contact/confirm.twig', [
  132.                         'form' => $form->createView(),
  133.                         'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
  134.                     ]);
  135.                 case 'complete':
  136.                     $data $form->getData();
  137.                     $event = new EventArgs(
  138.                         [
  139.                             'form' => $form,
  140.                             'data' => $data,
  141.                         ],
  142.                         $request
  143.                     );
  144.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
  145.                     $data $event->getArgument('data');
  146.                     // メール送信
  147.                     $this->mailService->sendContactMail($data);
  148.                     return $this->redirect($this->generateUrl('contact_complete'));
  149.             }
  150.         }
  151.         // 商品検索フォーム
  152.         $builder $this->formFactory
  153.             ->createBuilder(SearchProductType::class);
  154.         $event = new EventArgs(
  155.             [
  156.                 'builder' => $builder,
  157.             ],
  158.             $request
  159.         );
  160.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE);
  161.         $searchProductModalForm $builder->getForm();
  162.         return [
  163.             'form' => $form->createView(),
  164.             'searchProductModalForm' => $searchProductModalForm->createView(),
  165.         ];
  166.     }
  167.     /**
  168.      * @Route("/contact/search/product", name="order_search_product", methods={"GET", "POST"})
  169.      * @Route("/contact/search/product/page/{page_no}", requirements={"page_no" = "\d+"}, name="order_search_product_page", methods={"GET", "POST"})
  170.      * @Template("Contact/search_product.twig")
  171.      */
  172.     public function searchProduct(Request $requestPaginatorInterface $paginator$page_no null)
  173.     {
  174.         if ($request->isXmlHttpRequest() && $this->isTokenValid()) {
  175.             log_debug('search product start.');
  176.             $page_count $this->eccubeConfig['eccube_default_page_count'];
  177.             $session $this->session;
  178.             if ('POST' === $request->getMethod()) {
  179.                 $page_no 1;
  180.                 $searchData = [
  181.                     'id' => $request->get('id'),
  182.                 ];
  183.                 if ($categoryId $request->get('category_id')) {
  184.                     $Category $this->categoryRepository->find($categoryId);
  185.                     $searchData['category_id'] = $Category;
  186.                 }
  187.                 $session->set('eccube.admin.order.product.search'$searchData);
  188.                 $session->set('eccube.admin.order.product.search.page_no'$page_no);
  189.             } else {
  190.                 $searchData = (array) $session->get('eccube.admin.order.product.search');
  191.                 if (is_null($page_no)) {
  192.                     $page_no intval($session->get('eccube.admin.order.product.search.page_no'));
  193.                 } else {
  194.                     $session->set('eccube.admin.order.product.search.page_no'$page_no);
  195.                 }
  196.             }
  197.             $qb $this->productRepository
  198.                 ->getQueryBuilderBySearchDataForAdmin($searchData);
  199.             $event = new EventArgs(
  200.                 [
  201.                     'qb' => $qb,
  202.                     'searchData' => $searchData,
  203.                 ],
  204.                 $request
  205.             );
  206.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_SEARCH);
  207.             /** @var \Knp\Component\Pager\Pagination\SlidingPagination $pagination */
  208.             $pagination $paginator->paginate(
  209.                 $qb,
  210.                 $page_no,
  211.                 $page_count,
  212.                 ['wrap-queries' => true]
  213.             );
  214.             /** @var $Products \Eccube\Entity\Product[] */
  215.             $Products $pagination->getItems();
  216.             if (empty($Products)) {
  217.                 log_debug('search product not found.');
  218.             }
  219.             $forms = [];
  220.             foreach ($Products as $Product) {
  221.                 /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  222.                 $builder $this->formFactory->createNamedBuilder(''AddCartType::class, null, [
  223.                     'product' => $Product,
  224.                 ]);
  225.                 $addCartForm $builder->getForm();
  226.                 $forms[$Product->getId()] = $addCartForm->createView();
  227.             }
  228.             $event = new EventArgs(
  229.                 [
  230.                     'forms' => $forms,
  231.                     'Products' => $Products,
  232.                     'pagination' => $pagination,
  233.                 ],
  234.                 $request
  235.             );
  236.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_COMPLETE);
  237.             return [
  238.                 'forms' => $forms,
  239.                 'Products' => $Products,
  240.                 'pagination' => $pagination,
  241.             ];
  242.         }
  243.     }
  244.     /**
  245.      * お問い合わせ完了画面.
  246.      *
  247.      * @Route("/contact/complete", name="contact_complete", methods={"GET"})
  248.      * @Template("Contact/complete.twig")
  249.      */
  250.     public function complete()
  251.     {
  252.         return [];
  253.     }
  254.     /**
  255.      * オーダーフォーム送信画面.
  256.      *
  257.      * @Route("/contact/orderform", name="orderform", methods={"GET", "POST"})
  258.      * @Route("/contact/orderform/confirm", name="orderform_confirm", methods={"GET", "POST"})
  259.      * @Template("OrderForm/index.twig")
  260.      */
  261.     public function orderForm(Request $request)
  262.     {
  263.         $builder $this->formFactory->createBuilder(OrderFormType::class);
  264.         $LayoutDefault $this->layoutRepository->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
  265.         if ($this->isGranted('ROLE_USER')) {
  266.             /** @var Customer $user */
  267.             $user $this->getUser();
  268.             $builder->setData(
  269.                 [
  270.                     'name01'        => $user->getName01(),
  271.                     'name02'        => $user->getName02(),
  272.                     'kana01'        => $user->getKana01(),
  273.                     'kana02'        => $user->getKana02(),
  274.                     'postal_code'   => $user->getPostalCode(),
  275.                     'pref'          => $user->getPref(),
  276.                     'addr01'        => $user->getAddr01(),
  277.                     'addr02'        => $user->getAddr02(),
  278.                     'phone_number'  => $user->getPhoneNumber(),
  279.                     'email'         => $user->getEmail(),
  280.                 ]
  281.             );
  282.         }
  283.         // FRONT_ORDERFORM_INDEX_INITIALIZE
  284.         $event = new EventArgs(
  285.             [
  286.                 'builder' => $builder,
  287.             ],
  288.             $request
  289.         );
  290.         $this->eventDispatcher->dispatch($eventCustomEvents::FRONT_ORDERFORM_INDEX_INITIALIZE);
  291.         $form $builder->getForm();
  292.         $form->handleRequest($request);
  293.         if ($form->isSubmitted() && $form->isValid()) {
  294.             switch ($request->get('mode')) {
  295.                 case 'confirm':
  296.                     return $this->render('OrderForm/confirm.twig', [
  297.                         'form' => $form->createView(),
  298.                         'Page' => $this->pageRepository->getPageByRoute('orderform_confirm'),
  299.                         'Layout' => $LayoutDefault,
  300.                     ]);
  301.                 case 'complete':
  302.                     $data $form->getData();
  303.                     $event = new EventArgs(
  304.                         [
  305.                             'form' => $form,
  306.                             'data' => $data,
  307.                         ],
  308.                         $request
  309.                     );
  310.                     $this->eventDispatcher->dispatch($eventCustomEvents::FRONT_ORDERFORM_INDEX_COMPLETE);
  311.                     $data $event->getArgument('data');
  312.                     $border_line "\r\n ────────────────────────────────────────────────────────\r\n";
  313.                     $item_list "";
  314.                     for($c=1;$c<9;$c++){
  315.                         $item_list .= " ".
  316.                             str_pad($data['order_detail_block']["product_code".$c], 14" "STR_PAD_LEFT)." ".
  317.                             str_pad($data['order_detail_block']["product_name".$c], 40" "STR_PAD_LEFT)." ".
  318.                             str_pad($data['order_detail_block']["quantity".$c], 6" "STR_PAD_LEFT)." ".
  319.                             str_pad($data['order_detail_block']["print".$c], 14" "STR_PAD_LEFT)." ".
  320.                             str_pad($data['order_detail_block']["han".$c], 10" "STR_PAD_LEFT)." ".
  321.                             str_pad($data['order_detail_block']["color".$c], 18" "STR_PAD_LEFT).
  322.                             $border_line;
  323.                     }
  324.                     $data['item_list'] = $item_list;
  325.                     // メール送信
  326.                     $this->mailService->sendOrderFormMail($data);
  327.                     return $this->redirect($this->generateUrl('orderform_complete'));
  328.             }
  329.         }
  330.         return [
  331.             'form' => $form->createView(),
  332.             'Layout' => $LayoutDefault,
  333.         ];
  334.     }
  335.     /**
  336.      * お問い合わせ完了画面.
  337.      *
  338.      * @Route("/contact/orderform/complete", name="orderform_complete", methods={"GET"})
  339.      * @Template("OrderForm/complete.twig")
  340.      */
  341.     public function orderformComplete()
  342.     {
  343.         $LayoutDefault $this->layoutRepository->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
  344.         return ['Layout' => $LayoutDefault];
  345.     }
  346. }