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