forked from cystbear/ForumBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicController.php
More file actions
180 lines (152 loc) · 6.7 KB
/
Copy pathTopicController.php
File metadata and controls
180 lines (152 loc) · 6.7 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Herzult\Bundle\ForumBundle\Controller;
use Herzult\Bundle\ForumBundle\Form\TopicForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Herzult\Bundle\ForumBundle\Model\Topic;
use Herzult\Bundle\ForumBundle\Model\Category;
use Symfony\Component\HttpFoundation\RedirectResponse;
class TopicController extends Controller
{
public function newAction(Category $category = null)
{
$form = $this->get('herzult_forum.form.new_topic');
$topic = $this->get('herzult_forum.repository.topic')->createNewTopic();
if ($category) {
$topic->setCategory($category);
}
$form->setData($topic);
$template = sprintf('%s:new.html.%s', $this->container->getParameter('herzult_forum.templating.location.topic'), $this->getRenderer());
return $this->get('templating')->renderResponse($template, array(
'form' => $form->createView(),
'category' => $category
));
}
public function createAction(Category $category = null)
{
$form = $this->get('herzult_forum.form.new_topic');
$form->bindRequest($this->get('request'));
$topic = $form->getData();
if (!$form->isValid()) {
$template = sprintf('%s:new.html.%s', $this->container->getParameter('herzult_forum.templating.location.topic'), $this->getRenderer());
return $this->get('templating')->renderResponse('HerzultForumBundle:Topic:new.html.'.$this->getRenderer(), array(
'form' => $form->createView(),
'category' => $category
));
}
$this->get('herzult_forum.creator.topic')->create($topic);
$this->get('herzult_forum.blamer.topic')->blame($topic);
$this->get('herzult_forum.creator.post')->create($topic->getFirstPost());
$this->get('herzult_forum.blamer.post')->blame($topic->getFirstPost());
$objectManager = $this->get('herzult_forum.object_manager');
$objectManager->persist($topic);
$objectManager->persist($topic->getFirstPost());
$objectManager->flush();
$this->get('session')->setFlash('herzult_forum_topic_create/success', true);
$url = $this->get('herzult_forum.router.url_generator')->urlForTopic($topic);
return new RedirectResponse($url);
}
public function listAction($categorySlug, array $pagerOptions)
{
if (null === $categorySlug) {
$category = null;
$topics = $this->get('herzult_forum.repository.topic')->findAll(true);
} else {
$category = $this->findCategoryOr404($categorySlug);
$topics = $this->get('herzult_forum.repository.topic')->findAllByCategory($category, true);
}
$topics->setCurrentPage($pagerOptions['page']);
$topics->setMaxPerPage($this->container->getParameter('herzult_forum.paginator.topics_per_page'));
$template = sprintf('%s:list.%s.%s', $this->container->getParameter('herzult_forum.templating.location.topic'), $this->get('request')->getRequestFormat(), $this->getRenderer());
return $this->get('templating')->renderResponse($template, array(
'topics' => $topics,
'category' => $category,
'pagerOptions' => $pagerOptions
));
}
public function showAction($categorySlug, $slug)
{
$topic = $this->findTopic($categorySlug, $slug);
$this->get('herzult_forum.repository.topic')->incrementTopicNumViews($topic);
if ('html' === $this->get('request')->getRequestFormat()) {
$page = $this->get('request')->query->get('page', 1);
$posts = $this->get('herzult_forum.repository.post')->findAllByTopic($topic, true);
$posts->setCurrentPage($page);
$posts->setMaxPerPage($this->container->getParameter('herzult_forum.paginator.posts_per_page'));
} else {
$posts = $this->get('herzult_forum.repository.post')->findRecentByTopic($topic, 30);
}
$template = sprintf('%s:show.%s.%s', $this->container->getParameter('herzult_forum.templating.location.topic'), $this->get('request')->getRequestFormat(), $this->getRenderer());
return $this->get('templating')->renderResponse($template, array(
'topic' => $topic,
'posts' => $posts
));
}
public function postNewAction($categorySlug, $slug)
{
return $this->forward('HerzultForumBundle:Post:new', array(
'categorySlug' => $categorySlug,
'slug' => $slug
));
}
public function postCreateAction($categorySlug, $slug)
{
return $this->forward('HerzultForumBundle:Post:create', array(
'categorySlug' => $categorySlug,
'slug' => $slug
));
}
public function deleteAction($id)
{
$topic = $this->get('herzult_forum.repository.topic')->find($id);
if (!$topic) {
throw new NotFoundHttpException(sprintf('No topic found with id "%s"', $id));
}
$this->get('herzult_forum.remover.topic')->remove($topic);
$this->get('herzult_forum.object_manager')->flush();
return new RedirectResponse($this->get('herzult_forum.router.url_generator')->urlForCategory($topic->getCategory()));
}
protected function getRenderer()
{
return $this->container->getParameter('herzult_forum.templating.engine');
}
/**
* Find a topic by its category slug and topic slug
*
* @return Topic
**/
public function findTopic($categorySlug, $topicSlug)
{
$category = $this->get('herzult_forum.repository.category')->findOneBySlug($categorySlug);
if (!$category) {
throw new NotFoundHttpException(sprintf('The category with slug "%s" does not exist', $categorySlug));
}
$topic = $this->get('herzult_forum.repository.topic')->findOneByCategoryAndSlug($category, $topicSlug);
if (!$topic) {
throw new NotFoundHttpException(sprintf('The topic with slug "%s" does not exist', $topicSlug));
}
return $topic;
}
/**
* Finds the category having the specified slug or throws a 404 exception
*
* @param string $slug
*
* @return Category
* @throws NotFoundHttpException
*/
protected function findCategoryOr404($slug)
{
$category = $this
->get('herzult_forum.repository.category')
->findOneBySlug($slug)
;
if (!$category) {
throw new NotFoundHttpException(sprintf(
'The category with slug "%s" was not found.',
$slug
));
}
return $category;
}
}