<?php
namespace Eccube\Controller;
use Customize\Service\WordPressService;
use Eccube\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class SpecialController extends AbstractController
{
/** @var WordPressService */
protected $wordPressService;
const CATEGORY_ESSAY = 'essay';
const CATEGORY_DOCUMENT = 'document';
const CATEGORY_REPORT = 'report';
const CATEGORY_GUIDE = 'guide';
const CATEGORY_PHILOSOPHY = 'philosophy';
public function __construct(
WordPressService $wordPressService
) {
$this->wordPressService = $wordPressService;
}
/**
* @Route("/special", name="special_posts", methods={"GET"})
* @Template("Special/index.twig")
*/
public function index(Request $request)
{
$categories = [
self::CATEGORY_ESSAY,
self::CATEGORY_DOCUMENT,
self::CATEGORY_REPORT,
self::CATEGORY_GUIDE,
self::CATEGORY_PHILOSOPHY,
];
foreach ($categories as $category) {
$variableName = $category . '_posts';
$allPosts[$variableName] = $this->wordPressService->getSpecialPosts(
$category,
'-1'
);
}
return [
'essay_posts' => $allPosts['essay_posts'],
'document_posts' => $allPosts['document_posts'],
'report_posts' => $allPosts['report_posts'],
'guide_posts' => $allPosts['guide_posts'],
'philosophy_posts' => $allPosts['philosophy_posts'],
];
}
/**
* @Route("/special/{id}", name="special_post", methods={"GET"})
* @Template("Special/detail.twig")
*/
public function detail(Request $request, $id)
{
$special_post = $this->wordPressService->getSpecialPost($id);
// 投稿のカテゴリをループして親カテゴリを取得
$parentCategory = null;
foreach ($special_post->category as $category) {
if ($category->post_parent == 0) {
$parentCategory = $category;
break;
}
}
// 前後の投稿を取得
list($prevPost, $nextPost) = $this->wordPressService->getPrevAndNextPost($special_post, 'special', $parentCategory);
return [
'Post' => $special_post,
'NextPost' => $nextPost,
'PrevPost' => $prevPost,
];
}
}