app/Plugin/Synplgbase/Controller/ProductController.php line 134

Open in your IDE?
  1. <?php
  2. namespace Plugin\Synplgbase\Controller;
  3. use Eccube\Controller\ProductController as ParentProductController;
  4. use Eccube\Repository\PageRepository;
  5. use Eccube\Entity\BaseInfo;
  6. use Eccube\Entity\Master\ProductStatus;
  7. use Eccube\Entity\Product;
  8. use Eccube\Entity\ProductClass;
  9. use Eccube\Entity\ClassName;
  10. use Eccube\Event\EccubeEvents;
  11. use Eccube\Event\EventArgs;
  12. use Eccube\Form\Type\AddCartType;
  13. use Eccube\Form\Type\Master\ProductListMaxType;
  14. use Eccube\Form\Type\Master\ProductListOrderByType;
  15. use Eccube\Form\Type\SearchProductType;
  16. use Eccube\Repository\BaseInfoRepository;
  17. use Eccube\Repository\CustomerFavoriteProductRepository;
  18. use Eccube\Repository\Master\ProductListMaxRepository;
  19. use Eccube\Repository\ProductRepository;
  20. use Eccube\Repository\ClassNameRepository;
  21. use Eccube\Repository\ClassCategoryRepository;
  22. use Eccube\Service\CartService;
  23. use Eccube\Service\PurchaseFlow\PurchaseContext;
  24. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  25. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  26. use Knp\Component\Pager\Paginator;
  27. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  28. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  34. class ProductController extends ParentProductController
  35. {
  36.     /**
  37.      * @var PurchaseFlow
  38.      */
  39.     protected $purchaseFlow;
  40.     /**
  41.      * @var CustomerFavoriteProductRepository
  42.      */
  43.     protected $customerFavoriteProductRepository;
  44.     /**
  45.      * @var CartService
  46.      */
  47.     protected $cartService;
  48.     /**
  49.      * @var ProductRepository
  50.      */
  51.     protected $productRepository;
  52.     /**
  53.      * @var BaseInfo
  54.      */
  55.     protected $BaseInfo;
  56.     /**
  57.      * @var AuthenticationUtils
  58.      */
  59.     protected $helper;
  60.     /**
  61.      * @var ProductListMaxRepository
  62.      */
  63.     protected $productListMaxRepository;
  64.     /**
  65.      * @var PageRepository
  66.      */
  67.     protected $pageRepository;
  68.     /**
  69.      * @var ClassNameRepository
  70.      */
  71.     protected $classNameRepository;
  72.     /**
  73.      * @var ClassCategoryRepository
  74.      */
  75.     protected $classCategoryRepository;
  76.     private $title '';
  77.     /**
  78.      * ProductController constructor.
  79.      *
  80.      * @param PurchaseFlow $cartPurchaseFlow
  81.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  82.      * @param CartService $cartService
  83.      * @param ProductRepository $productRepository
  84.      * @param BaseInfoRepository $baseInfoRepository
  85.      * @param AuthenticationUtils $helper
  86.      * @param ProductListMaxRepository $productListMaxRepository
  87.      * @param PageRepository $pageRepository
  88.      */
  89.     public function __construct(
  90.         PurchaseFlow $cartPurchaseFlow,
  91.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  92.         CartService $cartService,
  93.         ProductRepository $productRepository,
  94.         BaseInfoRepository $baseInfoRepository,
  95.         AuthenticationUtils $helper,
  96.         ProductListMaxRepository $productListMaxRepository,
  97.         PageRepository $pageRepository,
  98.         ClassNameRepository $classNameRepository,
  99.         ClassCategoryRepository $classCategoryRepository
  100.     ) {
  101.         $this->purchaseFlow $cartPurchaseFlow;
  102.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  103.         $this->cartService $cartService;
  104.         $this->productRepository $productRepository;
  105.         $this->BaseInfo $baseInfoRepository->get();
  106.         $this->helper $helper;
  107.         $this->productListMaxRepository $productListMaxRepository;
  108.         $this->pageRepository $pageRepository;
  109.         $this->classNameRepository $classNameRepository;
  110.         $this->classCategoryRepository $classCategoryRepository;
  111.     }
  112.      /**
  113.      * 商品一覧画面.
  114.      *
  115.      * @Route("/products/list", name="product_list")
  116.      * @Template("Product/list.twig")
  117.      */
  118.     public function index(Request $requestPaginator $paginator)
  119.     {
  120.         $arrRet parent::index($request$paginator);
  121.         //タグの情報を作成
  122.         $search_form=$arrRet['search_form'];
  123.         $arrVars $search_form->vars;
  124.         $arrValues $arrVars['value'];
  125.         $arrRet['Tag'] = $arrValues['tag_id'];
  126.         return $arrRet;
  127.     }
  128.     /**
  129.      * 商品詳細画面.
  130.      *
  131.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  132.      * @Template("Product/detail.twig")
  133.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  134.      *
  135.      * @param Request $request
  136.      * @param Product $Product
  137.      *
  138.      * @return array
  139.      */
  140.     public function detail(Request $requestProduct $Product)
  141.     {
  142.         //親のdetailで作成される変数を用意する。titleとsub_titleは使わない
  143.         $arrRarent parent::detail($request,$Product);
  144.         $form $arrRarent['form'];
  145.         $Product $arrRarent['Product'];
  146.         $is_favorite $arrRarent['is_favorite'];
  147.         // 規格名1/2から組み合わせを生成し, DBから取得した商品規格とマージする.
  148.         // Synplgbase/Resource/template/sample_classcategory_table.twigのコードを商品詳細ページで使うための変数作成
  149.         if(isset($form['classcategory_id1'])){
  150.             $ClassName1 $this->classNameRepository->findOneBy(['name'=>$Product->getClassName1()]);
  151.             $ClassName2 $this->classNameRepository->findOneBy(['name'=>$Product->getClassName2()]);
  152.             $ProductClasses $Product->getProductClasses();
  153.             $ProductClasses $this->mergeProductClasses(
  154.                 $this->createProductClasses($ClassName1$ClassName2),
  155.                 $ProductClasses);
  156.         }
  157.         //ページのタイトル変更用
  158.         $Page $this->pageRepository
  159.         ->findOneBy([
  160.             'id' => 3,
  161.         ]);
  162.         if(!empty($Product->getAuthor())) $Page->setAuthor($Product->getAuthor());
  163.         if(!empty($Product->getDescription())) $Page->setDescription($Product->getDescription());
  164.         if(!empty($Product->getKeyword())) $Page->setKeyword($Product->getKeyword());
  165.         if(!empty($Product->getMetaRobots())) $Page->setMetaRobots($Product->getMetaRobots());
  166.         if(!empty($Product->getMetaTags())) $Page->setMetaTags($Product->getMetaTags());
  167.         if(!empty($Product->getTitle())){
  168.             $subtitle=$Product->getTitle();
  169.         }
  170.         else{
  171.             $subtitle=$Product->getName();
  172.         }
  173.         //「関連カテゴリ」表示で、親カテを重複して表示させない
  174.         $arrTemp=array();
  175.         foreach($Product->getProductCategories() as $ProductCategory){
  176.             if($ProductCategory->getCategory()->getParent()){
  177.                 $arrTemp[]=$ProductCategory->getCategory()->getParent()->getId();
  178.             };
  179.         }
  180.         foreach($Product->getProductCategories() as $key=>$value){
  181.             if(in_array($value['category_id'],$arrTemp)){
  182.                 unset($Product->getProductCategories()[$key]);
  183.             }
  184.         }
  185.         return [
  186.             'Page' => $Page,
  187.             'subtitle' => $subtitle,
  188.             'form' => $form,
  189.             'Product' => $Product,
  190.             'is_favorite' => $is_favorite,
  191.             'ProductClasses' => $ProductClasses??NULL
  192.         ];// オリジナルはここで'title' => $this->titleで空の値をパスしてますが、そのラインを削除して、他ページと同様、ページ管理の値がtitleに入るようにしてます。
  193.     }
  194.     /**
  195.      * ページタイトルの設定
  196.      *
  197.      * @param  null|array $searchData
  198.      *
  199.      * @return str
  200.      */
  201.     protected function getPageTitle($searchData)
  202.     {
  203.         //タグの情報を追加して、検索結果のタイトルを変更
  204.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  205.             return '「'.$searchData['name'].'」の'.trans('front.product.search_result');
  206.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  207.             return $searchData['category_id']->getName();
  208.         } elseif (isset($searchData['tag_id']) && $searchData['tag_id']) {
  209.             return $searchData['tag_id']->getName();
  210.         } else {
  211.             return trans('front.product.all_products');
  212.         }
  213.     }
  214.     /**
  215.      * 規格名1/2から, 商品規格の組み合わせを生成する. copied from src/Eccube/Controller/Admin/Product/ProductClassController.php
  216.      *
  217.      * @param ClassName $ClassName1
  218.      * @param ClassName|null $ClassName2
  219.      *
  220.      * @return array|ProductClass[]
  221.      */
  222.     protected function createProductClasses(ClassName $ClassName1ClassName $ClassName2 null)
  223.     {
  224.         $ProductClasses = [];
  225.         $ClassCategories1 $this->classCategoryRepository->findBy(['ClassName' => $ClassName1], ['sort_no' => 'DESC']);
  226.         $ClassCategories2 = [];
  227.         if ($ClassName2) {
  228.             $ClassCategories2 $this->classCategoryRepository->findBy(['ClassName' => $ClassName2],['sort_no' => 'DESC']);
  229.         }
  230.         foreach ($ClassCategories1 as $ClassCategory1) {
  231.             // 規格1のみ
  232.             if (!$ClassName2) {
  233.                 $ProductClass = new ProductClass();
  234.                 $ProductClass->setClassCategory1($ClassCategory1);
  235.                 $ProductClasses[] = $ProductClass;
  236.                 continue;
  237.             }
  238.             // 規格1/2
  239.             foreach ($ClassCategories2 as $ClassCategory2) {
  240.                 $ProductClass = new ProductClass();
  241.                 $ProductClass->setClassCategory1($ClassCategory1);
  242.                 $ProductClass->setClassCategory2($ClassCategory2);
  243.                 $ProductClasses[] = $ProductClass;
  244.             }
  245.         }
  246.         return $ProductClasses;
  247.     }
  248.     /**
  249.      * 商品規格の配列をマージする. copied from src/Eccube/Controller/Admin/Product/ProductClassController.php
  250.      *
  251.      * @param $ProductClassesForMatrix
  252.      * @param $ProductClasses
  253.      *
  254.      * @return array|ProductClass[]
  255.      */
  256.     protected function mergeProductClasses($ProductClassesForMatrix$ProductClasses)
  257.     {
  258.         $mergedProductClasses = [];
  259.         foreach ($ProductClassesForMatrix as $pcfm) {
  260.             foreach ($ProductClasses as $pc) {
  261.                 if ($pcfm->getClassCategory1()->getId() === $pc->getClassCategory1()->getId()) {
  262.                     $cc2fm $pcfm->getClassCategory2();
  263.                     $cc2 $pc->getClassCategory2();
  264.                     if (null === $cc2fm && null === $cc2) {
  265.                         $mergedProductClasses[] = $pc;
  266.                         continue 2;
  267.                     }
  268.                     if ($cc2fm && $cc2 && $cc2fm->getId() === $cc2->getId()) {
  269.                         $mergedProductClasses[] = $pc;
  270.                         continue 2;
  271.                     }
  272.                 }
  273.             }
  274.             $mergedProductClasses[] = $pcfm;
  275.         }
  276.         return $mergedProductClasses;
  277.     }
  278. }