]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ExportController.php
Update deps
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ExportController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8 use Symfony\Component\Routing\Annotation\Route;
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 string $format
21 *
22 * @Route("/export/{id}.{format}", name="export_entry", requirements={
23 * "format": "epub|mobi|pdf|json|xml|txt|csv",
24 * "id": "\d+"
25 * })
26 *
27 * @return \Symfony\Component\HttpFoundation\Response
28 */
29 public function downloadEntryAction(Entry $entry, $format)
30 {
31 try {
32 return $this->get('wallabag_core.helper.entries_export')
33 ->setEntries($entry)
34 ->updateTitle('entry')
35 ->updateAuthor('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 $title = $method;
61
62 if ('tag_entries' === $category) {
63 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
64
65 $entries = $repository->findAllByTagId(
66 $this->getUser()->getId(),
67 $tag->getId()
68 );
69
70 $title = 'Tag ' . $tag->getLabel();
71 } else {
72 $entries = $repository
73 ->$methodBuilder($this->getUser()->getId())
74 ->getQuery()
75 ->getResult();
76 }
77
78 try {
79 return $this->get('wallabag_core.helper.entries_export')
80 ->setEntries($entries)
81 ->updateTitle($title)
82 ->updateAuthor($method)
83 ->exportAs($format);
84 } catch (\InvalidArgumentException $e) {
85 throw new NotFoundHttpException($e->getMessage());
86 }
87 }
88 }