forked from symfony-cmf/blog-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogController.php
138 lines (116 loc) · 3.88 KB
/
BlogController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2014 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\BlogBundle\Controller;
use Symfony\Cmf\Bundle\BlogBundle\Document\Post;
use Symfony\Cmf\Bundle\BlogBundle\Repository\PostRepository;
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use FOS\RestBundle\View\View;
/**
* Blog Controller
*
* @author Daniel Leech <[email protected]>
*/
class BlogController
{
/**
* @var EngineInterface
*/
protected $templating;
/**
* @var ViewHandlerInterface
*/
protected $viewHandler;
/**
* @var SecurityContextInterface
*/
protected $securityContext;
/**
* @var PostRepository
*/
protected $postRepository;
/**
* The permission to check for when doing the publish workflow check.
*
* @var string
*/
private $publishWorkflowPermission = PublishWorkflowChecker::VIEW_ATTRIBUTE;
public function __construct(
EngineInterface $templating,
ViewHandlerInterface $viewHandler = null,
SecurityContextInterface $securityContext,
PostRepository $postRepository
) {
$this->templating = $templating;
$this->viewHandler = $viewHandler;
$this->securityContext = $securityContext;
$this->postRepository = $postRepository;
}
/**
* What attribute to use in the publish workflow check. This typically
* is VIEW or VIEW_ANONYMOUS.
*
* @param string $attribute
*/
public function setPublishWorkflowPermission($attribute)
{
$this->publishWorkflowPermission = $attribute;
}
protected function renderResponse($contentTemplate, $params)
{
if ($this->viewHandler) {
$view = new View($params);
$view->setTemplate($contentTemplate);
return $this->viewHandler->handle($view);
}
return $this->templating->renderResponse($contentTemplate, $params);
}
public function viewPostAction(Post $contentDocument, $contentTemplate = null)
{
$post = $contentDocument;
if (true !== $this->securityContext->isGranted($this->publishWorkflowPermission, $post)) {
throw new NotFoundHttpException(sprintf(
'Post "%s" is not published'
, $post->getTitle()));
}
$contentTemplate = $contentTemplate ? : 'CmfBlogBundle:Blog:view_post.html.twig';
return $this->renderResponse($contentTemplate, array(
'post' => $post,
));
}
public function listAction(Request $request, $contentDocument, $contentTemplate = null)
{
$blog = $contentDocument;
$tag = $request->get('tag', null);
// @todo: Pagination
$posts = $this->postRepository->search(array(
'tag' => $tag,
'blog_id' => $blog->getId(),
));
$contentTemplate = $contentTemplate ?: 'CmfBlogBundle:Blog:list.{_format}.twig';
// @todo: Copy and pasted from ContentBundle::ContentController
// I wonder if we can share some code between content-like
// bundles.
$contentTemplate = str_replace(
array('{_format}', '{_locale}'),
array($request->getRequestFormat(), $request->getLocale()),
$contentTemplate
);
return $this->renderResponse($contentTemplate, array(
'blog' => $blog,
'posts' => $posts,
'tag' => $tag
));
}
}