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