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