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