]>
Commit | Line | Data |
---|---|---|
ae669126 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Symfony\Component\HttpFoundation\Response; | |
9 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | |
10 | ||
11 | class BrowserController extends Controller | |
12 | { | |
13 | /** | |
14 | * Return the service to handle the import. | |
15 | * | |
16 | * @return \Wallabag\ImportBundle\Import\ImportInterface | |
17 | */ | |
18 | protected function getImportService() | |
19 | { | |
20 | return $this->get('wallabag_import.browser.import'); | |
21 | } | |
22 | ||
23 | /** | |
24 | * Return the template used for the form. | |
25 | * | |
26 | * @return string | |
27 | */ | |
28 | protected function getImportTemplate() | |
29 | { | |
30 | return 'WallabagImportBundle:Browser:index.html.twig'; | |
31 | } | |
32 | ||
33 | /** | |
34 | * @Route("/browser", name="import_browser") | |
35 | * | |
36 | * @param Request $request | |
37 | * | |
38 | * @return Response | |
39 | */ | |
40 | public function indexAction(Request $request) | |
41 | { | |
42 | $form = $this->createForm(UploadImportType::class); | |
43 | $form->handleRequest($request); | |
44 | ||
45 | $wallabag = $this->getImportService(); | |
46 | ||
47 | if ($form->isValid()) { | |
48 | $file = $form->get('file')->getData(); | |
49 | $markAsRead = $form->get('mark_as_read')->getData(); | |
50 | $name = $this->getUser()->getId().'.json'; | |
51 | ||
52 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | |
53 | $res = $wallabag | |
54 | ->setUser($this->getUser()) | |
55 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | |
56 | ->setMarkAsRead($markAsRead) | |
57 | ->import(); | |
58 | ||
59 | $message = 'flashes.import.notice.failed'; | |
60 | ||
61 | if (true === $res) { | |
62 | $summary = $wallabag->getSummary(); | |
63 | // TODO : Pluralize these messages | |
64 | $message = $this->get('translator')->trans('flashes.import.notice.summary', [ | |
65 | '%imported%' => $summary['imported'], | |
66 | '%skipped%' => $summary['skipped'], | |
67 | ]); | |
68 | ||
69 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | |
70 | } | |
71 | ||
72 | $this->get('session')->getFlashBag()->add( | |
73 | 'notice', | |
74 | $message | |
75 | ); | |
76 | ||
77 | return $this->redirect($this->generateUrl('homepage')); | |
78 | } else { | |
79 | $this->get('session')->getFlashBag()->add( | |
80 | 'notice', | |
81 | 'flashes.import.notice.failed_on_file' | |
82 | ); | |
83 | } | |
84 | } | |
85 | ||
86 | return $this->render($this->getImportTemplate(), [ | |
87 | 'form' => $form->createView(), | |
88 | 'import' => $wallabag, | |
89 | ]); | |
90 | } | |
91 | } |