aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller/WallabagController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller/WallabagController.php')
-rw-r--r--src/Wallabag/ImportBundle/Controller/WallabagController.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagController.php b/src/Wallabag/ImportBundle/Controller/WallabagController.php
new file mode 100644
index 00000000..01883d4a
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Controller/WallabagController.php
@@ -0,0 +1,85 @@
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Symfony\Component\HttpFoundation\Request;
7use Wallabag\ImportBundle\Form\Type\UploadImportType;
8
9/**
10 * Define Wallabag import for v1 and v2, since there are very similar.
11 */
12abstract class WallabagController extends Controller
13{
14 /**
15 * Return the service to handle the import.
16 *
17 * @return \Wallabag\ImportBundle\Import\ImportInterface
18 */
19 abstract protected function getImportService();
20
21 /**
22 * Return the template used for the form.
23 *
24 * @return string
25 */
26 abstract protected function getImportTemplate();
27
28 /**
29 * Handle import request.
30 *
31 * @param Request $request
32 *
33 * @return Response|RedirectResponse
34 */
35 public function indexAction(Request $request)
36 {
37 $form = $this->createForm(UploadImportType::class);
38 $form->handleRequest($request);
39
40 $wallabag = $this->getImportService();
41
42 if ($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 (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
48 $res = $wallabag
49 ->setUser($this->getUser())
50 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
51 ->setMarkAsRead($markAsRead)
52 ->import();
53
54 $message = 'flashes.import.notice.failed';
55
56 if (true === $res) {
57 $summary = $wallabag->getSummary();
58 $message = $this->get('translator')->trans('flashes.import.notice.summary', array(
59 '%imported%' => $summary['imported'],
60 '%skipped%' => $summary['skipped'],
61 ));
62
63 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
64 }
65
66 $this->get('session')->getFlashBag()->add(
67 'notice',
68 $message
69 );
70
71 return $this->redirect($this->generateUrl('homepage'));
72 } else {
73 $this->get('session')->getFlashBag()->add(
74 'notice',
75 'flashes.import.notice.failed_on_file'
76 );
77 }
78 }
79
80 return $this->render($this->getImportTemplate(), [
81 'form' => $form->createView(),
82 'import' => $wallabag,
83 ]);
84 }
85}