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