From 8c3c77c1bd5c3763c127bfea52e908e77dc751b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 30 Sep 2015 15:10:46 +0200 Subject: [PATCH] create controller to launch import command --- .../CoreBundle/Command/ImportCommand.php | 19 +++++---- .../Controller/ImportController.php | 40 +++++++++++++++++++ .../views/themes/material/layout.html.twig | 1 + 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Controller/ImportController.php diff --git a/src/Wallabag/CoreBundle/Command/ImportCommand.php b/src/Wallabag/CoreBundle/Command/ImportCommand.php index 4a174202..16c84229 100644 --- a/src/Wallabag/CoreBundle/Command/ImportCommand.php +++ b/src/Wallabag/CoreBundle/Command/ImportCommand.php @@ -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 index 00000000..53211eec --- /dev/null +++ b/src/Wallabag/CoreBundle/Controller/ImportController.php @@ -0,0 +1,40 @@ +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('/'); + } +} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig index f426e25b..6b8d7adf 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig @@ -45,6 +45,7 @@
  • {% trans %}all{% endtrans %}
  • {% trans %}tags{% endtrans %}
  • {% trans %}config{% endtrans %}
  • +
  • {% trans %}import{% endtrans %}
  • {% trans %}howto{% endtrans %}
  • {% trans %}logout{% endtrans %}
  • -- 2.41.0