]> git.immae.eu Git - github/wallabag/wallabag.git/blob - 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
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 use Wallabag\CoreBundle\Entity\Entry;
10
11 /**
12 * The try/catch can be removed once all formats will be implemented.
13 * Still need implementation: txt.
14 */
15 class ExportController extends Controller
16 {
17 /**
18 * Gets one entry content.
19 *
20 * @param Entry $entry
21 * @param string $format
22 *
23 * @Route("/export/{id}.{format}", name="export_entry", requirements={
24 * "format": "epub|mobi|pdf|json|xml|txt|csv",
25 * "id": "\d+"
26 * })
27 *
28 * @return \Symfony\Component\HttpFoundation\Response
29 */
30 public function downloadEntryAction(Entry $entry, $format)
31 {
32 try {
33 return $this->get('wallabag_core.helper.entries_export')
34 ->setEntries($entry)
35 ->updateTitle('entry')
36 ->updateAuthor('entry')
37 ->exportAs($format);
38 } catch (\InvalidArgumentException $e) {
39 throw new NotFoundHttpException($e->getMessage());
40 }
41 }
42
43 /**
44 * Export all entries for current user.
45 *
46 * @param string $format
47 * @param string $category
48 *
49 * @Route("/export/{category}.{format}", name="export_entries", requirements={
50 * "format": "epub|mobi|pdf|json|xml|txt|csv",
51 * "category": "all|unread|starred|archive|tag_entries|untagged|search"
52 * })
53 *
54 * @return \Symfony\Component\HttpFoundation\Response
55 */
56 public function downloadEntriesAction(Request $request, $format, $category)
57 {
58 $method = ucfirst($category);
59 $methodBuilder = 'getBuilderFor' . $method . 'ByUser';
60 $repository = $this->get('wallabag_core.entry_repository');
61 $title = $method;
62
63 if ('tag_entries' === $category) {
64 $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
65
66 $entries = $repository->findAllByTagId(
67 $this->getUser()->getId(),
68 $tag->getId()
69 );
70
71 $title = 'Tag ' . $tag->getLabel();
72 } else {
73 $entries = $repository
74 ->$methodBuilder($this->getUser()->getId())
75 ->getQuery()
76 ->getResult();
77 }
78
79 try {
80 return $this->get('wallabag_core.helper.entries_export')
81 ->setEntries($entries)
82 ->updateTitle($title)
83 ->updateAuthor($method)
84 ->exportAs($format);
85 } catch (\InvalidArgumentException $e) {
86 throw new NotFoundHttpException($e->getMessage());
87 }
88 }
89 }