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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.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\Entity\OrderItem;
  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. use Knp\Component\Pager\Paginator;
  29. use Eccube\Repository\OrderItemRepository;
  30. use Eccube\Repository\ProductRepository;
  31. use Eccube\Service\ProductReviewService;
  32. /**
  33.  * Class ProductReviewController front.
  34.  */
  35. class ProductReviewController extends AbstractController
  36. {
  37.     /**
  38.      * @var ProductReviewStatusRepository
  39.      */
  40.     private $productReviewStatusRepository;
  41.     /**
  42.      * @var ProductReviewRepository
  43.      */
  44.     private $productReviewRepository;
  45.     private $orderItemRepository;
  46.     private $productReviewService;
  47.     private $productRepository;
  48.     /**
  49.      * ProductReviewController constructor.
  50.      *
  51.      * @param ProductReviewStatusRepository $productStatusRepository
  52.      * @param ProductReviewRepository $productReviewRepository
  53.      */
  54.     public function __construct(
  55.         ProductReviewStatusRepository $productStatusRepository,
  56.         ProductReviewRepository $productReviewRepository,
  57.         OrderItemRepository $orderItemRepository,
  58.         ProductReviewService $productReviewService,
  59.         ProductRepository $productRepository
  60.     ) {
  61.         $this->productReviewStatusRepository $productStatusRepository;
  62.         $this->productReviewRepository $productReviewRepository;
  63.         $this->orderItemRepository $orderItemRepository;
  64.         $this->productReviewService $productReviewService;
  65.         $this->productRepository $productRepository;
  66.     }
  67.     /**
  68.      * @Route("/product_review/{id}/review", name="product_review_index", requirements={"id" = "\d+"})
  69.      *
  70.      * @param Request $request
  71.      * @param Product $Product
  72.      *
  73.      * @return RedirectResponse|Response
  74.      */
  75.     public function index(Request $requestProduct $Product)
  76.     {
  77.         if (!$this->session->has('_security_admin') && $Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  78.             //log_info('Product review', ['status' => 'Not permission']);
  79.             //throw new NotFoundHttpException();
  80.             return;
  81.         }
  82.         $ProductReview = new ProductReview();
  83.         $form $this->createForm(ProductReviewType::class, $ProductReview);
  84.         $order_item_id $request->get('order_item_id') ? $request->get('order_item_id') : 0;
  85.         $add_point 0;
  86.         
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             /** @var $ProductReview ProductReview */
  90.             $ProductReview $form->getData();
  91.             switch ($request->get('mode')) {
  92.                 case 'confirm':
  93.                     log_info('Product review config confirm');
  94.                     return $this->render('@ProductReview4/default/confirm.twig', [
  95.                         'form' => $form->createView(),
  96.                         'Product' => $Product,
  97.                         'ProductReview' => $ProductReview,
  98.                         'order_item_id' => $order_item_id,
  99.                     ]);
  100.                     break;
  101.                 case 'complete':
  102.                     log_info('Product review complete');
  103.                     if ($this->isGranted('ROLE_USER')) {
  104.                         $Customer $this->getUser();
  105.                         $ProductReview->setCustomer($Customer);
  106.                         if($order_item_id 0){
  107.                             $OrderItem $this->orderItemRepository->find($order_item_id);
  108.                             if($OrderItem){
  109.                                 if($OrderItem->getReviewId() == 0){
  110.                                     if($ProductReview->getPic1() || $ProductReview->getPic2() || $ProductReview->getPic3() || $ProductReview->getPic4()){
  111.                                         $add_point 15;
  112.                                     }else{
  113.                                         $add_point 10;
  114.                                     }
  115.                                 }
  116.                                 $ProductReview->setOrderItem($OrderItem);
  117.                             }
  118.                         }
  119.                     }
  120.                     $ProductReview->setProduct($Product);
  121.                     $ProductReview->setIsAiReview(0);
  122.                     $ProductReview->setStatus($this->productReviewStatusRepository->find(ProductReviewStatus::SHOW));
  123.                     $this->entityManager->persist($ProductReview);
  124.                     $this->entityManager->flush($ProductReview);
  125.                     log_info('Product review complete', ['id' => $Product->getId()]);
  126.                     // point
  127.                     if($order_item_id && $add_point 0){
  128.                         // Point
  129.                         $OrderItem $this->orderItemRepository->find($order_item_id);
  130.                         $OrderItem->setReviewId($ProductReview->getId());
  131.                         $OrderItem->setReviewPoint($add_point);
  132.                         $this->entityManager->persist($OrderItem);
  133.                         $this->entityManager->flush($OrderItem);
  134.                         // ポイント付与
  135.                         $point $Customer->getPoint() + $add_point;
  136.                         $Customer->setPoint($point);
  137.                         $this->entityManager->persist($Customer);
  138.                         $this->entityManager->flush($Customer);
  139.                     }
  140.                     $this->session->getFlashBag()->set('add_point'$add_point);
  141.                     return $this->redirectToRoute('product_review_complete', ['id' => $Product->getId()]);
  142.                     break;
  143.                 case 'back':
  144.                     // 確認画面から投稿画面へ戻る
  145.                     break;
  146.                 default:
  147.                     // do nothing
  148.                     break;
  149.             }
  150.         }
  151.         return $this->render('@ProductReview4/default/index.twig', [
  152.             'Product' => $Product,
  153.             'ProductReview' => $ProductReview,
  154.             'form' => $form->createView(),
  155.             'order_item_id' => $order_item_id,
  156.             'add_point' => $add_point,
  157.         ]);
  158.     }
  159.     /**
  160.      * Complete.
  161.      *
  162.      * @Route("/product_review/{id}/complete", name="product_review_complete", requirements={"id" = "\d+"})
  163.      * @Template("@ProductReview4/default/complete.twig")
  164.      *
  165.      * @param $id
  166.      *
  167.      * @return array
  168.      */
  169.     public function complete($id)
  170.     {
  171.         $Product $this->entityManager->getRepository('Eccube\Entity\Product')->findOneBy(['id'=>$id]);
  172.         //https://kyogokupro.com/note/special/verification/feed
  173.         //https://kyogokupro.com/note/special/kg/feed
  174.         $html file_get_contents("https://kyogokupro.com/note/special/verification/feed");
  175.         //$obj = simplexml_load_string($html, LIBXML_NOCDATA);
  176.         $rss simplexml_load_string($html,'SimpleXMLElement'LIBXML_NOCDATA);
  177.         $item $rss->channel->item[0];
  178.         
  179.         $title $item->title;
  180.         $link $item->link;
  181.         $desc $item->description;
  182.         $rss_html simplexml_load_string("<html>".$desc."</html>");
  183.                 //echo "<pre>";
  184.         //var_dump(simplexml_load_string("<html>".$desc."</html>"));
  185.         //var_dump($rss_html);
  186.         //echo "</pre>";
  187.         $src $rss_html->p[0]->img->attributes()->src;
  188.         $summary $rss_html->p[1];
  189. /*
  190. echo "<pre>";
  191. var_dump($rss_html->p[0]->img);
  192. var_dump($rss_html->p[0]->img->attributes());
  193. echo $src;
  194. echo "</pre>";
  195. */
  196.         //$src = "";
  197.         //if(preg_match('/src="([^"]+)"/', $desc, $mat)){
  198.         //    $src = $mat[1];
  199.         //    //var_dump($mat);
  200.         //}
  201.         //$rss_html = simplexml_load_string($desc);
  202.         /*
  203.         $domDocument = new \DOMDocument();
  204.         $domDocument->loadHTML($html);
  205.         $xmlString = $domDocument->saveXML();
  206.         $xmlObject = simplexml_load_string($xmlString);
  207.         var_dump($xmlObject);
  208.         */
  209.         //$pattern = '/<ul(.+)<\\//';
  210.         //if(preg_match($pattern, $html, $mat)){
  211.         //    var_dump($mat);
  212.         //}
  213.         //exit;
  214.         return ['Product'=>$Product'id' => $id
  215.         'item'=>[
  216.             "title"=>$title,
  217.             "src"=>$src,
  218.             "desc"=>$desc,
  219.             "link"=>$link,
  220.             "summary"=>$summary,
  221.         ]];
  222.     }
  223.     /**
  224.      * @Route("/product_review/{id}/list", name="product_review_list", requirements={"id" = "\d+"})
  225.      *
  226.      * @param Request $request
  227.      * @param Product $Product
  228.      *
  229.      * @return RedirectResponse|Response
  230.      */
  231.     public function list(Request $requestProduct $Product,  Paginator $paginator)
  232.     {
  233.         $currentPage 1;
  234.         if (isset($_GET['pageno'])) {
  235.             $currentPage $_GET['pageno'];
  236.         }
  237.         $limitPerPage 10;
  238.         $offset = ($currentPage 1) * $limitPerPage;
  239.         $level null;
  240.         if(isset($_GET["l"])){
  241.             $level $_GET["l"];
  242.         }
  243.         $pic null;
  244.         if(isset($_GET["pic"])){
  245.             $pic $_GET["pic"];
  246.         }
  247.         
  248.         $options = [];
  249.         $Products = [];
  250.         if ($Product->getIsVariant()) {
  251.             $ProductVariants $Product->getProductVariant()->toArray();
  252.             foreach ($ProductVariants as $ProductVariant) {
  253.                 $Products[] = $ProductVariant->getChildProduct();
  254.             }
  255.         } elseif ($this->productRepository->findVariantsByProductId($Product->getId())) {
  256.             $ProductVariants $this->productRepository->findVariantsByProductId($Product->getId());
  257.             foreach ($ProductVariants->getProductVariant()->toArray() as $ProductVariant) {
  258.                 $Products[] = $ProductVariant->getChildProduct();
  259.             }
  260.         } else {
  261.             $Products[] = $Product;
  262.         }
  263.         $ProductIds = [];
  264.         foreach ($Products as $Product) {
  265.             $ProductIds[] = $Product->getId();
  266.         }
  267.         $qb $this->entityManager->createQueryBuilder();
  268.         $qb->select('a')->from('Plugin\ProductReview4\Entity\ProductReview','a');
  269.         $qb->where('a.Product IN (:product_id)')->setParameter('product_id'$ProductIds);
  270.         $qb->andwhere('a.Status = 1');
  271.         $qb->orderBy('a.id','desc');
  272.         if($level){
  273.             $qb->andwhere('a.recommend_level = :recommend_level')->setParameter('recommend_level',$level);
  274.         }
  275.         if($pic){
  276.             $qb->andwhere('a.pic1 is not null');
  277.         }
  278.         $data $qb->getQuery();
  279.         
  280.         $pagination $paginator->paginate($data$currentPage$limitPerPage$options);
  281.         $user_id "";
  282.         $Customer $this->getUser();
  283.         if($Customer){
  284.             $user_id $Customer->getId();
  285.         }
  286.         $categories $this->entityManager->getRepository('Eccube\Entity\Category')->findBy(["id"=>[7,18,8,12,17,11,9,19,20,21,10,22]]);
  287.         $category_map = [];
  288.         foreach($categories as $cate){
  289.             $category_map[] = $cate->getId();
  290.         }
  291.         $rateAll $this->productReviewService->getReview($Product);
  292.         $rateStar $this->entityManager->getRepository('Plugin\ProductReview4\Entity\ProductReview')->getStarAll($Product);
  293.         $rateStar2 = [
  294.             => ["recommend_level"=>5"count"=>0"per"=>0],
  295.             => ["recommend_level"=>4"count"=>0"per"=>0],
  296.             => ["recommend_level"=>3"count"=>0"per"=>0],
  297.             => ["recommend_level"=>2"count"=>0"per"=>0],
  298.             => ["recommend_level"=>1"count"=>0"per"=>0],
  299.         ];
  300.         foreach($rateStar as $star){
  301.             $rateStar2[$star["recommend_level"]]["count"] = $star["recommend_count"];
  302.             $rateStar2[$star["recommend_level"]]["per"] = round$star["recommend_count"] * 100 $rateAll["ReviewCntList"] );
  303.         }
  304.         $rateAll["recommend_avg_text"] = sprintf("%.1f"$rateAll["RecommendAvg"]);
  305.         return $this->render('@ProductReview4/default/list.twig', [
  306.             'Product' => $Product,
  307.             'pagination' => $pagination,
  308.             "category_map"=>$category_map,
  309.             'rateAll' => $rateAll,
  310.             'rateStar' => $rateStar2,
  311.             "user_id" => $user_id,
  312.         ]);
  313.     }
  314. }