src/Eccube/Controller/SitemapController.php line 194

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Page;
  15. use Eccube\Repository\BaseInfoRepository;
  16. use Eccube\Repository\CategoryRepository;
  17. use Eccube\Repository\Master\ProductListOrderByRepository;
  18. use Eccube\Repository\PageRepository;
  19. use Eccube\Repository\ProductRepository;
  20. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  21. use Knp\Component\Pager\PaginatorInterface;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Routing\RouterInterface;
  27. class SitemapController extends AbstractController
  28. {
  29.     /**
  30.      * @var CategoryRepository
  31.      */
  32.     private $categoryRepository;
  33.     /**
  34.      * @var PageRepository
  35.      */
  36.     private $pageRepository;
  37.     /**
  38.      * @var ProductListOrderByRepository
  39.      */
  40.     private $productListOrderByRepository;
  41.     /**
  42.      * @var ProductRepository
  43.      */
  44.     private $productRepository;
  45.     /**
  46.      * @var RouterInterface
  47.      */
  48.     private $router;
  49.     /**
  50.      * @var BaseInfo
  51.      */
  52.     protected $BaseInfo;
  53.     /**
  54.      * SitemapController constructor.
  55.      */
  56.     public function __construct(
  57.         CategoryRepository $categoryRepository,
  58.         PageRepository $pageRepository,
  59.         ProductListOrderByRepository $productListOrderByRepository,
  60.         ProductRepository $productRepository,
  61.         RouterInterface $router,
  62.         BaseInfoRepository $baseInfoRepository
  63.     ) {
  64.         $this->categoryRepository $categoryRepository;
  65.         $this->pageRepository $pageRepository;
  66.         $this->productListOrderByRepository $productListOrderByRepository;
  67.         $this->productRepository $productRepository;
  68.         $this->router $router;
  69.         $this->BaseInfo $baseInfoRepository->get();
  70.     }
  71.     /**
  72.      * Output sitemap index
  73.      *
  74.      * @Route("/sitemap.xml", name="sitemap_xml", methods={"GET"})
  75.      */
  76.     public function index(PaginatorInterface $paginator)
  77.     {
  78.         $pageQueryBuilder $this->pageRepository->createQueryBuilder('p');
  79.         $Page $pageQueryBuilder->select('p')
  80.             ->where("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)")
  81.             ->andWhere('p.id <> 0')
  82.             ->andWhere('p.MasterPage is null')
  83.             ->orderBy('p.update_date''DESC')
  84.             ->setMaxResults(1)
  85.             ->getQuery()
  86.             ->getSingleResult();
  87.         $Product $this->productRepository->findOneBy(['Status' => 1], ['update_date' => 'DESC']);
  88.         // フロントの商品一覧の条件で商品情報を取得
  89.         $ProductListOrder $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
  90.         $productQueryBuilder $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
  91.         /** @var SlidingPagination $pagination */
  92.         $pagination $paginator->paginate(
  93.             $productQueryBuilder,
  94.             1,
  95.             $this->eccubeConfig['eccube_sitemap_products_per_page']
  96.         );
  97.         $paginationData $pagination->getPaginationData();
  98.         $Category $this->categoryRepository->findOneBy([], ['update_date' => 'DESC']);
  99.         return $this->outputXml(
  100.             [
  101.                 'Category' => $Category,
  102.                 'Product' => $Product,
  103.                 'productPageCount' => $paginationData['pageCount'],
  104.                 'Page' => $Page,
  105.             ],
  106.             'sitemap_index.xml.twig'
  107.         );
  108.     }
  109.     /**
  110.      * Output sitemap of product categories
  111.      *
  112.      * @Route("/sitemap_category.xml", name="sitemap_category_xml", methods={"GET"})
  113.      */
  114.     public function category()
  115.     {
  116.         $Categories $this->categoryRepository->getList(nulltrue);
  117.         return $this->outputXml(['Categories' => $Categories]);
  118.     }
  119.     /**
  120.      * Output sitemap of products
  121.      *
  122.      * Output sitemap of products as status is 1
  123.      *
  124.      * @Route("/sitemap_product_{page}.xml", name="sitemap_product_xml", requirements={"page" = "\d+"}, methods={"GET"})
  125.      *
  126.      * @return Response
  127.      */
  128.     public function product(Request $requestPaginatorInterface $paginator)
  129.     {
  130.         // Doctrine SQLFilter
  131.         if ($this->BaseInfo->isOptionNostockHidden()) {
  132.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  133.         }
  134.         // フロントの商品一覧の条件で商品情報を取得
  135.         $ProductListOrder $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']);
  136.         $productQueryBuilder $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]);
  137.         /** @var SlidingPagination $pagination */
  138.         $pagination $paginator->paginate(
  139.             $productQueryBuilder,
  140.             $request->get('page'),
  141.             $this->eccubeConfig['eccube_sitemap_products_per_page']
  142.         );
  143.         $paginationData $pagination->getPaginationData();
  144.         if ($paginationData['currentItemCount'] === 0) {
  145.             throw new NotFoundHttpException();
  146.         }
  147.         return $this->outputXml(['Products' => $pagination]);
  148.     }
  149.     /**
  150.      * Output sitemap of pages
  151.      *
  152.      * Output sitemap of pages without 'noindex' in meta robots.
  153.      *
  154.      * @Route("/sitemap_page.xml", name="sitemap_page_xml", methods={"GET"})
  155.      */
  156.     public function page()
  157.     {
  158.         $Pages $this->pageRepository->getPageList("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)");
  159.         // URL に変数が含まれる場合は URL の生成ができないためここで除外する
  160.         $DefaultPages array_filter($Pages, function (Page $Page) {
  161.             // 管理画面から作成されたページは除外
  162.             if ($Page->getEditType() === Page::EDIT_TYPE_USER) {
  163.                 return false;
  164.             }
  165.             $route $this->router->getRouteCollection()->get($Page->getUrl());
  166.             if (is_null($route)) {
  167.                 return false;
  168.             }
  169.             return count($route->compile()->getPathVariables()) < 1;
  170.         });
  171.         // 管理画面から作成されたページ
  172.         $UserPages array_filter($Pages, function (Page $Page) {
  173.             return $Page->getEditType() === Page::EDIT_TYPE_USER;
  174.         });
  175.         return $this->outputXml([
  176.             'DefaultPages' => $DefaultPages,
  177.             'UserPages' => $UserPages,
  178.         ]);
  179.     }
  180.     /**
  181.      * Output XML response by data.
  182.      *
  183.      * @param array $data
  184.      * @param string $template_name
  185.      *
  186.      * @return Response
  187.      */
  188.     private function outputXml(array $data$template_name 'sitemap.xml.twig')
  189.     {
  190.         $response = new Response();
  191.         $response->headers->set('Content-Type''application/xml'); // Content-Typeを設定
  192.         return $this->render(
  193.             $template_name,
  194.             $data,
  195.             $response
  196.         );
  197.     }
  198. }