diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@smile.fr> | 2015-10-22 16:57:56 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-02 23:24:17 +0100 |
commit | d275bdf4d36f90ff61f1e6a714c9ef64d210596f (patch) | |
tree | 1fb9cd40f2d75e1a39cec2636d3caa510045fc48 /src/Wallabag | |
parent | 8c3c77c1bd5c3763c127bfea52e908e77dc751b9 (diff) | |
download | wallabag-d275bdf4d36f90ff61f1e6a714c9ef64d210596f.tar.gz wallabag-d275bdf4d36f90ff61f1e6a714c9ef64d210596f.tar.zst wallabag-d275bdf4d36f90ff61f1e6a714c9ef64d210596f.zip |
form to upload file
Diffstat (limited to 'src/Wallabag')
6 files changed, 108 insertions, 17 deletions
diff --git a/src/Wallabag/CoreBundle/Command/ImportCommand.php b/src/Wallabag/CoreBundle/Command/ImportCommand.php index 16c84229..6be6f5e1 100644 --- a/src/Wallabag/CoreBundle/Command/ImportCommand.php +++ b/src/Wallabag/CoreBundle/Command/ImportCommand.php | |||
@@ -51,7 +51,7 @@ class ImportCommand extends ContainerAwareCommand | |||
51 | $batchSize = 20; | 51 | $batchSize = 20; |
52 | $i = 1; | 52 | $i = 1; |
53 | 53 | ||
54 | $user = $em->getRepository('WallabagCoreBundle:User') | 54 | $user = $em->getRepository('WallabagUserBundle:User') |
55 | ->findOneById($input->getArgument('userId')); | 55 | ->findOneById($input->getArgument('userId')); |
56 | 56 | ||
57 | if (!is_object($user)) { | 57 | if (!is_object($user)) { |
@@ -116,6 +116,7 @@ class ImportCommand extends ContainerAwareCommand | |||
116 | protected function get(InputInterface $input, OutputInterface $output) | 116 | protected function get(InputInterface $input, OutputInterface $output) |
117 | { | 117 | { |
118 | $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json'; | 118 | $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json'; |
119 | |||
119 | $data = $this->convert($filename); | 120 | $data = $this->convert($filename); |
120 | 121 | ||
121 | return $data; | 122 | return $data; |
diff --git a/src/Wallabag/CoreBundle/Controller/ImportController.php b/src/Wallabag/CoreBundle/Controller/ImportController.php index 53211eec..33087710 100644 --- a/src/Wallabag/CoreBundle/Controller/ImportController.php +++ b/src/Wallabag/CoreBundle/Controller/ImportController.php | |||
@@ -8,6 +8,7 @@ use Symfony\Component\Console\Input\ArrayInput; | |||
8 | use Symfony\Component\Console\Output\NullOutput; | 8 | use Symfony\Component\Console\Output\NullOutput; |
9 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Wallabag\CoreBundle\Command\ImportCommand; | 10 | use Wallabag\CoreBundle\Command\ImportCommand; |
11 | use Wallabag\CoreBundle\Form\Type\UploadImportType; | ||
11 | 12 | ||
12 | class ImportController extends Controller | 13 | class ImportController extends Controller |
13 | { | 14 | { |
@@ -18,23 +19,46 @@ class ImportController extends Controller | |||
18 | */ | 19 | */ |
19 | public function importAction(Request $request) | 20 | public function importAction(Request $request) |
20 | { | 21 | { |
21 | $command = new ImportCommand(); | 22 | $importForm = $this->createForm(new UploadImportType()); |
22 | $command->setContainer($this->container); | 23 | $importForm->handleRequest($request); |
23 | $input = new ArrayInput(array('userId' => $this->getUser()->getId())); | 24 | $user = $this->getUser(); |
24 | $return = $command->run($input, new NullOutput()); | 25 | $importConfig = $this->container->getParameter('wallabag_core.import'); |
25 | 26 | ||
26 | if ($return == 0) { | 27 | if ($importForm->isValid()) { |
27 | $this->get('session')->getFlashBag()->add( | 28 | $file = $importForm->get('file')->getData(); |
28 | 'notice', | 29 | $name = $user->getId().'.json'; |
29 | 'Import successful' | 30 | $dir = __DIR__.'/../../../../web/uploads/import'; |
30 | ); | 31 | |
31 | } else { | 32 | if (in_array($file->getMimeType(), $importConfig['allow_mimetypes']) && $file->move($dir, $name)) { |
32 | $this->get('session')->getFlashBag()->add( | 33 | $command = new ImportCommand(); |
33 | 'warning', | 34 | $command->setContainer($this->container); |
34 | 'Import failed' | 35 | $input = new ArrayInput(array('userId' => $user->getId())); |
35 | ); | 36 | $return = $command->run($input, new NullOutput()); |
37 | |||
38 | if ($return == 0) { | ||
39 | $this->get('session')->getFlashBag()->add( | ||
40 | 'notice', | ||
41 | 'Import successful' | ||
42 | ); | ||
43 | } else { | ||
44 | $this->get('session')->getFlashBag()->add( | ||
45 | 'notice', | ||
46 | 'Import failed' | ||
47 | ); | ||
48 | } | ||
49 | |||
50 | return $this->redirect('/'); | ||
51 | } else { | ||
52 | $this->get('session')->getFlashBag()->add( | ||
53 | 'notice', | ||
54 | 'Error while processing import. Please verify your import file.' | ||
55 | ); | ||
56 | } | ||
36 | } | 57 | } |
37 | 58 | ||
38 | return $this->redirect('/'); | 59 | return $this->render('WallabagCoreBundle:Import:index.html.twig', array( |
60 | 'form' => array( | ||
61 | 'import' => $importForm->createView(), ), | ||
62 | )); | ||
39 | } | 63 | } |
40 | } | 64 | } |
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php index 32acd1f1..fb1941b8 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php | |||
@@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\DependencyInjection; | |||
4 | 4 | ||
5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | 5 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
6 | use Symfony\Component\Config\Definition\ConfigurationInterface; | 6 | use Symfony\Component\Config\Definition\ConfigurationInterface; |
7 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
7 | 8 | ||
8 | class Configuration implements ConfigurationInterface | 9 | class Configuration implements ConfigurationInterface |
9 | { | 10 | { |
@@ -17,9 +18,21 @@ class Configuration implements ConfigurationInterface | |||
17 | ->arrayNode('languages') | 18 | ->arrayNode('languages') |
18 | ->prototype('scalar')->end() | 19 | ->prototype('scalar')->end() |
19 | ->end() | 20 | ->end() |
21 | ->arrayNode('import') | ||
22 | ->append($this->getAllowMimetypes()) | ||
23 | ->end() | ||
20 | ->end() | 24 | ->end() |
21 | ; | 25 | ; |
22 | 26 | ||
23 | return $treeBuilder; | 27 | return $treeBuilder; |
24 | } | 28 | } |
29 | |||
30 | private function getAllowMimetypes() | ||
31 | { | ||
32 | $node = new ArrayNodeDefinition('allow_mimetypes'); | ||
33 | |||
34 | $node->prototype('scalar')->end(); | ||
35 | |||
36 | return $node; | ||
37 | } | ||
25 | } | 38 | } |
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php index 330cc957..e2a3ad55 100644 --- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php +++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php | |||
@@ -14,6 +14,7 @@ class WallabagCoreExtension extends Extension | |||
14 | $configuration = new Configuration(); | 14 | $configuration = new Configuration(); |
15 | $config = $this->processConfiguration($configuration, $configs); | 15 | $config = $this->processConfiguration($configuration, $configs); |
16 | $container->setParameter('wallabag_core.languages', $config['languages']); | 16 | $container->setParameter('wallabag_core.languages', $config['languages']); |
17 | $container->setParameter('wallabag_core.import', $config['import']); | ||
17 | 18 | ||
18 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | 19 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
19 | $loader->load('services.yml'); | 20 | $loader->load('services.yml'); |
diff --git a/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php b/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php new file mode 100644 index 00000000..b9a9c465 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php | |||
@@ -0,0 +1,29 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Form\Type; | ||
4 | |||
5 | use Symfony\Component\Form\AbstractType; | ||
6 | use Symfony\Component\Form\FormBuilderInterface; | ||
7 | |||
8 | class UploadImportType extends AbstractType | ||
9 | { | ||
10 | public function buildForm(FormBuilderInterface $builder, array $options) | ||
11 | { | ||
12 | $builder | ||
13 | ->add('file', 'file') | ||
14 | ->add('save', 'submit') | ||
15 | ; | ||
16 | } | ||
17 | |||
18 | public function getDefaultOptions(array $options) | ||
19 | { | ||
20 | return array( | ||
21 | 'csrf_protection' => false, | ||
22 | ); | ||
23 | } | ||
24 | |||
25 | public function getName() | ||
26 | { | ||
27 | return 'upload_import_file'; | ||
28 | } | ||
29 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig new file mode 100644 index 00000000..2c8c158d --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig | |||
@@ -0,0 +1,23 @@ | |||
1 | {% extends "WallabagCoreBundle::layout.html.twig" %} | ||
2 | |||
3 | {% block title %}{% trans %}import{% endtrans %}{% endblock %} | ||
4 | |||
5 | {% block content %} | ||
6 | |||
7 | <div id="set1" class="col s12"> | ||
8 | <form action="{{ path('import') }}" method="post" {{ form_enctype(form.import) }}> | ||
9 | {{ form_errors(form.import) }} | ||
10 | <div class="row"> | ||
11 | <div class="input-field col s12"> | ||
12 | {{ form_errors(form.import.file) }} | ||
13 | {{ form_widget(form.import.file) }} | ||
14 | <label class="required">{% trans %}File{% endtrans %}</label> | ||
15 | </div> | ||
16 | </div> | ||
17 | <div class="hidden">{{ form_rest(form.import) }}</div> | ||
18 | <button class="btn waves-effect waves-light" type="submit" name="action"> | ||
19 | {% trans %}Upload file{% endtrans %} | ||
20 | </button> | ||
21 | </form> | ||
22 | </div> | ||
23 | {% endblock %} | ||