]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Controller/ExportController.php
ExportController: fix entries export from search view
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ExportController.php
CommitLineData
03690d13
TC
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
03690d13 5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
920d8859 6use Symfony\Component\HttpFoundation\Request;
add597ba 7use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
115de64e 8use Symfony\Component\Routing\Annotation\Route;
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 20 * @param string $format
add597ba 21 *
cceca9ea
JB
22 * @Route("/export/{id}.{format}", name="export_entry", requirements={
23 * "format": "epub|mobi|pdf|json|xml|txt|csv",
24 * "id": "\d+"
25 * })
4094ea47
JB
26 *
27 * @return \Symfony\Component\HttpFoundation\Response
03690d13 28 */
add597ba 29 public function downloadEntryAction(Entry $entry, $format)
03690d13 30 {
add597ba
JB
31 try {
32 return $this->get('wallabag_core.helper.entries_export')
33 ->setEntries($entry)
34 ->updateTitle('entry')
07320a2b 35 ->updateAuthor('entry')
add597ba
JB
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');
f8108346 60 $title = $method;
920d8859 61
3ef055ce 62 if ('tag_entries' === $category) {
25203e50 63 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
920d8859 64
ebf2d923 65 $entries = $repository->findAllByTagId(
25203e50
JB
66 $this->getUser()->getId(),
67 $tag->getId()
68 );
f8108346
KD
69
70 $title = 'Tag ' . $tag->getLabel();
93c5b47e
KD
71 } elseif ('search' === $category) {
72 $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
73 $currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
74
75 $entries = $repository->getBuilderForSearchByUser(
76 $this->getUser()->getId(),
77 $searchTerm,
78 $currentRoute
79 )->getQuery()
80 ->getResult();
81
82 $title = 'Search ' . $searchTerm;
920d8859 83 } else {
ebf2d923 84 $entries = $repository
920d8859
NL
85 ->$methodBuilder($this->getUser()->getId())
86 ->getQuery()
87 ->getResult();
88 }
add597ba
JB
89
90 try {
91 return $this->get('wallabag_core.helper.entries_export')
92 ->setEntries($entries)
f8108346 93 ->updateTitle($title)
07320a2b 94 ->updateAuthor($method)
add597ba
JB
95 ->exportAs($format);
96 } catch (\InvalidArgumentException $e) {
97 throw new NotFoundHttpException($e->getMessage());
98 }
03690d13
TC
99 }
100}