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