]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Controller/PocketController.php
french translation & pocket
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / PocketController.php
CommitLineData
1f4408de
NL
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1d76102a 6use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1f4408de 7use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
c10fcb3b
TC
8use Wallabag\ImportBundle\Import\PocketImport;
9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
11
1f4408de
NL
12
13class PocketController extends Controller
14{
15 /**
7019c7cf 16 * @Route("/pocket", name="import_pocket")
1f4408de 17 */
d51b38ed 18 public function indexAction()
1f4408de 19 {
c10fcb3b
TC
20 $pocket = $this->get('wallabag_import.pocket.import');
21 $form = $this->createFormBuilder($pocket)
22 ->add('read', CheckboxType::class, array(
23 'label' => 'Mark all as read',
24 'required' => false,
25 ))
26 ->getForm();
27 ;
28
7019c7cf
JB
29 return $this->render('WallabagImportBundle:Pocket:index.html.twig', [
30 'import' => $this->get('wallabag_import.pocket.import'),
1e3ed714 31 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? false : true,
c10fcb3b 32 'form' => $form->createView(),
7019c7cf 33 ]);
1f4408de
NL
34 }
35
1f4408de 36 /**
7019c7cf 37 * @Route("/pocket/auth", name="import_pocket_auth")
1f4408de 38 */
c10fcb3b 39 public function authAction(Request $request)
1f4408de 40 {
252ebd60 41 $requestToken = $this->get('wallabag_import.pocket.import')
27e475a9 42 ->getRequestToken($this->generateUrl('import', array(), UrlGeneratorInterface::ABSOLUTE_URL));
252ebd60
JB
43
44 $this->get('session')->set('import.pocket.code', $requestToken);
c10fcb3b
TC
45 $markAsRead = $request->request->get('form')['read'];
46 $this->get('session')->set('read', $markAsRead);
1f4408de 47
252ebd60 48 return $this->redirect(
27e475a9 49 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL),
252ebd60
JB
50 301
51 );
1f4408de
NL
52 }
53
54 /**
7019c7cf 55 * @Route("/pocket/callback", name="import_pocket_callback")
1f4408de
NL
56 */
57 public function callbackAction()
58 {
252ebd60 59 $message = 'Import failed, please try again.';
0aa344dc 60 $pocket = $this->get('wallabag_import.pocket.import');
c10fcb3b 61 $markAsRead = $this->get('session')->get('read');
252ebd60
JB
62
63 // something bad happend on pocket side
64 if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) {
65 $this->get('session')->getFlashBag()->add(
66 'notice',
67 $message
68 );
69
70 return $this->redirect($this->generateUrl('import_pocket'));
71 }
72
c10fcb3b 73 if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
252ebd60 74 $summary = $pocket->getSummary();
b1d05721 75 $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.';
252ebd60
JB
76 }
77
c10fcb3b
TC
78 $this->get('session')->remove('read');
79
252ebd60
JB
80 $this->get('session')->getFlashBag()->add(
81 'notice',
82 $message
83 );
1f4408de
NL
84
85 return $this->redirect($this->generateUrl('homepage'));
86 }
1f4408de 87}