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