]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php
cs & tests for wllbg v1 import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / WallabagV1Controller.php
CommitLineData
b1d05721
JB
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request;
8use Wallabag\ImportBundle\Form\Type\UploadImportType;
9
10class WallabagV1Controller extends Controller
11{
12 /**
7019c7cf 13 * @Route("/wallabag-v1", name="import_wallabag_v1")
b1d05721
JB
14 */
15 public function indexAction(Request $request)
16 {
1d76102a 17 $form = $this->createForm(UploadImportType::class);
7019c7cf 18 $form->handleRequest($request);
b1d05721 19
7019c7cf
JB
20 $wallabag = $this->get('wallabag_import.wallabag_v1.import');
21
22 if ($form->isValid()) {
23 $file = $form->get('file')->getData();
fe8b37c1 24 $markAsRead = $form->get('mark_as_read')->getData();
7019c7cf 25 $name = $this->getUser()->getId().'.json';
b1d05721
JB
26
27 if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
b1d05721
JB
28 $res = $wallabag
29 ->setUser($this->getUser())
30 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
fe8b37c1 31 ->setMarkAsRead($markAsRead)
b1d05721
JB
32 ->import();
33
34 $message = 'Import failed, please try again.';
35 if (true === $res) {
36 $summary = $wallabag->getSummary();
37 $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.';
38
7019c7cf 39 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
b1d05721
JB
40 }
41
42 $this->get('session')->getFlashBag()->add(
43 'notice',
44 $message
45 );
46
47 return $this->redirect($this->generateUrl('homepage'));
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:WallabagV1:index.html.twig', [
7019c7cf
JB
57 'form' => $form->createView(),
58 'import' => $wallabag,
b1d05721
JB
59 ]);
60 }
61}