app/Plugin/tbsFeaturePage42/Controller/FeatureController.php line 90

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