diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/EntryController.php | 177 |
1 files changed, 84 insertions, 93 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index b7fdea27..ba5bfbe2 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -2,12 +2,13 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | 3 | namespace Wallabag\CoreBundle\Controller; |
4 | 4 | ||
5 | use Doctrine\ORM\NoResultException; | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | 7 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | 8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10 | use Symfony\Component\HttpFoundation\Request; | 10 | use Symfony\Component\HttpFoundation\Request; |
11 | use Symfony\Component\Routing\Annotation\Route; | ||
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
12 | use Wallabag\CoreBundle\Entity\Entry; | 13 | use Wallabag\CoreBundle\Entity\Entry; |
13 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | 14 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
@@ -20,8 +21,7 @@ use Wallabag\CoreBundle\Form\Type\SearchEntryType; | |||
20 | class EntryController extends Controller | 21 | class EntryController extends Controller |
21 | { | 22 | { |
22 | /** | 23 | /** |
23 | * @param Request $request | 24 | * @param int $page |
24 | * @param int $page | ||
25 | * | 25 | * |
26 | * @Route("/search/{page}", name="search", defaults={"page" = 1}) | 26 | * @Route("/search/{page}", name="search", defaults={"page" = 1}) |
27 | * | 27 | * |
@@ -52,8 +52,6 @@ class EntryController extends Controller | |||
52 | } | 52 | } |
53 | 53 | ||
54 | /** | 54 | /** |
55 | * @param Request $request | ||
56 | * | ||
57 | * @Route("/new-entry", name="new_entry") | 55 | * @Route("/new-entry", name="new_entry") |
58 | * | 56 | * |
59 | * @return \Symfony\Component\HttpFoundation\Response | 57 | * @return \Symfony\Component\HttpFoundation\Response |
@@ -96,8 +94,6 @@ class EntryController extends Controller | |||
96 | } | 94 | } |
97 | 95 | ||
98 | /** | 96 | /** |
99 | * @param Request $request | ||
100 | * | ||
101 | * @Route("/bookmarklet", name="bookmarklet") | 97 | * @Route("/bookmarklet", name="bookmarklet") |
102 | * | 98 | * |
103 | * @return \Symfony\Component\HttpFoundation\Response | 99 | * @return \Symfony\Component\HttpFoundation\Response |
@@ -134,9 +130,6 @@ class EntryController extends Controller | |||
134 | /** | 130 | /** |
135 | * Edit an entry content. | 131 | * Edit an entry content. |
136 | * | 132 | * |
137 | * @param Request $request | ||
138 | * @param Entry $entry | ||
139 | * | ||
140 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") | 133 | * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") |
141 | * | 134 | * |
142 | * @return \Symfony\Component\HttpFoundation\Response | 135 | * @return \Symfony\Component\HttpFoundation\Response |
@@ -170,8 +163,7 @@ class EntryController extends Controller | |||
170 | /** | 163 | /** |
171 | * Shows all entries for current user. | 164 | * Shows all entries for current user. |
172 | * | 165 | * |
173 | * @param Request $request | 166 | * @param int $page |
174 | * @param int $page | ||
175 | * | 167 | * |
176 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) | 168 | * @Route("/all/list/{page}", name="all", defaults={"page" = "1"}) |
177 | * | 169 | * |
@@ -185,8 +177,7 @@ class EntryController extends Controller | |||
185 | /** | 177 | /** |
186 | * Shows unread entries for current user. | 178 | * Shows unread entries for current user. |
187 | * | 179 | * |
188 | * @param Request $request | 180 | * @param int $page |
189 | * @param int $page | ||
190 | * | 181 | * |
191 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) | 182 | * @Route("/unread/list/{page}", name="unread", defaults={"page" = "1"}) |
192 | * | 183 | * |
@@ -205,8 +196,7 @@ class EntryController extends Controller | |||
205 | /** | 196 | /** |
206 | * Shows read entries for current user. | 197 | * Shows read entries for current user. |
207 | * | 198 | * |
208 | * @param Request $request | 199 | * @param int $page |
209 | * @param int $page | ||
210 | * | 200 | * |
211 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) | 201 | * @Route("/archive/list/{page}", name="archive", defaults={"page" = "1"}) |
212 | * | 202 | * |
@@ -220,8 +210,7 @@ class EntryController extends Controller | |||
220 | /** | 210 | /** |
221 | * Shows starred entries for current user. | 211 | * Shows starred entries for current user. |
222 | * | 212 | * |
223 | * @param Request $request | 213 | * @param int $page |
224 | * @param int $page | ||
225 | * | 214 | * |
226 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) | 215 | * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) |
227 | * | 216 | * |
@@ -233,9 +222,46 @@ class EntryController extends Controller | |||
233 | } | 222 | } |
234 | 223 | ||
235 | /** | 224 | /** |
236 | * Shows entry content. | 225 | * Shows untagged articles for current user. |
226 | * | ||
227 | * @param int $page | ||
228 | * | ||
229 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | ||
237 | * | 230 | * |
238 | * @param Entry $entry | 231 | * @return \Symfony\Component\HttpFoundation\Response |
232 | */ | ||
233 | public function showUntaggedEntriesAction(Request $request, $page) | ||
234 | { | ||
235 | return $this->showEntries('untagged', $request, $page); | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * Shows random entry depending on the given type. | ||
240 | * | ||
241 | * @param string $type | ||
242 | * | ||
243 | * @Route("/{type}/random", name="random_entry", requirements={"type": "unread|starred|archive|untagged|all"}) | ||
244 | * | ||
245 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
246 | */ | ||
247 | public function redirectRandomEntryAction($type = 'all') | ||
248 | { | ||
249 | try { | ||
250 | $entry = $this->get('wallabag_core.entry_repository') | ||
251 | ->getRandomEntry($this->getUser()->getId(), $type); | ||
252 | } catch (NoResultException $e) { | ||
253 | $bag = $this->get('session')->getFlashBag(); | ||
254 | $bag->clear(); | ||
255 | $bag->add('notice', 'flashes.entry.notice.no_random_entry'); | ||
256 | |||
257 | return $this->redirect($this->generateUrl($type)); | ||
258 | } | ||
259 | |||
260 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | ||
261 | } | ||
262 | |||
263 | /** | ||
264 | * Shows entry content. | ||
239 | * | 265 | * |
240 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") | 266 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
241 | * | 267 | * |
@@ -255,8 +281,6 @@ class EntryController extends Controller | |||
255 | * Reload an entry. | 281 | * Reload an entry. |
256 | * Refetch content from the website and make it readable again. | 282 | * Refetch content from the website and make it readable again. |
257 | * | 283 | * |
258 | * @param Entry $entry | ||
259 | * | ||
260 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | 284 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") |
261 | * | 285 | * |
262 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | 286 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
@@ -289,9 +313,6 @@ class EntryController extends Controller | |||
289 | /** | 313 | /** |
290 | * Changes read status for an entry. | 314 | * Changes read status for an entry. |
291 | * | 315 | * |
292 | * @param Request $request | ||
293 | * @param Entry $entry | ||
294 | * | ||
295 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") | 316 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
296 | * | 317 | * |
297 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | 318 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
@@ -321,9 +342,6 @@ class EntryController extends Controller | |||
321 | /** | 342 | /** |
322 | * Changes starred status for an entry. | 343 | * Changes starred status for an entry. |
323 | * | 344 | * |
324 | * @param Request $request | ||
325 | * @param Entry $entry | ||
326 | * | ||
327 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") | 345 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
328 | * | 346 | * |
329 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | 347 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
@@ -354,8 +372,6 @@ class EntryController extends Controller | |||
354 | /** | 372 | /** |
355 | * Deletes entry and redirect to the homepage or the last viewed page. | 373 | * Deletes entry and redirect to the homepage or the last viewed page. |
356 | * | 374 | * |
357 | * @param Entry $entry | ||
358 | * | ||
359 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") | 375 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
360 | * | 376 | * |
361 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | 377 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
@@ -396,8 +412,6 @@ class EntryController extends Controller | |||
396 | /** | 412 | /** |
397 | * Get public URL for entry (and generate it if necessary). | 413 | * Get public URL for entry (and generate it if necessary). |
398 | * | 414 | * |
399 | * @param Entry $entry | ||
400 | * | ||
401 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | 415 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") |
402 | * | 416 | * |
403 | * @return \Symfony\Component\HttpFoundation\Response | 417 | * @return \Symfony\Component\HttpFoundation\Response |
@@ -422,8 +436,6 @@ class EntryController extends Controller | |||
422 | /** | 436 | /** |
423 | * Disable public sharing for an entry. | 437 | * Disable public sharing for an entry. |
424 | * | 438 | * |
425 | * @param Entry $entry | ||
426 | * | ||
427 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | 439 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") |
428 | * | 440 | * |
429 | * @return \Symfony\Component\HttpFoundation\Response | 441 | * @return \Symfony\Component\HttpFoundation\Response |
@@ -446,8 +458,6 @@ class EntryController extends Controller | |||
446 | /** | 458 | /** |
447 | * Ability to view a content publicly. | 459 | * Ability to view a content publicly. |
448 | * | 460 | * |
449 | * @param Entry $entry | ||
450 | * | ||
451 | * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry") | 461 | * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry") |
452 | * @Cache(maxage="25200", smaxage="25200", public=true) | 462 | * @Cache(maxage="25200", smaxage="25200", public=true) |
453 | * | 463 | * |
@@ -466,60 +476,11 @@ class EntryController extends Controller | |||
466 | } | 476 | } |
467 | 477 | ||
468 | /** | 478 | /** |
469 | * Shows untagged articles for current user. | ||
470 | * | ||
471 | * @param Request $request | ||
472 | * @param int $page | ||
473 | * | ||
474 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | ||
475 | * | ||
476 | * @return \Symfony\Component\HttpFoundation\Response | ||
477 | */ | ||
478 | public function showUntaggedEntriesAction(Request $request, $page) | ||
479 | { | ||
480 | return $this->showEntries('untagged', $request, $page); | ||
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 | if (empty($entry->getDomainName())) { | ||
506 | $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); | ||
507 | } | ||
508 | |||
509 | if (empty($entry->getTitle())) { | ||
510 | $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); | ||
511 | } | ||
512 | |||
513 | $this->get('session')->getFlashBag()->add('notice', $message); | ||
514 | } | ||
515 | |||
516 | /** | ||
517 | * Global method to retrieve entries depending on the given type | 479 | * Global method to retrieve entries depending on the given type |
518 | * It returns the response to be send. | 480 | * It returns the response to be send. |
519 | * | 481 | * |
520 | * @param string $type Entries type: unread, starred or archive | 482 | * @param string $type Entries type: unread, starred or archive |
521 | * @param Request $request | 483 | * @param int $page |
522 | * @param int $page | ||
523 | * | 484 | * |
524 | * @return \Symfony\Component\HttpFoundation\Response | 485 | * @return \Symfony\Component\HttpFoundation\Response |
525 | */ | 486 | */ |
@@ -532,11 +493,9 @@ class EntryController extends Controller | |||
532 | switch ($type) { | 493 | switch ($type) { |
533 | case 'search': | 494 | case 'search': |
534 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); | 495 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); |
535 | |||
536 | break; | 496 | break; |
537 | case 'untagged': | 497 | case 'untagged': |
538 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | 498 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); |
539 | |||
540 | break; | 499 | break; |
541 | case 'starred': | 500 | case 'starred': |
542 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | 501 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); |
@@ -576,6 +535,9 @@ class EntryController extends Controller | |||
576 | } | 535 | } |
577 | } | 536 | } |
578 | 537 | ||
538 | $nbEntriesUntagged = $this->get('wallabag_core.entry_repository') | ||
539 | ->countUntaggedEntriesByUser($this->getUser()->getId()); | ||
540 | |||
579 | return $this->render( | 541 | return $this->render( |
580 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | 542 | 'WallabagCoreBundle:Entry:entries.html.twig', [ |
581 | 'form' => $form->createView(), | 543 | 'form' => $form->createView(), |
@@ -583,14 +545,45 @@ class EntryController extends Controller | |||
583 | 'currentPage' => $page, | 545 | 'currentPage' => $page, |
584 | 'searchTerm' => $searchTerm, | 546 | 'searchTerm' => $searchTerm, |
585 | 'isFiltered' => $form->isSubmitted(), | 547 | 'isFiltered' => $form->isSubmitted(), |
548 | 'nbEntriesUntagged' => $nbEntriesUntagged, | ||
586 | ] | 549 | ] |
587 | ); | 550 | ); |
588 | } | 551 | } |
589 | 552 | ||
590 | /** | 553 | /** |
591 | * Check if the logged user can manage the given entry. | 554 | * Fetch content and update entry. |
555 | * In case it fails, $entry->getContent will return an error message. | ||
592 | * | 556 | * |
593 | * @param Entry $entry | 557 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded |
558 | */ | ||
559 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | ||
560 | { | ||
561 | $message = 'flashes.entry.notice.' . $prefixMessage; | ||
562 | |||
563 | try { | ||
564 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | ||
565 | } catch (\Exception $e) { | ||
566 | $this->get('logger')->error('Error while saving an entry', [ | ||
567 | 'exception' => $e, | ||
568 | 'entry' => $entry, | ||
569 | ]); | ||
570 | |||
571 | $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; | ||
572 | } | ||
573 | |||
574 | if (empty($entry->getDomainName())) { | ||
575 | $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); | ||
576 | } | ||
577 | |||
578 | if (empty($entry->getTitle())) { | ||
579 | $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); | ||
580 | } | ||
581 | |||
582 | $this->get('session')->getFlashBag()->add('notice', $message); | ||
583 | } | ||
584 | |||
585 | /** | ||
586 | * Check if the logged user can manage the given entry. | ||
594 | */ | 587 | */ |
595 | private function checkUserAction(Entry $entry) | 588 | private function checkUserAction(Entry $entry) |
596 | { | 589 | { |
@@ -602,8 +595,6 @@ class EntryController extends Controller | |||
602 | /** | 595 | /** |
603 | * Check for existing entry, if it exists, redirect to it with a message. | 596 | * Check for existing entry, if it exists, redirect to it with a message. |
604 | * | 597 | * |
605 | * @param Entry $entry | ||
606 | * | ||
607 | * @return Entry|bool | 598 | * @return Entry|bool |
608 | */ | 599 | */ |
609 | private function checkIfEntryAlreadyExists(Entry $entry) | 600 | private function checkIfEntryAlreadyExists(Entry $entry) |