app/Customize/Controller/RecommendedController.php line 66

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