]> 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 123e491abeac7b577b4ae006672bbe99cdaeb46e..9e9dbe49a96f1446411c77e2cfd8ab4c9d37a610 100644 (file)
@@ -4,62 +4,86 @@ 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;
-use Wallabag\CoreBundle\Helper\EntriesExport;
 
+/**
+ * The try/catch can be removed once all formats will be implemented.
+ * Still need implementation: txt.
+ */
 class ExportController extends Controller
 {
     /**
-     * Gets all entries for current user.
+     * Gets one entry content.
+     *
+     * @param Entry  $entry
+     * @param string $format
      *
-     * @Route("/export/{category}.{format}", name="ebook", requirements={
-     *     "_format": "epub|mobi|pdf|json|xml|txt|csv"
+     * @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 getEntriesAction($format, $category)
+    public function downloadEntryAction(Entry $entry, $format)
     {
-        $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
-        switch ($category) {
-            case 'all':
-                $method = 'All';
-                break;
-
-            case 'unread':
-                $method = 'Unread';
-                break;
-
-            case 'starred':
-                $method = 'Starred';
-                break;
-
-            case 'archive':
-                $method = 'Archive';
-                break;
-
-            default:
-                break;
+        try {
+            return $this->get('wallabag_core.helper.entries_export')
+                ->setEntries($entry)
+                ->updateTitle('entry')
+                ->updateAuthor('entry')
+                ->exportAs($format);
+        } catch (\InvalidArgumentException $e) {
+            throw new NotFoundHttpException($e->getMessage());
         }
-
-        $methodBuilder = 'getBuilderFor'.$method.'ByUser';
-        $qb = $repository->$methodBuilder($this->getUser()->getId());
-        $entries = $qb->getQuery()->getResult();
-
-        $export = new EntriesExport($entries);
-        $export->setMethod($method);
-        $export->exportAs($format);
     }
 
     /**
-     * Gets one entry content.
+     * Export all entries for current user.
+     *
+     * @param string $format
+     * @param string $category
      *
-     * @param Entry $entry
+     * @Route("/export/{category}.{format}", name="export_entries", requirements={
+     *     "format": "epub|mobi|pdf|json|xml|txt|csv",
+     *     "category": "all|unread|starred|archive|tag_entries|untagged|search"
+     * })
      *
-     * @Route("/export/id/{id}.{format}", requirements={"id" = "\d+"}, name="ebook_entry")
+     * @return \Symfony\Component\HttpFoundation\Response
      */
-    public function getEntryAction(Entry $entry, $format)
+    public function downloadEntriesAction(Request $request, $format, $category)
     {
-        $export = new EntriesExport(array($entry));
-        $export->setMethod('entry');
-        $export->exportAs($format);
+        $method = ucfirst($category);
+        $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($title)
+                ->updateAuthor($method)
+                ->exportAs($format);
+        } catch (\InvalidArgumentException $e) {
+            throw new NotFoundHttpException($e->getMessage());
+        }
     }
 }