1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Blog; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Comment; |
6
|
|
|
use AppBundle\Form\Type\CommentType; |
7
|
|
|
use AppBundle\Form\Type\SearchType; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
11
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
19
|
|
|
|
20
|
|
|
class BlogController extends Controller |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $page |
25
|
|
|
* @Method("GET") |
26
|
|
|
* @Route("/{pager}/{page}", name="homepage", |
27
|
|
|
* defaults={"pager": "page", "page": 1}, |
28
|
|
|
* requirements={ |
29
|
|
|
* "pager": "page", |
30
|
|
|
* "page": "[1-9]\d*", |
31
|
|
|
* }) |
32
|
|
|
* @Template("AppBundle:blog:blogPosts.html.twig") |
33
|
|
|
* |
34
|
|
|
* @return Response |
35
|
|
|
*/ |
36
|
1 |
View Code Duplication |
public function indexAction($page = 1) |
|
|
|
|
37
|
|
|
{ |
38
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
39
|
1 |
|
$articles = $em->getRepository("AppBundle:Article") |
40
|
1 |
|
->getArticlesWithCountComment($page); |
41
|
|
|
|
42
|
|
|
return [ |
43
|
1 |
|
'articles' => $articles, |
44
|
1 |
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $slug |
49
|
|
|
* @Method("GET") |
50
|
|
|
* @Route("/article/{slug}", name="showArticle") |
51
|
|
|
* @Template("AppBundle:blog:blogSingle.html.twig") |
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
1 |
|
public function showAction($slug) |
56
|
|
|
{ |
57
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
58
|
1 |
|
$article = $em->getRepository("AppBundle:Article") |
59
|
1 |
|
->getArticleWithDep($slug); |
60
|
|
|
|
61
|
|
|
return [ |
62
|
1 |
|
'article' => $article, |
63
|
1 |
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $sortBy |
68
|
|
|
* @param $param |
69
|
|
|
* @param $page |
70
|
|
|
* @Method("GET") |
71
|
|
|
* @Route("/{sortBy}/{param}/{pager}/{page}", name="sortArticles", |
72
|
|
|
* defaults={"pager": "page", "page": 1}, |
73
|
|
|
* requirements={ |
74
|
|
|
* "sortBy": "category|tag|author|date", |
75
|
|
|
* "pager": "page", |
76
|
|
|
* "page": "[1-9]\d*", |
77
|
|
|
* }) |
78
|
|
|
* @Template("AppBundle:blog:blogPosts.html.twig") |
79
|
|
|
* |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
3 |
|
public function sortAction($sortBy, $param, $page = 1) |
83
|
|
|
{ |
84
|
3 |
|
if ($sortBy == 'date') { |
85
|
|
|
$dateConstraint = new Assert\Date(); |
86
|
|
|
$dateConstraint->message = 'Invalid date'; |
87
|
|
|
|
88
|
|
|
$errorList = $this->get('validator')->validate($param, $dateConstraint); |
89
|
|
|
|
90
|
|
|
if (0 !== count($errorList)) { |
91
|
|
|
throw $this->createNotFoundException( |
92
|
|
|
$errorMessage = $errorList[0]->getMessage() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
$em = $this->getDoctrine()->getManager(); |
98
|
3 |
|
$articles = $em->getRepository("AppBundle:Article") |
99
|
3 |
|
->getArticlesSorted($sortBy, $param, $page); |
100
|
|
|
|
101
|
|
|
return [ |
102
|
3 |
|
'articles' => $articles, |
103
|
3 |
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Request $request |
108
|
|
|
* @Method("POST") |
109
|
|
|
* @Route("/search", name="searchArticles") |
110
|
|
|
* @Template("AppBundle:blog:blogPosts.html.twig") |
111
|
|
|
* |
112
|
|
|
* @return Response |
113
|
|
|
*/ |
114
|
|
|
public function searchAction(Request $request) |
115
|
|
|
{ |
116
|
|
|
$form = $this->createForm(SearchType::class); |
117
|
|
|
|
118
|
|
|
$form->handleRequest($request); |
119
|
|
|
if ($form->isValid()) { |
120
|
|
|
$data = $form->getData(); |
121
|
|
|
$em = $this->getDoctrine()->getManager(); |
122
|
|
|
$articles = $em->getRepository("AppBundle:Article") |
123
|
|
|
->getArticlesSorted('search', $data['param'], 1, 100); |
124
|
|
|
|
125
|
|
|
return [ |
126
|
|
|
'articles' => $articles, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
else { |
130
|
|
|
return []; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Request $request |
136
|
|
|
* @param $slug |
137
|
|
|
* @Route("/newCommentFor/{slug}", name="commentForm") |
138
|
|
|
* @Template("AppBundle:blog:commentForm.html.twig") |
139
|
|
|
* |
140
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
141
|
|
|
*/ |
142
|
2 |
|
public function newCommentAction(Request $request, $slug) |
143
|
|
|
{ |
144
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
145
|
2 |
|
$article = $em->getRepository("AppBundle:Article") |
|
|
|
|
146
|
2 |
|
->findOneBySlug($slug); |
147
|
2 |
|
$comment = new Comment(); |
148
|
2 |
|
$comment->setArticle($article); |
149
|
|
|
|
150
|
2 |
|
$form = $this->createForm(CommentType::class, $comment, [ |
151
|
2 |
|
'em' => $em, |
152
|
2 |
|
'action' => $this->generateUrl('commentForm', ['slug' => $slug]), |
153
|
2 |
|
'method' => Request::METHOD_POST, |
154
|
2 |
|
]); |
155
|
|
|
|
156
|
|
|
//ToDo: check isUserAnonymous |
157
|
2 |
|
if ($user = 'anonymous') { |
|
|
|
|
158
|
|
|
$form |
159
|
2 |
|
->add('user', EntityType::class, array( |
160
|
2 |
|
'class' => 'AppBundle:User', |
161
|
2 |
|
'choice_label' => 'name', |
162
|
2 |
|
'placeholder' => '* Choose user (remove after security)', |
163
|
2 |
|
)) |
164
|
2 |
|
->add('name', TextType::class, array( |
165
|
2 |
|
'attr' => array('placeholder' => '* Name (anonymous)') |
166
|
2 |
|
) |
167
|
2 |
|
) |
168
|
2 |
|
->add('email', EmailType::class, array( |
169
|
2 |
|
'attr' => array('placeholder' => '* Email (anonymous)') |
170
|
2 |
|
) |
171
|
2 |
|
); |
172
|
2 |
|
} |
173
|
|
|
|
174
|
|
|
$form |
175
|
2 |
|
->add('save', SubmitType::class, array( |
176
|
2 |
|
'label' => 'Submit Comment', |
177
|
2 |
|
'attr' => array('class' => "btn btn-primary") |
178
|
2 |
|
)); |
179
|
|
|
|
180
|
2 |
View Code Duplication |
if ($request->getMethod() == 'POST') { |
|
|
|
|
181
|
|
|
$form->handleRequest($request); |
182
|
|
|
if ($form->isValid()) { |
183
|
|
|
$em->persist($comment); |
184
|
|
|
$em->flush(); |
185
|
|
|
|
186
|
|
|
return $this->redirectToRoute('success'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return [ |
191
|
2 |
|
'form' => $form->createView(), |
192
|
2 |
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $slug |
197
|
|
|
* @param $page |
198
|
|
|
* @Route("/comments/{slug}/{pager}/{page}", name="articleComments", |
199
|
|
|
* defaults={"pager": "page", "page": 1}, |
200
|
|
|
* requirements={ |
201
|
|
|
* "pager": "page", |
202
|
|
|
* "page": "[1-9]\d*", |
203
|
|
|
* }) |
204
|
|
|
* @Template("AppBundle:blog:comments.html.twig") |
205
|
|
|
* |
206
|
|
|
* @return Response |
207
|
|
|
*/ |
208
|
2 |
|
public function showArticleCommentsAction($slug, $page = 1) |
209
|
|
|
{ |
210
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
211
|
2 |
|
$comments = $em->getRepository("AppBundle:Comment") |
212
|
2 |
|
->getArticleComment($slug, $page, 5); |
213
|
|
|
|
214
|
|
|
return [ |
215
|
2 |
|
'comments' => $comments, |
216
|
2 |
|
]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @Route("/success", name="success") |
221
|
|
|
* @Template("AppBundle:blog:success.html.twig") |
222
|
|
|
* |
223
|
|
|
* @return Response |
224
|
|
|
*/ |
225
|
1 |
|
public function successAction() |
226
|
|
|
{ |
227
|
1 |
|
return []; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @Route("/search_form_render", name="searchFormRender") |
232
|
|
|
* @Template("AppBundle:blog:widgetSearchForm.html.twig") |
233
|
|
|
* |
234
|
|
|
* @return Response |
235
|
|
|
*/ |
236
|
5 |
|
public function createSearchFormAction() |
237
|
|
|
{ |
238
|
5 |
|
$form = $this->createForm(SearchType::class); |
239
|
|
|
|
240
|
|
|
return [ |
241
|
5 |
|
'form' => $form->createView(), |
242
|
5 |
|
]; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.