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