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