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