wallabag/src/Wallabag/CoreBundle/Controller/ExportController.php

106 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\CoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
2022-08-28 14:59:43 +00:00
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Helper\EntriesExport;
use Wallabag\CoreBundle\Repository\EntryRepository;
use Wallabag\CoreBundle\Repository\TagRepository;
/**
* The try/catch can be removed once all formats will be implemented.
* Still need implementation: txt.
*/
class ExportController extends Controller
{
/**
* Gets one entry content.
*
* @Route("/export/{id}.{format}", name="export_entry", requirements={
* "format": "epub|mobi|pdf|json|xml|txt|csv",
* "id": "\d+"
* })
*
2022-08-28 14:59:43 +00:00
* @return Response
*/
public function downloadEntryAction(Entry $entry, EntriesExport $entriesExport, string $format)
{
try {
return $entriesExport
->setEntries($entry)
->updateTitle('entry')
->updateAuthor('entry')
->exportAs($format);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException($e->getMessage());
}
}
/**
* Export all entries for current user.
*
* @Route("/export/{category}.{format}", name="export_entries", requirements={
* "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"
* })
*
2022-08-28 14:59:43 +00:00
* @return Response
*/
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category)
{
$method = ucfirst($category);
2017-07-01 07:52:38 +00:00
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
$title = $method;
2017-10-09 14:47:15 +00:00
if ('tag_entries' === $category) {
$tag = $tagRepository->findOneBySlug($request->query->get('tag'));
$entries = $entryRepository->findAllByTagId(
$this->getUser()->getId(),
$tag->getId()
);
$title = 'Tag ' . $tag->getLabel();
} 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') : '');
$entries = $entryRepository->getBuilderForSearchByUser(
$this->getUser()->getId(),
$searchTerm,
$currentRoute
)->getQuery()
->getResult();
$title = 'Search ' . $searchTerm;
2020-04-26 12:09:16 +00:00
} elseif ('annotated' === $category) {
$entries = $entryRepository->getBuilderForAnnotationsByUser(
$this->getUser()->getId()
)->getQuery()
->getResult();
$title = 'With annotations';
} else {
$entries = $entryRepository
->$methodBuilder($this->getUser()->getId())
->getQuery()
->getResult();
}
try {
return $entriesExport
->setEntries($entries)
->updateTitle($title)
->updateAuthor($method)
->exportAs($format);
} catch (\InvalidArgumentException $e) {
throw new NotFoundHttpException($e->getMessage());
}
}
}