app/Plugin/Recommend42/Controller/RecommendController.php line 88

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 Plugin\Recommend42\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Form\Type\Admin\SearchProductType;
  15. use Plugin\Recommend42\Entity\RecommendProduct;
  16. use Plugin\Recommend42\Form\Type\RecommendProductType;
  17. use Plugin\Recommend42\Repository\RecommendProductRepository;
  18. use Plugin\Recommend42\Service\RecommendService;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\Form\Form;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. /**
  26.  * Class RecommendController.
  27.  */
  28. class RecommendController extends AbstractController
  29. {
  30.     /**
  31.      * @var RecommendProductRepository
  32.      */
  33.     private $recommendProductRepository;
  34.     /**
  35.      * @var RecommendService
  36.      */
  37.     private $recommendService;
  38.     /**
  39.      * RecommendController constructor.
  40.      *
  41.      * @param RecommendProductRepository $recommendProductRepository
  42.      * @param RecommendService $recommendService
  43.      */
  44.     public function __construct(RecommendProductRepository $recommendProductRepositoryRecommendService $recommendService)
  45.     {
  46.         $this->recommendProductRepository $recommendProductRepository;
  47.         $this->recommendService $recommendService;
  48.     }
  49.     /**
  50.      * おすすめ商品一覧.
  51.      *
  52.      * @param Request     $request
  53.      *
  54.      * @return array
  55.      * @Route("/%eccube_admin_route%/plugin/recommend", name="plugin_recommend_list")
  56.      * @Template("@Recommend42/admin/index.twig")
  57.      */
  58.     public function index(Request $request)
  59.     {
  60.         $pagination $this->recommendProductRepository->getRecommendList();
  61.         return [
  62.             'pagination' => $pagination,
  63.             'total_item_count' => count($pagination),
  64.         ];
  65.     }
  66.     /**
  67.      * Create & Edit.
  68.      *
  69.      * @param Request     $request
  70.      * @param int         $id
  71.      *
  72.      * @throws \Exception
  73.      *
  74.      * @return array|RedirectResponse
  75.      * @Route("/%eccube_admin_route%/plugin/recommend/new", name="plugin_recommend_new")
  76.      * @Route("/%eccube_admin_route%/plugin/recommend/{id}/edit", name="plugin_recommend_edit", requirements={"id" = "\d+"})
  77.      * @Template("@Recommend42/admin/regist.twig")
  78.      */
  79.     public function edit(Request $request$id null)
  80.     {
  81.         /* @var RecommendProduct $Recommend */
  82.         $Recommend null;
  83.         $Product null;
  84.         if (!is_null($id)) {
  85.             // IDからおすすめ商品情報を取得する
  86.             $Recommend $this->recommendProductRepository->find($id);
  87.             if (!$Recommend) {
  88.                 $this->addError('plugin_recommend.admin.not_found''admin');
  89.                 log_info('The recommend product is not found.', ['Recommend id' => $id]);
  90.                 return $this->redirectToRoute('plugin_recommend_list');
  91.             }
  92.             $Product $Recommend->getProduct();
  93.         }
  94.         // formの作成
  95.         /* @var Form $form */
  96.         $form $this->formFactory
  97.             ->createBuilder(RecommendProductType::class, $Recommend)
  98.             ->getForm();
  99.         $form->handleRequest($request);
  100.         $data $form->getData();
  101.         if ($form->isSubmitted() && $form->isValid()) {
  102.             $service $this->recommendService;
  103.             if (is_null($data['id'])) {
  104.                 if ($status $service->createRecommend($data)) {
  105.                     $this->addSuccess('plugin_recommend.admin.register.success''admin');
  106.                     log_info('Add the new recommend product success.', ['Product id' => $data['Product']->getId()]);
  107.                 }
  108.             } else {
  109.                 if ($status $service->updateRecommend($data)) {
  110.                     $this->addSuccess('plugin_recommend.admin.update.success''admin');
  111.                     log_info('Update the recommend product success.', ['Recommend id' => $Recommend->getId(), 'Product id' => $data['Product']->getId()]);
  112.                 }
  113.             }
  114.             if (!$status) {
  115.                 $this->addError('plugin_recommend.admin.not_found''admin');
  116.                 log_info('Failed the recommend product updating.', ['Product id' => $data['Product']->getId()]);
  117.             }
  118.             return $this->redirectToRoute('plugin_recommend_list');
  119.         }
  120.         if (!empty($data['Product'])) {
  121.             $Product $data['Product'];
  122.         }
  123.         $arrProductIdByRecommend $this->recommendProductRepository->getRecommendProductIdAll();
  124.         return $this->registerView(
  125.             [
  126.                 'form' => $form->createView(),
  127.                 'recommend_products' => json_encode($arrProductIdByRecommend),
  128.                 'Product' => $Product,
  129.             ]
  130.         );
  131.     }
  132.     /**
  133.      * おすすめ商品の削除.
  134.      *
  135.      * @param Request     $request
  136.      * @param RecommendProduct $RecommendProduct
  137.      *
  138.      * @throws \Exception
  139.      *
  140.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  141.      * @Route("/%eccube_admin_route%/plugin/recommend/{id}/delete", name="plugin_recommend_delete", requirements={"id" = "\d+"}, methods={"DELETE"})
  142.      */
  143.     public function delete(Request $requestRecommendProduct $RecommendProduct)
  144.     {
  145.         // Valid token
  146.         $this->isTokenValid();
  147.         // おすすめ商品情報を削除する
  148.         if ($this->recommendProductRepository->deleteRecommend($RecommendProduct)) {
  149.             log_info('The recommend product delete success!', ['Recommend id' => $RecommendProduct->getId()]);
  150.             $this->addSuccess('plugin_recommend.admin.delete.success''admin');
  151.         } else {
  152.             $this->addError('plugin_recommend.admin.not_found''admin');
  153.             log_info('The recommend product is not found.', ['Recommend id' => $RecommendProduct->getId()]);
  154.         }
  155.         return $this->redirectToRoute('plugin_recommend_list');
  156.     }
  157.     /**
  158.      * Move rank with ajax.
  159.      *
  160.      * @param Request     $request
  161.      *
  162.      * @throws \Exception
  163.      *
  164.      * @return Response
  165.      *
  166.      * @Route("/%eccube_admin_route%/plugin/recommend/sort_no/move", name="plugin_recommend_rank_move")
  167.      */
  168.     public function moveRank(Request $request)
  169.     {
  170.         if ($request->isXmlHttpRequest()) {
  171.             $arrRank $request->request->all();
  172.             $arrRankMoved $this->recommendProductRepository->moveRecommendRank($arrRank);
  173.             log_info('Recommend move rank'$arrRankMoved);
  174.         }
  175.         return new Response('OK');
  176.     }
  177.     /**
  178.      * 編集画面用のrender.
  179.      *
  180.      * @param array       $parameters
  181.      *
  182.      * @return array
  183.      */
  184.     protected function registerView($parameters = [])
  185.     {
  186.         // 商品検索フォーム
  187.         $searchProductModalForm $this->formFactory->createBuilder(SearchProductType::class)->getForm();
  188.         $viewParameters = [
  189.             'searchProductModalForm' => $searchProductModalForm->createView(),
  190.         ];
  191.         $viewParameters += $parameters;
  192.         return $viewParameters;
  193.     }
  194. }