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