app/Plugin/tbsFeaturePage/Controller/FeatureController.php line 70

Open in your IDE?
  1. <?php
  2. namespace Plugin\tbsFeaturePage\Controller;
  3. use Eccube\Application;
  4. use Eccube\Controller\AbstractController;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Event\EventArgs;
  7. use Eccube\Form\Type\AddCartType;
  8. use Eccube\Service\CartService;
  9. use Eccube\Repository\BaseInfoRepository;
  10. use Eccube\Repository\ProductRepository;
  11. use Plugin\tbsFeaturePage\Entity\FeatureStatus;
  12. use Plugin\tbsFeaturePage\Repository\FeatureProductRepository;
  13. use Plugin\tbsFeaturePage\Repository\FeatureRepository;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  15. use Symfony\Component\DomCrawler\Crawler;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. class FeatureController extends AbstractController
  19. {
  20.     /** @var FeatureRepository */
  21.     private $featureRepository;
  22.     /** @var FeatureProductRepository */
  23.     private $featureProductRepository;
  24.     /** @var ProductRepository */
  25.     private $productRepository;
  26.     /** @var CartService */
  27.     protected $cartService;
  28.     /**
  29.      * FeatureController constructor.
  30.      *
  31.      * @param FeatureRepository $featureRepository
  32.      * @param FeatureProductRepository $featureProductRepository
  33.      * @param ProductRepository $productRepository
  34.      * @param BaseInfoRepository $baseInfoRepository
  35.      * @param CartService $cartService
  36.      */
  37.     public function __construct(
  38.         FeatureRepository $featureRepository,
  39.         FeatureProductRepository $featureProductRepository,
  40.         ProductRepository $productRepository,
  41.         BaseInfoRepository $baseInfoRepository,
  42.         CartService $cartService
  43.     ) {
  44.         $this->featureRepository = $featureRepository;
  45.         $this->featureProductRepository = $featureProductRepository;
  46.         $this->productRepository = $productRepository;
  47.         $this->baseInfo = $baseInfoRepository->get();
  48.         $this->cartService = $cartService;
  49.     }
  50.     /**
  51.      * @Route("/feature_list", name="feature_list")
  52.      *
  53.      * @param Request $request
  54.      *
  55.      * @return RedirectResponse|Response
  56.      */
  57.     public function index(Request $request)
  58.     {
  59.         $Features = $this->featureRepository->getList();
  60.         return $this->render('@tbsFeaturePage/default/feature_list.twig', [
  61.             'Features' => $Features,
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/feature/{url}", name="feature_detail", requirements={"url"=".+"})
  66.      *
  67.      * @param Request $request
  68.      * @param string $url
  69.      *
  70.      * @return RedirectResponse|Response
  71.      */
  72.     public function detail(Request $request, string $url)
  73.     {
  74.         $Feature = $this->featureRepository->findOneBy(array('url' => $url));
  75.         
  76.         if (!$request->getSession()->has('_security_admin') && $Feature->getStatus()->getId() !== 1) {
  77.             throw new NotFoundHttpException();
  78.         }
  79.         $arrFeatureProducts = array('1' => null, '2' => null, '3' => null, '4' => null, '5' => null);
  80.         $FeatureProducts = $this->featureProductRepository->findBy(array('Feature' => $Feature->getId()), array('rank' => 'DESC'));
  81.         $ids = [];
  82.         foreach ($FeatureProducts as $FeatureProduct) {
  83.             if (!in_array($FeatureProduct->getProduct()->getId(), $ids)) {
  84.                 $ids[] = $FeatureProduct->getProduct()->getId();
  85.             }
  86.         }
  87.         $ProductsAndClassCategories = $this->productRepository->findProductsWithSortedClassCategories($ids, 'p.id');
  88.         $forms = array();
  89.         foreach ($FeatureProducts as $FeatureProduct) {
  90.             $Product = $FeatureProduct->getProduct();
  91.             // addCart form
  92.             if ($Feature->getDisplayCart()) {
  93.                 /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  94.                 $builder = $this->formFactory->createNamedBuilder(
  95.                     '',
  96.                     AddCartType::class,
  97.                     null,
  98.                     [
  99.                         'product' => $ProductsAndClassCategories[$Product->getId()],
  100.                         'allow_extra_fields' => true,
  101.                     ]
  102.                 );
  103.                 $addCartForm = $builder->getForm();
  104.                 if ($request->getMethod() === 'POST' && (string)$Product->getId() === $request->get('product_id')) {
  105.                     $addCartForm->handleRequest($request);
  106.                     if ($addCartForm->isValid()) {
  107.                         $addCartData = $addCartForm->getData();
  108.                         try {
  109.                             $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity'])->save();
  110.                         } catch (CartException $e) {
  111.                             $this->addRequestError($e->getMessage());
  112.                         }
  113.                         $event = new EventArgs(
  114.                             [
  115.                                 'form' => $addCartForm,
  116.                                 'Product' => $Product,
  117.                             ],
  118.                             $request
  119.                         );
  120.                         $this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE);
  121.                         if ($event->getResponse() !== null) {
  122.                             return $event->getResponse();
  123.                         }
  124.                         return $this->redirectToRoute('cart');
  125.                     }
  126.                 }
  127.                 $forms[$FeatureProduct->getPosition()][$Product->getId()] = $addCartForm->createView();
  128.             }
  129.             // 位置指定
  130.             if ($Product->getStatus()->getId() !== FeatureStatus::SHOW) {
  131.                 continue;
  132.             }
  133.             $arrFeatureProducts[$FeatureProduct->getPosition()][] = $FeatureProduct;
  134.         }
  135.         $response = $this->render('@tbsFeaturePage/default/feature_detail.twig', [
  136.             'Feature' => $Feature,
  137.             'FeatureProducts' => $arrFeatureProducts,
  138.             'Products' => $ProductsAndClassCategories,
  139.             'forms' => $forms,
  140.         ]);
  141.         $response->setContent($this->getCustomHtml($Feature, $response->getContent()));
  142.         return $response;
  143.     }
  144.     /**
  145.      * カスタマイズしたHTMLを取得する.
  146.      */
  147.     protected function getCustomHtml($Feature, $content)
  148.     {
  149.         $crawler = new Crawler($content);
  150.         $html = $this->getHtml($crawler);
  151.         // 情報を取得
  152.         $title = $Feature->getTitle();
  153.         $description = $Feature->getMetaDescription();
  154.         // タイトルを置換
  155.         $replace = '<title>' . $this->baseInfo->getShopName() . ' / ' . $title . '</title>';
  156.         $html = mb_ereg_replace('<title>(.+?)<\/title>', $replace, $html);
  157.         // ヘッダーにメタタグを挿入
  158.         $snipet = '';
  159.         $snipet .= ( $description ) ? '<meta name="description" content="' . $description . '" />' : '';
  160.         if ($snipet) {
  161.             $search = '<meta name="viewport"';
  162.             $snipet = trim($snipet);
  163.             $snipet = str_replace(array("\r", "\n"), '', $snipet);
  164.             $replace = $snipet.$search;
  165.             $html = str_replace($search, $replace, $html);
  166.         }
  167.         return $html;
  168.     }
  169.     /**
  170.      * 解析用HTMLを取得
  171.      *
  172.      * @param Crawler $crawler
  173.      * @return string
  174.      */
  175.     private function getHtml(Crawler $crawler)
  176.     {
  177.         $html = '';
  178.         foreach ($crawler as $domElement) {
  179.             $domElement->ownerDocument->formatOutput = true;
  180.             $html .= $domElement->ownerDocument->saveHTML();
  181.         }
  182.         return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8');
  183.     }
  184. }