aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Controller')
-rw-r--r--src/Wallabag/ImportBundle/Controller/ImportController.php48
-rw-r--r--src/Wallabag/ImportBundle/Controller/PocketController.php45
2 files changed, 35 insertions, 58 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php
index 6ebd6a0a..2a0d6ab5 100644
--- a/src/Wallabag/ImportBundle/Controller/ImportController.php
+++ b/src/Wallabag/ImportBundle/Controller/ImportController.php
@@ -4,58 +4,14 @@ namespace Wallabag\ImportBundle\Controller;
4 4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller; 5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\Console\Input\ArrayInput;
8use Symfony\Component\Console\Output\NullOutput;
9use Symfony\Component\HttpFoundation\Request;
10use Wallabag\ImportBundle\Command\ImportCommand;
11use Wallabag\ImportBundle\Form\Type\UploadImportType;
12 7
13class ImportController extends Controller 8class ImportController extends Controller
14{ 9{
15 /** 10 /**
16 * @Route("/import", name="import") 11 * @Route("/import", name="import")
17 */ 12 */
18 public function importAction(Request $request) 13 public function importAction()
19 { 14 {
20 $importForm = $this->createForm(new UploadImportType()); 15 return $this->render('WallabagImportBundle:Import:index.html.twig', []);
21 $importForm->handleRequest($request);
22 $user = $this->getUser();
23
24 if ($importForm->isValid()) {
25 $file = $importForm->get('file')->getData();
26 $name = $user->getId().'.json';
27 $dir = __DIR__.'/../../../../web/uploads/import';
28
29 if (in_array($file->getMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($dir, $name)) {
30 $command = new ImportCommand();
31 $command->setContainer($this->container);
32 $input = new ArrayInput(array('userId' => $user->getId()));
33 $return = $command->run($input, new NullOutput());
34
35 if ($return == 0) {
36 $this->get('session')->getFlashBag()->add(
37 'notice',
38 'Import successful'
39 );
40 } else {
41 $this->get('session')->getFlashBag()->add(
42 'notice',
43 'Import failed'
44 );
45 }
46
47 return $this->redirect('/');
48 } else {
49 $this->get('session')->getFlashBag()->add(
50 'notice',
51 'Error while processing import. Please verify your import file.'
52 );
53 }
54 }
55
56 return $this->render('WallabagImportBundle:Import:index.html.twig', array(
57 'form' => array(
58 'import' => $importForm->createView(), ),
59 ));
60 } 16 }
61} 17}
diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php
index 2ab062e7..61eeba43 100644
--- a/src/Wallabag/ImportBundle/Controller/PocketController.php
+++ b/src/Wallabag/ImportBundle/Controller/PocketController.php
@@ -8,35 +8,56 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8class PocketController extends Controller 8class PocketController extends Controller
9{ 9{
10 /** 10 /**
11 * @Route("/import/pocket", name="pocket_import") 11 * @Route("/import/pocket", name="import_pocket")
12 */ 12 */
13 public function indexAction() 13 public function indexAction()
14 { 14 {
15 return $this->render('WallabagImportBundle:Pocket:index.html.twig', array()); 15 return $this->render('WallabagImportBundle:Pocket:index.html.twig', []);
16 } 16 }
17 17
18 /** 18 /**
19 * @Route("/import/pocket/auth", name="pocket_auth") 19 * @Route("/import/pocket/auth", name="import_pocket_auth")
20 */ 20 */
21 public function authAction() 21 public function authAction()
22 { 22 {
23 $pocket = $this->get('wallabag_import.pocket.import'); 23 $requestToken = $this->get('wallabag_import.pocket.import')
24 $authUrl = $pocket->oAuthRequest( 24 ->getRequestToken($this->generateUrl('import', [], true));
25 $this->generateUrl('import', array(), true), 25
26 $this->generateUrl('pocket_callback', array(), true) 26 $this->get('session')->set('import.pocket.code', $requestToken);
27 );
28 27
29 return $this->redirect($authUrl, 301); 28 return $this->redirect(
29 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], true),
30 301
31 );
30 } 32 }
31 33
32 /** 34 /**
33 * @Route("/import/pocket/callback", name="pocket_callback") 35 * @Route("/import/pocket/callback", name="import_pocket_callback")
34 */ 36 */
35 public function callbackAction() 37 public function callbackAction()
36 { 38 {
39 $message = 'Import failed, please try again.';
37 $pocket = $this->get('wallabag_import.pocket.import'); 40 $pocket = $this->get('wallabag_import.pocket.import');
38 $accessToken = $pocket->oAuthAuthorize(); 41
39 $pocket->import($accessToken); 42 // something bad happend on pocket side
43 if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) {
44 $this->get('session')->getFlashBag()->add(
45 'notice',
46 $message
47 );
48
49 return $this->redirect($this->generateUrl('import_pocket'));
50 }
51
52 if (true === $pocket->import()) {
53 $summary = $pocket->getSummary();
54 $message = $summary['imported'].' entrie(s) imported, '.$summary['skipped'].' already saved.';
55 }
56
57 $this->get('session')->getFlashBag()->add(
58 'notice',
59 $message
60 );
40 61
41 return $this->redirect($this->generateUrl('homepage')); 62 return $this->redirect($this->generateUrl('homepage'));
42 } 63 }