src/Eccube/Controller/SitemapController.php line 78

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