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