]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ExportController.php
Fixed entries export filtered with a tag
[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\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Wallabag\CoreBundle\Entity\Tag;
11
12 /**
13 * The try/catch can be removed once all formats will be implemented.
14 * Still need implementation: txt.
15 */
16 class ExportController extends Controller
17 {
18 /**
19 * Gets one entry content.
20 *
21 * @param Entry $entry
22 * @param string $format
23 *
24 * @Route("/export/{id}.{format}", name="export_entry", requirements={
25 * "format": "epub|mobi|pdf|json|xml|txt|csv",
26 * "id": "\d+"
27 * })
28 *
29 * @return \Symfony\Component\HttpFoundation\Response
30 */
31 public function downloadEntryAction(Entry $entry, $format)
32 {
33 try {
34 return $this->get('wallabag_core.helper.entries_export')
35 ->setEntries($entry)
36 ->updateTitle('entry')
37 ->exportAs($format);
38 } catch (\InvalidArgumentException $e) {
39 throw new NotFoundHttpException($e->getMessage());
40 }
41 }
42
43 /**
44 * Export all entries for current user.
45 *
46 * @param string $format
47 * @param string $category
48 *
49 * @Route("/export/{category}.{format}", name="export_entries", requirements={
50 * "format": "epub|mobi|pdf|json|xml|txt|csv",
51 * "category": "all|unread|starred|archive|tag_entries|untagged"
52 * })
53 *
54 * @return \Symfony\Component\HttpFoundation\Response
55 */
56 public function downloadEntriesAction(Request $request, $format, $category)
57 {
58 $method = ucfirst($category);
59 $methodBuilder = 'getBuilderFor'.$method.'ByUser';
60
61 if ($category == 'tag_entries') {
62 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneBySlug($request->query->get('tag'));
63
64 $entries = $this->getDoctrine()
65 ->getRepository('WallabagCoreBundle:Entry')
66 ->findAllByTagId($this->getUser()->getId(), $tag->getId());
67 } else {
68 $entries = $this->getDoctrine()
69 ->getRepository('WallabagCoreBundle:Entry')
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 }