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