]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
11 | use Wallabag\CoreBundle\Entity\Entry; | |
12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | |
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | |
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | |
15 | ||
16 | class EntryController extends Controller | |
17 | { | |
18 | /** | |
19 | * @param Entry $entry | |
20 | */ | |
21 | private function updateEntry(Entry $entry) | |
22 | { | |
23 | try { | |
24 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
25 | ||
26 | $em = $this->getDoctrine()->getManager(); | |
27 | $em->persist($entry); | |
28 | $em->flush(); | |
29 | } catch (\Exception $e) { | |
30 | $this->get('logger')->error('Error while saving an entry', [ | |
31 | 'exception' => $e, | |
32 | 'entry' => $entry, | |
33 | ]); | |
34 | ||
35 | return false; | |
36 | } | |
37 | ||
38 | return true; | |
39 | } | |
40 | ||
41 | /** | |
42 | * @param Request $request | |
43 | * | |
44 | * @Route("/new-entry", name="new_entry") | |
45 | * | |
46 | * @return \Symfony\Component\HttpFoundation\Response | |
47 | */ | |
48 | public function addEntryFormAction(Request $request) | |
49 | { | |
50 | $entry = new Entry($this->getUser()); | |
51 | ||
52 | $form = $this->createForm(NewEntryType::class, $entry); | |
53 | ||
54 | $form->handleRequest($request); | |
55 | ||
56 | if ($form->isValid()) { | |
57 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); | |
58 | ||
59 | if (false !== $existingEntry) { | |
60 | $this->get('session')->getFlashBag()->add( | |
61 | 'notice', | |
62 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) | |
63 | ); | |
64 | ||
65 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); | |
66 | } | |
67 | ||
68 | $message = 'flashes.entry.notice.entry_saved'; | |
69 | if (false === $this->updateEntry($entry)) { | |
70 | $message = 'flashes.entry.notice.entry_saved_failed'; | |
71 | } | |
72 | ||
73 | $this->get('session')->getFlashBag()->add('notice', $message); | |
74 | ||
75 | return $this->redirect($this->generateUrl('homepage')); | |
76 | } | |
77 | ||
78 | return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [ | |
79 | 'form' => $form->createView(), | |
80 | ]); | |
81 | } | |
82 | ||
83 | /** | |
84 | * @param Request $request | |
85 | * | |
86 | * @Route("/bookmarklet", name="bookmarklet") | |
87 | * | |
88 | * @return \Symfony\Component\HttpFoundation\Response | |
89 | */ | |
90 | public function addEntryViaBookmarkletAction(Request $request) | |
91 | { | |
92 | $entry = new Entry($this->getUser()); | |
93 | $entry->setUrl($request->get('url')); | |
94 | ||
95 | if (false === $this->checkIfEntryAlreadyExists($entry)) { | |
96 | $this->updateEntry($entry); | |
97 | } | |
98 | ||
99 | return $this->redirect($this->generateUrl('homepage')); | |
100 | } | |
101 | ||
102 | /** | |
103 | * @Route("/new", name="new") | |
104 | * | |
105 | * @return \Symfony\Component\HttpFoundation\Response | |
106 | */ | |
107 | public function addEntryAction() | |
108 | { | |
109 | return $this->render('WallabagCoreBundle:Entry:new.html.twig'); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Edit an entry content. | |
114 | * | |
115 | * @param Request $request | |
116 | * @param Entry $entry | |
117 | * | |
118 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | |
119 | * | |
120 | * @return \Symfony\Component\HttpFoundation\Response | |
121 | */ | |
122 | public function editEntryAction(Request $request, Entry $entry) | |
123 | { | |
124 | $this->checkUserAction($entry); | |
125 | ||
126 | $form = $this->createForm(EditEntryType::class, $entry); | |
127 | ||
128 | $form->handleRequest($request); | |
129 | ||
130 | if ($form->isValid()) { | |
131 | $em = $this->getDoctrine()->getManager(); | |
132 | $em->persist($entry); | |
133 | $em->flush(); | |
134 | ||
135 | $this->get('session')->getFlashBag()->add( | |
136 | 'notice', | |
137 | 'flashes.entry.notice.entry_updated' | |
138 | ); | |
139 | ||
140 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
141 | } | |
142 | ||
143 | return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [ | |
144 | 'form' => $form->createView(), | |
145 | ]); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Shows all entries for current user. | |
150 | * | |
151 | * @param Request $request | |
152 | * @param int $page | |
153 | * | |
154 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | |
155 | * | |
156 | * @return \Symfony\Component\HttpFoundation\Response | |
157 | */ | |
158 | public function showAllAction(Request $request, $page) | |
159 | { | |
160 | return $this->showEntries('all', $request, $page); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Shows unread entries for current user. | |
165 | * | |
166 | * @param Request $request | |
167 | * @param int $page | |
168 | * | |
169 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | |
170 | * | |
171 | * @return \Symfony\Component\HttpFoundation\Response | |
172 | */ | |
173 | public function showUnreadAction(Request $request, $page) | |
174 | { | |
175 | // load the quickstart if no entry in database | |
176 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | |
177 | return $this->redirect($this->generateUrl('quickstart')); | |
178 | } | |
179 | ||
180 | return $this->showEntries('unread', $request, $page); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Shows read entries for current user. | |
185 | * | |
186 | * @param Request $request | |
187 | * @param int $page | |
188 | * | |
189 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | |
190 | * | |
191 | * @return \Symfony\Component\HttpFoundation\Response | |
192 | */ | |
193 | public function showArchiveAction(Request $request, $page) | |
194 | { | |
195 | return $this->showEntries('archive', $request, $page); | |
196 | } | |
197 | ||
198 | /** | |
199 | * Shows starred entries for current user. | |
200 | * | |
201 | * @param Request $request | |
202 | * @param int $page | |
203 | * | |
204 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | |
205 | * | |
206 | * @return \Symfony\Component\HttpFoundation\Response | |
207 | */ | |
208 | public function showStarredAction(Request $request, $page) | |
209 | { | |
210 | return $this->showEntries('starred', $request, $page); | |
211 | } | |
212 | ||
213 | /** | |
214 | * Global method to retrieve entries depending on the given type | |
215 | * It returns the response to be send. | |
216 | * | |
217 | * @param string $type Entries type: unread, starred or archive | |
218 | * @param Request $request | |
219 | * @param int $page | |
220 | * | |
221 | * @return \Symfony\Component\HttpFoundation\Response | |
222 | */ | |
223 | private function showEntries($type, Request $request, $page) | |
224 | { | |
225 | $repository = $this->get('wallabag_core.entry_repository'); | |
226 | ||
227 | switch ($type) { | |
228 | case 'starred': | |
229 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
230 | break; | |
231 | ||
232 | case 'archive': | |
233 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
234 | break; | |
235 | ||
236 | case 'unread': | |
237 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
238 | break; | |
239 | ||
240 | case 'all': | |
241 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
242 | break; | |
243 | ||
244 | default: | |
245 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
246 | } | |
247 | ||
248 | $form = $this->createForm(EntryFilterType::class); | |
249 | ||
250 | if ($request->query->has($form->getName())) { | |
251 | // manually bind values from the request | |
252 | $form->submit($request->query->get($form->getName())); | |
253 | ||
254 | // build the query from the given form object | |
255 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
256 | } | |
257 | ||
258 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | |
259 | ||
260 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') | |
261 | ->prepare($pagerAdapter, $page); | |
262 | ||
263 | try { | |
264 | $entries->setCurrentPage($page); | |
265 | } catch (OutOfRangeCurrentPageException $e) { | |
266 | if ($page > 1) { | |
267 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | |
268 | } | |
269 | } | |
270 | ||
271 | return $this->render( | |
272 | 'WallabagCoreBundle:Entry:entries.html.twig', | |
273 | [ | |
274 | 'form' => $form->createView(), | |
275 | 'entries' => $entries, | |
276 | 'currentPage' => $page, | |
277 | ] | |
278 | ); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Shows entry content. | |
283 | * | |
284 | * @param Entry $entry | |
285 | * | |
286 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | |
287 | * | |
288 | * @return \Symfony\Component\HttpFoundation\Response | |
289 | */ | |
290 | public function viewAction(Entry $entry) | |
291 | { | |
292 | $this->checkUserAction($entry); | |
293 | ||
294 | return $this->render( | |
295 | 'WallabagCoreBundle:Entry:entry.html.twig', | |
296 | ['entry' => $entry] | |
297 | ); | |
298 | } | |
299 | ||
300 | /** | |
301 | * Reload an entry. | |
302 | * Refetch content from the website and make it readable again. | |
303 | * | |
304 | * @param Entry $entry | |
305 | * | |
306 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
307 | * | |
308 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
309 | */ | |
310 | public function reloadAction(Entry $entry) | |
311 | { | |
312 | $this->checkUserAction($entry); | |
313 | ||
314 | $message = 'flashes.entry.notice.entry_reloaded'; | |
315 | if (false === $this->updateEntry($entry)) { | |
316 | $message = 'flashes.entry.notice.entry_reload_failed'; | |
317 | } | |
318 | ||
319 | $this->get('session')->getFlashBag()->add( | |
320 | 'notice', | |
321 | $message | |
322 | ); | |
323 | ||
324 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
325 | } | |
326 | ||
327 | /** | |
328 | * Changes read status for an entry. | |
329 | * | |
330 | * @param Request $request | |
331 | * @param Entry $entry | |
332 | * | |
333 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | |
334 | * | |
335 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
336 | */ | |
337 | public function toggleArchiveAction(Request $request, Entry $entry) | |
338 | { | |
339 | $this->checkUserAction($entry); | |
340 | ||
341 | $entry->toggleArchive(); | |
342 | $this->getDoctrine()->getManager()->flush(); | |
343 | ||
344 | $message = 'flashes.entry.notice.entry_unarchived'; | |
345 | if ($entry->isArchived()) { | |
346 | $message = 'flashes.entry.notice.entry_archived'; | |
347 | } | |
348 | ||
349 | $this->get('session')->getFlashBag()->add( | |
350 | 'notice', | |
351 | $message | |
352 | ); | |
353 | ||
354 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
355 | ||
356 | return $this->redirect($redirectUrl); | |
357 | } | |
358 | ||
359 | /** | |
360 | * Changes starred status for an entry. | |
361 | * | |
362 | * @param Request $request | |
363 | * @param Entry $entry | |
364 | * | |
365 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | |
366 | * | |
367 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
368 | */ | |
369 | public function toggleStarAction(Request $request, Entry $entry) | |
370 | { | |
371 | $this->checkUserAction($entry); | |
372 | ||
373 | $entry->toggleStar(); | |
374 | $this->getDoctrine()->getManager()->flush(); | |
375 | ||
376 | $message = 'flashes.entry.notice.entry_unstarred'; | |
377 | if ($entry->isStarred()) { | |
378 | $message = 'flashes.entry.notice.entry_starred'; | |
379 | } | |
380 | ||
381 | $this->get('session')->getFlashBag()->add( | |
382 | 'notice', | |
383 | $message | |
384 | ); | |
385 | ||
386 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | |
387 | ||
388 | return $this->redirect($redirectUrl); | |
389 | } | |
390 | ||
391 | /** | |
392 | * Deletes entry and redirect to the homepage or the last viewed page. | |
393 | * | |
394 | * @param Entry $entry | |
395 | * | |
396 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | |
397 | * | |
398 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
399 | */ | |
400 | public function deleteEntryAction(Request $request, Entry $entry) | |
401 | { | |
402 | $this->checkUserAction($entry); | |
403 | ||
404 | // generates the view url for this entry to check for redirection later | |
405 | // to avoid redirecting to the deleted entry. Ugh. | |
406 | $url = $this->generateUrl( | |
407 | 'view', | |
408 | ['id' => $entry->getId()], | |
409 | UrlGeneratorInterface::ABSOLUTE_PATH | |
410 | ); | |
411 | ||
412 | $em = $this->getDoctrine()->getManager(); | |
413 | $em->remove($entry); | |
414 | $em->flush(); | |
415 | ||
416 | $this->get('session')->getFlashBag()->add( | |
417 | 'notice', | |
418 | 'flashes.entry.notice.entry_deleted' | |
419 | ); | |
420 | ||
421 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) | |
422 | $referer = $request->headers->get('referer'); | |
423 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | |
424 | ||
425 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
426 | ||
427 | return $this->redirect($redirectUrl); | |
428 | } | |
429 | ||
430 | /** | |
431 | * Check if the logged user can manage the given entry. | |
432 | * | |
433 | * @param Entry $entry | |
434 | */ | |
435 | private function checkUserAction(Entry $entry) | |
436 | { | |
437 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | |
438 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
439 | } | |
440 | } | |
441 | ||
442 | /** | |
443 | * Check for existing entry, if it exists, redirect to it with a message. | |
444 | * | |
445 | * @param Entry $entry | |
446 | * | |
447 | * @return Entry|bool | |
448 | */ | |
449 | private function checkIfEntryAlreadyExists(Entry $entry) | |
450 | { | |
451 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
452 | } | |
453 | } |