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

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