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