]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/EntriesExport.php
Exported entries were added twice in export file
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / EntriesExport.php
index d6a4d094aba9e709cc699937d288fb45ccb5ca04..6ecdf019672a65ac003f28ab320f145c7ddf7e13 100644 (file)
@@ -2,12 +2,13 @@
 
 namespace Wallabag\CoreBundle\Helper;
 
+use JMS\Serializer;
+use JMS\Serializer\SerializationContext;
+use JMS\Serializer\SerializerBuilder;
 use PHPePub\Core\EPub;
 use PHPePub\Core\Structure\OPF\DublinCore;
 use Symfony\Component\HttpFoundation\Response;
-use JMS\Serializer;
-use JMS\Serializer\SerializerBuilder;
-use JMS\Serializer\SerializationContext;
+use Craue\ConfigBundle\Util\Config;
 
 /**
  * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
@@ -27,12 +28,12 @@ class EntriesExport
         </div';
 
     /**
-     * @param string $wallabagUrl Wallabag instance url
+     * @param Config $craueConfig CraueConfig instance to get wallabag instance url from database
      * @param string $logoPath    Path to the logo FROM THE BUNDLE SCOPE
      */
-    public function __construct($wallabagUrl, $logoPath)
+    public function __construct(Config $craueConfig, $logoPath)
     {
-        $this->wallabagUrl = $wallabagUrl;
+        $this->wallabagUrl = $craueConfig->get('wallabag_url');
         $this->logoPath = $logoPath;
     }
 
@@ -98,6 +99,9 @@ class EntriesExport
 
             case 'xml':
                 return $this->produceXML();
+
+            case 'txt':
+                return $this->produceTXT();
         }
 
         throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
@@ -182,7 +186,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -232,7 +236,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -286,7 +290,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -329,7 +333,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"',
                 'Content-Transfer-Encoding' => 'UTF-8',
             )
-        )->send();
+        );
     }
 
     private function produceJSON()
@@ -342,7 +346,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"',
                 'Content-Transfer-Encoding' => 'UTF-8',
             )
-        )->send();
+        );
     }
 
     private function produceXML()
@@ -355,7 +359,27 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"',
                 'Content-Transfer-Encoding' => 'UTF-8',
             )
-        )->send();
+        );
+    }
+
+    private function produceTXT()
+    {
+        $content = '';
+        $bar = str_repeat('=', 100);
+        foreach ($this->entries as $entry) {
+            $content .= "\n\n".$bar."\n\n".$entry->getTitle()."\n\n".$bar."\n\n";
+            $content .= trim(preg_replace('/\s+/S', ' ', strip_tags($entry->getContent())))."\n\n";
+        }
+
+        return Response::create(
+            $content,
+            200,
+            array(
+                'Content-type' => 'text/plain',
+                'Content-Disposition' => 'attachment; filename="'.$this->title.'.txt"',
+                'Content-Transfer-Encoding' => 'UTF-8',
+            )
+        );
     }
 
     /**