]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ExportController.php
Fixed entries export filtered with a tag
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ExportController.php
CommitLineData
03690d13
TC
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
920d8859 7use Symfony\Component\HttpFoundation\Request;
add597ba 8use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
03690d13 9use Wallabag\CoreBundle\Entity\Entry;
920d8859 10use Wallabag\CoreBundle\Entity\Tag;
03690d13 11
cceca9ea
JB
12/**
13 * The try/catch can be removed once all formats will be implemented.
14 * Still need implementation: txt.
15 */
03690d13
TC
16class ExportController extends Controller
17{
18 /**
add597ba 19 * Gets one entry content.
03690d13 20 *
4094ea47
JB
21 * @param Entry $entry
22 * @param string $format
add597ba 23 *
cceca9ea
JB
24 * @Route("/export/{id}.{format}", name="export_entry", requirements={
25 * "format": "epub|mobi|pdf|json|xml|txt|csv",
26 * "id": "\d+"
27 * })
4094ea47
JB
28 *
29 * @return \Symfony\Component\HttpFoundation\Response
03690d13 30 */
add597ba 31 public function downloadEntryAction(Entry $entry, $format)
03690d13 32 {
add597ba
JB
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());
03690d13 40 }
03690d13
TC
41 }
42
43 /**
add597ba 44 * Export all entries for current user.
03690d13 45 *
4094ea47
JB
46 * @param string $format
47 * @param string $category
48 *
add597ba 49 * @Route("/export/{category}.{format}", name="export_entries", requirements={
cceca9ea 50 * "format": "epub|mobi|pdf|json|xml|txt|csv",
b6520f0b 51 * "category": "all|unread|starred|archive|tag_entries|untagged"
add597ba 52 * })
4094ea47
JB
53 *
54 * @return \Symfony\Component\HttpFoundation\Response
03690d13 55 */
920d8859 56 public function downloadEntriesAction(Request $request, $format, $category)
03690d13 57 {
add597ba
JB
58 $method = ucfirst($category);
59 $methodBuilder = 'getBuilderFor'.$method.'ByUser';
920d8859
NL
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 }
add597ba
JB
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 }
03690d13
TC
83 }
84}