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