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