<?php
namespace Plugin\HigashiyamaForProduct\Controller;
use Eccube\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Entity\Product;
use Eccube\Entity\Master\ProductStatus;
use Eccube\Repository\ProductRepository;
use Plugin\HigashiyamaForProduct\Repository\StandReadingImageRepository;
use Plugin\HigashiyamaForProduct\Repository\HigashiyamaForProductRepository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class CampaignController.
*/
class StandReadingController extends AbstractController
{
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var StandReadingImageRepository
*/
protected $HigashiyamaForProductRepository;
/**
* @var HigashiyamaForProductRepository
*/
protected $higashiyamaForProductRepository;
/**
* NewsController constructor.
*
* @param ProductRepository $productRepository
* @param StandReadingImageRepository $standReadingImageRepository
* @param HigashiyamaForProductRepository $higashiyamaForProductRepository
*/
public function __construct(
ProductRepository $productRepository,
StandReadingImageRepository $standReadingImageRepository,
HigashiyamaForProductRepository $higashiyamaForProductRepository
)
{
$this->productRepository = $productRepository;
$this->standReadingImageRepository = $standReadingImageRepository;
$this->higashiyamaForProductRepository = $higashiyamaForProductRepository;
}
/**
* 健康教室「最新号」.
*
* @Route("/products/stand_reading/new", name="stand_reading_new")
* @Template("@HigashiyamaForProduct/default/stand_reading.twig")
*/
public function New(Request $request, $id = null)
{
//健康教室の最新号を取得する
$NewProduct = $this->higashiyamaForProductRepository->getNewProduct();
//404設定
if (!$NewProduct[0]) {
throw new NotFoundHttpException();
}
$Standings = $this->standReadingImageRepository->findBy(['Product' => $NewProduct[0]], ['sort_no' => 'ASC']);
//404設定
if (!$Standings || !$this->checkVisibility($NewProduct[0])) {
throw new NotFoundHttpException();
}
foreach ($Standings as $Standing) {
$StandingFiles[] = $Standing->getFileName();
}
return [
'standing_files' => $StandingFiles,
];
}
/**
* 立ち読み画面.
*
* @Route("/products/stand_reading/{id}", name="stand_reading", requirements={"id":"\d+"})
* @Template("@HigashiyamaForProduct/default/stand_reading.twig")
*/
public function Detail(Request $request, $id=null)
{
$Product = $this->productRepository->find($id);
//404設定
if (!$Product) {
throw new NotFoundHttpException();
}
$Standings = $this->standReadingImageRepository->findBy(['Product' => $Product], ['sort_no' => 'ASC']);
//404設定
if(!$Standings || !$this->checkVisibility($Product)){
throw new NotFoundHttpException();
}
foreach ($Standings as $Standing) {
$StandingFiles[] = $Standing->getFileName();
}
return [
'standing_files' => $StandingFiles,
];
}
/**
* 閲覧可能な商品かどうかを判定
*
* @param Product $Product
*
* @return boolean 閲覧可能な場合はtrue
*/
protected function checkVisibility(Product $Product)
{
$is_admin = $this->session->has('_security_admin');
// 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
if (!$is_admin) {
// 在庫なし商品の非表示オプションが有効な場合.
// if ($this->BaseInfo->isOptionNostockHidden()) {
// if (!$Product->getStockFind()) {
// return false;
// }
// }
// 公開ステータスでない商品は表示しない.
if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
return false;
}
}
return true;
}
}