]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ExportController.php
EntriesExport: change authors and title when not single entry export
[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;
03690d13 10
cceca9ea
JB
11/**
12 * The try/catch can be removed once all formats will be implemented.
13 * Still need implementation: txt.
14 */
03690d13
TC
15class ExportController extends Controller
16{
17 /**
add597ba 18 * Gets one entry content.
03690d13 19 *
4094ea47
JB
20 * @param Entry $entry
21 * @param string $format
add597ba 22 *
cceca9ea
JB
23 * @Route("/export/{id}.{format}", name="export_entry", requirements={
24 * "format": "epub|mobi|pdf|json|xml|txt|csv",
25 * "id": "\d+"
26 * })
4094ea47
JB
27 *
28 * @return \Symfony\Component\HttpFoundation\Response
03690d13 29 */
add597ba 30 public function downloadEntryAction(Entry $entry, $format)
03690d13 31 {
add597ba
JB
32 try {
33 return $this->get('wallabag_core.helper.entries_export')
34 ->setEntries($entry)
35 ->updateTitle('entry')
07320a2b 36 ->updateAuthor('entry')
add597ba
JB
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 58 $method = ucfirst($category);
f808b016 59 $methodBuilder = 'getBuilderFor' . $method . 'ByUser';
ebf2d923 60 $repository = $this->get('wallabag_core.entry_repository');
f8108346 61 $title = $method;
920d8859 62
3ef055ce 63 if ('tag_entries' === $category) {
25203e50 64 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
920d8859 65
ebf2d923 66 $entries = $repository->findAllByTagId(
25203e50
JB
67 $this->getUser()->getId(),
68 $tag->getId()
69 );
f8108346
KD
70
71 $title = 'Tag ' . $tag->getLabel();
920d8859 72 } else {
ebf2d923 73 $entries = $repository
920d8859
NL
74 ->$methodBuilder($this->getUser()->getId())
75 ->getQuery()
76 ->getResult();
77 }
add597ba
JB
78
79 try {
80 return $this->get('wallabag_core.helper.entries_export')
81 ->setEntries($entries)
f8108346 82 ->updateTitle($title)
07320a2b 83 ->updateAuthor($method)
add597ba
JB
84 ->exportAs($format);
85 } catch (\InvalidArgumentException $e) {
86 throw new NotFoundHttpException($e->getMessage());
87 }
03690d13
TC
88 }
89}