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