]>
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 | * | |
b787a775 JB |
19 | * @return Response|RedirectResponse |
20 | */ | |
21 | public function indexAction(Request $request) | |
22 | { | |
23 | $form = $this->createForm(UploadImportType::class); | |
24 | $form->handleRequest($request); | |
25 | ||
26 | $wallabag = $this->getImportService(); | |
c98db1b6 | 27 | $wallabag->setUser($this->getUser()); |
b787a775 | 28 | |
21e7ccef | 29 | if ($form->isSubmitted() && $form->isValid()) { |
b787a775 JB |
30 | $file = $form->get('file')->getData(); |
31 | $markAsRead = $form->get('mark_as_read')->getData(); | |
f808b016 | 32 | $name = $this->getUser()->getId() . '.json'; |
b787a775 | 33 | |
2a1ceb67 | 34 | if (null !== $file && \in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { |
b787a775 | 35 | $res = $wallabag |
f808b016 | 36 | ->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name) |
b787a775 JB |
37 | ->setMarkAsRead($markAsRead) |
38 | ->import(); | |
39 | ||
40 | $message = 'flashes.import.notice.failed'; | |
41 | ||
42 | if (true === $res) { | |
43 | $summary = $wallabag->getSummary(); | |
4094ea47 | 44 | $message = $this->get('translator')->trans('flashes.import.notice.summary', [ |
b787a775 JB |
45 | '%imported%' => $summary['imported'], |
46 | '%skipped%' => $summary['skipped'], | |
4094ea47 | 47 | ]); |
b787a775 | 48 | |
c80cc01a JB |
49 | if (0 < $summary['queued']) { |
50 | $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [ | |
51 | '%queued%' => $summary['queued'], | |
52 | ]); | |
53 | } | |
54 | ||
f808b016 | 55 | unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name); |
b787a775 JB |
56 | } |
57 | ||
58 | $this->get('session')->getFlashBag()->add( | |
59 | 'notice', | |
60 | $message | |
61 | ); | |
62 | ||
63 | return $this->redirect($this->generateUrl('homepage')); | |
b787a775 | 64 | } |
f808b016 JB |
65 | |
66 | $this->get('session')->getFlashBag()->add( | |
67 | 'notice', | |
68 | 'flashes.import.notice.failed_on_file' | |
69 | ); | |
b787a775 JB |
70 | } |
71 | ||
72 | return $this->render($this->getImportTemplate(), [ | |
73 | 'form' => $form->createView(), | |
74 | 'import' => $wallabag, | |
75 | ]); | |
76 | } | |
f808b016 JB |
77 | |
78 | /** | |
79 | * Return the service to handle the import. | |
80 | * | |
81 | * @return \Wallabag\ImportBundle\Import\ImportInterface | |
82 | */ | |
83 | abstract protected function getImportService(); | |
84 | ||
85 | /** | |
86 | * Return the template used for the form. | |
87 | * | |
88 | * @return string | |
89 | */ | |
90 | abstract protected function getImportTemplate(); | |
b787a775 | 91 | } |