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