]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - 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
index dd3cb7ca5d84cd5807b4f7953876c235bad5616d..9e9dbe49a96f1446411c77e2cfd8ab4c9d37a610 100644 (file)
@@ -4,17 +4,28 @@ namespace Wallabag\CoreBundle\Controller;
 
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Wallabag\CoreBundle\Entity\Entry;
 
+/**
+ * The try/catch can be removed once all formats will be implemented.
+ * Still need implementation: txt.
+ */
 class ExportController extends Controller
 {
     /**
      * Gets one entry content.
      *
-     * @param Entry $entry
+     * @param Entry  $entry
+     * @param string $format
      *
-     * @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry")
+     * @Route("/export/{id}.{format}", name="export_entry", requirements={
+     *     "format": "epub|mobi|pdf|json|xml|txt|csv",
+     *     "id": "\d+"
+     * })
+     *
+     * @return \Symfony\Component\HttpFoundation\Response
      */
     public function downloadEntryAction(Entry $entry, $format)
     {
@@ -22,6 +33,7 @@ class ExportController extends Controller
             return $this->get('wallabag_core.helper.entries_export')
                 ->setEntries($entry)
                 ->updateTitle('entry')
+                ->updateAuthor('entry')
                 ->exportAs($format);
         } catch (\InvalidArgumentException $e) {
             throw new NotFoundHttpException($e->getMessage());
@@ -31,25 +43,44 @@ class ExportController extends Controller
     /**
      * Export all entries for current user.
      *
+     * @param string $format
+     * @param string $category
+     *
      * @Route("/export/{category}.{format}", name="export_entries", requirements={
-     *     "_format": "epub|mobi|pdf|json|xml|txt|csv",
-     *     "category": "all|unread|starred|archive"
+     *     "format": "epub|mobi|pdf|json|xml|txt|csv",
+     *     "category": "all|unread|starred|archive|tag_entries|untagged|search"
      * })
+     *
+     * @return \Symfony\Component\HttpFoundation\Response
      */
-    public function downloadEntriesAction($format, $category)
+    public function downloadEntriesAction(Request $request, $format, $category)
     {
         $method = ucfirst($category);
-        $methodBuilder = 'getBuilderFor'.$method.'ByUser';
-        $entries = $this->getDoctrine()
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->$methodBuilder($this->getUser()->getId())
-            ->getQuery()
-            ->getResult();
+        $methodBuilder = 'getBuilderFor' . $method . 'ByUser';
+        $repository = $this->get('wallabag_core.entry_repository');
+        $title = $method;
+
+        if ('tag_entries' === $category) {
+            $tag = $this->get('wallabag_core.tag_repository')->findOneBySlug($request->query->get('tag'));
+
+            $entries = $repository->findAllByTagId(
+                $this->getUser()->getId(),
+                $tag->getId()
+            );
+
+            $title = 'Tag ' . $tag->getLabel();
+        } else {
+            $entries = $repository
+                ->$methodBuilder($this->getUser()->getId())
+                ->getQuery()
+                ->getResult();
+        }
 
         try {
             return $this->get('wallabag_core.helper.entries_export')
                 ->setEntries($entries)
-                ->updateTitle($method)
+                ->updateTitle($title)
+                ->updateAuthor($method)
                 ->exportAs($format);
         } catch (\InvalidArgumentException $e) {
             throw new NotFoundHttpException($e->getMessage());