2015-10-15 18:06:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2016-10-27 07:41:19 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 14:59:43 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2015-10-16 08:51:53 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-10-04 12:07:20 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2015-10-15 18:06:59 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2022-04-24 15:48:59 +00:00
|
|
|
use Wallabag\CoreBundle\Helper\EntriesExport;
|
2022-04-24 15:58:57 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
|
|
|
use Wallabag\CoreBundle\Repository\TagRepository;
|
2015-10-15 18:06:59 +00:00
|
|
|
|
2015-10-30 19:57:10 +00:00
|
|
|
/**
|
|
|
|
* The try/catch can be removed once all formats will be implemented.
|
|
|
|
* Still need implementation: txt.
|
|
|
|
*/
|
2015-10-15 18:06:59 +00:00
|
|
|
class ExportController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2015-10-16 08:51:53 +00:00
|
|
|
* Gets one entry content.
|
2015-10-15 18:06:59 +00:00
|
|
|
*
|
2015-10-30 19:57:10 +00:00
|
|
|
* @Route("/export/{id}.{format}", name="export_entry", requirements={
|
|
|
|
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
|
|
|
* "id": "\d+"
|
|
|
|
* })
|
2016-04-12 09:36:01 +00:00
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return Response
|
2015-10-15 18:06:59 +00:00
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function downloadEntryAction(Entry $entry, EntriesExport $entriesExport, string $format)
|
2015-10-15 18:06:59 +00:00
|
|
|
{
|
2015-10-16 08:51:53 +00:00
|
|
|
try {
|
2022-12-19 09:37:22 +00:00
|
|
|
return $entriesExport
|
2015-10-16 08:51:53 +00:00
|
|
|
->setEntries($entry)
|
|
|
|
->updateTitle('entry')
|
2017-07-08 15:55:58 +00:00
|
|
|
->updateAuthor('entry')
|
2015-10-16 08:51:53 +00:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
2015-10-15 18:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-16 08:51:53 +00:00
|
|
|
* Export all entries for current user.
|
2015-10-15 18:06:59 +00:00
|
|
|
*
|
2015-10-16 08:51:53 +00:00
|
|
|
* @Route("/export/{category}.{format}", name="export_entries", requirements={
|
2015-10-30 19:57:10 +00:00
|
|
|
* "format": "epub|mobi|pdf|json|xml|txt|csv",
|
2020-04-26 12:09:16 +00:00
|
|
|
* "category": "all|unread|starred|archive|tag_entries|untagged|search|annotated|same_domain"
|
2015-10-16 08:51:53 +00:00
|
|
|
* })
|
2016-04-12 09:36:01 +00:00
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return Response
|
2015-10-15 18:06:59 +00:00
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category)
|
2015-10-15 18:06:59 +00:00
|
|
|
{
|
2015-10-16 08:51:53 +00:00
|
|
|
$method = ucfirst($category);
|
2017-07-01 07:52:38 +00:00
|
|
|
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
|
2019-01-06 19:17:35 +00:00
|
|
|
$title = $method;
|
2016-10-27 07:41:19 +00:00
|
|
|
|
2017-10-09 14:47:15 +00:00
|
|
|
if ('tag_entries' === $category) {
|
2022-12-19 09:37:22 +00:00
|
|
|
$tag = $tagRepository->findOneBySlug($request->query->get('tag'));
|
2016-10-27 07:41:19 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entries = $entryRepository->findAllByTagId(
|
2017-06-10 10:33:58 +00:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$tag->getId()
|
|
|
|
);
|
2019-01-06 19:17:35 +00:00
|
|
|
|
|
|
|
$title = 'Tag ' . $tag->getLabel();
|
2020-02-04 21:19:45 +00:00
|
|
|
} elseif ('search' === $category) {
|
|
|
|
$searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : '');
|
|
|
|
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entries = $entryRepository->getBuilderForSearchByUser(
|
2020-02-04 21:19:45 +00:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$searchTerm,
|
|
|
|
$currentRoute
|
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'Search ' . $searchTerm;
|
2020-04-26 12:09:16 +00:00
|
|
|
} elseif ('annotated' === $category) {
|
2022-12-19 09:37:22 +00:00
|
|
|
$entries = $entryRepository->getBuilderForAnnotationsByUser(
|
2020-04-20 17:00:58 +00:00
|
|
|
$this->getUser()->getId()
|
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'With annotations';
|
2016-10-27 07:41:19 +00:00
|
|
|
} else {
|
2022-12-19 09:37:22 +00:00
|
|
|
$entries = $entryRepository
|
2016-10-27 07:41:19 +00:00
|
|
|
->$methodBuilder($this->getUser()->getId())
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
}
|
2015-10-16 08:51:53 +00:00
|
|
|
|
|
|
|
try {
|
2022-12-19 09:37:22 +00:00
|
|
|
return $entriesExport
|
2015-10-16 08:51:53 +00:00
|
|
|
->setEntries($entries)
|
2019-01-06 19:17:35 +00:00
|
|
|
->updateTitle($title)
|
2017-07-08 15:55:58 +00:00
|
|
|
->updateAuthor($method)
|
2015-10-16 08:51:53 +00:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
|
|
|
}
|
2015-10-15 18:06:59 +00:00
|
|
|
}
|
|
|
|
}
|