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

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\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. use Eccube\Controller\AbstractController;
  17. use Eccube\Entity\Master\ProductStatus;
  18. use Eccube\Entity\Product;
  19. use Plugin\ProductReview4\Entity\ProductReview;
  20. use Plugin\ProductReview4\Entity\ProductReviewStatus;
  21. use Plugin\ProductReview4\Form\Type\ProductReviewType;
  22. use Plugin\ProductReview4\Repository\ProductReviewRepository;
  23. use Plugin\ProductReview4\Repository\ProductReviewStatusRepository;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  30. /**
  31.  * Class ProductReviewController front.
  32.  */
  33. class ProductReviewController extends AbstractController
  34. {
  35.     /**
  36.      * @var ProductReviewStatusRepository
  37.      */
  38.     private $productReviewStatusRepository;
  39.     /**
  40.      * @var ProductReviewRepository
  41.      */
  42.     private $productReviewRepository;
  43.     /**
  44.      * ProductReviewController constructor.
  45.      *
  46.      * @param ProductReviewStatusRepository $productStatusRepository
  47.      * @param ProductReviewRepository $productReviewRepository
  48.      */
  49.     public function __construct(
  50.         ProductReviewStatusRepository $productStatusRepository,
  51.         ProductReviewRepository $productReviewRepository
  52.     ) {
  53.         $this->productReviewStatusRepository $productStatusRepository;
  54.         $this->productReviewRepository $productReviewRepository;
  55.     }
  56.     /**
  57.      * @Route("/product_review/{id}/review", name="product_review_index", requirements={"id" = "\d+"})
  58.      * @Route("/product_review/{id}/review", name="product_review_confirm", requirements={"id" = "\d+"})
  59.      *
  60.      * @param Request $request
  61.      * @param Product $Product
  62.      *
  63.      * @return RedirectResponse|Response
  64.      */
  65.     public function index(Request $requestProduct $Product)
  66.     {
  67.         if (!$this->session->has('_security_admin') && $Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  68.             log_info('Product review', ['status' => 'Not permission']);
  69.             throw new NotFoundHttpException();
  70.         }
  71.         $ProductReview = new ProductReview();
  72.         $form $this->createForm(ProductReviewType::class, $ProductReview);
  73.         $form->handleRequest($request);
  74.         if ($form->isSubmitted() && $form->isValid()) {
  75.             /** @var $ProductReview ProductReview */
  76.             $ProductReview $form->getData();
  77.             switch ($request->get('mode')) {
  78.                 case 'confirm':
  79.                     log_info('Product review config confirm');
  80.                     $file $form['upload_file']->getData();
  81.                     $fs = new Filesystem();
  82.                     if ($file && $fs->exists($this->getParameter('eccube_temp_image_dir').'/'.$file)) {
  83.                         $fs->rename(
  84.                             $this->getParameter('eccube_temp_image_dir').'/'.$file,
  85.                             $this->getParameter('eccube_save_image_dir').'/'.$file
  86.                         );
  87.                     }
  88.                     return $this->render('ProductReview4/Resource/template/default/confirm.twig', [
  89.                         'form' => $form->createView(),
  90.                         'Product' => $Product,
  91.                         'ProductReview' => $ProductReview,
  92.                     ]);
  93.                     break;
  94.                 case 'complete':
  95.                     log_info('Product review complete');
  96.                     if ($this->isGranted('ROLE_USER')) {
  97.                         $Customer $this->getUser();
  98.                         $ProductReview->setCustomer($Customer);
  99.                     }
  100.                     $ProductReview->setProduct($Product);
  101.                     $ProductReview->setStatus($this->productReviewStatusRepository->find(ProductReviewStatus::HIDE));
  102.                     $this->entityManager->persist($ProductReview);
  103.                     $this->entityManager->flush($ProductReview);
  104.                     log_info('Product review complete', ['id' => $Product->getId()]);
  105.                     return $this->redirectToRoute('product_review_complete', ['id' => $Product->getId()]);
  106.                     break;
  107.                 case 'back':
  108.                     // 確認画面から投稿画面へ戻る
  109.                     break;
  110.                 default:
  111.                     // do nothing
  112.                     break;
  113.             }
  114.         }
  115.         return $this->render('ProductReview4/Resource/template/default/index.twig', [
  116.             'Product' => $Product,
  117.             'ProductReview' => $ProductReview,
  118.             'form' => $form->createView(),
  119.         ]);
  120.     }
  121.     /**
  122.      * Complete.
  123.      *
  124.      * @Route("/product_review/{id}/complete", name="product_review_complete", requirements={"id" = "\d+"})
  125.      * @Template("ProductReview4/Resource/template/default/complete.twig")
  126.      *
  127.      * @param $id
  128.      *
  129.      * @return array
  130.      */
  131.     public function complete($id)
  132.     {
  133.         return ['id' => $id];
  134.     }
  135.     /**
  136.      * ページ管理表示用のダミールーティング.
  137.      *
  138.      * @Route("/product_review/display", name="product_review_display")
  139.      */
  140.     public function display()
  141.     {
  142.         return new Response();
  143.     }
  144.     /**
  145.      * @Route("/product_review/uploadFile/add", name="product_review_upload_file_add")
  146.      */
  147.     public function uploadFileAdd(Request $request)
  148.     {
  149.         if (!$request->isXmlHttpRequest()) {
  150.             throw new BadRequestHttpException();
  151.         }
  152.         $images $request->files->get('product_review');
  153.         $allowExtensions = ['gif''jpg''jpeg''png'];
  154.         $filename null;
  155.         if (isset($images['upload_file_file'])) {
  156.             $image $images['upload_file_file'];
  157.             //ファイルフォーマット検証
  158.             $mimeType $image->getMimeType();
  159.             if (!== strpos($mimeType'image')) {
  160.                 throw new UnsupportedMediaTypeHttpException();
  161.             }
  162.             // 拡張子
  163.             $extension $image->getClientOriginalExtension();
  164.             if (!in_array(strtolower($extension), $allowExtensions)) {
  165.                 throw new UnsupportedMediaTypeHttpException();
  166.             }
  167.             $filename date('mdHis').uniqid('_').'.'.$extension;
  168.             $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
  169.         }
  170.         $event = new EventArgs(
  171.             [
  172.                 'images' => $images,
  173.                 'filename' => $filename,
  174.             ],
  175.             $request
  176.         );
  177.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_REVIEW_UPLOAD_FILE_ADD_COMPLETE$event);
  178.         $filename $event->getArgument('filename');
  179.         return $this->json(['filename' => $filename], 200);
  180.     }
  181. }