]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Controller; |
9d50517c | 4 | |
619cc453 | 5 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
671a2b88 | 6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; |
f808b016 | 7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; |
9d50517c NL |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
163eae0b | 10 | use Symfony\Component\HttpFoundation\Request; |
2863bf2a | 11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
be463487 | 12 | use Wallabag\CoreBundle\Entity\Entry; |
f808b016 JB |
13 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
14 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | |
619cc453 | 15 | use Wallabag\CoreBundle\Form\Type\EditEntryType; |
f808b016 | 16 | use Wallabag\CoreBundle\Form\Type\EntryFilterType; |
619cc453 | 17 | use Wallabag\CoreBundle\Form\Type\NewEntryType; |
ee122a75 | 18 | use Wallabag\CoreBundle\Form\Type\SearchEntryType; |
9d50517c NL |
19 | |
20 | class EntryController extends Controller | |
21 | { | |
ee122a75 NL |
22 | /** |
23 | * @param Request $request | |
24 | * @param int $page | |
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 | /** |
3d2b2d62 J |
55 | * @param Request $request |
56 | * | |
053b9568 | 57 | * @Route("/new-entry", name="new_entry") |
3d2b2d62 | 58 | * |
b84a8055 NL |
59 | * @return \Symfony\Component\HttpFoundation\Response |
60 | */ | |
053b9568 | 61 | public function addEntryFormAction(Request $request) |
b84a8055 | 62 | { |
3b815d2d | 63 | $entry = new Entry($this->getUser()); |
b84a8055 | 64 | |
5c895a7f | 65 | $form = $this->createForm(NewEntryType::class, $entry); |
b84a8055 NL |
66 | |
67 | $form->handleRequest($request); | |
68 | ||
21e7ccef | 69 | if ($form->isSubmitted() && $form->isValid()) { |
b00a89e0 | 70 | $existingEntry = $this->checkIfEntryAlreadyExists($entry); |
dda57bb9 | 71 | |
5a4bbcc9 | 72 | if (false !== $existingEntry) { |
dda57bb9 NL |
73 | $this->get('session')->getFlashBag()->add( |
74 | 'notice', | |
4094ea47 | 75 | $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')]) |
dda57bb9 NL |
76 | ); |
77 | ||
4094ea47 | 78 | return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); |
dda57bb9 NL |
79 | } |
80 | ||
c1c0f099 | 81 | $entry->setGivenUrl($entry->getUrl()); |
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 | |
bd9f0815 | 236 | /** |
4346a860 | 237 | * Shows entry content. |
163eae0b | 238 | * |
3d2b2d62 J |
239 | * @param Entry $entry |
240 | * | |
bd9f0815 | 241 | * @Route("/view/{id}", requirements={"id" = "\d+"}, name="view") |
3d2b2d62 | 242 | * |
163eae0b | 243 | * @return \Symfony\Component\HttpFoundation\Response |
bd9f0815 | 244 | */ |
be463487 | 245 | public function viewAction(Entry $entry) |
bd9f0815 | 246 | { |
3d2b2d62 J |
247 | $this->checkUserAction($entry); |
248 | ||
bd9f0815 | 249 | return $this->render( |
ad4d1caa | 250 | 'WallabagCoreBundle:Entry:entry.html.twig', |
4094ea47 | 251 | ['entry' => $entry] |
bd9f0815 | 252 | ); |
163eae0b NL |
253 | } |
254 | ||
831b02aa JB |
255 | /** |
256 | * Reload an entry. | |
257 | * Refetch content from the website and make it readable again. | |
258 | * | |
259 | * @param Entry $entry | |
260 | * | |
261 | * @Route("/reload/{id}", requirements={"id" = "\d+"}, name="reload_entry") | |
262 | * | |
263 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
264 | */ | |
265 | public function reloadAction(Entry $entry) | |
266 | { | |
267 | $this->checkUserAction($entry); | |
268 | ||
59b97fae | 269 | $this->updateEntry($entry, 'entry_reloaded'); |
831b02aa | 270 | |
2297d60f JB |
271 | // if refreshing entry failed, don't save it |
272 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
273 | $bag = $this->get('session')->getFlashBag(); | |
274 | $bag->clear(); | |
275 | $bag->add('notice', 'flashes.entry.notice.entry_reloaded_failed'); | |
276 | ||
277 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); | |
278 | } | |
279 | ||
59b97fae JB |
280 | $em = $this->getDoctrine()->getManager(); |
281 | $em->persist($entry); | |
282 | $em->flush(); | |
831b02aa | 283 | |
e0597476 JB |
284 | // entry saved, dispatch event about it! |
285 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
286 | ||
4094ea47 | 287 | return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); |
831b02aa JB |
288 | } |
289 | ||
163eae0b | 290 | /** |
4346a860 | 291 | * Changes read status for an entry. |
163eae0b | 292 | * |
3d2b2d62 J |
293 | * @param Request $request |
294 | * @param Entry $entry | |
295 | * | |
163eae0b | 296 | * @Route("/archive/{id}", requirements={"id" = "\d+"}, name="archive_entry") |
3d2b2d62 | 297 | * |
163eae0b NL |
298 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
299 | */ | |
be463487 | 300 | public function toggleArchiveAction(Request $request, Entry $entry) |
163eae0b | 301 | { |
3d2b2d62 J |
302 | $this->checkUserAction($entry); |
303 | ||
163eae0b NL |
304 | $entry->toggleArchive(); |
305 | $this->getDoctrine()->getManager()->flush(); | |
306 | ||
4204a06b JB |
307 | $message = 'flashes.entry.notice.entry_unarchived'; |
308 | if ($entry->isArchived()) { | |
309 | $message = 'flashes.entry.notice.entry_archived'; | |
310 | } | |
311 | ||
163eae0b NL |
312 | $this->get('session')->getFlashBag()->add( |
313 | 'notice', | |
4204a06b | 314 | $message |
163eae0b NL |
315 | ); |
316 | ||
af497a64 NL |
317 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
318 | ||
319 | return $this->redirect($redirectUrl); | |
163eae0b NL |
320 | } |
321 | ||
322 | /** | |
eecd7e40 | 323 | * Changes starred status for an entry. |
163eae0b | 324 | * |
3d2b2d62 J |
325 | * @param Request $request |
326 | * @param Entry $entry | |
327 | * | |
163eae0b | 328 | * @Route("/star/{id}", requirements={"id" = "\d+"}, name="star_entry") |
3d2b2d62 | 329 | * |
163eae0b NL |
330 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
331 | */ | |
be463487 | 332 | public function toggleStarAction(Request $request, Entry $entry) |
163eae0b | 333 | { |
3d2b2d62 J |
334 | $this->checkUserAction($entry); |
335 | ||
163eae0b | 336 | $entry->toggleStar(); |
a991c46e | 337 | $entry->updateStar($entry->isStarred()); |
163eae0b NL |
338 | $this->getDoctrine()->getManager()->flush(); |
339 | ||
4204a06b JB |
340 | $message = 'flashes.entry.notice.entry_unstarred'; |
341 | if ($entry->isStarred()) { | |
342 | $message = 'flashes.entry.notice.entry_starred'; | |
343 | } | |
344 | ||
163eae0b NL |
345 | $this->get('session')->getFlashBag()->add( |
346 | 'notice', | |
4204a06b | 347 | $message |
163eae0b NL |
348 | ); |
349 | ||
af497a64 NL |
350 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
351 | ||
352 | return $this->redirect($redirectUrl); | |
163eae0b NL |
353 | } |
354 | ||
355 | /** | |
2863bf2a | 356 | * Deletes entry and redirect to the homepage or the last viewed page. |
163eae0b | 357 | * |
16a3d04c | 358 | * @param Entry $entry |
3d2b2d62 | 359 | * |
163eae0b | 360 | * @Route("/delete/{id}", requirements={"id" = "\d+"}, name="delete_entry") |
3d2b2d62 | 361 | * |
163eae0b NL |
362 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
363 | */ | |
18d5f454 | 364 | public function deleteEntryAction(Request $request, Entry $entry) |
163eae0b | 365 | { |
3d2b2d62 J |
366 | $this->checkUserAction($entry); |
367 | ||
2863bf2a JB |
368 | // generates the view url for this entry to check for redirection later |
369 | // to avoid redirecting to the deleted entry. Ugh. | |
370 | $url = $this->generateUrl( | |
371 | 'view', | |
4094ea47 | 372 | ['id' => $entry->getId()], |
ce0e9ec3 | 373 | UrlGeneratorInterface::ABSOLUTE_PATH |
2863bf2a JB |
374 | ); |
375 | ||
e0597476 JB |
376 | // entry deleted, dispatch event about it! |
377 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
378 | ||
1d147791 NL |
379 | $em = $this->getDoctrine()->getManager(); |
380 | $em->remove($entry); | |
381 | $em->flush(); | |
163eae0b NL |
382 | |
383 | $this->get('session')->getFlashBag()->add( | |
384 | 'notice', | |
4204a06b | 385 | 'flashes.entry.notice.entry_deleted' |
163eae0b | 386 | ); |
bd9f0815 | 387 | |
ce0e9ec3 JB |
388 | // don't redirect user to the deleted entry (check that the referer doesn't end with the same url) |
389 | $referer = $request->headers->get('referer'); | |
f808b016 | 390 | $to = (1 !== preg_match('#' . $url . '$#i', $referer) ? $referer : null); |
af497a64 NL |
391 | |
392 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | |
393 | ||
394 | return $this->redirect($redirectUrl); | |
bd9f0815 | 395 | } |
3d2b2d62 | 396 | |
f1be7af4 NL |
397 | /** |
398 | * Get public URL for entry (and generate it if necessary). | |
399 | * | |
400 | * @param Entry $entry | |
401 | * | |
402 | * @Route("/share/{id}", requirements={"id" = "\d+"}, name="share") | |
403 | * | |
404 | * @return \Symfony\Component\HttpFoundation\Response | |
405 | */ | |
406 | public function shareAction(Entry $entry) | |
407 | { | |
408 | $this->checkUserAction($entry); | |
409 | ||
7239082a NL |
410 | if (null === $entry->getUid()) { |
411 | $entry->generateUid(); | |
eddda878 JB |
412 | |
413 | $em = $this->getDoctrine()->getManager(); | |
414 | $em->persist($entry); | |
415 | $em->flush(); | |
f1be7af4 NL |
416 | } |
417 | ||
418 | return $this->redirect($this->generateUrl('share_entry', [ | |
7239082a | 419 | 'uid' => $entry->getUid(), |
f1be7af4 NL |
420 | ])); |
421 | } | |
422 | ||
423 | /** | |
424 | * Disable public sharing for an entry. | |
425 | * | |
426 | * @param Entry $entry | |
427 | * | |
428 | * @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share") | |
429 | * | |
430 | * @return \Symfony\Component\HttpFoundation\Response | |
431 | */ | |
432 | public function deleteShareAction(Entry $entry) | |
433 | { | |
434 | $this->checkUserAction($entry); | |
435 | ||
7239082a | 436 | $entry->cleanUid(); |
eddda878 | 437 | |
f1be7af4 NL |
438 | $em = $this->getDoctrine()->getManager(); |
439 | $em->persist($entry); | |
440 | $em->flush(); | |
441 | ||
442 | return $this->redirect($this->generateUrl('view', [ | |
443 | 'id' => $entry->getId(), | |
444 | ])); | |
445 | } | |
446 | ||
d0545b6b | 447 | /** |
eddda878 | 448 | * Ability to view a content publicly. |
f3d0cb91 NL |
449 | * |
450 | * @param Entry $entry | |
451 | * | |
7239082a | 452 | * @Route("/share/{uid}", requirements={"uid" = ".+"}, name="share_entry") |
eddda878 | 453 | * @Cache(maxage="25200", smaxage="25200", public=true) |
f3d0cb91 NL |
454 | * |
455 | * @return \Symfony\Component\HttpFoundation\Response | |
456 | */ | |
d0545b6b | 457 | public function shareEntryAction(Entry $entry) |
f3d0cb91 | 458 | { |
eddda878 JB |
459 | if (!$this->get('craue_config')->get('share_public')) { |
460 | throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.'); | |
461 | } | |
462 | ||
f3d0cb91 | 463 | return $this->render( |
2ff9991a | 464 | '@WallabagCore/themes/common/Entry/share.html.twig', |
eddda878 | 465 | ['entry' => $entry] |
f3d0cb91 NL |
466 | ); |
467 | } | |
b6520f0b NL |
468 | |
469 | /** | |
470 | * Shows untagged articles for current user. | |
471 | * | |
472 | * @param Request $request | |
473 | * @param int $page | |
474 | * | |
475 | * @Route("/untagged/list/{page}", name="untagged", defaults={"page" = "1"}) | |
476 | * | |
477 | * @return \Symfony\Component\HttpFoundation\Response | |
478 | */ | |
479 | public function showUntaggedEntriesAction(Request $request, $page) | |
480 | { | |
481 | return $this->showEntries('untagged', $request, $page); | |
482 | } | |
f808b016 JB |
483 | |
484 | /** | |
485 | * Fetch content and update entry. | |
486 | * In case it fails, $entry->getContent will return an error message. | |
487 | * | |
488 | * @param Entry $entry | |
489 | * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded | |
490 | */ | |
491 | private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved') | |
492 | { | |
493 | $message = 'flashes.entry.notice.' . $prefixMessage; | |
494 | ||
495 | try { | |
496 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
497 | } catch (\Exception $e) { | |
498 | $this->get('logger')->error('Error while saving an entry', [ | |
499 | 'exception' => $e, | |
500 | 'entry' => $entry, | |
501 | ]); | |
502 | ||
503 | $message = 'flashes.entry.notice.' . $prefixMessage . '_failed'; | |
504 | } | |
505 | ||
af29e1bf KD |
506 | if (empty($entry->getDomainName())) { |
507 | $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); | |
508 | } | |
509 | ||
510 | if (empty($entry->getTitle())) { | |
511 | $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); | |
512 | } | |
513 | ||
f808b016 JB |
514 | $this->get('session')->getFlashBag()->add('notice', $message); |
515 | } | |
516 | ||
517 | /** | |
518 | * Global method to retrieve entries depending on the given type | |
519 | * It returns the response to be send. | |
520 | * | |
521 | * @param string $type Entries type: unread, starred or archive | |
522 | * @param Request $request | |
523 | * @param int $page | |
524 | * | |
525 | * @return \Symfony\Component\HttpFoundation\Response | |
526 | */ | |
527 | private function showEntries($type, Request $request, $page) | |
528 | { | |
529 | $repository = $this->get('wallabag_core.entry_repository'); | |
530 | $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); | |
531 | $currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : ''); | |
532 | ||
533 | switch ($type) { | |
534 | case 'search': | |
535 | $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute); | |
536 | ||
537 | break; | |
538 | case 'untagged': | |
539 | $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); | |
540 | ||
541 | break; | |
542 | case 'starred': | |
543 | $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); | |
544 | break; | |
545 | case 'archive': | |
546 | $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); | |
547 | break; | |
548 | case 'unread': | |
549 | $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); | |
550 | break; | |
551 | case 'all': | |
552 | $qb = $repository->getBuilderForAllByUser($this->getUser()->getId()); | |
553 | break; | |
554 | default: | |
555 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
556 | } | |
557 | ||
558 | $form = $this->createForm(EntryFilterType::class); | |
559 | ||
560 | if ($request->query->has($form->getName())) { | |
561 | // manually bind values from the request | |
562 | $form->submit($request->query->get($form->getName())); | |
563 | ||
564 | // build the query from the given form object | |
565 | $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); | |
566 | } | |
567 | ||
568 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | |
569 | ||
570 | $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); | |
571 | ||
572 | try { | |
573 | $entries->setCurrentPage($page); | |
574 | } catch (OutOfRangeCurrentPageException $e) { | |
575 | if ($page > 1) { | |
576 | return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302); | |
577 | } | |
578 | } | |
579 | ||
580 | return $this->render( | |
581 | 'WallabagCoreBundle:Entry:entries.html.twig', [ | |
582 | 'form' => $form->createView(), | |
583 | 'entries' => $entries, | |
584 | 'currentPage' => $page, | |
585 | 'searchTerm' => $searchTerm, | |
38321586 | 586 | 'isFiltered' => $form->isSubmitted(), |
f808b016 JB |
587 | ] |
588 | ); | |
589 | } | |
590 | ||
591 | /** | |
592 | * Check if the logged user can manage the given entry. | |
593 | * | |
594 | * @param Entry $entry | |
595 | */ | |
596 | private function checkUserAction(Entry $entry) | |
597 | { | |
598 | if (null === $this->getUser() || $this->getUser()->getId() !== $entry->getUser()->getId()) { | |
599 | throw $this->createAccessDeniedException('You can not access this entry.'); | |
600 | } | |
601 | } | |
602 | ||
603 | /** | |
604 | * Check for existing entry, if it exists, redirect to it with a message. | |
605 | * | |
606 | * @param Entry $entry | |
607 | * | |
608 | * @return Entry|bool | |
609 | */ | |
610 | private function checkIfEntryAlreadyExists(Entry $entry) | |
611 | { | |
612 | return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId()); | |
613 | } | |
9d50517c | 614 | } |