]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | ||
11 | /** | |
12 | * The try/catch can be removed once all formats will be implemented. | |
13 | * Still need implementation: txt. | |
14 | */ | |
15 | class ExportController extends Controller | |
16 | { | |
17 | /** | |
18 | * Gets one entry content. | |
19 | * | |
20 | * @param Entry $entry | |
21 | * @param string $format | |
22 | * | |
23 | * @Route("/export/{id}.{format}", name="export_entry", requirements={ | |
24 | * "format": "epub|mobi|pdf|json|xml|txt|csv", | |
25 | * "id": "\d+" | |
26 | * }) | |
27 | * | |
28 | * @return \Symfony\Component\HttpFoundation\Response | |
29 | */ | |
30 | public function downloadEntryAction(Entry $entry, $format) | |
31 | { | |
32 | try { | |
33 | return $this->get('wallabag_core.helper.entries_export') | |
34 | ->setEntries($entry) | |
35 | ->updateTitle('entry') | |
36 | ->exportAs($format); | |
37 | } catch (\InvalidArgumentException $e) { | |
38 | throw new NotFoundHttpException($e->getMessage()); | |
39 | } | |
40 | } | |
41 | ||
42 | /** | |
43 | * Export all entries for current user. | |
44 | * | |
45 | * @param string $format | |
46 | * @param string $category | |
47 | * | |
48 | * @Route("/export/{category}.{format}", name="export_entries", requirements={ | |
49 | * "format": "epub|mobi|pdf|json|xml|txt|csv", | |
50 | * "category": "all|unread|starred|archive|tag_entries|untagged|search" | |
51 | * }) | |
52 | * | |
53 | * @return \Symfony\Component\HttpFoundation\Response | |
54 | */ | |
55 | public function downloadEntriesAction(Request $request, $format, $category) | |
56 | { | |
57 | $method = ucfirst($category); | |
58 | $methodBuilder = 'getBuilderFor'.$method.'ByUser'; | |
59 | $repository = $this->get('wallabag_core.entry_repository'); | |
60 | ||
61 | if ($category == 'tag_entries') { | |
62 | $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag')); | |
63 | ||
64 | $entries = $repository->findAllByTagId( | |
65 | $this->getUser()->getId(), | |
66 | $tag->getId() | |
67 | ); | |
68 | } else { | |
69 | $entries = $repository | |
70 | ->$methodBuilder($this->getUser()->getId()) | |
71 | ->getQuery() | |
72 | ->getResult(); | |
73 | } | |
74 | ||
75 | try { | |
76 | return $this->get('wallabag_core.helper.entries_export') | |
77 | ->setEntries($entries) | |
78 | ->updateTitle($method) | |
79 | ->exportAs($format); | |
80 | } catch (\InvalidArgumentException $e) { | |
81 | throw new NotFoundHttpException($e->getMessage()); | |
82 | } | |
83 | } | |
84 | } |