]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/EntryController.php
d7f53aeab155d4f6be5d1c5831ad15cd1d3b52ec
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
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\JsonResponse;
10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\HttpFoundation\Response;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13 use Wallabag\CoreBundle\Entity\Entry;
14 use Wallabag\CoreBundle\Form\Type\EntryFilterType;
15 use Wallabag\CoreBundle\Form\Type\EditEntryType;
16 use Wallabag\CoreBundle\Form\Type\NewEntryType;
17 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
18 use Wallabag\CoreBundle\Event\EntrySavedEvent;
19 use Wallabag\CoreBundle\Event\EntryDeletedEvent;
20 use Wallabag\CoreBundle\Form\Type\SearchEntryType;
21
22 class EntryController extends Controller
23 {
24 /**
25 * @param Request $request
26 * @param int $page
27 *
28 * @Route("/search/{page}", name="search", defaults={"page" = 1})
29 *
30 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
31 * because this controller is also called inside the layout template without any page as argument
32 *
33 * @return \Symfony\Component\HttpFoundation\Response
34 */
35 public function searchFormAction(Request $request, $page = 1, $currentRoute = null)
36 {
37 // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template)
38 if (null === $currentRoute && $request->query->has('currentRoute')) {
39 $currentRoute = $request->query->get('currentRoute');
40 }
41
42 $form = $this->createForm(SearchEntryType::class);
43
44 $form->handleRequest($request);
45
46 if ($form->isSubmitted() && $form->isValid()) {
47 return $this->showEntries('search', $request, $page);
48 }
49
50 return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [
51 'form' => $form->createView(),
52 'currentRoute' => $currentRoute,
53 ]);
54 }
55
56 /**
57 * Fetch content and update entry.
58 * In case it fails, $entry->getContent will return an error message.
59 *
60 * @param Entry $entry
61 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
62 */
63 private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved')
64 {
65 $message = 'flashes.entry.notice.'.$prefixMessage;
66
67 try {
68 $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
69 } catch (\Exception $e) {
70 $this->get('logger')->error('Error while saving an entry', [
71 'exception' => $e,
72 'entry' => $entry,
73 ]);
74
75 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
76 }
77
78 $this->get('session')->getFlashBag()->add('notice', $message);
79 }
80
81 /**
82 * @param Request $request
83 *
84 * @Route("/new-entry", name="new_entry")
85 *
86 * @return \Symfony\Component\HttpFoundation\Response
87 */
88 public function addEntryFormAction(Request $request)
89 {
90 $entry = new Entry($this->getUser());
91
92 $form = $this->createForm(NewEntryType::class, $entry);
93
94 $form->handleRequest($request);
95
96 if ($form->isSubmitted() && $form->isValid()) {
97 $existingEntry = $this->checkIfEntryAlreadyExists($entry);
98
99 if (false !== $existingEntry) {
100 $this->get('session')->getFlashBag()->add(
101 'notice',
102 $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
103 );
104
105 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
106 }
107
108 $this->updateEntry($entry);
109
110 $em = $this->getDoctrine()->getManager();
111 $em->persist($entry);
112 $em->flush();
113
114 // entry saved, dispatch event about it!
115 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
116
117 return $this->redirect($this->generateUrl('homepage'));
118 }
119
120 return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
121 'form' => $form->createView(),
122 ]);
123 }
124
125 /**
126 * @param Request $request
127 *
128 * @Route("/bookmarklet", name="bookmarklet")
129 *
130 * @return \Symfony\Component\HttpFoundation\Response
131 */
132 public function addEntryViaBookmarkletAction(Request $request)
133 {
134 $entry = new Entry($this->getUser());
135 $entry->setUrl($request->get('url'));
136
137 if (false === $this->checkIfEntryAlreadyExists($entry)) {
138 $this->updateEntry($entry);
139
140 $em = $this->getDoctrine()->getManager();
141 $em->persist($entry);
142 $em->flush();
143
144 // entry saved, dispatch event about it!
145 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
146 }
147
148 return $this->redirect($this->generateUrl('homepage'));
149 }
150
151 /**
152 * @Route("/new", name="new")
153 *
154 * @return \Symfony\Component\HttpFoundation\Response
155 */
156 public function addEntryAction()
157 {
158 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
159 }
160
161 /**
162 * Edit an entry content.
163 *
164 * @param Request $request
165 * @param Entry $entry
166 *
167 * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit")
168 *
169 * @return \Symfony\Component\HttpFoundation\Response
170 */
171 public function editEntryAction(Request $request, Entry $entry)
172 {
173 $this->checkUserAction($entry);
174
175 $form = $this->createForm(EditEntryType::class, $entry);
176
177 $form->handleRequest($request);
178
179 if ($form->isSubmitted() && $form->isValid()) {
180 $em = $this->getDoctrine()->getManager();
181 $em->persist($entry);
182 $em->flush();
183
184 $this->get('session')->getFlashBag()->add(
185 'notice',
186 'flashes.entry.notice.entry_updated'
187 );
188
189 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
190 }
191
192 return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
193 'form' => $form->createView(),
194 ]);
195 }
196
197 /**
198 * Shows all entries for current user.
199 *
200 * @param Request $request
201 * @param int $page
202 *
203 * @Route("/all/list/{page}", name="all", defaults={"page" = "1"})
204 *
205 * @return \Symfony\Component\HttpFoundation\Response
206 */
207 public function showAllAction(Request $request, $page)
208 {
209 return $this->showEntries('all', $request, $page);
210 }
211
212 /**
213 * Shows unread entries for current user.
214 *
215 * @param Request $request
216 * @param int $page
217 *
218 * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"})
219 *
220 * @return \Symfony\Component\HttpFoundation\Response
221 */
222 public function showUnreadAction(Request $request, $page)
223 {
224 // load the quickstart if no entry in database
225 if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUser($this->getUser()->getId()) == 0) {
226 return $this->redirect($this->generateUrl('quickstart'));
227 }
228
229 return $this->showEntries('unread', $request, $page);
230 }
231
232 /**
233 * Shows read entries for current user.
234 *
235 * @param Request $request
236 * @param int $page
237 *
238 * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"})
239 *
240 * @return \Symfony\Component\HttpFoundation\Response
241 */
242 public function showArchiveAction(Request $request, $page)
243 {
244 return $this->showEntries('archive', $request, $page);
245 }
246
247 /**
248 * Shows starred entries for current user.
249 *
250 * @param Request $request
251 * @param int $page
252 *
253 * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"})
254 *
255 * @return \Symfony\Component\HttpFoundation\Response
256 */
257 public function showStarredAction(Request $request, $page)
258 {
259 return $this->showEntries('starred', $request, $page);
260 }
261
262 /**
263 * Global method to retrieve entries depending on the given type
264 * It returns the response to be send.
265 *
266 * @param string $type Entries type: unread, starred or archive
267 * @param Request $request
268 * @param int $page
269 *
270 * @return \Symfony\Component\HttpFoundation\Response
271 */
272 private function showEntries($type, Request $request, $page)
273 {
274 $repository = $this->get('wallabag_core.entry_repository');
275 $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
276 $currentRoute = (!is_null($request->query->get('currentRoute')) ? $request->query->get('currentRoute') : '');
277
278 switch ($type) {
279 case 'search':
280 $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
281
282 break;
283 case 'untagged':
284 $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId());
285
286 break;
287 case 'starred':
288 $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId());
289 break;
290
291 case 'archive':
292 $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId());
293 break;
294
295 case 'unread':
296 $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId());
297 break;
298
299 case 'all':
300 $qb = $repository->getBuilderForAllByUser($this->getUser()->getId());
301 break;
302
303 default:
304 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
305 }
306
307 $form = $this->createForm(EntryFilterType::class);
308
309 if ($request->query->has($form->getName())) {
310 // manually bind values from the request
311 $form->submit($request->query->get($form->getName()));
312
313 // build the query from the given form object
314 $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
315 }
316
317 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
318
319 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
320
321 try {
322 $entries->setCurrentPage($page);
323 } catch (OutOfRangeCurrentPageException $e) {
324 if ($page > 1) {
325 return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
326 }
327 }
328
329 return $this->render(
330 'WallabagCoreBundle:Entry:entries.html.twig', [
331 'form' => $form->createView(),
332 'entries' => $entries,
333 'currentPage' => $page,
334 'searchTerm' => $searchTerm,
335 ]
336 );
337 }
338
339 /**
340 * Shows entry content.
341 *
342 * @param Entry $entry
343 *
344 * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view")
345 *
346 * @return \Symfony\Component\HttpFoundation\Response
347 */
348 public function viewAction(Entry $entry)
349 {
350 $this->checkUserAction($entry);
351
352 return $this->render(
353 'WallabagCoreBundle:Entry:entry.html.twig',
354 ['entry' => $entry]
355 );
356 }
357
358 /**
359 * Reload an entry.
360 * Refetch content from the website and make it readable again.
361 *
362 * @param Entry $entry
363 *
364 * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry")
365 *
366 * @return \Symfony\Component\HttpFoundation\RedirectResponse
367 */
368 public function reloadAction(Entry $entry)
369 {
370 $this->checkUserAction($entry);
371
372 $this->updateEntry($entry, 'entry_reloaded');
373
374 // if refreshing entry failed, don't save it
375 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
376 $bag = $this->get('session')->getFlashBag();
377 $bag->clear();
378 $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed');
379
380 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
381 }
382
383 $em = $this->getDoctrine()->getManager();
384 $em->persist($entry);
385 $em->flush();
386
387 // entry saved, dispatch event about it!
388 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
389
390 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
391 }
392
393 /**
394 * Changes read status for an entry.
395 *
396 * @param Request $request
397 * @param Entry $entry
398 *
399 * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry")
400 *
401 * @return \Symfony\Component\HttpFoundation\RedirectResponse
402 */
403 public function toggleArchiveAction(Request $request, Entry $entry)
404 {
405 $this->checkUserAction($entry);
406
407 $entry->toggleArchive();
408 $this->getDoctrine()->getManager()->flush();
409
410 $message = 'flashes.entry.notice.entry_unarchived';
411 if ($entry->isArchived()) {
412 $message = 'flashes.entry.notice.entry_archived';
413 }
414
415 $this->get('session')->getFlashBag()->add(
416 'notice',
417 $message
418 );
419
420 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
421
422 return $this->redirect($redirectUrl);
423 }
424
425 /**
426 * Changes starred status for an entry.
427 *
428 * @param Request $request
429 * @param Entry $entry
430 *
431 * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry")
432 *
433 * @return \Symfony\Component\HttpFoundation\RedirectResponse
434 */
435 public function toggleStarAction(Request $request, Entry $entry)
436 {
437 $this->checkUserAction($entry);
438
439 $entry->toggleStar();
440 $this->getDoctrine()->getManager()->flush();
441
442 $message = 'flashes.entry.notice.entry_unstarred';
443 if ($entry->isStarred()) {
444 $message = 'flashes.entry.notice.entry_starred';
445 }
446
447 $this->get('session')->getFlashBag()->add(
448 'notice',
449 $message
450 );
451
452 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
453
454 return $this->redirect($redirectUrl);
455 }
456
457 /**
458 * Deletes entry and redirect to the homepage or the last viewed page.
459 *
460 * @param Entry $entry
461 *
462 * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry")
463 *
464 * @return \Symfony\Component\HttpFoundation\RedirectResponse
465 */
466 public function deleteEntryAction(Request $request, Entry $entry)
467 {
468 $this->checkUserAction($entry);
469
470 // generates the view url for this entry to check for redirection later
471 // to avoid redirecting to the deleted entry. Ugh.
472 $url = $this->generateUrl(
473 'view',
474 ['id' => $entry->getId()],
475 UrlGeneratorInterface::ABSOLUTE_PATH
476 );
477
478 // entry deleted, dispatch event about it!
479 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
480
481 $em = $this->getDoctrine()->getManager();
482 $em->remove($entry);
483 $em->flush();
484
485 $this->get('session')->getFlashBag()->add(
486 'notice',
487 'flashes.entry.notice.entry_deleted'
488 );
489
490 // don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
491 $referer = $request->headers->get('referer');
492 $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null);
493
494 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to);
495
496 return $this->redirect($redirectUrl);
497 }
498
499 /**
500 * Check if the logged user can manage the given entry.
501 *
502 * @param Entry $entry
503 */
504 private function checkUserAction(Entry $entry)
505 {
506 if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
507 throw $this->createAccessDeniedException('You can not access this entry.');
508 }
509 }
510
511 /**
512 * Check for existing entry, if it exists, redirect to it with a message.
513 *
514 * @param Entry $entry
515 *
516 * @return Entry|bool
517 */
518 private function checkIfEntryAlreadyExists(Entry $entry)
519 {
520 return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
521 }
522
523 /**
524 * Get public URL for entry (and generate it if necessary).
525 *
526 * @param Entry $entry
527 *
528 * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
529 *
530 * @return \Symfony\Component\HttpFoundation\Response
531 */
532 public function shareAction(Entry $entry)
533 {
534 $this->checkUserAction($entry);
535
536 if (null === $entry->getUid()) {
537 $entry->generateUid();
538
539 $em = $this->getDoctrine()->getManager();
540 $em->persist($entry);
541 $em->flush();
542 }
543
544 return $this->redirect($this->generateUrl('share_entry', [
545 'uid' => $entry->getUid(),
546 ]));
547 }
548
549 /**
550 * Disable public sharing for an entry.
551 *
552 * @param Entry $entry
553 *
554 * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
555 *
556 * @return \Symfony\Component\HttpFoundation\Response
557 */
558 public function deleteShareAction(Entry $entry)
559 {
560 $this->checkUserAction($entry);
561
562 $entry->cleanUid();
563
564 $em = $this->getDoctrine()->getManager();
565 $em->persist($entry);
566 $em->flush();
567
568 return $this->redirect($this->generateUrl('view', [
569 'id' => $entry->getId(),
570 ]));
571 }
572
573 /**
574 * Ability to view a content publicly.
575 *
576 * @param Entry $entry
577 *
578 * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry")
579 * @Cache(maxage="25200", smaxage="25200", public=true)
580 *
581 * @return \Symfony\Component\HttpFoundation\Response
582 */
583 public function shareEntryAction(Entry $entry)
584 {
585 if (!$this->get('craue_config')->get('share_public')) {
586 throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
587 }
588
589 return $this->render(
590 '@WallabagCore/themes/common/Entry/share.html.twig',
591 ['entry' => $entry]
592 );
593 }
594
595 /**
596 * Shows untagged articles for current user.
597 *
598 * @param Request $request
599 * @param int $page
600 *
601 * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"})
602 *
603 * @return \Symfony\Component\HttpFoundation\Response
604 */
605 public function showUntaggedEntriesAction(Request $request, $page)
606 {
607 return $this->showEntries('untagged', $request, $page);
608 }
609
610 /**
611 * Set the progress of an entry.
612 *
613 * @param Entry $entry
614 * @param int $progress
615 *
616 * @Route("/progress/{entry}/{progress}", name="set_progress")
617 *
618 * @return JsonResponse
619 */
620 public function setEntriesProgressAction(Entry $entry, $progress = 0)
621 {
622 $this->checkUserAction($entry);
623 $response = new JsonResponse();
624
625 $progress = (int) $progress;
626 if ($progress >= 0 && $progress <= 100) {
627 $em = $this->getDoctrine()->getManager();
628
629 $entry->setProgress($progress);
630
631 $this->get('logger')->info('Set progress to ' . $progress);
632
633 $em->persist($entry);
634 $em->flush();
635
636 $response->setStatusCode(Response::HTTP_OK);
637 }
638
639 return $response;
640 }
641 }