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