app/Plugin/CMBlogPro42/Controller/Blog/BlogController.php line 103

Open in your IDE?
  1. <?php
  2. namespace Plugin\CMBlogPro42\Controller\Blog;
  3. use Eccube\Controller\AbstractController;
  4. use Knp\Component\Pager\PaginatorInterface;
  5. use Plugin\CMBlogPro42\Entity\Blog;
  6. use Plugin\CMBlogPro42\Entity\BlogStatus;
  7. use Plugin\CMBlogPro42\Form\Type\Admin\BlogType;
  8. use Plugin\CMBlogPro42\Repository\BlogRepository;
  9. use Plugin\CMBlogPro42\Repository\CategoryRepository;
  10. use Plugin\CMBlogPro42\Repository\ConfigRepository;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Eccube\Repository\PageRepository;
  15. use Eccube\Repository\ProductRepository;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class BlogController extends AbstractController
  18. {
  19.     /**
  20.      * @var BlogRepository
  21.      */
  22.     protected $blogRepository;
  23.     /**
  24.      * @var CategoryRepository
  25.      */
  26.     protected $categoryRepository;
  27.     /**
  28.      * @var ProductRepository
  29.      */
  30.     protected $productRepository;
  31.     /**
  32.      * @var ConfigRepository
  33.      */
  34.     protected $configRepository;
  35.     /**
  36.      * @var PageRepository
  37.      */
  38.     protected $pageRepository;
  39.     /**
  40.      * BlogController constructor.
  41.      *
  42.      * @param BlogRepository $blogRepository
  43.      * @param ProductRepository $productRepository
  44.      * @param ConfigRepository $blogRepository
  45.      */
  46.     public function __construct(
  47.         BlogRepository $blogRepository,
  48.         CategoryRepository $categoryRepository,
  49.         PageRepository $pageRepository,
  50.         ProductRepository $productRepository,
  51.         ConfigRepository $configRepository)
  52.     {
  53.         $this->blogRepository $blogRepository;
  54.         $this->categoryRepository $categoryRepository;
  55.         $this->productRepository $productRepository;
  56.         $this->configRepository $configRepository;
  57.         $this->pageRepository $pageRepository;
  58.     }
  59.     /**
  60.      * @Route("/news/", name="cm_blog_pro_page_list")
  61.      * @Template("blog/list.twig")
  62.      */
  63.     public function index(Request $requestPaginatorInterface $paginator)
  64.     {
  65.         $form $this->createForm(BlogType::class);
  66.         $search $request->query->all();
  67.         $search["status"] = 1;
  68.         //$qb = $this->blogRepository->getQueryBuilderBySearchData($search);
  69.         //重要なお知らせカテゴリを除外
  70.         $qb $this->blogRepository->getQueryBuilderBySearchDataWithoutCategory2($search);
  71.         $config $this->configRepository->get();
  72.         $pagination $paginator->paginate(
  73.             $qb,
  74.             !empty($search['pageno']) ? $search['pageno'] : 1,
  75.             !empty($search['disp_number']) ? $search['disp_number'] : $config->getDisplayPage()
  76.         );
  77.         return [
  78.             'form' => $form->createView(),
  79.             'categories' => $this->categoryRepository->getFrontCategoryList(),
  80.             'pagination' => $pagination,
  81.             'monthArr' => $this->setMonthArchive($search)
  82.         ];
  83.     }
  84.     /**
  85.      * @Route("/special/", name="cm_blog_pro_page_list_special")
  86.      * @Template("blog/list_special.twig")
  87.      */
  88.     public function index_special(Request $requestPaginatorInterface $paginator)
  89.     {
  90.         $form $this->createForm(BlogType::class);
  91.         $search $request->query->all();
  92.         $search["status"] = 1;
  93.         // カテゴリID 6だけを表示するように変更
  94.         $qb $this->blogRepository->getQueryBuilderBySearchDataForCategory6($search);
  95.         $config $this->configRepository->get();
  96.         $pagination $paginator->paginate(
  97.             $qb,
  98.             !empty($search['pageno']) ? $search['pageno'] : 1,
  99.             !empty($search['disp_number']) ? $search['disp_number'] : $config->getDisplayPage()
  100.         );
  101.         return [
  102.             'form' => $form->createView(),
  103.             'categories' => $this->categoryRepository->getFrontCategoryList(),
  104.             'pagination' => $pagination,
  105.             'monthArr' => $this->setMonthArchive($search)
  106.         ];
  107.     }
  108.     /**
  109.      * @Route("/news/{id}/", name="cm_blog_pro_page_detail")
  110.      * @Template("blog/detail.twig")
  111.      */
  112.     public function detail(Request $request$id)
  113.     {
  114.         //postgresql→int の最大値:2147483647だから、最大値を超えるとSlug として判断
  115.         if(is_numeric($id) && $id <= 2147483647) {
  116.             $blog $this->blogRepository->get($id);
  117.             //IDで検索できない場合、Slugで検索する
  118.             if(!$blog) {
  119.                 $blog $this->blogRepository->findBy(['slug' => $id]);
  120.                 $blog $blog[0];
  121.             }
  122.         } else {
  123.             $blog $this->blogRepository->findBy(['slug' => $id]);
  124.             $blog $blog[0];
  125.         }
  126.         if (!$blog || !$this->checkVisibility($blog)) {
  127.             $this->addError('ブログを見つかりませんでした。');
  128.             return $this->redirectToRoute('cm_blog_pro_page_list');
  129.         }
  130.         $config $this->configRepository->get();
  131.         $form $this->createForm(BlogType::class, $blog);
  132.         $cmPage $this->pageRepository->findOneBy(['url'  => 'cm_blog_pro_page_detail']);
  133.         if($blog->getAuthor() != Null){
  134.             $Page["author"] = $blog->getAuthor();
  135.         } else {
  136.             $Page["author"] = $cmPage->getAuthor();
  137.         }
  138.         if($blog->getDescription() != Null){
  139.             $Page["description"] = $blog->getDescription();
  140.         } else {
  141.             $Page["description"] = $cmPage->getDescription();
  142.         };
  143.         if($blog->getKeyword() != Null){
  144.             $Page["keyword"] = $blog->getKeyword();
  145.         } else {
  146.             $Page["keyword"] = $cmPage->getKeyword();
  147.         };
  148.         if($blog->getRobot() != Null){
  149.             $Page["meta_robots"] = $blog->getRobot();
  150.         } else {
  151.             $Page["meta_robots"] = $cmPage->getMetaRobots();
  152.         }
  153.         if($blog->getMetatag() != Null){
  154.             $Page["meta_tags"] = $blog->getMetatag();
  155.         } else {
  156.             $Page["meta_tags"] = $cmPage->getMetaTags();
  157.         }
  158.         $tagArr = [];
  159.         if($blog->getTag() != Null){
  160.             $tagArr preg_split('/[;, 、]+/u'$blog->getTag());
  161.         }
  162.         $Page["edit_type"] = 0;
  163.         return [
  164.             'form' => $form->createView(),
  165.             'blog' => $blog,
  166.             'Page' => $Page,
  167.             'subtitle' => $blog->getTitle(),
  168.             'tags' => $tagArr,
  169.             'monthArr' => $this->setMonthArchive()
  170.         ];
  171.     }
  172.     /**
  173.      * @Route("/special/{id}/", name="cm_blog_pro_page_detail_special")
  174.      * @Template("blog/detail_special.twig")
  175.      */
  176.     public function detail_special(Request $request$id)
  177.     {
  178.         //postgresql→int の最大値:2147483647だから、最大値を超えるとSlug として判断
  179.         if(is_numeric($id) && $id <= 2147483647) {
  180.             $blog $this->blogRepository->get($id);
  181.             //IDで検索できない場合、Slugで検索する
  182.             if(!$blog) {
  183.                 $blog $this->blogRepository->findBy(['slug' => $id]);
  184.                 $blog $blog[0];
  185.             }
  186.         } else {
  187.             $blog $this->blogRepository->findBy(['slug' => $id]);
  188.             $blog $blog[0];
  189.         }
  190.         if (!$blog || !$this->checkVisibility($blog)) {
  191.             $this->addError('ブログを見つかりませんでした。');
  192.             return $this->redirectToRoute('cm_blog_pro_page_list');
  193.         }
  194.         $config $this->configRepository->get();
  195.         $form $this->createForm(BlogType::class, $blog);
  196.         $cmPage $this->pageRepository->findOneBy(['url'  => 'cm_blog_pro_page_detail']);
  197.         if($blog->getAuthor() != Null){
  198.             $Page["author"] = $blog->getAuthor();
  199.         } else {
  200.             $Page["author"] = $cmPage->getAuthor();
  201.         }
  202.         if($blog->getDescription() != Null){
  203.             $Page["description"] = $blog->getDescription();
  204.         } else {
  205.             $Page["description"] = $cmPage->getDescription();
  206.         };
  207.         if($blog->getKeyword() != Null){
  208.             $Page["keyword"] = $blog->getKeyword();
  209.         } else {
  210.             $Page["keyword"] = $cmPage->getKeyword();
  211.         };
  212.         if($blog->getRobot() != Null){
  213.             $Page["meta_robots"] = $blog->getRobot();
  214.         } else {
  215.             $Page["meta_robots"] = $cmPage->getMetaRobots();
  216.         }
  217.         if($blog->getMetatag() != Null){
  218.             $Page["meta_tags"] = $blog->getMetatag();
  219.         } else {
  220.             $Page["meta_tags"] = $cmPage->getMetaTags();
  221.         }
  222.         $tagArr = [];
  223.         if($blog->getTag() != Null){
  224.             $tagArr preg_split('/[;, 、]+/u'$blog->getTag());
  225.         }
  226.         $Page["edit_type"] = 0;
  227.         return [
  228.             'form' => $form->createView(),
  229.             'blog' => $blog,
  230.             'Page' => $Page,
  231.             'subtitle' => $blog->getTitle(),
  232.             'tags' => $tagArr,
  233.             'monthArr' => $this->setMonthArchive()
  234.         ];
  235.     }
  236.     /**
  237.      * 閲覧可能なブログかどうかを判定
  238.      *
  239.      * @param Blog $blog
  240.      *
  241.      * @return boolean 閲覧可能な場合はtrue
  242.      */
  243.     protected function checkVisibility(Blog $blog)
  244.     {
  245.         $is_admin $this->session->has('_security_admin');
  246.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  247.         if (!$is_admin) {
  248.             if ($blog->getStatus()->getId() !== BlogStatus::DISPLAY_SHOW) {
  249.                 return false;
  250.             }
  251.         }
  252.         return true;
  253.     }
  254.     /**
  255.      * 月別アーカイブ表示のため取得
  256.      *
  257.      * @param
  258.      *
  259.      * @return array 月別配列
  260.      */
  261.     private function setMonthArchive($search = []) {
  262.         $releaseDate $this->blogRepository->getReleaseDate($search);
  263.         $monthArr = [];
  264.         foreach($releaseDate as $val) {
  265.             if(is_null($val['release_date'])) {
  266.                 continue;
  267.             }
  268.             $key $val['release_date']->format('Y-m');
  269.             if(isset($monthArr[$key])) {
  270.                 continue;
  271.             }
  272.             $monthArr[$key] = $val['release_date']->format('Y年m月');
  273.         }
  274.         return $monthArr;
  275.     }
  276.     /**
  277.      *
  278.      * 重要なお知らせブロック
  279.      *
  280.      * @Route("/news/important/", name="cm_blog_block_important")
  281.      * @Template("Block/cm_blog_block_important.twig")
  282.      */
  283.     public function importantBlog(Request $requestPaginatorInterface $paginator)
  284.     {
  285.         $search = ['categories' => [2], 'status' => 1];
  286.         $qb $this->blogRepository->getQueryBuilderBySearchData($search);
  287.         $pagination $paginator->paginate(
  288.             $qb,
  289.             !empty($search['pageno']) ? $search['pageno'] : 1,
  290.             !empty($search['disp_number']) ? $search['disp_number'] : $config->getDisplayPage()
  291.         );
  292.         return [
  293.             'blogs' => $pagination,
  294.         ];
  295.     }
  296. }