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

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_page_list")
  61.      * @Template("web/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.         $config $this->configRepository->get();
  70.         $pagination $paginator->paginate(
  71.             $qb,
  72.             !empty($search['pageno']) ? $search['pageno'] : 1,
  73.             !empty($search['disp_number']) ? $search['disp_number'] : $config->getDisplayPage()
  74.         );
  75.         return [
  76.             'form' => $form->createView(),
  77.             'categories' => $this->categoryRepository->getFrontCategoryList(),
  78.             'pagination' => $pagination,
  79.             'monthArr' => $this->setMonthArchive($search)
  80.         ];
  81.     }
  82.     /**
  83.      * @Route("/news/{id}/", name="cm_blog_page_detail")
  84.      * @Template("web/detail.twig")
  85.      */
  86.     public function detail(Request $request$id)
  87.     {
  88.         //postgresql→int の最大値:2147483647だから、最大値を超えるとSlug として判断
  89.         if(is_numeric($id) && $id <= 2147483647) {
  90.             $blog $this->blogRepository->get($id);
  91.             //IDで検索できない場合、Slugで検索する
  92.             if(!$blog) {
  93.                 $blog $this->blogRepository->findBy(['slug' => $id]);
  94.                 $blog $blog[0];
  95.             }
  96.         } else {
  97.             $blog $this->blogRepository->findBy(['slug' => $id]);
  98.             $blog $blog[0];
  99.         }
  100.         if (!$blog || !$this->checkVisibility($blog)) {
  101.             $this->addError('ブログを見つかりませんでした。');
  102.             return $this->redirectToRoute('cm_blog_pro_page_list');
  103.         }
  104.         $config $this->configRepository->get();
  105.         $form $this->createForm(BlogType::class, $blog);
  106.         $cmPage $this->pageRepository->findOneBy(['url'  => 'cm_blog_pro_page_detail']);
  107.         if($blog->getAuthor() != Null){
  108.             $Page["author"] = $blog->getAuthor();
  109.         } else {
  110.             $Page["author"] = $cmPage->getAuthor();
  111.         }
  112.             
  113.         if($blog->getDescription() != Null){
  114.             $Page["description"] = $blog->getDescription();
  115.         } else {
  116.             $Page["description"] = $cmPage->getDescription();
  117.         };
  118.         
  119.         if($blog->getKeyword() != Null){
  120.             $Page["keyword"] = $blog->getKeyword();
  121.         } else {
  122.             $Page["keyword"] = $cmPage->getKeyword();
  123.         };
  124.         
  125.         if($blog->getRobot() != Null){
  126.             $Page["meta_robots"] = $blog->getRobot();
  127.         } else {
  128.             $Page["meta_robots"] = $cmPage->getMetaRobots();
  129.         }
  130.         
  131.         if($blog->getMetatag() != Null){
  132.             $Page["meta_tags"] = $blog->getMetatag();
  133.         } else {
  134.             $Page["meta_tags"] = $cmPage->getMetaTags();
  135.         }
  136.         $tagArr = [];
  137.         if($blog->getTag() != Null){
  138.             $tagArr preg_split('/[;, 、]+/u'$blog->getTag());
  139.         }
  140.         $Page["edit_type"] = 0;
  141.         return [
  142.             'form' => $form->createView(),
  143.             'blog' => $blog,
  144.             'Page' => $Page,
  145.             'subtitle' => $blog->getTitle(),
  146.             'tags' => $tagArr,
  147.             'monthArr' => $this->setMonthArchive()
  148.         ];
  149.     }
  150.     /**
  151.      * 閲覧可能なブログかどうかを判定
  152.      *
  153.      * @param Blog $blog
  154.      *
  155.      * @return boolean 閲覧可能な場合はtrue
  156.      */
  157.     protected function checkVisibility(Blog $blog)
  158.     {
  159.         $is_admin $this->session->has('_security_admin');
  160.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  161.         if (!$is_admin) {
  162.             if ($blog->getStatus()->getId() !== BlogStatus::DISPLAY_SHOW) {
  163.                 return false;
  164.             }
  165.         }
  166.         return true;
  167.     }
  168.     /**
  169.      * 月別アーカイブ表示のため取得
  170.      *
  171.      * @param 
  172.      *
  173.      * @return array 月別配列
  174.      */
  175.     private function setMonthArchive($search = []) {
  176.         
  177.         $releaseDate $this->blogRepository->getReleaseDate($search);
  178.         $monthArr = [];
  179.         foreach($releaseDate as $val) {
  180.             if(is_null($val['release_date'])) {
  181.                 continue;
  182.             }
  183.             $key $val['release_date']->format('Y-m');
  184.             if(isset($monthArr[$key])) {
  185.                 continue;
  186.             }
  187.             $monthArr[$key] = $val['release_date']->format('Y年m月');
  188.             
  189.         }
  190.         return $monthArr;
  191.     }
  192. }