1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Controller\MembersArea; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Application\Form\Type\PostType; |
9
|
|
|
use Application\Entity\PostEntity; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Borut Balažek <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class PostsController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param Application $app |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function listAction(Request $request, Application $app) |
23
|
|
|
{ |
24
|
|
|
if ( |
25
|
|
|
!$app['security']->isGranted('ROLE_POSTS_EDITOR') && |
26
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
27
|
|
|
) { |
28
|
|
|
$app->abort(403); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$limitPerPage = $request->query->get('limit_per_page', 20); |
32
|
|
|
$currentPage = $request->query->get('page'); |
33
|
|
|
|
34
|
|
|
$postResults = $app['orm.em'] |
35
|
|
|
->createQueryBuilder() |
36
|
|
|
->select('p') |
37
|
|
|
->from('Application\Entity\PostEntity', 'p') |
38
|
|
|
->leftJoin('p.user', 'u') |
39
|
|
|
; |
40
|
|
|
|
41
|
|
|
$pagination = $app['paginator']->paginate( |
42
|
|
|
$postResults, |
43
|
|
|
$currentPage, |
44
|
|
|
$limitPerPage, |
45
|
|
|
array( |
46
|
|
|
'route' => 'members-area.posts', |
47
|
|
|
'defaultSortFieldName' => 'p.timeCreated', |
48
|
|
|
'defaultSortDirection' => 'desc', |
49
|
|
|
'searchFields' => array( |
50
|
|
|
'p.title', |
51
|
|
|
'p.content', |
52
|
|
|
'u.username', |
53
|
|
|
'u.email', |
54
|
|
|
), |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return new Response( |
59
|
|
|
$app['twig']->render( |
60
|
|
|
'contents/members-area/posts/list.html.twig', |
61
|
|
|
array( |
62
|
|
|
'pagination' => $pagination, |
63
|
|
|
) |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Request $request |
70
|
|
|
* @param Application $app |
71
|
|
|
* |
72
|
|
|
* @return Response |
73
|
|
|
*/ |
74
|
|
|
public function newAction(Request $request, Application $app) |
75
|
|
|
{ |
76
|
|
|
if ( |
77
|
|
|
!$app['security']->isGranted('ROLE_POSTS_EDITOR') && |
78
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
79
|
|
|
) { |
80
|
|
|
$app->abort(403); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$form = $app['form.factory']->create( |
84
|
|
|
new PostType(), |
85
|
|
|
new PostEntity() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
View Code Duplication |
if ($request->getMethod() == 'POST') { |
|
|
|
|
89
|
|
|
$form->handleRequest($request); |
90
|
|
|
|
91
|
|
|
if ($form->isValid()) { |
92
|
|
|
$postEntity = $form->getData(); |
93
|
|
|
|
94
|
|
|
/*** Image ***/ |
95
|
|
|
$postEntity |
96
|
|
|
->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
97
|
|
|
->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
98
|
|
|
->imageUpload() |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
$app['orm.em']->persist($postEntity); |
102
|
|
|
$app['orm.em']->flush(); |
103
|
|
|
|
104
|
|
|
$app['flashbag']->add( |
105
|
|
|
'success', |
106
|
|
|
$app['translator']->trans( |
107
|
|
|
'members-area.posts.new.successText' |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $app->redirect( |
112
|
|
|
$app['url_generator']->generate( |
113
|
|
|
'members-area.posts.edit', |
114
|
|
|
array( |
115
|
|
|
'id' => $postEntity->getId(), |
116
|
|
|
) |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return new Response( |
123
|
|
|
$app['twig']->render( |
124
|
|
|
'contents/members-area/posts/new.html.twig', |
125
|
|
|
array( |
126
|
|
|
'form' => $form->createView(), |
127
|
|
|
) |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $id |
134
|
|
|
* @param Request $request |
135
|
|
|
* @param Application $app |
136
|
|
|
* |
137
|
|
|
* @return Response |
138
|
|
|
*/ |
139
|
|
|
public function editAction($id, Request $request, Application $app) |
140
|
|
|
{ |
141
|
|
|
if ( |
142
|
|
|
!$app['security']->isGranted('ROLE_POSTS_EDITOR') && |
143
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
144
|
|
|
) { |
145
|
|
|
$app->abort(403); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$post = $app['orm.em']->find('Application\Entity\PostEntity', $id); |
149
|
|
|
|
150
|
|
|
if (!$post) { |
151
|
|
|
$app->abort(404); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$form = $app['form.factory']->create( |
155
|
|
|
new PostType(), |
156
|
|
|
$post |
157
|
|
|
); |
158
|
|
|
|
159
|
|
View Code Duplication |
if ($request->getMethod() == 'POST') { |
|
|
|
|
160
|
|
|
$form->handleRequest($request); |
161
|
|
|
|
162
|
|
|
if ($form->isValid()) { |
163
|
|
|
$postEntity = $form->getData(); |
164
|
|
|
|
165
|
|
|
/*** Image ***/ |
166
|
|
|
$postEntity |
167
|
|
|
->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
168
|
|
|
->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
169
|
|
|
->imageUpload() |
170
|
|
|
; |
171
|
|
|
|
172
|
|
|
$app['orm.em']->persist($postEntity); |
173
|
|
|
$app['orm.em']->flush(); |
174
|
|
|
|
175
|
|
|
$app['flashbag']->add( |
176
|
|
|
'success', |
177
|
|
|
$app['translator']->trans( |
178
|
|
|
'members-area.posts.edit.successText' |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return $app->redirect( |
183
|
|
|
$app['url_generator']->generate( |
184
|
|
|
'members-area.posts.edit', |
185
|
|
|
array( |
186
|
|
|
'id' => $postEntity->getId(), |
187
|
|
|
) |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return new Response( |
194
|
|
|
$app['twig']->render( |
195
|
|
|
'contents/members-area/posts/edit.html.twig', |
196
|
|
|
array( |
197
|
|
|
'form' => $form->createView(), |
198
|
|
|
'post' => $post, |
199
|
|
|
) |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $id |
206
|
|
|
* @param Request $request |
207
|
|
|
* @param Application $app |
208
|
|
|
* |
209
|
|
|
* @return Response |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function removeAction($id, Request $request, Application $app) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
if ( |
214
|
|
|
!$app['security']->isGranted('ROLE_POSTS_EDITOR') && |
215
|
|
|
!$app['security']->isGranted('ROLE_ADMIN') |
216
|
|
|
) { |
217
|
|
|
$app->abort(403); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$posts = array(); |
221
|
|
|
$ids = $request->query->get('ids', false); |
222
|
|
|
$idsExploded = explode(',', $ids); |
223
|
|
|
foreach ($idsExploded as $singleId) { |
224
|
|
|
$singleEntity = $app['orm.em']->find( |
225
|
|
|
'Application\Entity\PostEntity', |
226
|
|
|
$singleId |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
if ($singleEntity) { |
230
|
|
|
$posts[] = $singleEntity; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$post = $app['orm.em']->find('Application\Entity\PostEntity', $id); |
235
|
|
|
|
236
|
|
|
if ( |
237
|
|
|
( |
238
|
|
|
!$post && |
239
|
|
|
$ids === false |
240
|
|
|
) || |
241
|
|
|
( |
242
|
|
|
empty($posts) && |
243
|
|
|
$ids !== false |
244
|
|
|
) |
245
|
|
|
) { |
246
|
|
|
$app->abort(404); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$confirmAction = $app['request']->query->has('action') && |
250
|
|
|
$app['request']->query->get('action') == 'confirm' |
251
|
|
|
; |
252
|
|
|
|
253
|
|
|
if ($confirmAction) { |
254
|
|
|
try { |
255
|
|
|
if (!empty($posts)) { |
256
|
|
|
foreach ($posts as $post) { |
257
|
|
|
$app['orm.em']->remove($post); |
258
|
|
|
} |
259
|
|
|
} else { |
260
|
|
|
$app['orm.em']->remove($post); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$app['orm.em']->flush(); |
264
|
|
|
|
265
|
|
|
$app['flashbag']->add( |
266
|
|
|
'success', |
267
|
|
|
$app['translator']->trans( |
268
|
|
|
'members-area.posts.remove.successText' |
269
|
|
|
) |
270
|
|
|
); |
271
|
|
|
} catch (\Exception $e) { |
272
|
|
|
$app['flashbag']->add( |
273
|
|
|
'danger', |
274
|
|
|
$app['translator']->trans( |
275
|
|
|
$e->getMessage() |
276
|
|
|
) |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $app->redirect( |
281
|
|
|
$app['url_generator']->generate( |
282
|
|
|
'members-area.posts' |
283
|
|
|
) |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return new Response( |
288
|
|
|
$app['twig']->render( |
289
|
|
|
'contents/members-area/posts/remove.html.twig', |
290
|
|
|
array( |
291
|
|
|
'post' => $post, |
292
|
|
|
'posts' => $posts, |
293
|
|
|
'ids' => $ids, |
294
|
|
|
) |
295
|
|
|
) |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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.