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