app/Plugin/HigashiyamaForProduct/Controller/StandReadingController.php line 123

Open in your IDE?
  1. <?php
  2. namespace Plugin\HigashiyamaForProduct\Controller;
  3. use Eccube\Controller\AbstractController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Eccube\Entity\Product;
  8. use Eccube\Entity\Master\ProductStatus;
  9. use Eccube\Repository\ProductRepository;
  10. use Plugin\HigashiyamaForProduct\Repository\StandReadingImageRepository;
  11. use Plugin\HigashiyamaForProduct\Repository\HigashiyamaForProductRepository;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. /**
  14.  * Class CampaignController.
  15.  */
  16. class StandReadingController extends AbstractController
  17. {
  18.    /**
  19.      * @var ProductRepository
  20.      */
  21.     protected $productRepository;
  22.     /**
  23.      * @var StandReadingImageRepository
  24.      */
  25.     protected $HigashiyamaForProductRepository;
  26.     /**
  27.      * @var HigashiyamaForProductRepository
  28.      */
  29.     protected $higashiyamaForProductRepository;
  30.     /**
  31.      * NewsController constructor.
  32.      *
  33.      * @param ProductRepository $productRepository
  34.      * @param StandReadingImageRepository $standReadingImageRepository
  35.      * @param HigashiyamaForProductRepository $higashiyamaForProductRepository
  36.      */
  37.     public function __construct(
  38.         ProductRepository $productRepository,
  39.         StandReadingImageRepository $standReadingImageRepository,
  40.         HigashiyamaForProductRepository $higashiyamaForProductRepository
  41.     )
  42.     {
  43.         $this->productRepository $productRepository;
  44.         $this->standReadingImageRepository $standReadingImageRepository;
  45.         $this->higashiyamaForProductRepository $higashiyamaForProductRepository;
  46.     }
  47.     /**
  48.      * 健康教室「最新号」.
  49.      *
  50.      * @Route("/products/stand_reading/new", name="stand_reading_new")
  51.      * @Template("@HigashiyamaForProduct/default/stand_reading.twig")
  52.      */
  53.     public function New(Request $request$id null)
  54.     {
  55.         //健康教室の最新号を取得する
  56.         $NewProduct $this->higashiyamaForProductRepository->getNewProduct();
  57.         //404設定
  58.         if (!$NewProduct[0]) {
  59.             throw new NotFoundHttpException();
  60.         }
  61.         $Standings $this->standReadingImageRepository->findBy(['Product' => $NewProduct[0]], ['sort_no' => 'ASC']);
  62.         //404設定
  63.         if (!$Standings || !$this->checkVisibility($NewProduct[0])) {
  64.             throw new NotFoundHttpException();
  65.         }
  66.         foreach ($Standings as $Standing) {
  67.             $StandingFiles[] = $Standing->getFileName();
  68.         }
  69.         return [
  70.             'standing_files' => $StandingFiles,
  71.         ];
  72.     }
  73.     /**
  74.      * 立ち読み画面.
  75.      *
  76.      * @Route("/products/stand_reading/{id}", name="stand_reading", requirements={"id":"\d+"})
  77.      * @Template("@HigashiyamaForProduct/default/stand_reading.twig")
  78.      */
  79.     public function Detail(Request $request$id=null)
  80.     {
  81.         $Product $this->productRepository->find($id);
  82.         //404設定
  83.         if (!$Product) {
  84.             throw new NotFoundHttpException();
  85.         }
  86.         $Standings $this->standReadingImageRepository->findBy(['Product' => $Product], ['sort_no' => 'ASC']);
  87.         //404設定
  88.         if(!$Standings || !$this->checkVisibility($Product)){
  89.             throw new NotFoundHttpException();
  90.         }
  91.         foreach ($Standings as $Standing) {
  92.             $StandingFiles[] = $Standing->getFileName();
  93.         }
  94.         return [
  95.             'standing_files' => $StandingFiles,
  96.         ];
  97.     }
  98.     /**
  99.      * 閲覧可能な商品かどうかを判定
  100.      *
  101.      * @param Product $Product
  102.      *
  103.      * @return boolean 閲覧可能な場合はtrue
  104.      */
  105.     protected function checkVisibility(Product $Product)
  106.     {
  107.         $is_admin $this->session->has('_security_admin');
  108.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  109.         if (!$is_admin) {
  110.             // 在庫なし商品の非表示オプションが有効な場合.
  111.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  112.             //     if (!$Product->getStockFind()) {
  113.             //         return false;
  114.             //     }
  115.             // }
  116.             // 公開ステータスでない商品は表示しない.
  117.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  118.                 return false;
  119.             }
  120.         }
  121.         return true;
  122.     }
  123. }