]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Controller/WallabagController.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Controller / WallabagController.php
CommitLineData
b787a775
JB
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Symfony\Component\HttpFoundation\Request;
7use Wallabag\ImportBundle\Form\Type\UploadImportType;
8
9/**
10 * Define Wallabag import for v1 and v2, since there are very similar.
11 */
12abstract class WallabagController extends Controller
13{
b787a775
JB
14 /**
15 * Handle import request.
16 *
17 * @param Request $request
18 *
19 * @return Response|RedirectResponse
20 */
21 public function indexAction(Request $request)
22 {
23 $form = $this->createForm(UploadImportType::class);
24 $form->handleRequest($request);
25
26 $wallabag = $this->getImportService();
c98db1b6 27 $wallabag->setUser($this->getUser());
b787a775 28
21e7ccef 29 if ($form->isSubmitted() && $form->isValid()) {
b787a775
JB
30 $file = $form->get('file')->getData();
31 $markAsRead = $form->get('mark_as_read')->getData();
f808b016 32 $name = $this->getUser()->getId() . '.json';
b787a775 33
f808b016 34 if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes'), true) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
b787a775 35 $res = $wallabag
f808b016 36 ->setFilepath($this->getParameter('wallabag_import.resource_dir') . '/' . $name)
b787a775
JB
37 ->setMarkAsRead($markAsRead)
38 ->import();
39
40 $message = 'flashes.import.notice.failed';
41
42 if (true === $res) {
43 $summary = $wallabag->getSummary();
4094ea47 44 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
b787a775
JB
45 '%imported%' => $summary['imported'],
46 '%skipped%' => $summary['skipped'],
4094ea47 47 ]);
b787a775 48
c80cc01a
JB
49 if (0 < $summary['queued']) {
50 $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [
51 '%queued%' => $summary['queued'],
52 ]);
53 }
54
f808b016 55 unlink($this->getParameter('wallabag_import.resource_dir') . '/' . $name);
b787a775
JB
56 }
57
58 $this->get('session')->getFlashBag()->add(
59 'notice',
60 $message
61 );
62
63 return $this->redirect($this->generateUrl('homepage'));
b787a775 64 }
f808b016
JB
65
66 $this->get('session')->getFlashBag()->add(
67 'notice',
68 'flashes.import.notice.failed_on_file'
69 );
b787a775
JB
70 }
71
72 return $this->render($this->getImportTemplate(), [
73 'form' => $form->createView(),
74 'import' => $wallabag,
75 ]);
76 }
f808b016
JB
77
78 /**
79 * Return the service to handle the import.
80 *
81 * @return \Wallabag\ImportBundle\Import\ImportInterface
82 */
83 abstract protected function getImportService();
84
85 /**
86 * Return the template used for the form.
87 *
88 * @return string
89 */
90 abstract protected function getImportTemplate();
b787a775 91}