1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00
wallabag/src/Controller/ExportController.php

116 lines
4 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Controller;
2025-03-10 23:17:43 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
2022-08-28 16:59:43 +02:00
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
2025-03-10 23:17:43 +01:00
use Wallabag\Entity\Entry;
2024-02-19 01:30:12 +01:00
use Wallabag\Helper\EntriesExport;
use Wallabag\Repository\EntryRepository;
use Wallabag\Repository\TagRepository;
/**
* The try/catch can be removed once all formats will be implemented.
* Still need implementation: txt.
*/
class ExportController extends AbstractController
{
/**
* Gets one entry content.
*
2025-03-10 23:17:43 +01:00
* @Route("/export/{entry}.{format}", name="export_entry", methods={"GET"}, requirements={
2024-11-12 15:21:23 +01:00
* "format": "epub|pdf|json|xml|txt|csv|md",
2025-03-10 23:17:43 +01:00
* "entry": "\d+"
* })
2025-03-10 23:17:43 +01:00
* @IsGranted("EXPORT", subject="entry")
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-03-10 23:17:43 +01:00
public function downloadEntryAction(Request $request, EntryRepository $entryRepository, EntriesExport $entriesExport, string $format, Entry $entry)
{
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.
*
2025-03-10 01:15:45 +01:00
* @Route("/export/{category}.{format}", name="export_entries", methods={"GET"}, requirements={
2024-11-12 15:21:23 +01:00
* "format": "epub|pdf|json|xml|txt|csv|md",
2020-04-26 14:09:16 +02:00
* "category": "all|unread|starred|archive|tag_entries|untagged|search|annotated|same_domain"
* })
2025-03-10 23:17:43 +01:00
* @IsGranted("EXPORT_ENTRIES")
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2023-06-29 17:10:23 +02:00
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category, int $entry = 0)
{
$method = ucfirst($category);
2017-07-01 09:52:38 +02:00
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
$title = $method;
2023-06-29 17:10:23 +02:00
if ('same_domain' === $category) {
$entries = $entryRepository->getBuilderForSameDomainByUser(
$this->getUser()->getId(),
2025-01-19 02:06:54 +01:00
$request->query->get('entry')
2023-06-29 17:10:23 +02:00
)->getQuery()
->getResult();
$title = 'Same domain';
} elseif ('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) {
2025-01-19 02:06:54 +01:00
$searchTerm = (isset($request->query->get('search_entry')['term']) ? $request->query->get('search_entry')['term'] : '');
$currentRoute = (null !== $request->query->get('currentRoute') ? $request->query->get('currentRoute') : '');
$entries = $entryRepository->getBuilderForSearchByUser(
2024-01-01 19:11:01 +01:00
$this->getUser()->getId(),
$searchTerm,
$currentRoute
)->getQuery()
->getResult();
$title = 'Search ' . $searchTerm;
2020-04-26 14:09:16 +02: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());
}
}
}