diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/EntryController.php | 270 |
1 files changed, 130 insertions, 140 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index f7398e69..840dc254 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -4,17 +4,17 @@ 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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | ||
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9 | use Symfony\Component\HttpFoundation\Request; | 10 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11 | use Wallabag\CoreBundle\Entity\Entry; | 12 | use Wallabag\CoreBundle\Entity\Entry; |
12 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | 13 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
14 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
13 | use Wallabag\CoreBundle\Form\Type\EditEntryType; | 15 | use Wallabag\CoreBundle\Form\Type\EditEntryType; |
16 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; | ||
14 | use Wallabag\CoreBundle\Form\Type\NewEntryType; | 17 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
15 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | ||
16 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
17 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | ||
18 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; | 18 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; |
19 | 19 | ||
20 | class EntryController extends Controller | 20 | class EntryController extends Controller |
@@ -52,38 +52,6 @@ class EntryController extends Controller | |||
52 | } | 52 | } |
53 | 53 | ||
54 | /** | 54 | /** |
55 | * Fetch content and update entry. | ||
56 | * In case it fails, entry will return to avod loosing the data. | ||
57 | * | ||
58 | * @param Entry $entry | ||
59 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | ||
60 | * | ||
61 | * @return Entry | ||
62 | */ | ||
63 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | ||
64 | { | ||
65 | // put default title in case of fetching content failed | ||
66 | $entry->setTitle('No title found'); | ||
67 | |||
68 | $message = 'flashes.entry.notice.'.$prefixMessage; | ||
69 | |||
70 | try { | ||
71 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
72 | } catch (\Exception $e) { | ||
73 | $this->get('logger')->error('Error while saving an entry', [ | ||
74 | 'exception' => $e, | ||
75 | 'entry' => $entry, | ||
76 | ]); | ||
77 | |||
78 | $message = 'flashes.entry.notice.'.$prefixMessage.'_failed'; | ||
79 | } | ||
80 | |||
81 | $this->get('session')->getFlashBag()->add('notice', $message); | ||
82 | |||
83 | return $entry; | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * @param Request $request | 55 | * @param Request $request |
88 | * | 56 | * |
89 | * @Route("/new-entry", name="new_entry") | 57 | * @Route("/new-entry", name="new_entry") |
@@ -227,7 +195,7 @@ class EntryController extends Controller | |||
227 | public function showUnreadAction(Request $request, $page) | 195 | public function showUnreadAction(Request $request, $page) |
228 | { | 196 | { |
229 | // load the quickstart if no entry in database | 197 | // load the quickstart if no entry in database |
230 | if ($page == 1 && $this->get('wallabag_core.entry_repository')->countAllEntriesByUsername($this->getUser()->getId()) == 0) { | 198 | if (1 === (int) $page && 0 === $this->get('wallabag_core.entry_repository')->countAllEntriesByUser($this->getUser()->getId())) { |
231 | return $this->redirect($this->generateUrl('quickstart')); | 199 | return $this->redirect($this->generateUrl('quickstart')); |
232 | } | 200 | } |
233 | 201 | ||
@@ -265,84 +233,6 @@ class EntryController extends Controller | |||
265 | } | 233 | } |
266 | 234 | ||
267 | /** | 235 | /** |
268 | * Global method to retrieve entries depending on the given type | ||
269 | * It returns the response to be send. | ||
270 | * | ||
271 | * @param string $type Entries type: unread, starred or archive | ||
272 | * @param Request $request | ||
273 | * @param int $page | ||
274 | * | ||
275 | * @return \Symfony\Component\HttpFoundation\Response | ||
276 | */ | ||
277 | private function showEntries($type, Request $request, $page) | ||
278 | { | ||
279 | $repository = $this->get('wallabag_core.entry_repository'); | ||
280 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | ||
281 | $currentRoute = (!is_null($request->query->get('currentRoute')) ? $request->query->get('currentRoute') : ''); | ||
282 | |||
283 | switch ($type) { | ||
284 | case 'search': | ||
285 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); | ||
286 | |||
287 | break; | ||
288 | case 'untagged': | ||
289 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | ||
290 | |||
291 | break; | ||
292 | case 'starred': | ||
293 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | ||
294 | break; | ||
295 | |||
296 | case 'archive': | ||
297 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | ||
298 | break; | ||
299 | |||
300 | case 'unread': | ||
301 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | ||
302 | break; | ||
303 | |||
304 | case 'all': | ||
305 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | ||
306 | break; | ||
307 | |||
308 | default: | ||
309 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | ||
310 | } | ||
311 | |||
312 | $form = $this->createForm(EntryFilterType::class); | ||
313 | |||
314 | if ($request->query->has($form->getName())) { | ||
315 | // manually bind values from the request | ||
316 | $form->submit($request->query->get($form->getName())); | ||
317 | |||
318 | // build the query from the given form object | ||
319 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | ||
320 | } | ||
321 | |||
322 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | ||
323 | |||
324 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries') | ||
325 | ->prepare($pagerAdapter, $page); | ||
326 | |||
327 | try { | ||
328 | $entries->setCurrentPage($page); | ||
329 | } catch (OutOfRangeCurrentPageException $e) { | ||
330 | if ($page > 1) { | ||
331 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | ||
332 | } | ||
333 | } | ||
334 | |||
335 | return $this->render( | ||
336 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | ||
337 | 'form' => $form->createView(), | ||
338 | 'entries' => $entries, | ||
339 | 'currentPage' => $page, | ||
340 | 'searchTerm' => $searchTerm, | ||
341 | ] | ||
342 | ); | ||
343 | } | ||
344 | |||
345 | /** | ||
346 | * Shows entry content. | 236 | * Shows entry content. |
347 | * | 237 | * |
348 | * @param Entry $entry | 238 | * @param Entry $entry |
@@ -443,6 +333,7 @@ class EntryController extends Controller | |||
443 | $this->checkUserAction($entry); | 333 | $this->checkUserAction($entry); |
444 | 334 | ||
445 | $entry->toggleStar(); | 335 | $entry->toggleStar(); |
336 | $entry->updateStar($entry->isStarred()); | ||
446 | $this->getDoctrine()->getManager()->flush(); | 337 | $this->getDoctrine()->getManager()->flush(); |
447 | 338 | ||
448 | $message = 'flashes.entry.notice.entry_unstarred'; | 339 | $message = 'flashes.entry.notice.entry_unstarred'; |
@@ -495,7 +386,7 @@ class EntryController extends Controller | |||
495 | 386 | ||
496 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) | 387 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) |
497 | $referer = $request->headers->get('referer'); | 388 | $referer = $request->headers->get('referer'); |
498 | $to = (1 !== preg_match('#'.$url.'$#i', $referer) ? $referer : null); | 389 | $to = (1 !== preg_match('#' . $url . '$#i', $referer) ? $referer : null); |
499 | 390 | ||
500 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | 391 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); |
501 | 392 | ||
@@ -503,30 +394,6 @@ class EntryController extends Controller | |||
503 | } | 394 | } |
504 | 395 | ||
505 | /** | 396 | /** |
506 | * Check if the logged user can manage the given entry. | ||
507 | * | ||
508 | * @param Entry $entry | ||
509 | */ | ||
510 | private function checkUserAction(Entry $entry) | ||
511 | { | ||
512 | if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) { | ||
513 | throw $this->createAccessDeniedException('You can not access this entry.'); | ||
514 | } | ||
515 | } | ||
516 | |||
517 | /** | ||
518 | * Check for existing entry, if it exists, redirect to it with a message. | ||
519 | * | ||
520 | * @param Entry $entry | ||
521 | * | ||
522 | * @return Entry|bool | ||
523 | */ | ||
524 | private function checkIfEntryAlreadyExists(Entry $entry) | ||
525 | { | ||
526 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | ||
527 | } | ||
528 | |||
529 | /** | ||
530 | * Get public URL for entry (and generate it if necessary). | 397 | * Get public URL for entry (and generate it if necessary). |
531 | * | 398 | * |
532 | * @param Entry $entry | 399 | * @param Entry $entry |
@@ -612,4 +479,127 @@ class EntryController extends Controller | |||
612 | { | 479 | { |
613 | return $this->showEntries('untagged', $request, $page); | 480 | return $this->showEntries('untagged', $request, $page); |
614 | } | 481 | } |
482 | |||
483 | /** | ||
484 | * Fetch content and update entry. | ||
485 | * In case it fails, $entry->getContent will return an error message. | ||
486 | * | ||
487 | * @param Entry $entry | ||
488 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | ||
489 | */ | ||
490 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | ||
491 | { | ||
492 | $message = 'flashes.entry.notice.' . $prefixMessage; | ||
493 | |||
494 | try { | ||
495 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
496 | } catch (\Exception $e) { | ||
497 | $this->get('logger')->error('Error while saving an entry', [ | ||
498 | 'exception' => $e, | ||
499 | 'entry' => $entry, | ||
500 | ]); | ||
501 | |||
502 | $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; | ||
503 | } | ||
504 | |||
505 | $this->get('session')->getFlashBag()->add('notice', $message); | ||
506 | } | ||
507 | |||
508 | /** | ||
509 | * Global method to retrieve entries depending on the given type | ||
510 | * It returns the response to be send. | ||
511 | * | ||
512 | * @param string $type Entries type: unread, starred or archive | ||
513 | * @param Request $request | ||
514 | * @param int $page | ||
515 | * | ||
516 | * @return \Symfony\Component\HttpFoundation\Response | ||
517 | */ | ||
518 | private function showEntries($type, Request $request, $page) | ||
519 | { | ||
520 | $repository = $this->get('wallabag_core.entry_repository'); | ||
521 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | ||
522 | $currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : ''); | ||
523 | |||
524 | switch ($type) { | ||
525 | case 'search': | ||
526 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); | ||
527 | |||
528 | break; | ||
529 | case 'untagged': | ||
530 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | ||
531 | |||
532 | break; | ||
533 | case 'starred': | ||
534 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | ||
535 | break; | ||
536 | case 'archive': | ||
537 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | ||
538 | break; | ||
539 | case 'unread': | ||
540 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | ||
541 | break; | ||
542 | case 'all': | ||
543 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | ||
544 | break; | ||
545 | default: | ||
546 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | ||
547 | } | ||
548 | |||
549 | $form = $this->createForm(EntryFilterType::class); | ||
550 | |||
551 | if ($request->query->has($form->getName())) { | ||
552 | // manually bind values from the request | ||
553 | $form->submit($request->query->get($form->getName())); | ||
554 | |||
555 | // build the query from the given form object | ||
556 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | ||
557 | } | ||
558 | |||
559 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | ||
560 | |||
561 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); | ||
562 | |||
563 | try { | ||
564 | $entries->setCurrentPage($page); | ||
565 | } catch (OutOfRangeCurrentPageException $e) { | ||
566 | if ($page > 1) { | ||
567 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | ||
568 | } | ||
569 | } | ||
570 | |||
571 | return $this->render( | ||
572 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | ||
573 | 'form' => $form->createView(), | ||
574 | 'entries' => $entries, | ||
575 | 'currentPage' => $page, | ||
576 | 'searchTerm' => $searchTerm, | ||
577 | 'isFiltered' => $form->isSubmitted(), | ||
578 | ] | ||
579 | ); | ||
580 | } | ||
581 | |||
582 | /** | ||
583 | * Check if the logged user can manage the given entry. | ||
584 | * | ||
585 | * @param Entry $entry | ||
586 | */ | ||
587 | private function checkUserAction(Entry $entry) | ||
588 | { | ||
589 | if (null === $this->getUser() || $this->getUser()->getId() !== $entry->getUser()->getId()) { | ||
590 | throw $this->createAccessDeniedException('You can not access this entry.'); | ||
591 | } | ||
592 | } | ||
593 | |||
594 | /** | ||
595 | * Check for existing entry, if it exists, redirect to it with a message. | ||
596 | * | ||
597 | * @param Entry $entry | ||
598 | * | ||
599 | * @return Entry|bool | ||
600 | */ | ||
601 | private function checkIfEntryAlreadyExists(Entry $entry) | ||
602 | { | ||
603 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | ||
604 | } | ||
615 | } | 605 | } |