]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ExportController.php
User existing service instead of getDoctrine
[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",
ee122a75 51 * "category": "all|unread|starred|archive|tag_entries|untagged|search"
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';
25203e50 60 $epository = $this->get('wallabag_core.entry_repository');
920d8859
NL
61
62 if ($category == 'tag_entries') {
25203e50 63 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
920d8859 64
25203e50
JB
65 $entries = $epository->findAllByTagId(
66 $this->getUser()->getId(),
67 $tag->getId()
68 );
920d8859 69 } else {
25203e50 70 $entries = $epository
920d8859
NL
71 ->$methodBuilder($this->getUser()->getId())
72 ->getQuery()
73 ->getResult();
74 }
add597ba
JB
75
76 try {
77 return $this->get('wallabag_core.helper.entries_export')
78 ->setEntries($entries)
79 ->updateTitle($method)
80 ->exportAs($format);
81 } catch (\InvalidArgumentException $e) {
82 throw new NotFoundHttpException($e->getMessage());
83 }
03690d13
TC
84 }
85}