]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
create controller to launch import command
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Wed, 30 Sep 2015 13:10:46 +0000 (15:10 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 2 Jan 2016 22:24:17 +0000 (23:24 +0100)
src/Wallabag/CoreBundle/Command/ImportCommand.php
src/Wallabag/CoreBundle/Controller/ImportController.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig

index 4a174202a3bb32423d221d946a8a211b7f88df69..16c84229fb441fe4b60a8a6dc32dc1c1e5db1f9e 100644 (file)
@@ -3,6 +3,7 @@
 namespace Wallabag\CoreBundle\Command;
 
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Config\Definition\Exception\Exception;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -15,7 +16,7 @@ class ImportCommand extends ContainerAwareCommand
     protected function configure()
     {
         $this
-            ->setName('import:json')
+            ->setName('wallabag:import')
             ->setDescription('Import entries from JSON file')
             ->addArgument(
                 'userId',
@@ -38,8 +39,6 @@ class ImportCommand extends ContainerAwareCommand
 
     protected function import(InputInterface $input, OutputInterface $output)
     {
-        $userId = $input->getArgument('userId');
-
         // Getting php array of data from CSV
         $data = $this->get($input, $output);
 
@@ -52,12 +51,16 @@ class ImportCommand extends ContainerAwareCommand
         $batchSize = 20;
         $i = 1;
 
+        $user = $em->getRepository('WallabagCoreBundle:User')
+            ->findOneById($input->getArgument('userId'));
+
+        if (!is_object($user)) {
+            throw new Exception('User not found');
+        }
+
         $progress = new ProgressBar($output, $size);
         $progress->start();
 
-        $user = $em->getRepository('WallabagCoreBundle:User')
-            ->findOneById($userId);
-
         foreach ($data as $object) {
             $array = (array) $object;
             $entry = $em->getRepository('WallabagCoreBundle:Entry')
@@ -112,8 +115,8 @@ class ImportCommand extends ContainerAwareCommand
 
     protected function get(InputInterface $input, OutputInterface $output)
     {
-        $fileName = 'web/uploads/import/import.json';
-        $data = $this->convert($fileName);
+        $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
+        $data = $this->convert($filename);
 
         return $data;
     }
diff --git a/src/Wallabag/CoreBundle/Controller/ImportController.php b/src/Wallabag/CoreBundle/Controller/ImportController.php
new file mode 100644 (file)
index 0000000..53211ee
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace Wallabag\CoreBundle\Controller;
+
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\NullOutput;
+use Symfony\Component\HttpFoundation\Request;
+use Wallabag\CoreBundle\Command\ImportCommand;
+
+class ImportController extends Controller
+{
+    /**
+     * @param Request $request
+     *
+     * @Route("/import", name="import")
+     */
+    public function importAction(Request $request)
+    {
+        $command = new ImportCommand();
+        $command->setContainer($this->container);
+        $input = new ArrayInput(array('userId' => $this->getUser()->getId()));
+        $return = $command->run($input, new NullOutput());
+
+        if ($return == 0) {
+            $this->get('session')->getFlashBag()->add(
+                'notice',
+                'Import successful'
+            );
+        } else {
+            $this->get('session')->getFlashBag()->add(
+                'warning',
+                'Import failed'
+            );
+        }
+
+        return $this->redirect('/');
+    }
+}
index f426e25b97addb1a0fea2410b97cb10abac89f9f..6b8d7adf4a1db8a090dd276dba6655f8e53e682f 100644 (file)
@@ -45,6 +45,7 @@
             <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"><a class="waves-effect" href="{{ path('all') }}">{% trans %}all{% endtrans %}</a></li>
             <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"><a class="waves-effect" href="{{ path('tag') }}">{% trans %}tags{% endtrans %}</a></li>
             <li class="bold {% if currentRoute == 'config' %}active{% endif %}"><a class="waves-effect" href="{{ path('config') }}">{% trans %}config{% endtrans %}</a></li>
+            <li class="bold {% if currentRoute == 'import' %}active{% endif %}"><a class="waves-effect" href="{{ path('import') }}">{% trans %}import{% endtrans %}</a></li>
             <li class="bold {% if currentRoute == 'howto' %}active{% endif %}"><a class="waves-effect" href="{{ path('howto') }}">{% trans %}howto{% endtrans %}</a></li>
             <li class="bold"><a class="waves-effect" class="icon icon-power" href="{{ path('fos_user_security_logout') }}" title="{% trans %}logout{% endtrans %}">{% trans %}logout{% endtrans %}</a></li>
         </ul>