diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/EntryController.php | 150 |
1 files changed, 125 insertions, 25 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index ccdf9406..3b28e635 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -4,7 +4,6 @@ namespace Wallabag\CoreBundle\Controller; | |||
4 | 4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 5 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | 6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
7 | use Pagerfanta\Pagerfanta; | ||
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
@@ -13,30 +12,40 @@ use Wallabag\CoreBundle\Entity\Entry; | |||
13 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | 12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; |
14 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | 13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; |
15 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | 14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
15 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | ||
16 | 16 | ||
17 | class EntryController extends Controller | 17 | class EntryController extends Controller |
18 | { | 18 | { |
19 | /** | 19 | /** |
20 | * @param Entry $entry | 20 | * Fetch content and update entry. |
21 | * In case it fails, entry will return to avod loosing the data. | ||
22 | * | ||
23 | * @param Entry $entry | ||
24 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | ||
25 | * | ||
26 | * @return Entry | ||
21 | */ | 27 | */ |
22 | private function updateEntry(Entry $entry) | 28 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') |
23 | { | 29 | { |
30 | // put default title in case of fetching content failed | ||
31 | $entry->setTitle('No title found'); | ||
32 | |||
33 | $message = 'flashes.entry.notice.'.$prefixMessage; | ||
34 | |||
24 | try { | 35 | try { |
25 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | 36 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); |
26 | |||
27 | $em = $this->getDoctrine()->getManager(); | ||
28 | $em->persist($entry); | ||
29 | $em->flush(); | ||
30 | } catch (\Exception $e) { | 37 | } catch (\Exception $e) { |
31 | $this->get('logger')->error('Error while saving an entry', [ | 38 | $this->get('logger')->error('Error while saving an entry', [ |
32 | 'exception' => $e, | 39 | 'exception' => $e, |
33 | 'entry' => $entry, | 40 | 'entry' => $entry, |
34 | ]); | 41 | ]); |
35 | 42 | ||
36 | return false; | 43 | $message = 'flashes.entry.notice.'.$prefixMessage.'_failed'; |
37 | } | 44 | } |
38 | 45 | ||
39 | return true; | 46 | $this->get('session')->getFlashBag()->add('notice', $message); |
47 | |||
48 | return $entry; | ||
40 | } | 49 | } |
41 | 50 | ||
42 | /** | 51 | /** |
@@ -66,12 +75,11 @@ class EntryController extends Controller | |||
66 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); | 75 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); |
67 | } | 76 | } |
68 | 77 | ||
69 | $message = 'flashes.entry.notice.entry_saved'; | 78 | $this->updateEntry($entry); |
70 | if (false === $this->updateEntry($entry)) { | ||
71 | $message = 'flashes.entry.notice.entry_saved_failed'; | ||
72 | } | ||
73 | 79 | ||
74 | $this->get('session')->getFlashBag()->add('notice', $message); | 80 | $em = $this->getDoctrine()->getManager(); |
81 | $em->persist($entry); | ||
82 | $em->flush(); | ||
75 | 83 | ||
76 | return $this->redirect($this->generateUrl('homepage')); | 84 | return $this->redirect($this->generateUrl('homepage')); |
77 | } | 85 | } |
@@ -95,6 +103,10 @@ class EntryController extends Controller | |||
95 | 103 | ||
96 | if (false === $this->checkIfEntryAlreadyExists($entry)) { | 104 | if (false === $this->checkIfEntryAlreadyExists($entry)) { |
97 | $this->updateEntry($entry); | 105 | $this->updateEntry($entry); |
106 | |||
107 | $em = $this->getDoctrine()->getManager(); | ||
108 | $em->persist($entry); | ||
109 | $em->flush(); | ||
98 | } | 110 | } |
99 | 111 | ||
100 | return $this->redirect($this->generateUrl('homepage')); | 112 | return $this->redirect($this->generateUrl('homepage')); |
@@ -226,6 +238,10 @@ class EntryController extends Controller | |||
226 | $repository = $this->get('wallabag_core.entry_repository'); | 238 | $repository = $this->get('wallabag_core.entry_repository'); |
227 | 239 | ||
228 | switch ($type) { | 240 | switch ($type) { |
241 | case 'untagged': | ||
242 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | ||
243 | |||
244 | break; | ||
229 | case 'starred': | 245 | case 'starred': |
230 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | 246 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); |
231 | break; | 247 | break; |
@@ -257,9 +273,10 @@ class EntryController extends Controller | |||
257 | } | 273 | } |
258 | 274 | ||
259 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); | 275 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); |
260 | $entries = new Pagerfanta($pagerAdapter); | ||
261 | 276 | ||
262 | $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); | 277 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') |
278 | ->prepare($pagerAdapter, $page); | ||
279 | |||
263 | try { | 280 | try { |
264 | $entries->setCurrentPage($page); | 281 | $entries->setCurrentPage($page); |
265 | } catch (OutOfRangeCurrentPageException $e) { | 282 | } catch (OutOfRangeCurrentPageException $e) { |
@@ -311,15 +328,11 @@ class EntryController extends Controller | |||
311 | { | 328 | { |
312 | $this->checkUserAction($entry); | 329 | $this->checkUserAction($entry); |
313 | 330 | ||
314 | $message = 'flashes.entry.notice.entry_reloaded'; | 331 | $this->updateEntry($entry, 'entry_reloaded'); |
315 | if (false === $this->updateEntry($entry)) { | ||
316 | $message = 'flashes.entry.notice.entry_reload_failed'; | ||
317 | } | ||
318 | 332 | ||
319 | $this->get('session')->getFlashBag()->add( | 333 | $em = $this->getDoctrine()->getManager(); |
320 | 'notice', | 334 | $em->persist($entry); |
321 | $message | 335 | $em->flush(); |
322 | ); | ||
323 | 336 | ||
324 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | 337 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
325 | } | 338 | } |
@@ -434,7 +447,7 @@ class EntryController extends Controller | |||
434 | */ | 447 | */ |
435 | private function checkUserAction(Entry $entry) | 448 | private function checkUserAction(Entry $entry) |
436 | { | 449 | { |
437 | if ($this->getUser()->getId() != $entry->getUser()->getId()) { | 450 | if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) { |
438 | throw $this->createAccessDeniedException('You can not access this entry.'); | 451 | throw $this->createAccessDeniedException('You can not access this entry.'); |
439 | } | 452 | } |
440 | } | 453 | } |
@@ -450,4 +463,91 @@ class EntryController extends Controller | |||
450 | { | 463 | { |
451 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | 464 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); |
452 | } | 465 | } |
466 | |||
467 | /** | ||
468 | * Get public URL for entry (and generate it if necessary). | ||
469 | * | ||
470 | * @param Entry $entry | ||
471 | * | ||
472 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | ||
473 | * | ||
474 | * @return \Symfony\Component\HttpFoundation\Response | ||
475 | */ | ||
476 | public function shareAction(Entry $entry) | ||
477 | { | ||
478 | $this->checkUserAction($entry); | ||
479 | |||
480 | if (null === $entry->getUuid()) { | ||
481 | $entry->generateUuid(); | ||
482 | |||
483 | $em = $this->getDoctrine()->getManager(); | ||
484 | $em->persist($entry); | ||
485 | $em->flush(); | ||
486 | } | ||
487 | |||
488 | return $this->redirect($this->generateUrl('share_entry', [ | ||
489 | 'uuid' => $entry->getUuid(), | ||
490 | ])); | ||
491 | } | ||
492 | |||
493 | /** | ||
494 | * Disable public sharing for an entry. | ||
495 | * | ||
496 | * @param Entry $entry | ||
497 | * | ||
498 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | ||
499 | * | ||
500 | * @return \Symfony\Component\HttpFoundation\Response | ||
501 | */ | ||
502 | public function deleteShareAction(Entry $entry) | ||
503 | { | ||
504 | $this->checkUserAction($entry); | ||
505 | |||
506 | $entry->cleanUuid(); | ||
507 | |||
508 | $em = $this->getDoctrine()->getManager(); | ||
509 | $em->persist($entry); | ||
510 | $em->flush(); | ||
511 | |||
512 | return $this->redirect($this->generateUrl('view', [ | ||
513 | 'id' => $entry->getId(), | ||
514 | ])); | ||
515 | } | ||
516 | |||
517 | /** | ||
518 | * Ability to view a content publicly. | ||
519 | * | ||
520 | * @param Entry $entry | ||
521 | * | ||
522 | * @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry") | ||
523 | * @Cache(maxage="25200", smaxage="25200", public=true) | ||
524 | * | ||
525 | * @return \Symfony\Component\HttpFoundation\Response | ||
526 | */ | ||
527 | public function shareEntryAction(Entry $entry) | ||
528 | { | ||
529 | if (!$this->get('craue_config')->get('share_public')) { | ||
530 | throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.'); | ||
531 | } | ||
532 | |||
533 | return $this->render( | ||
534 | '@WallabagCore/themes/common/Entry/share.html.twig', | ||
535 | ['entry' => $entry] | ||
536 | ); | ||
537 | } | ||
538 | |||
539 | /** | ||
540 | * Shows untagged articles for current user. | ||
541 | * | ||
542 | * @param Request $request | ||
543 | * @param int $page | ||
544 | * | ||
545 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | ||
546 | * | ||
547 | * @return \Symfony\Component\HttpFoundation\Response | ||
548 | */ | ||
549 | public function showUntaggedEntriesAction(Request $request, $page) | ||
550 | { | ||
551 | return $this->showEntries('untagged', $request, $page); | ||
552 | } | ||
453 | } | 553 | } |