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