1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BlogBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\NoResultException; |
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11
|
|
|
use Symfony\Component\Form\FormInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Victoire\Bundle\BlogBundle\Entity\Article; |
15
|
|
|
use Victoire\Bundle\BlogBundle\Entity\Blog; |
16
|
|
|
use Victoire\Bundle\BlogBundle\Event\ArticleEvent; |
17
|
|
|
use Victoire\Bundle\BlogBundle\Form\ArticleSettingsType; |
18
|
|
|
use Victoire\Bundle\BlogBundle\Form\ArticleType; |
19
|
|
|
use Victoire\Bundle\BlogBundle\VictoireBlogEvents; |
20
|
|
|
use Victoire\Bundle\CoreBundle\Controller\VictoireAlertifyControllerTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* article Controller. |
24
|
|
|
* |
25
|
|
|
* @Route("/victoire-dcms/article") |
26
|
|
|
*/ |
27
|
|
|
class ArticleController extends Controller |
28
|
|
|
{ |
29
|
|
|
use VictoireAlertifyControllerTrait; |
30
|
|
|
|
31
|
|
|
/** |
|
|
|
|
32
|
|
|
* Display a form to create a new Blog Article. |
33
|
|
|
* |
34
|
|
|
* @Route("/new/{id}", name="victoire_blog_article_new") |
35
|
|
|
* @Method("GET") |
36
|
|
|
* @Template() |
37
|
|
|
* |
38
|
|
|
* @return JsonResponse |
39
|
|
|
*/ |
40
|
|
|
public function newAction(Blog $blog) |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
$article = new Article(); |
44
|
|
|
$article->setBlog($blog); |
45
|
|
|
$form = $this->createForm(ArticleType::class, $article); |
46
|
|
|
|
47
|
|
|
return new JsonResponse([ |
48
|
|
|
'html' => $this->container->get('templating')->render( |
49
|
|
|
'VictoireBlogBundle:Article:new.html.twig', |
50
|
|
|
[ |
51
|
|
|
'form' => $form->createView(), |
52
|
|
|
'blogId' => $blog->getId(), |
53
|
|
|
] |
54
|
|
|
), |
55
|
|
|
]); |
56
|
|
|
} catch (NoResultException $e) { |
57
|
|
|
return new JsonResponse([ |
58
|
|
|
'success' => false, |
59
|
|
|
'message' => $e->getMessage(), |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* Create a new Blog Article. |
66
|
|
|
* |
67
|
|
|
* @Route("/new/{id}", name="victoire_blog_article_new_post") |
68
|
|
|
* @Method("POST") |
69
|
|
|
* @ParamConverter("blog", class="VictoireBlogBundle:Blog") |
70
|
|
|
* |
71
|
|
|
* @return JsonResponse |
72
|
|
|
*/ |
73
|
|
|
public function newPostAction(Request $request, Blog $blog) |
74
|
|
|
{ |
75
|
|
|
$article = new Article(); |
76
|
|
|
$article->setBlog($blog); |
77
|
|
|
$form = $this->createForm(ArticleType::class, $article); |
78
|
|
|
|
79
|
|
|
$form->handleRequest($request); |
80
|
|
|
if ($form->isValid()) { |
81
|
|
|
$page = $this->get('victoire_blog.manager.article')->create( |
82
|
|
|
$article, |
83
|
|
|
$this->getUser() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$dispatcher = $this->get('event_dispatcher'); |
87
|
|
|
$event = new ArticleEvent($article); |
88
|
|
|
$dispatcher->dispatch(VictoireBlogEvents::CREATE_ARTICLE, $event); |
89
|
|
|
|
90
|
|
|
if (null === $response = $event->getResponse()) { |
91
|
|
|
$response = new JsonResponse([ |
92
|
|
|
'success' => true, |
93
|
|
|
'url' => $this->generateUrl('victoire_core_page_show', [ |
94
|
|
|
'_locale' => $request->getLocale(), |
95
|
|
|
'url' => $this->container->get('victoire_core.url_builder')->buildUrl($page), |
96
|
|
|
]), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $response; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new JsonResponse([ |
104
|
|
|
'success' => false, |
105
|
|
|
'message' => $this->container->get('victoire_form.error_helper')->getRecursiveReadableErrors($form), |
106
|
|
|
'html' => $this->container->get('templating')->render( |
107
|
|
|
'VictoireBlogBundle:Article:new.html.twig', |
108
|
|
|
[ |
109
|
|
|
'form' => $form->createView(), |
110
|
|
|
'blogId' => $blog->getId(), |
111
|
|
|
] |
112
|
|
|
), |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Display a form to edit Blog Article settings. |
118
|
|
|
* |
119
|
|
|
* @param Request $request |
120
|
|
|
* @param Article $article |
121
|
|
|
* |
122
|
|
|
* @Route("/{id}/settings", name="victoire_blog_article_settings") |
123
|
|
|
* @Method("GET") |
124
|
|
|
* |
125
|
|
|
* @ParamConverter("article", class="VictoireBlogBundle:Article") |
126
|
|
|
* |
127
|
|
|
* @return JsonResponse |
128
|
|
|
*/ |
129
|
|
|
public function settingsAction(Request $request, Article $article) |
130
|
|
|
{ |
131
|
|
|
$form = $this->createForm(ArticleSettingsType::class, $article); |
132
|
|
|
$form->handleRequest($request); |
133
|
|
|
|
134
|
|
|
$response = $this->getNotPersistedSettingsResponse( |
135
|
|
|
$form, |
136
|
|
|
$article, |
137
|
|
|
$request->query->get('novalidate', false) |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return new JsonResponse($response); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Save Blog Article settings. |
145
|
|
|
* |
146
|
|
|
* @param Request $request |
147
|
|
|
* @param Article $article |
148
|
|
|
* |
149
|
|
|
* @Route("/{id}/settings", name="victoire_blog_article_settings_post") |
150
|
|
|
* @Method("POST") |
151
|
|
|
* |
152
|
|
|
* @ParamConverter("article", class="VictoireBlogBundle:Article") |
153
|
|
|
* |
154
|
|
|
* @return JsonResponse |
155
|
|
|
*/ |
156
|
|
|
public function settingsPostAction(Request $request, Article $article) |
157
|
|
|
{ |
158
|
|
|
$form = $this->createForm(ArticleSettingsType::class, $article); |
159
|
|
|
$form->handleRequest($request); |
160
|
|
|
|
161
|
|
|
$novalidate = $request->query->get('novalidate', false); |
162
|
|
|
|
163
|
|
|
if ($novalidate === false && $form->isValid()) { |
164
|
|
|
$page = $this->get('victoire_blog.manager.article')->updateSettings( |
165
|
|
|
$article, |
166
|
|
|
$this->getUser() |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$response = [ |
170
|
|
|
'success' => true, |
171
|
|
|
'url' => $this->generateUrl('victoire_core_page_show', [ |
172
|
|
|
'_locale' => $page->getCurrentLocale(), |
173
|
|
|
'url' => $page->getReference()->getUrl(), |
174
|
|
|
]), |
175
|
|
|
]; |
176
|
|
|
} else { |
177
|
|
|
$response = $this->getNotPersistedSettingsResponse($form, $article, $novalidate); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new JsonResponse($response); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Delete a BLog Article. |
185
|
|
|
* |
186
|
|
|
* @param Article $article |
187
|
|
|
* |
188
|
|
|
* @Route("/{id}/delete", name="victoire_core_article_delete") |
189
|
|
|
* @Template() |
190
|
|
|
* @ParamConverter("article", class="VictoireBlogBundle:Article") |
191
|
|
|
* |
192
|
|
|
* @return JsonResponse |
193
|
|
|
*/ |
194
|
|
|
public function deleteAction(Article $article) |
195
|
|
|
{ |
196
|
|
|
$blogViewReference = $this->container->get('victoire_view_reference.repository') |
197
|
|
|
->getOneReferenceByParameters(['viewId' => $article->getBlog()->getId()]); |
198
|
|
|
|
199
|
|
|
$this->get('victoire_blog.manager.article')->delete($article); |
200
|
|
|
|
201
|
|
|
$message = $this->get('translator')->trans('victoire.blog.article.delete.success', [], 'victoire'); |
202
|
|
|
$this->congrat($message); |
203
|
|
|
|
204
|
|
|
$response = [ |
205
|
|
|
'success' => true, |
206
|
|
|
'url' => $this->generateUrl('victoire_core_page_show', [ |
207
|
|
|
'url' => $blogViewReference->getUrl(), |
208
|
|
|
] |
209
|
|
|
), |
210
|
|
|
'message' => $message, |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
return new JsonResponse($response); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get JsonResponse array for Settings novalidate and form display. |
218
|
|
|
* |
219
|
|
|
* @param FormInterface $form |
220
|
|
|
* @param Article $article |
221
|
|
|
* @param $novalidate |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
private function getNotPersistedSettingsResponse(FormInterface $form, Article $article, $novalidate) |
226
|
|
|
{ |
227
|
|
|
$template = sprintf( |
228
|
|
|
'%s:%s', |
229
|
|
|
$this->getBaseTemplatePath(), |
230
|
|
|
($novalidate === false) ? 'settings.html.twig' : '_form.html.twig' |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$page = $this->get('victoire_page.page_helper')->findPageByParameters([ |
234
|
|
|
'viewId' => $article->getTemplate()->getId(), |
235
|
|
|
'entityId' => $article->getId(), |
236
|
|
|
]); |
237
|
|
|
|
238
|
|
|
return [ |
239
|
|
|
'success' => !$form->isSubmitted(), |
240
|
|
|
'html' => $this->get('templating')->render($template, [ |
241
|
|
|
'action' => $this->generateUrl('victoire_blog_article_settings_post', [ |
242
|
|
|
'id' => $article->getId(), |
243
|
|
|
]), |
244
|
|
|
'article' => $article, |
245
|
|
|
'form' => $form->createView(), |
246
|
|
|
'businessProperties' => [], |
247
|
|
|
'page' => $page, |
248
|
|
|
]), |
249
|
|
|
]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
protected function getBaseTemplatePath() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
return 'VictoireBlogBundle:Article'; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|