]>
Commit | Line | Data |
---|---|---|
d51b38ed NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
77a7752a JB |
7 | use Symfony\Component\Console\Input\ArrayInput; |
8 | use Symfony\Component\Console\Output\NullOutput; | |
9 | use Symfony\Component\HttpFoundation\Request; | |
10 | use Wallabag\ImportBundle\Command\ImportCommand; | |
11 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | |
d51b38ed NL |
12 | |
13 | class ImportController extends Controller | |
14 | { | |
15 | /** | |
16 | * @Route("/import", name="import") | |
17 | */ | |
77a7752a | 18 | public function importAction(Request $request) |
d51b38ed | 19 | { |
77a7752a JB |
20 | $importForm = $this->createForm(new UploadImportType()); |
21 | $importForm->handleRequest($request); | |
22 | $user = $this->getUser(); | |
23 | ||
24 | if ($importForm->isValid()) { | |
25 | $file = $importForm->get('file')->getData(); | |
26 | $name = $user->getId().'.json'; | |
27 | $dir = __DIR__.'/../../../../web/uploads/import'; | |
28 | ||
29 | if (in_array($file->getMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($dir, $name)) { | |
30 | $command = new ImportCommand(); | |
31 | $command->setContainer($this->container); | |
32 | $input = new ArrayInput(array('userId' => $user->getId())); | |
33 | $return = $command->run($input, new NullOutput()); | |
34 | ||
35 | if ($return == 0) { | |
36 | $this->get('session')->getFlashBag()->add( | |
37 | 'notice', | |
38 | 'Import successful' | |
39 | ); | |
40 | } else { | |
41 | $this->get('session')->getFlashBag()->add( | |
42 | 'notice', | |
43 | 'Import failed' | |
44 | ); | |
45 | } | |
46 | ||
47 | return $this->redirect('/'); | |
48 | } else { | |
49 | $this->get('session')->getFlashBag()->add( | |
50 | 'notice', | |
51 | 'Error while processing import. Please verify your import file.' | |
52 | ); | |
53 | } | |
54 | } | |
55 | ||
56 | return $this->render('WallabagImportBundle:Import:index.html.twig', array( | |
57 | 'form' => array( | |
58 | 'import' => $importForm->createView(), ), | |
59 | )); | |
d51b38ed NL |
60 | } |
61 | } |