aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-09-30 15:10:46 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:24:17 +0100
commit8c3c77c1bd5c3763c127bfea52e908e77dc751b9 (patch)
treeb2e4dc1d22969748660da0fd7bd5fe0727081897 /src
parenta1bb1b3c2a0631ad41262ef92f6cce02c3d376bf (diff)
downloadwallabag-8c3c77c1bd5c3763c127bfea52e908e77dc751b9.tar.gz
wallabag-8c3c77c1bd5c3763c127bfea52e908e77dc751b9.tar.zst
wallabag-8c3c77c1bd5c3763c127bfea52e908e77dc751b9.zip
create controller to launch import command
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Command/ImportCommand.php19
-rw-r--r--src/Wallabag/CoreBundle/Controller/ImportController.php40
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig1
3 files changed, 52 insertions, 8 deletions
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 @@
3namespace Wallabag\CoreBundle\Command; 3namespace Wallabag\CoreBundle\Command;
4 4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; 5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Config\Definition\Exception\Exception;
6use Symfony\Component\Console\Input\InputArgument; 7use Symfony\Component\Console\Input\InputArgument;
7use Symfony\Component\Console\Input\InputInterface; 8use Symfony\Component\Console\Input\InputInterface;
8use Symfony\Component\Console\Output\OutputInterface; 9use Symfony\Component\Console\Output\OutputInterface;
@@ -15,7 +16,7 @@ class ImportCommand extends ContainerAwareCommand
15 protected function configure() 16 protected function configure()
16 { 17 {
17 $this 18 $this
18 ->setName('import:json') 19 ->setName('wallabag:import')
19 ->setDescription('Import entries from JSON file') 20 ->setDescription('Import entries from JSON file')
20 ->addArgument( 21 ->addArgument(
21 'userId', 22 'userId',
@@ -38,8 +39,6 @@ class ImportCommand extends ContainerAwareCommand
38 39
39 protected function import(InputInterface $input, OutputInterface $output) 40 protected function import(InputInterface $input, OutputInterface $output)
40 { 41 {
41 $userId = $input->getArgument('userId');
42
43 // Getting php array of data from CSV 42 // Getting php array of data from CSV
44 $data = $this->get($input, $output); 43 $data = $this->get($input, $output);
45 44
@@ -52,12 +51,16 @@ class ImportCommand extends ContainerAwareCommand
52 $batchSize = 20; 51 $batchSize = 20;
53 $i = 1; 52 $i = 1;
54 53
54 $user = $em->getRepository('WallabagCoreBundle:User')
55 ->findOneById($input->getArgument('userId'));
56
57 if (!is_object($user)) {
58 throw new Exception('User not found');
59 }
60
55 $progress = new ProgressBar($output, $size); 61 $progress = new ProgressBar($output, $size);
56 $progress->start(); 62 $progress->start();
57 63
58 $user = $em->getRepository('WallabagCoreBundle:User')
59 ->findOneById($userId);
60
61 foreach ($data as $object) { 64 foreach ($data as $object) {
62 $array = (array) $object; 65 $array = (array) $object;
63 $entry = $em->getRepository('WallabagCoreBundle:Entry') 66 $entry = $em->getRepository('WallabagCoreBundle:Entry')
@@ -112,8 +115,8 @@ class ImportCommand extends ContainerAwareCommand
112 115
113 protected function get(InputInterface $input, OutputInterface $output) 116 protected function get(InputInterface $input, OutputInterface $output)
114 { 117 {
115 $fileName = 'web/uploads/import/import.json'; 118 $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
116 $data = $this->convert($fileName); 119 $data = $this->convert($filename);
117 120
118 return $data; 121 return $data;
119 } 122 }
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\Console\Input\ArrayInput;
8use Symfony\Component\Console\Output\NullOutput;
9use Symfony\Component\HttpFoundation\Request;
10use Wallabag\CoreBundle\Command\ImportCommand;
11
12class ImportController extends Controller
13{
14 /**
15 * @param Request $request
16 *
17 * @Route("/import", name="import")
18 */
19 public function importAction(Request $request)
20 {
21 $command = new ImportCommand();
22 $command->setContainer($this->container);
23 $input = new ArrayInput(array('userId' => $this->getUser()->getId()));
24 $return = $command->run($input, new NullOutput());
25
26 if ($return == 0) {
27 $this->get('session')->getFlashBag()->add(
28 'notice',
29 'Import successful'
30 );
31 } else {
32 $this->get('session')->getFlashBag()->add(
33 'warning',
34 'Import failed'
35 );
36 }
37
38 return $this->redirect('/');
39 }
40}
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 @@
45 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"><a class="waves-effect" href="{{ path('all') }}">{% trans %}all{% endtrans %}</a></li> 45 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"><a class="waves-effect" href="{{ path('all') }}">{% trans %}all{% endtrans %}</a></li>
46 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"><a class="waves-effect" href="{{ path('tag') }}">{% trans %}tags{% endtrans %}</a></li> 46 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"><a class="waves-effect" href="{{ path('tag') }}">{% trans %}tags{% endtrans %}</a></li>
47 <li class="bold {% if currentRoute == 'config' %}active{% endif %}"><a class="waves-effect" href="{{ path('config') }}">{% trans %}config{% endtrans %}</a></li> 47 <li class="bold {% if currentRoute == 'config' %}active{% endif %}"><a class="waves-effect" href="{{ path('config') }}">{% trans %}config{% endtrans %}</a></li>
48 <li class="bold {% if currentRoute == 'import' %}active{% endif %}"><a class="waves-effect" href="{{ path('import') }}">{% trans %}import{% endtrans %}</a></li>
48 <li class="bold {% if currentRoute == 'howto' %}active{% endif %}"><a class="waves-effect" href="{{ path('howto') }}">{% trans %}howto{% endtrans %}</a></li> 49 <li class="bold {% if currentRoute == 'howto' %}active{% endif %}"><a class="waves-effect" href="{{ path('howto') }}">{% trans %}howto{% endtrans %}</a></li>
49 <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> 50 <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>
50 </ul> 51 </ul>