From 09ef25c3c3882db94b3941f3ba33ebc78b5dbe4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 22 Dec 2017 15:44:00 +0100 Subject: Added random feature --- .../CoreBundle/Controller/EntryController.php | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index ac372a33..6c843ba7 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -2,6 +2,7 @@ namespace Wallabag\CoreBundle\Controller; +use Doctrine\ORM\NoResultException; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Exception\OutOfRangeCurrentPageException; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; @@ -232,6 +233,110 @@ class EntryController extends Controller return $this->showEntries('starred', $request, $page); } + /** + * Shows random unread entry. + * + * @param Entry $entry + * + * @Route("/unread/random", name="unread_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomUnreadEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'unread'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random favorite entry. + * + * @param Entry $entry + * + * @Route("/starred/random", name="starred_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomStarredEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random archived entry. + * + * @param Entry $entry + * + * @Route("/archive/random", name="archive_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomArchiveEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random all entry. + * + * @param Entry $entry + * + * @Route("/all/random", name="all_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomAllEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId()); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + /** * Shows entry content. * -- cgit v1.2.3 From f85d220c19872dd7e9199fd150060555584a2886 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 15:01:19 +0200 Subject: Fix tests --- .../CoreBundle/Controller/EntryController.php | 188 ++++++++++----------- 1 file changed, 89 insertions(+), 99 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 6c843ba7..669e15d7 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -233,6 +233,21 @@ class EntryController extends Controller return $this->showEntries('starred', $request, $page); } + /** + * Shows untagged articles for current user. + * + * @param Request $request + * @param int $page + * + * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showUntaggedEntriesAction(Request $request, $page) + { + return $this->showEntries('untagged', $request, $page); + } + /** * Shows random unread entry. * @@ -244,19 +259,7 @@ class EntryController extends Controller */ public function showRandomUnreadEntryAction() { - $repository = $this->get('wallabag_core.entry_repository'); - - try { - $entry = $repository->getRandomEntry($this->getUser()->getId(), 'unread'); - } catch (NoResultException $e) { - $bag = $this->get('session')->getFlashBag(); - $bag->clear(); - $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - - return $this->redirect($this->generateUrl('homepage')); - } - - return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + return $this->showRandomEntries('unread'); } /** @@ -270,19 +273,7 @@ class EntryController extends Controller */ public function showRandomStarredEntryAction() { - $repository = $this->get('wallabag_core.entry_repository'); - - try { - $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); - } catch (NoResultException $e) { - $bag = $this->get('session')->getFlashBag(); - $bag->clear(); - $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - - return $this->redirect($this->generateUrl('homepage')); - } - - return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + return $this->showRandomEntries('starred'); } /** @@ -296,19 +287,21 @@ class EntryController extends Controller */ public function showRandomArchiveEntryAction() { - $repository = $this->get('wallabag_core.entry_repository'); - - try { - $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); - } catch (NoResultException $e) { - $bag = $this->get('session')->getFlashBag(); - $bag->clear(); - $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - - return $this->redirect($this->generateUrl('homepage')); - } + return $this->showRandomEntries('archive'); + } - return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + /** + * Shows random all entry. + * + * @param Entry $entry + * + * @Route("/untagged/random", name="untagged_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomUntaggedEntryAction() + { + return $this->showRandomEntries('untagged'); } /** @@ -322,19 +315,7 @@ class EntryController extends Controller */ public function showRandomAllEntryAction() { - $repository = $this->get('wallabag_core.entry_repository'); - - try { - $entry = $repository->getRandomEntry($this->getUser()->getId()); - } catch (NoResultException $e) { - $bag = $this->get('session')->getFlashBag(); - $bag->clear(); - $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - - return $this->redirect($this->generateUrl('homepage')); - } - - return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + return $this->showRandomEntries(); } /** @@ -570,54 +551,6 @@ class EntryController extends Controller ); } - /** - * Shows untagged articles for current user. - * - * @param Request $request - * @param int $page - * - * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showUntaggedEntriesAction(Request $request, $page) - { - return $this->showEntries('untagged', $request, $page); - } - - /** - * Fetch content and update entry. - * In case it fails, $entry->getContent will return an error message. - * - * @param Entry $entry - * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded - */ - private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') - { - $message = 'flashes.entry.notice.' . $prefixMessage; - - try { - $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); - } catch (\Exception $e) { - $this->get('logger')->error('Error while saving an entry', [ - 'exception' => $e, - 'entry' => $entry, - ]); - - $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; - } - - if (empty($entry->getDomainName())) { - $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); - } - - if (empty($entry->getTitle())) { - $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); - } - - $this->get('session')->getFlashBag()->add('notice', $message); - } - /** * Global method to retrieve entries depending on the given type * It returns the response to be send. @@ -690,6 +623,63 @@ class EntryController extends Controller ); } + /** + * Global method to retrieve random entries depending on the given type. + * + * @param string $type Entries type: unread, starred, archive or untagged + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + */ + private function showRandomEntries($type) + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), $type); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Fetch content and update entry. + * In case it fails, $entry->getContent will return an error message. + * + * @param Entry $entry + * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded + */ + private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') + { + $message = 'flashes.entry.notice.' . $prefixMessage; + + try { + $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + + $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; + } + + if (empty($entry->getDomainName())) { + $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); + } + + if (empty($entry->getTitle())) { + $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); + } + + $this->get('session')->getFlashBag()->add('notice', $message); + } + /** * Check if the logged user can manage the given entry. * -- cgit v1.2.3 From 0447a75b06142afe59a179bb59ee94f1978aa7a9 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 21:41:05 +0200 Subject: Use only one method to randomize Instead of one per type, one for all is ok. --- .../CoreBundle/Controller/EntryController.php | 97 ++++------------------ 1 file changed, 14 insertions(+), 83 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 669e15d7..ab50ebdf 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -249,73 +249,28 @@ class EntryController extends Controller } /** - * Shows random unread entry. + * Shows random entry depending on the given type. * * @param Entry $entry * - * @Route("/unread/random", name="unread_random") + * @Route("/{type}/random", name="random_entry", requirements={"_locale": "unread|starred|archive|untagged|all"}) * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showRandomUnreadEntryAction() - { - return $this->showRandomEntries('unread'); - } - - /** - * Shows random favorite entry. - * - * @param Entry $entry - * - * @Route("/starred/random", name="starred_random") - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showRandomStarredEntryAction() - { - return $this->showRandomEntries('starred'); - } - - /** - * Shows random archived entry. - * - * @param Entry $entry - * - * @Route("/archive/random", name="archive_random") - * - * @return \Symfony\Component\HttpFoundation\Response + * @return \Symfony\Component\HttpFoundation\RedirectResponse */ - public function showRandomArchiveEntryAction() + public function redirectRandomEntryAction($type = 'all') { - return $this->showRandomEntries('archive'); - } + try { + $entry = $this->get('wallabag_core.entry_repository') + ->getRandomEntry($this->getUser()->getId(), $type); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - /** - * Shows random all entry. - * - * @param Entry $entry - * - * @Route("/untagged/random", name="untagged_random") - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showRandomUntaggedEntryAction() - { - return $this->showRandomEntries('untagged'); - } + return $this->redirect($this->generateUrl('homepage')); + } - /** - * Shows random all entry. - * - * @param Entry $entry - * - * @Route("/all/random", name="all_random") - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showRandomAllEntryAction() - { - return $this->showRandomEntries(); + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); } /** @@ -623,30 +578,6 @@ class EntryController extends Controller ); } - /** - * Global method to retrieve random entries depending on the given type. - * - * @param string $type Entries type: unread, starred, archive or untagged - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - */ - private function showRandomEntries($type) - { - $repository = $this->get('wallabag_core.entry_repository'); - - try { - $entry = $repository->getRandomEntry($this->getUser()->getId(), $type); - } catch (NoResultException $e) { - $bag = $this->get('session')->getFlashBag(); - $bag->clear(); - $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - - return $this->redirect($this->generateUrl('homepage')); - } - - return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); - } - /** * Fetch content and update entry. * In case it fails, $entry->getContent will return an error message. -- cgit v1.2.3 From 9a57653aec85b0f5220436d5cb76545e66c24a11 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 Oct 2018 22:13:33 +0200 Subject: Redirect to the current view instead of homepage --- src/Wallabag/CoreBundle/Controller/EntryController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index ab50ebdf..dfb5eb54 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -251,7 +251,7 @@ class EntryController extends Controller /** * Shows random entry depending on the given type. * - * @param Entry $entry + * @param string $type * * @Route("/{type}/random", name="random_entry", requirements={"_locale": "unread|starred|archive|untagged|all"}) * @@ -267,7 +267,7 @@ class EntryController extends Controller $bag->clear(); $bag->add('notice', 'flashes.entry.notice.no_random_entry'); - return $this->redirect($this->generateUrl('homepage')); + return $this->redirect($this->generateUrl($type)); } return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); -- cgit v1.2.3 From 50f35f0db2be9586205e793f315608eec59c9666 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 19 Jan 2019 22:08:29 +0100 Subject: Move icon into the top menu bar Change the way to select a random entry: - select all ids from the given user (with filters) - choose randomly one in php - find that entry --- src/Wallabag/CoreBundle/Controller/EntryController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index dfb5eb54..5c8ecb40 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -253,7 +253,7 @@ class EntryController extends Controller * * @param string $type * - * @Route("/{type}/random", name="random_entry", requirements={"_locale": "unread|starred|archive|untagged|all"}) + * @Route("/{type}/random", name="random_entry", requirements={"type": "unread|starred|archive|untagged|all"}) * * @return \Symfony\Component\HttpFoundation\RedirectResponse */ -- cgit v1.2.3