]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Controller/BrowserController.php
e119098f21040a9805b5eacfe8f1d1487ca2593f
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / BrowserController.php
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
11 abstract class BrowserController extends Controller
12 {
13 /**
14 * Return the service to handle the import.
15 *
16 * @return \Wallabag\ImportBundle\Import\ImportInterface
17 */
18 abstract protected function getImportService();
19
20 /**
21 * Return the template used for the form.
22 *
23 * @return string
24 */
25 abstract protected function getImportTemplate();
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();
40 $wallabag->setUser($this->getUser());
41
42 if ($form->isSubmitted() && $form->isValid()) {
43 $file = $form->get('file')->getData();
44 $markAsRead = $form->get('mark_as_read')->getData();
45 $name = $this->getUser()->getId().'.json';
46
47 if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
48 $res = $wallabag
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();
57 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
58 '%imported%' => $summary['imported'],
59 '%skipped%' => $summary['skipped'],
60 ]);
61
62 if (0 < $summary['queued']) {
63 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
64 '%queued%' => $summary['queued'],
65 ]);
66 }
67
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 }