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