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