diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller')
4 files changed, 141 insertions, 101 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index c88e115e..11ce649d 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php | |||
@@ -17,8 +17,8 @@ class PocketController extends Controller | |||
17 | { | 17 | { |
18 | $pocket = $this->get('wallabag_import.pocket.import'); | 18 | $pocket = $this->get('wallabag_import.pocket.import'); |
19 | $form = $this->createFormBuilder($pocket) | 19 | $form = $this->createFormBuilder($pocket) |
20 | ->add('read', CheckboxType::class, array( | 20 | ->add('mark_as_read', CheckboxType::class, array( |
21 | 'label' => 'Mark all as read', | 21 | 'label' => 'import.form.mark_as_read_label', |
22 | 'required' => false, | 22 | 'required' => false, |
23 | )) | 23 | )) |
24 | ->getForm(); | 24 | ->getForm(); |
@@ -38,8 +38,17 @@ class PocketController extends Controller | |||
38 | $requestToken = $this->get('wallabag_import.pocket.import') | 38 | $requestToken = $this->get('wallabag_import.pocket.import') |
39 | ->getRequestToken($this->generateUrl('import', array(), UrlGeneratorInterface::ABSOLUTE_URL)); | 39 | ->getRequestToken($this->generateUrl('import', array(), UrlGeneratorInterface::ABSOLUTE_URL)); |
40 | 40 | ||
41 | if (false === $requestToken) { | ||
42 | $this->get('session')->getFlashBag()->add( | ||
43 | 'notice', | ||
44 | 'flashes.import.notice.failed' | ||
45 | ); | ||
46 | |||
47 | return $this->redirect($this->generateUrl('import_pocket')); | ||
48 | } | ||
49 | |||
41 | $this->get('session')->set('import.pocket.code', $requestToken); | 50 | $this->get('session')->set('import.pocket.code', $requestToken); |
42 | $this->get('session')->set('read', $request->request->get('form')['read']); | 51 | $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']); |
43 | 52 | ||
44 | return $this->redirect( | 53 | return $this->redirect( |
45 | 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL), | 54 | 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL), |
@@ -52,10 +61,11 @@ class PocketController extends Controller | |||
52 | */ | 61 | */ |
53 | public function callbackAction() | 62 | public function callbackAction() |
54 | { | 63 | { |
55 | $message = 'Import failed, please try again.'; | 64 | $message = 'flashes.import.notice.failed'; |
56 | $pocket = $this->get('wallabag_import.pocket.import'); | 65 | $pocket = $this->get('wallabag_import.pocket.import'); |
57 | $markAsRead = $this->get('session')->get('read'); | 66 | |
58 | $this->get('session')->remove('read'); | 67 | $markAsRead = $this->get('session')->get('mark_as_read'); |
68 | $this->get('session')->remove('mark_as_read'); | ||
59 | 69 | ||
60 | // something bad happend on pocket side | 70 | // something bad happend on pocket side |
61 | if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { | 71 | if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { |
@@ -69,7 +79,10 @@ class PocketController extends Controller | |||
69 | 79 | ||
70 | if (true === $pocket->setMarkAsRead($markAsRead)->import()) { | 80 | if (true === $pocket->setMarkAsRead($markAsRead)->import()) { |
71 | $summary = $pocket->getSummary(); | 81 | $summary = $pocket->getSummary(); |
72 | $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; | 82 | $message = $this->get('translator')->trans('flashes.import.notice.summary', array( |
83 | '%imported%' => $summary['imported'], | ||
84 | '%skipped%' => $summary['skipped'], | ||
85 | )); | ||
73 | } | 86 | } |
74 | 87 | ||
75 | $this->get('session')->getFlashBag()->add( | 88 | $this->get('session')->getFlashBag()->add( |
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 | |||
3 | namespace Wallabag\ImportBundle\Controller; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
8 | |||
9 | /** | ||
10 | * Define Wallabag import for v1 and v2, since there are very similar. | ||
11 | */ | ||
12 | abstract 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 | } | ||
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php index 154a0769..3e748d57 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php | |||
@@ -2,61 +2,32 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | 3 | namespace Wallabag\ImportBundle\Controller; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
7 | use Symfony\Component\HttpFoundation\Request; | 6 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
9 | 7 | ||
10 | class WallabagV1Controller extends Controller | 8 | class WallabagV1Controller extends WallabagController |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * @Route("/wallabag-v1", name="import_wallabag_v1") | 11 | * {@inheritdoc} |
14 | */ | 12 | */ |
15 | public function indexAction(Request $request) | 13 | protected function getImportService() |
16 | { | 14 | { |
17 | $form = $this->createForm(UploadImportType::class); | 15 | return $this->get('wallabag_import.wallabag_v1.import'); |
18 | $form->handleRequest($request); | 16 | } |
19 | |||
20 | $wallabag = $this->get('wallabag_import.wallabag_v1.import'); | ||
21 | |||
22 | if ($form->isValid()) { | ||
23 | $file = $form->get('file')->getData(); | ||
24 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
25 | $name = $this->getUser()->getId().'.json'; | ||
26 | |||
27 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
28 | $res = $wallabag | ||
29 | ->setUser($this->getUser()) | ||
30 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
31 | ->setMarkAsRead($markAsRead) | ||
32 | ->import(); | ||
33 | |||
34 | $message = 'Import failed, please try again.'; | ||
35 | |||
36 | if (true === $res) { | ||
37 | $summary = $wallabag->getSummary(); | ||
38 | $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; | ||
39 | |||
40 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
41 | } | ||
42 | |||
43 | $this->get('session')->getFlashBag()->add( | ||
44 | 'notice', | ||
45 | $message | ||
46 | ); | ||
47 | 17 | ||
48 | return $this->redirect($this->generateUrl('homepage')); | 18 | /** |
49 | } else { | 19 | * {@inheritdoc} |
50 | $this->get('session')->getFlashBag()->add( | 20 | */ |
51 | 'notice', | 21 | protected function getImportTemplate() |
52 | 'Error while processing import. Please verify your import file.' | 22 | { |
53 | ); | 23 | return 'WallabagImportBundle:WallabagV1:index.html.twig'; |
54 | } | 24 | } |
55 | } | ||
56 | 25 | ||
57 | return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [ | 26 | /** |
58 | 'form' => $form->createView(), | 27 | * @Route("/wallabag-v1", name="import_wallabag_v1") |
59 | 'import' => $wallabag, | 28 | */ |
60 | ]); | 29 | public function indexAction(Request $request) |
30 | { | ||
31 | return parent::indexAction($request); | ||
61 | } | 32 | } |
62 | } | 33 | } |
diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php index 6dcd204a..c2a42165 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV2Controller.php | |||
@@ -2,61 +2,32 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | 3 | namespace Wallabag\ImportBundle\Controller; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
7 | use Symfony\Component\HttpFoundation\Request; | 6 | use Symfony\Component\HttpFoundation\Request; |
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
9 | 7 | ||
10 | class WallabagV2Controller extends Controller | 8 | class WallabagV2Controller extends WallabagController |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * @Route("/wallabag-v2", name="import_wallabag_v2") | 11 | * {@inheritdoc} |
14 | */ | 12 | */ |
15 | public function indexAction(Request $request) | 13 | protected function getImportService() |
16 | { | 14 | { |
17 | $form = $this->createForm(UploadImportType::class); | 15 | return $this->get('wallabag_import.wallabag_v2.import'); |
18 | $form->handleRequest($request); | 16 | } |
19 | |||
20 | $wallabag = $this->get('wallabag_import.wallabag_v2.import'); | ||
21 | |||
22 | if ($form->isValid()) { | ||
23 | $file = $form->get('file')->getData(); | ||
24 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
25 | $name = $this->getUser()->getId().'.json'; | ||
26 | |||
27 | if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
28 | $res = $wallabag | ||
29 | ->setUser($this->getUser()) | ||
30 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
31 | ->setMarkAsRead($markAsRead) | ||
32 | ->import(); | ||
33 | |||
34 | $message = 'Import failed, please try again.'; | ||
35 | |||
36 | if (true === $res) { | ||
37 | $summary = $wallabag->getSummary(); | ||
38 | $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; | ||
39 | |||
40 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
41 | } | ||
42 | |||
43 | $this->get('session')->getFlashBag()->add( | ||
44 | 'notice', | ||
45 | $message | ||
46 | ); | ||
47 | 17 | ||
48 | return $this->redirect($this->generateUrl('homepage')); | 18 | /** |
49 | } else { | 19 | * {@inheritdoc} |
50 | $this->get('session')->getFlashBag()->add( | 20 | */ |
51 | 'notice', | 21 | protected function getImportTemplate() |
52 | 'Error while processing import. Please verify your import file.' | 22 | { |
53 | ); | 23 | return 'WallabagImportBundle:WallabagV2:index.html.twig'; |
54 | } | 24 | } |
55 | } | ||
56 | 25 | ||
57 | return $this->render('WallabagImportBundle:WallabagV2:index.html.twig', [ | 26 | /** |
58 | 'form' => $form->createView(), | 27 | * @Route("/wallabag-v2", name="import_wallabag_v2") |
59 | 'import' => $wallabag, | 28 | */ |
60 | ]); | 29 | public function indexAction(Request $request) |
30 | { | ||
31 | return parent::indexAction($request); | ||
61 | } | 32 | } |
62 | } | 33 | } |