]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ExportController.php
Merge pull request #2454 from wallabag/fix-baggy-small-width
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ExportController.php
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\HttpKernel\Exception\NotFoundHttpException;
8 use Wallabag\CoreBundle\Entity\Entry;
9
10 /**
11 * The try/catch can be removed once all formats will be implemented.
12 * Still need implementation: txt.
13 */
14 class ExportController extends Controller
15 {
16 /**
17 * Gets one entry content.
18 *
19 * @param Entry $entry
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 ->exportAs($format);
36 } catch (\InvalidArgumentException $e) {
37 throw new NotFoundHttpException($e->getMessage());
38 }
39 }
40
41 /**
42 * Export all entries for current user.
43 *
44 * @param string $format
45 * @param string $category
46 *
47 * @Route("/export/{category}.{format}", name="export_entries", requirements={
48 * "format": "epub|mobi|pdf|json|xml|txt|csv",
49 * "category": "all|unread|starred|archive|tag_entries|untagged"
50 * })
51 *
52 * @return \Symfony\Component\HttpFoundation\Response
53 */
54 public function downloadEntriesAction($format, $category)
55 {
56 $method = ucfirst($category);
57 $methodBuilder = 'getBuilderFor'.$method.'ByUser';
58 $entries = $this->getDoctrine()
59 ->getRepository('WallabagCoreBundle:Entry')
60 ->$methodBuilder($this->getUser()->getId())
61 ->getQuery()
62 ->getResult();
63
64 try {
65 return $this->get('wallabag_core.helper.entries_export')
66 ->setEntries($entries)
67 ->updateTitle($method)
68 ->exportAs($format);
69 } catch (\InvalidArgumentException $e) {
70 throw new NotFoundHttpException($e->getMessage());
71 }
72 }
73 }