wallabag/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php

66 lines
2.3 KiB
PHP
Raw Normal View History

2015-12-30 12:26:30 +00:00
<?php
namespace Wallabag\ImportBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\ImportBundle\Form\Type\UploadImportType;
class WallabagV1Controller extends Controller
{
/**
* @Route("/wallabag-v1", name="import_wallabag_v1")
2015-12-30 12:26:30 +00:00
*/
public function indexAction(Request $request)
{
$form = $this->createForm(UploadImportType::class);
$form->handleRequest($request);
2015-12-30 12:26:30 +00:00
$wallabag = $this->get('wallabag_import.wallabag_v1.import');
if ($form->isValid()) {
$file = $form->get('file')->getData();
2016-02-12 14:59:13 +00:00
$markAsRead = $form->get('mark_as_read')->getData();
$name = $this->getUser()->getId().'.json';
2015-12-30 12:26:30 +00:00
if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
$res = $wallabag
->setUser($this->getUser())
->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
2016-02-12 14:59:13 +00:00
->setMarkAsRead($markAsRead)
2015-12-30 12:26:30 +00:00
->import();
2016-03-11 13:48:46 +00:00
$message = 'flashes.import.notice.failed';
2015-12-30 12:26:30 +00:00
if (true === $res) {
$summary = $wallabag->getSummary();
2016-03-11 13:48:46 +00:00
$message = $this->get('translator')->trans('flashes.import.notice.summary', array(
'%imported%' => $summary['imported'],
'%skipped%' => $summary['skipped'],
));
2015-12-30 12:26:30 +00:00
unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
2015-12-30 12:26:30 +00:00
}
$this->get('session')->getFlashBag()->add(
'notice',
$message
);
return $this->redirect($this->generateUrl('homepage'));
} else {
$this->get('session')->getFlashBag()->add(
'notice',
2016-03-11 13:48:46 +00:00
'flashes.import.notice.failed_on_file'
2015-12-30 12:26:30 +00:00
);
}
}
return $this->render('WallabagImportBundle:WallabagV1:index.html.twig', [
'form' => $form->createView(),
'import' => $wallabag,
2015-12-30 12:26:30 +00:00
]);
}
}