app/Plugin/ProductReview4/Controller/ProductReviewController.php line 189

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\ProductReview4\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Entity\Master\ProductStatus;
  15. use Eccube\Entity\Product;
  16. use Eccube\Repository\OrderRepository;
  17. use Plugin\ProductReview4\Entity\ProductReview;
  18. use Plugin\ProductReview4\Entity\ProductReviewStatus;
  19. use Plugin\ProductReview4\Form\Type\ProductReviewType;
  20. use Plugin\ProductReview4\Repository\ProductReviewRepository;
  21. use Plugin\ProductReview4\Repository\ProductReviewStatusRepository;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. /**
  29.  * Class ProductReviewController front.
  30.  */
  31. class ProductReviewController extends AbstractController
  32. {
  33.     /**
  34.      * @var ProductReviewStatusRepository
  35.      */
  36.     private $productReviewStatusRepository;
  37.     /**
  38.      * @var ProductReviewRepository
  39.      */
  40.     private $productReviewRepository;
  41.     /**
  42.      * @var OrderRepository
  43.      */
  44.     private $orderRepository;
  45.     /**
  46.      * ProductReviewController constructor.
  47.      *
  48.      * @param ProductReviewStatusRepository $productStatusRepository
  49.      * @param ProductReviewRepository $productReviewRepository
  50.      * @param OrderRepository $orderRepository
  51.      */
  52.     public function __construct(
  53.         ProductReviewStatusRepository $productStatusRepository,
  54.         ProductReviewRepository $productReviewRepository,
  55.         OrderRepository $orderRepository
  56.     ) {
  57.         $this->productReviewStatusRepository $productStatusRepository;
  58.         $this->productReviewRepository $productReviewRepository;
  59.         $this->orderRepository $orderRepository;
  60.     }
  61.     /**
  62.      * @Route("/product_review/{id}/review", name="product_review_index", requirements={"id" = "\d+"})
  63.      * @Route("/product_review/{id}/review", name="product_review_confirm", requirements={"id" = "\d+"})
  64.      *
  65.      * @param Request $request
  66.      * @param Product $Product
  67.      *
  68.      * @return RedirectResponse|Response
  69.      */
  70.     public function index(Request $requestProduct $Product)
  71.     {
  72.         if (!$this->session->has('_security_admin') && $Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  73.             log_info('Product review', ['status' => 'Not permission']);
  74.             throw new NotFoundHttpException();
  75.         }
  76.         
  77.         // 同一ユーザーの投稿
  78.         $has_review false;
  79.         if ($this->isGranted('ROLE_USER')) {
  80.           $Customer $this->getUser();
  81.           $res $this->productReviewRepository->findBy(['Customer'=>$Customer'Product'=>$Product]);
  82.           if($res){
  83.             $has_review true;
  84.           }
  85.         }
  86.         
  87.         // 注文履歴があるか
  88.         $has_order false;
  89.         
  90.         // 数量とカラバリ
  91.         $order_class_name1 '';
  92.         $order_class_name2 '';
  93.         $order_class_category_name1 '';
  94.         $order_class_category_name2 '';
  95.         $order_quantity 0;
  96.         
  97.         if ($this->isGranted('ROLE_USER')) {
  98.           $res $this->orderRepository->findBy(['Customer'=>$Customer]);
  99.           foreach($res as $Order){
  100.             foreach($Order->getProductOrderItems() as $item){
  101.               if($item->isProduct()){
  102.                 $p $item->getProduct();
  103.                 if($p->getId() == $Product->getId()){
  104.                   $has_order true;
  105.                   
  106.                   $order_class_name1 $item->getClassName1();
  107.                   $order_class_category_name1 $item->getClassCategoryName1();
  108.                   $order_class_name2 $item->getClassName2();
  109.                   $order_class_category_name2 $item->getClassCategoryName2();
  110.                   
  111.                   $order_quantity $item->getQuantity();
  112.                   break;
  113.                 }
  114.               }
  115.             }
  116.             if($has_order){
  117.               break;
  118.             }
  119.           }
  120.         }
  121.         $ProductReview = new ProductReview();
  122.         $form $this->createForm(ProductReviewType::class, $ProductReview);
  123.         
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             /** @var $ProductReview ProductReview */
  127.             $ProductReview $form->getData();
  128.             switch ($request->get('mode')) {
  129.                 case 'confirm':
  130.                     log_info('Product review config confirm');
  131.                     return $this->render('ProductReview4/Resource/template/default/confirm.twig', [
  132.                         'form' => $form->createView(),
  133.                         'Product' => $Product,
  134.                         'ProductReview' => $ProductReview,
  135.                     ]);
  136.                     break;
  137.                 case 'complete':
  138.                     log_info('Product review complete');
  139.                     if ($this->isGranted('ROLE_USER')) {
  140.                         $Customer $this->getUser();
  141.                         $ProductReview->setCustomer($Customer);
  142.                     }
  143.                     $ProductReview->setProduct($Product);
  144.                     $ProductReview->setStatus($this->productReviewStatusRepository->find(ProductReviewStatus::HIDE));
  145.                     $this->entityManager->persist($ProductReview);
  146.                     $this->entityManager->flush($ProductReview);
  147.                     log_info('Product review complete', ['id' => $Product->getId()]);
  148.                     return $this->redirectToRoute('product_review_complete', ['id' => $Product->getId()]);
  149.                     break;
  150.                 case 'back':
  151.                     // 確認画面から投稿画面へ戻る
  152.                     break;
  153.                 default:
  154.                     // do nothing
  155.                     break;
  156.             }
  157.         }
  158.         
  159.         
  160.         
  161.         
  162.         return $this->render('ProductReview4/Resource/template/default/index.twig', [
  163.             'Product' => $Product,
  164.             'ProductReview' => $ProductReview,
  165.             'has_review' => $has_review,
  166.             'has_order' => $has_order,
  167.             'order_class_name1' => $order_class_name1,
  168.             'order_class_name2' => $order_class_name2,
  169.             'order_class_category_name1' => $order_class_category_name1,
  170.             'order_class_category_name2' => $order_class_category_name2,
  171.             'order_quantity' => $order_quantity,
  172.             'form' => $form->createView(),
  173.         ]);
  174.     }
  175.     /**
  176.      * Complete.
  177.      *
  178.      * @Route("/product_review/{id}/complete", name="product_review_complete", requirements={"id" = "\d+"})
  179.      * @Template("ProductReview4/Resource/template/default/complete.twig")
  180.      *
  181.      * @param $id
  182.      *
  183.      * @return array
  184.      */
  185.     public function complete($id)
  186.     {
  187.         return ['id' => $id];
  188.     }
  189.     /**
  190.      * ページ管理表示用のダミールーティング.
  191.      *
  192.      * @Route("/product_review/display", name="product_review_display")
  193.      */
  194.     public function display()
  195.     {
  196.         return new Response();
  197.     }
  198.     
  199.     /**
  200.      * 
  201.      *
  202.      * @Route("/products/reviews/{id}", name="product_review_list", requirements={"id" = "\d+"})
  203.      * @Template("ProductReview4/Resource/template/default/review_list.twig")
  204.      *
  205.      * @param Request $request
  206.      * @param Product $Product
  207.      *
  208.      */
  209.     public function product_review_list(Request $requestProduct $Product){
  210.       
  211.       
  212.       
  213.       // ----------------- //
  214.       // ページ設定
  215.       // ----------------- //
  216.       $Page = [
  217.           'url' => 'products',
  218.           'meta_tags' => array(),
  219.           'edit_type' => 0,
  220.           'description' => '',
  221.           'author' => '',
  222.           'keyword' => '',
  223.           'meta_robots' => '',
  224.       ];
  225.       
  226.       return ['Page'=>$Page'Product'=>$Product, ];
  227.     }
  228. }