src/Eccube/Controller/SpecialController.php line 32

Open in your IDE?
  1. <?php
  2. namespace Eccube\Controller;
  3. use Customize\Service\WordPressService;
  4. use Eccube\Controller\AbstractController;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class SpecialController extends AbstractController
  9. {
  10.     /** @var WordPressService */
  11.     protected $wordPressService;
  12.     const CATEGORY_ESSAY 'essay';
  13.     const CATEGORY_DOCUMENT 'document';
  14.     const CATEGORY_REPORT 'report';
  15.     const CATEGORY_GUIDE 'guide';
  16.     const CATEGORY_PHILOSOPHY 'philosophy';
  17.     public function __construct(
  18.         WordPressService $wordPressService
  19.     ) {
  20.         $this->wordPressService $wordPressService;
  21.     }
  22.     /**
  23.      * @Route("/special", name="special_posts", methods={"GET"})
  24.      * @Template("Special/index.twig")
  25.      */
  26.     public function index(Request $request)
  27.     {
  28.         $categories = [
  29.             self::CATEGORY_ESSAY,
  30.             self::CATEGORY_DOCUMENT,
  31.             self::CATEGORY_REPORT,
  32.             self::CATEGORY_GUIDE,
  33.             self::CATEGORY_PHILOSOPHY,
  34.         ];
  35.         foreach ($categories as $category) {
  36.             $variableName $category '_posts';
  37.             $allPosts[$variableName] = $this->wordPressService->getSpecialPosts(
  38.                 $category,
  39.                 '-1'
  40.             );
  41.         }
  42.         return [
  43.             'essay_posts' => $allPosts['essay_posts'],
  44.             'document_posts' => $allPosts['document_posts'],
  45.             'report_posts' => $allPosts['report_posts'],
  46.             'guide_posts' => $allPosts['guide_posts'],
  47.             'philosophy_posts' => $allPosts['philosophy_posts'],
  48.         ];
  49.     }
  50.     /**
  51.      * @Route("/special/{id}", name="special_post", methods={"GET"})
  52.      * @Template("Special/detail.twig")
  53.      */
  54.     public function detail(Request $request$id)
  55.     {
  56.         $special_post $this->wordPressService->getSpecialPost($id);
  57.         // 投稿のカテゴリをループして親カテゴリを取得
  58.         $parentCategory null;
  59.         foreach ($special_post->category as $category) {
  60.             if ($category->post_parent == 0) {
  61.                 $parentCategory $category;
  62.                 break;
  63.             }
  64.         }
  65.         // 前後の投稿を取得
  66.         list($prevPost$nextPost) = $this->wordPressService->getPrevAndNextPost($special_post'special'$parentCategory);
  67.         return [
  68.             'Post' => $special_post,
  69.             'NextPost' => $nextPost,
  70.             'PrevPost' => $prevPost,
  71.         ];
  72.     }
  73. }