]>
Commit | Line | Data |
---|---|---|
1f4408de NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
1d76102a | 6 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
1f4408de | 7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
c10fcb3b TC |
8 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | |
10 | ||
1f4408de NL |
11 | class PocketController extends Controller |
12 | { | |
13 | /** | |
7019c7cf | 14 | * @Route("/pocket", name="import_pocket") |
1f4408de | 15 | */ |
d51b38ed | 16 | public function indexAction() |
1f4408de | 17 | { |
c10fcb3b TC |
18 | $pocket = $this->get('wallabag_import.pocket.import'); |
19 | $form = $this->createFormBuilder($pocket) | |
4094ea47 | 20 | ->add('mark_as_read', CheckboxType::class, [ |
0d42217e | 21 | 'label' => 'import.form.mark_as_read_label', |
79d0e38e | 22 | 'required' => false, |
4094ea47 | 23 | ]) |
c10fcb3b | 24 | ->getForm(); |
c10fcb3b | 25 | |
7019c7cf JB |
26 | return $this->render('WallabagImportBundle:Pocket:index.html.twig', [ |
27 | 'import' => $this->get('wallabag_import.pocket.import'), | |
1e3ed714 | 28 | 'has_consumer_key' => '' == trim($this->get('craue_config')->get('pocket_consumer_key')) ? false : true, |
c10fcb3b | 29 | 'form' => $form->createView(), |
7019c7cf | 30 | ]); |
1f4408de NL |
31 | } |
32 | ||
1f4408de | 33 | /** |
7019c7cf | 34 | * @Route("/pocket/auth", name="import_pocket_auth") |
1f4408de | 35 | */ |
c10fcb3b | 36 | public function authAction(Request $request) |
1f4408de | 37 | { |
252ebd60 | 38 | $requestToken = $this->get('wallabag_import.pocket.import') |
4094ea47 | 39 | ->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL)); |
252ebd60 | 40 | |
4d0ec0e7 JB |
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 | ||
252ebd60 | 50 | $this->get('session')->set('import.pocket.code', $requestToken); |
0d42217e | 51 | $this->get('session')->set('mark_as_read', $request->request->get('form')['mark_as_read']); |
1f4408de | 52 | |
252ebd60 | 53 | return $this->redirect( |
4094ea47 | 54 | 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL), |
252ebd60 JB |
55 | 301 |
56 | ); | |
1f4408de NL |
57 | } |
58 | ||
59 | /** | |
7019c7cf | 60 | * @Route("/pocket/callback", name="import_pocket_callback") |
1f4408de NL |
61 | */ |
62 | public function callbackAction() | |
63 | { | |
4204a06b | 64 | $message = 'flashes.import.notice.failed'; |
0aa344dc | 65 | $pocket = $this->get('wallabag_import.pocket.import'); |
4204a06b | 66 | |
0d42217e JB |
67 | $markAsRead = $this->get('session')->get('mark_as_read'); |
68 | $this->get('session')->remove('mark_as_read'); | |
252ebd60 JB |
69 | |
70 | // something bad happend on pocket side | |
71 | if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { | |
72 | $this->get('session')->getFlashBag()->add( | |
73 | 'notice', | |
74 | $message | |
75 | ); | |
76 | ||
77 | return $this->redirect($this->generateUrl('import_pocket')); | |
78 | } | |
79 | ||
c10fcb3b | 80 | if (true === $pocket->setMarkAsRead($markAsRead)->import()) { |
252ebd60 | 81 | $summary = $pocket->getSummary(); |
4094ea47 | 82 | $message = $this->get('translator')->trans('flashes.import.notice.summary', [ |
4204a06b JB |
83 | '%imported%' => $summary['imported'], |
84 | '%skipped%' => $summary['skipped'], | |
4094ea47 | 85 | ]); |
252ebd60 JB |
86 | } |
87 | ||
88 | $this->get('session')->getFlashBag()->add( | |
89 | 'notice', | |
90 | $message | |
91 | ); | |
1f4408de NL |
92 | |
93 | return $this->redirect($this->generateUrl('homepage')); | |
94 | } | |
1f4408de | 95 | } |