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