app/Plugin/BreadcrumbList4/Controller/Block/BreadcrumbList4Controller.php line 94

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the BreadcrumbList4-plugin package.
  4. *
  5. * (c) Nobuhiko Kimoto All Rights Reserved.
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\BreadcrumbList4\Controller\Block;
  11. use Eccube\Controller\AbstractController;
  12. use Eccube\Repository\Master\DeviceTypeRepository;
  13. use Eccube\Repository\BlockRepository;
  14. use Eccube\Repository\PageRepository;
  15. use Eccube\Repository\ProductRepository;
  16. use Eccube\Entity\Master\DeviceType;
  17. use Eccube\Form\Type\SearchProductType;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\Routing\RouterInterface;
  23. use Symfony\Component\Routing\Matcher\UrlMatcher;
  24. use Symfony\Component\Routing\RequestContext;
  25. use Symfony\Component\Routing\RouteCollection;
  26. class BreadcrumbList4Controller extends AbstractController
  27. {
  28.     public function __construct(
  29.         DeviceTypeRepository $deviceTypeRepository,
  30.         ProductRepository $productRepository,
  31.         PageRepository $pageRepository
  32.     ) {
  33.         $this->deviceTypeRepository $deviceTypeRepository;
  34.         $this->productRepository $productRepository;
  35.         $this->pageRepository $pageRepository;
  36.     }
  37.     /**
  38.      * @Route("/block/BreadcrumbList4", name="block_BreadcrumbList4")
  39.      */
  40.     public function index(RouterInterface $router)
  41.     {
  42.         // 初期化
  43.         $Breadcrumb[0][0] = array();
  44.         $arrBreadcrumb = array();
  45.         // サブdirを考慮
  46.         $request_uri str_replace($router->getContext()->getBaseUrl(), ''$_SERVER['REQUEST_URI']);
  47.         $request Request::create($request_uri);
  48.         // urlからrouteを作成
  49.         //$result = $route->match(strtok($request_uri, '?'));
  50.         $routes $this->get('router')->getRouteCollection();
  51.         $context = new RequestContext();
  52.         $context->fromRequest(Request::createFromGlobals());
  53.         $matcher = new UrlMatcher($routes$context);
  54.         $result $matcher->match(strtok($request_uri'?'));
  55.         $route $result['_route'];
  56.         $DeviceType $this->deviceTypeRepository->find(DeviceType::DEVICE_TYPE_PC);
  57.         if ($route == $this->container->getParameter('eccube_user_data_route')) {
  58.             $route $result['route'];
  59.         }
  60.         $PageLayout $this->pageRepository->getPageByRoute($route);
  61.         switch ($route) {
  62.         case 'product_list':
  63.             $id = isset($_GET['category_id']) ? $_GET['category_id'] : NULL;
  64.             if ($id) {
  65.                 $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  66.                 $builder->setMethod('GET');
  67.                 $searchForm $builder->getForm();
  68.                 $searchForm->handleRequest($request);
  69.                 $searchData $searchForm->getData();
  70.                 $Category $searchForm->get('category_id')->getData();
  71.                 $arrBreadcrumb[0]['category_name'] = $PageLayout->getName();
  72.                 $arrBreadcrumb[0]['url'] = 'product_list';
  73.                 $arrBreadcrumb[0]['q'] = '';
  74.                 $key 1;
  75.                 foreach($Category->getPath() as $cat) {
  76.                     if ((count($Category->getPath())) == $key) {
  77.                         $PageLayout->setName($cat->getName());
  78.                     } else {
  79.                         $arrBreadcrumb[$key]['category_name'] = $cat->getName();
  80.                         $arrBreadcrumb[$key]['url'] = 'product_list';
  81.                         $arrBreadcrumb[$key]['q'] = '?category_id='.$cat->getId();
  82.                     }
  83.                     $key++;
  84.                 }
  85.                 $Breadcrumb[0] = $arrBreadcrumb;
  86.             }
  87.             break;
  88.         case 'product_detail':
  89.             $Breadcrumb = array();
  90.             // 他にいい方法はないのか
  91.             list($url1$url2$url3$id) = explode('/'$request->getPathInfo());
  92.             $Product $this->productRepository->find($id);
  93.             foreach($Product->getProductCategories() as $ProductCategories) {
  94.                 // 同一ルートのカテゴリーは1つにまとめる
  95.                 $id $ProductCategories->getCategory()->getId();
  96.                 $parents $ProductCategories->getCategory()->getParents();
  97.                 if (!empty($parents)) {
  98.                     $id $parents[0]->getId();
  99.                 }
  100.                 $key 1;
  101.                 $arrBreadcrumb = array();
  102.                 $arrBreadcrumb[0]['category_name'] = '商品一覧ページ';
  103.                 $arrBreadcrumb[0]['url'] = 'product_list';
  104.                 $arrBreadcrumb[0]['q'] = '';
  105.                 foreach($ProductCategories->getCategory()->getPath() as $cat) {
  106.                     $arrBreadcrumb[$key]['category_name'] = $cat->getName();
  107.                     $arrBreadcrumb[$key]['url'] = 'product_list';
  108.                     $arrBreadcrumb[$key]['q'] = '?category_id='.$cat->getId();
  109.                     $key++;
  110.                 }
  111.                 $Breadcrumb[$id] = $arrBreadcrumb;
  112.             }
  113.             $PageLayout->setName($Product->getName());
  114.             break;
  115.         case 'homepage':
  116.             // TOPは表示しない
  117.             $PageLayout '';
  118.             break;
  119.         default:
  120.             // todo twigからtitleを自動取得したいが無理そう
  121.             if (strpos($PageLayout->getUrl(), 'mypage') !== false) {
  122.                 $arrBreadcrumb[0]['category_name'] = 'Myページ';
  123.                 $arrBreadcrumb[0]['url'] = 'mypage';
  124.                 $arrBreadcrumb[0]['q'] = '';
  125.                 $Breadcrumb[0] = $arrBreadcrumb;
  126.                 $PageLayout->setName(ltrim(strstr($PageLayout->getName(), '/'), '/'));
  127.             } elseif (strpos($PageLayout->getUrl(), 'shopping') !== false) {
  128.                 $PageLayout->setName('ショッピングカート');
  129.             }
  130.             break;
  131.         }
  132.         return $this->render('Block/BreadcrumbList4.twig', array(
  133.             'PageLayout' => $PageLayout,
  134.             'BreadcrumbList' => $Breadcrumb,
  135.         ));
  136.     }
  137. }