]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ExportController.php
Add a real configuration for CS-Fixer
[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')
36 ->exportAs($format);
37 } catch (\InvalidArgumentException $e) {
38 throw new NotFoundHttpException($e->getMessage());
03690d13 39 }
03690d13
TC
40 }
41
42 /**
add597ba 43 * Export all entries for current user.
03690d13 44 *
4094ea47
JB
45 * @param string $format
46 * @param string $category
47 *
add597ba 48 * @Route("/export/{category}.{format}", name="export_entries", requirements={
cceca9ea 49 * "format": "epub|mobi|pdf|json|xml|txt|csv",
ee122a75 50 * "category": "all|unread|starred|archive|tag_entries|untagged|search"
add597ba 51 * })
4094ea47
JB
52 *
53 * @return \Symfony\Component\HttpFoundation\Response
03690d13 54 */
920d8859 55 public function downloadEntriesAction(Request $request, $format, $category)
03690d13 56 {
add597ba 57 $method = ucfirst($category);
f808b016 58 $methodBuilder = 'getBuilderFor' . $method . 'ByUser';
ebf2d923 59 $repository = $this->get('wallabag_core.entry_repository');
920d8859 60
f808b016 61 if ($category === 'tag_entries') {
25203e50 62 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
920d8859 63
ebf2d923 64 $entries = $repository->findAllByTagId(
25203e50
JB
65 $this->getUser()->getId(),
66 $tag->getId()
67 );
920d8859 68 } else {
ebf2d923 69 $entries = $repository
920d8859
NL
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}