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

738 lines
25 KiB
PHP
Raw Normal View History

2015-01-22 17:18:56 +01:00
<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Controller;
2015-01-22 17:18:56 +01:00
2022-08-28 02:01:46 +02:00
use Craue\ConfigBundle\Util\Config;
use Doctrine\ORM\EntityManagerInterface;
2017-12-22 15:44:00 +01:00
use Doctrine\ORM\NoResultException;
2020-07-29 06:36:43 +02:00
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
2017-07-01 09:52:38 +02:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
2024-03-23 15:34:02 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Spiriit\Bundle\FormFilterBundle\Filter\FilterBuilderUpdaterInterface;
2022-08-28 02:01:46 +02:00
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2022-08-28 16:59:43 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
2015-01-23 12:45:24 +01:00
use Symfony\Component\HttpFoundation\Request;
2022-08-28 16:59:43 +02:00
use Symfony\Component\HttpFoundation\Response;
2025-03-19 01:31:35 +01:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
2024-03-23 15:34:02 +01:00
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
2024-02-19 01:30:12 +01:00
use Wallabag\Entity\Entry;
use Wallabag\Entity\Tag;
use Wallabag\Event\EntryDeletedEvent;
use Wallabag\Event\EntrySavedEvent;
use Wallabag\Form\Type\EditEntryType;
use Wallabag\Form\Type\EntryFilterType;
use Wallabag\Form\Type\NewEntryType;
use Wallabag\Form\Type\SearchEntryType;
use Wallabag\Helper\ContentProxy;
use Wallabag\Helper\PreparePagerForEntries;
use Wallabag\Helper\Redirect;
use Wallabag\Repository\EntryRepository;
use Wallabag\Repository\TagRepository;
2015-01-22 17:18:56 +01:00
class EntryController extends AbstractController
2015-01-22 17:18:56 +01:00
{
public function __construct(
2025-04-05 13:59:36 +02:00
private readonly EntityManagerInterface $entityManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly EntryRepository $entryRepository,
private readonly Redirect $redirectHelper,
private readonly PreparePagerForEntries $preparePagerForEntriesHelper,
private readonly FilterBuilderUpdaterInterface $filterBuilderUpdater,
private readonly ContentProxy $contentProxy,
private readonly Security $security,
) {
}
/**
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/mass', name: 'mass_action', methods: ['POST'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('EDIT_ENTRIES')]
public function massAction(Request $request, TagRepository $tagRepository)
{
2025-03-30 05:58:54 +02:00
if (!$this->isCsrfTokenValid('mass-action', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
$values = $request->request->all();
$tagsToAdd = [];
$tagsToRemove = [];
$action = 'toggle-read';
if (isset($values['toggle-star'])) {
$action = 'toggle-star';
} elseif (isset($values['delete'])) {
$action = 'delete';
} elseif (isset($values['tag'])) {
$action = 'tag';
if (isset($values['tags'])) {
2025-04-05 14:01:48 +02:00
$labels = array_filter(explode(',', (string) $values['tags']),
function ($v) {
$v = trim($v);
return '' !== $v;
});
foreach ($labels as $label) {
$remove = false;
2024-01-01 19:11:01 +01:00
if (str_starts_with($label, '-')) {
$label = substr($label, 1);
$remove = true;
}
$tag = $tagRepository->findOneByLabel($label);
if ($remove) {
if (null !== $tag) {
$tagsToRemove[] = $tag;
}
} else {
if (null === $tag) {
$tag = new Tag();
$tag->setLabel($label);
}
$tagsToAdd[] = $tag;
}
}
}
}
if (isset($values['entry-checkbox'])) {
foreach ($values['entry-checkbox'] as $id) {
/** @var Entry * */
2025-04-21 16:27:44 +02:00
$entry = $this->entryRepository->findById([(int) $id])[0];
2024-03-23 15:34:02 +01:00
if (!$this->security->isGranted('EDIT', $entry)) {
throw $this->createAccessDeniedException('You can not access this entry.');
}
if ('toggle-read' === $action) {
$entry->toggleArchive();
} elseif ('toggle-star' === $action) {
$entry->toggleStar();
} elseif ('tag' === $action) {
foreach ($tagsToAdd as $tag) {
$entry->addTag($tag);
}
foreach ($tagsToRemove as $tag) {
$entry->removeTag($tag);
}
} elseif ('delete' === $action) {
$this->eventDispatcher->dispatch(new EntryDeletedEvent($entry), EntryDeletedEvent::NAME);
$this->entityManager->remove($entry);
}
}
$this->entityManager->flush();
}
2023-12-25 21:42:08 +01:00
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'));
return $this->redirect($redirectUrl);
}
2016-11-04 23:24:43 +01:00
/**
* @param int $page
2016-11-04 23:24:43 +01:00
*
2016-12-14 11:54:30 +01:00
* Default parameter for page is hardcoded (in duplication of the defaults from the Route)
* because this controller is also called inside the layout template without any page as argument
2022-08-28 16:59:43 +02:00
* @return Response
2016-11-04 23:24:43 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/search/{page}', name: 'search', methods: ['GET', 'POST'], defaults: ['page' => 1])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
2016-12-14 11:54:30 +01:00
public function searchFormAction(Request $request, $page = 1, $currentRoute = null)
2016-11-04 23:24:43 +01:00
{
2016-12-14 11:54:30 +01:00
// fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template)
if (null === $currentRoute && $request->query->has('currentRoute')) {
$currentRoute = $request->query->get('currentRoute');
}
2016-11-04 23:24:43 +01:00
$form = $this->createForm(SearchEntryType::class);
$form->handleRequest($request);
2016-12-14 11:54:30 +01:00
if ($form->isSubmitted() && $form->isValid()) {
2016-11-04 23:24:43 +01:00
return $this->showEntries('search', $request, $page);
}
2024-02-19 00:03:14 +01:00
return $this->render('Entry/search_form.html.twig', [
2016-11-04 23:24:43 +01:00
'form' => $form->createView(),
'currentRoute' => $currentRoute,
2016-11-04 23:24:43 +01:00
]);
}
2015-01-23 14:58:17 +01:00
/**
2022-08-28 16:59:43 +02:00
* @return Response
2015-01-23 14:58:17 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/new-entry', name: 'new_entry', methods: ['GET', 'POST'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('CREATE_ENTRIES')]
public function addEntryFormAction(Request $request, TranslatorInterface $translator)
2015-01-23 14:58:17 +01:00
{
$entry = new Entry($this->getUser());
2015-01-23 14:58:17 +01:00
$form = $this->createForm(NewEntryType::class, $entry);
2015-01-23 14:58:17 +01:00
$form->handleRequest($request);
2016-12-14 11:54:30 +01:00
if ($form->isSubmitted() && $form->isValid()) {
2016-04-09 13:44:54 +02:00
$existingEntry = $this->checkIfEntryAlreadyExists($entry);
if (false !== $existingEntry) {
$this->addFlash(
'notice',
$translator->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
);
return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
}
$this->updateEntry($entry);
$this->entityManager->persist($entry);
$this->entityManager->flush();
2015-01-23 14:58:17 +01:00
// entry saved, dispatch event about it!
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
2023-08-29 14:17:53 +02:00
return $this->redirect($this->generateUrl('homepage'));
} elseif ($form->isSubmitted() && !$form->isValid()) {
2015-01-23 14:58:17 +01:00
return $this->redirect($this->generateUrl('homepage'));
}
2024-02-19 00:03:14 +01:00
return $this->render('Entry/new_form.html.twig', [
2015-01-23 14:58:17 +01:00
'form' => $form->createView(),
]);
2015-01-23 14:58:17 +01:00
}
2015-10-07 18:08:51 +02:00
/**
2022-08-28 16:59:43 +02:00
* @return Response
2015-10-07 18:08:51 +02:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/bookmarklet', name: 'bookmarklet', methods: ['GET'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('CREATE_ENTRIES')]
2016-01-20 17:16:17 +01:00
public function addEntryViaBookmarkletAction(Request $request)
2015-10-07 18:08:51 +02:00
{
$entry = new Entry($this->getUser());
2025-01-19 02:06:54 +01:00
$entry->setUrl($request->query->get('url'));
2016-04-09 13:44:54 +02:00
if (false === $this->checkIfEntryAlreadyExists($entry)) {
$this->updateEntry($entry);
$this->entityManager->persist($entry);
$this->entityManager->flush();
// entry saved, dispatch event about it!
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
}
2015-10-07 18:08:51 +02:00
return $this->redirect($this->generateUrl('homepage'));
}
2015-08-12 08:22:30 +02:00
/**
2022-08-28 16:59:43 +02:00
* @return Response
2015-08-12 08:22:30 +02:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/new', name: 'new', methods: ['GET'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('CREATE_ENTRIES')]
2016-03-27 20:35:56 +02:00
public function addEntryAction()
2015-08-12 08:22:30 +02:00
{
2024-02-19 00:03:14 +01:00
return $this->render('Entry/new.html.twig');
2015-08-12 08:22:30 +02:00
}
/**
* Edit an entry content.
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/edit/{id}', name: 'edit', methods: ['GET', 'POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('EDIT', subject: 'entry')]
public function editEntryAction(Request $request, Entry $entry)
{
$form = $this->createForm(EditEntryType::class, $entry);
$form->handleRequest($request);
2016-12-14 11:54:30 +01:00
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($entry);
$this->entityManager->flush();
$this->addFlash(
'notice',
2016-03-11 14:48:46 +01:00
'flashes.entry.notice.entry_updated'
);
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
}
2024-02-19 00:03:14 +01:00
return $this->render('Entry/edit.html.twig', [
'form' => $form->createView(),
]);
}
/**
* Shows all entries for current user.
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/all/list/{page}', name: 'all', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function showAllAction(Request $request, $page)
{
return $this->showEntries('all', $request, $page);
}
2015-01-22 17:18:56 +01:00
/**
2015-05-30 13:52:26 +02:00
* Shows unread entries for current user.
2015-01-23 12:45:24 +01:00
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
2015-01-22 17:18:56 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/unread/list/{page}', name: 'unread', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function showUnreadAction(Request $request, $page)
2015-01-22 17:18:56 +01:00
{
2016-01-09 14:34:49 +01:00
// load the quickstart if no entry in database
if (1 === (int) $page && 0 === $this->entryRepository->countAllEntriesByUser($this->getUser()->getId())) {
2016-01-09 14:34:49 +01:00
return $this->redirect($this->generateUrl('quickstart'));
}
return $this->showEntries('unread', $request, $page);
2015-01-22 17:18:56 +01:00
}
2015-01-22 21:11:22 +01:00
/**
2015-05-30 13:52:26 +02:00
* Shows read entries for current user.
2015-01-23 12:45:24 +01:00
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
2015-01-22 21:11:22 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/archive/list/{page}', name: 'archive', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function showArchiveAction(Request $request, $page)
2015-01-22 21:11:22 +01:00
{
return $this->showEntries('archive', $request, $page);
}
/**
* Shows starred entries for current user.
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/starred/list/{page}', name: 'starred', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function showStarredAction(Request $request, $page)
{
return $this->showEntries('starred', $request, $page);
}
2018-10-12 15:01:19 +02:00
/**
* Shows untagged articles for current user.
*
* @param int $page
2018-10-12 15:01:19 +02:00
*
2022-08-28 16:59:43 +02:00
* @return Response
2018-10-12 15:01:19 +02:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/untagged/list/{page}', name: 'untagged', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
2018-10-12 15:01:19 +02:00
public function showUntaggedEntriesAction(Request $request, $page)
{
return $this->showEntries('untagged', $request, $page);
}
/**
* Shows entries with annotations for current user.
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/annotated/list/{page}', name: 'annotated', methods: ['GET'], defaults: ['page' => '1'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function showWithAnnotationsEntriesAction(Request $request, $page)
{
2020-04-26 14:09:16 +02:00
return $this->showEntries('annotated', $request, $page);
}
2017-12-22 15:44:00 +01:00
/**
* Shows random entry depending on the given type.
2017-12-22 15:44:00 +01:00
*
2022-08-28 16:59:43 +02:00
* @return RedirectResponse
2017-12-22 15:44:00 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/{type}/random', name: 'random_entry', methods: ['GET'], requirements: ['type' => 'unread|starred|archive|untagged|annotated|all'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function redirectRandomEntryAction(string $type = 'all')
2017-12-22 15:44:00 +01:00
{
try {
$entry = $this->entryRepository
->getRandomEntry($this->getUser()->getId(), $type);
2025-04-05 13:56:56 +02:00
} catch (NoResultException) {
$this->addFlash('notice', 'flashes.entry.notice.no_random_entry');
2017-12-22 15:44:00 +01:00
return $this->redirect($this->generateUrl($type));
}
2017-12-22 15:44:00 +01:00
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
2017-12-22 15:44:00 +01:00
}
2015-01-22 21:11:22 +01:00
/**
2015-05-30 13:52:26 +02:00
* Shows entry content.
2015-01-23 12:45:24 +01:00
*
2022-08-28 16:59:43 +02:00
* @return Response
2015-01-22 21:11:22 +01:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/view/{id}', name: 'view', methods: ['GET'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('VIEW', subject: 'entry')]
2015-02-06 07:45:32 +01:00
public function viewAction(Entry $entry)
2015-01-22 21:11:22 +01:00
{
return $this->render(
2024-02-19 00:03:14 +01:00
'Entry/entry.html.twig',
['entry' => $entry]
2015-01-22 21:11:22 +01:00
);
2015-01-23 12:45:24 +01:00
}
/**
* Reload an entry.
* Refetch content from the website and make it readable again.
*
2022-08-28 16:59:43 +02:00
* @return RedirectResponse
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/reload/{id}', name: 'reload_entry', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('RELOAD', subject: 'entry')]
2025-03-19 01:31:35 +01:00
public function reloadAction(Request $request, Entry $entry)
{
2025-03-19 01:31:35 +01:00
if (!$this->isCsrfTokenValid('reload-entry', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
$this->updateEntry($entry, 'entry_reloaded');
// if refreshing entry failed, don't save it
2024-02-20 00:47:53 +01:00
if ($this->getParameter('wallabag.fetching_error_message') === $entry->getContent()) {
$this->addFlash('notice', 'flashes.entry.notice.entry_reloaded_failed');
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
}
$this->entityManager->persist($entry);
$this->entityManager->flush();
// entry saved, dispatch event about it!
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
}
2015-01-23 12:45:24 +01:00
/**
2015-05-30 13:52:26 +02:00
* Changes read status for an entry.
2015-01-23 12:45:24 +01:00
*
2022-08-28 16:59:43 +02:00
* @return RedirectResponse
2015-01-23 12:45:24 +01:00
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/archive/{id}', name: 'archive_entry', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('ARCHIVE', subject: 'entry')]
2015-02-06 07:45:32 +01:00
public function toggleArchiveAction(Request $request, Entry $entry)
2015-01-23 12:45:24 +01:00
{
if (!$this->isCsrfTokenValid('archive-entry', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
2015-01-23 12:45:24 +01:00
$entry->toggleArchive();
$this->entityManager->flush();
2015-01-23 12:45:24 +01:00
2016-03-11 14:48:46 +01:00
$message = 'flashes.entry.notice.entry_unarchived';
if ($entry->isArchived()) {
$message = 'flashes.entry.notice.entry_archived';
}
$this->addFlash(
2015-01-23 12:45:24 +01:00
'notice',
2016-03-11 14:48:46 +01:00
$message
2015-01-23 12:45:24 +01:00
);
2023-12-25 21:42:08 +01:00
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'));
return $this->redirect($redirectUrl);
2015-01-23 12:45:24 +01:00
}
/**
2016-08-18 15:30:28 +02:00
* Changes starred status for an entry.
2015-01-23 12:45:24 +01:00
*
2022-08-28 16:59:43 +02:00
* @return RedirectResponse
2015-01-23 12:45:24 +01:00
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/star/{id}', name: 'star_entry', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('STAR', subject: 'entry')]
2015-02-06 07:45:32 +01:00
public function toggleStarAction(Request $request, Entry $entry)
2015-01-23 12:45:24 +01:00
{
2025-03-21 23:28:08 +01:00
if (!$this->isCsrfTokenValid('star-entry', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
2015-01-23 12:45:24 +01:00
$entry->toggleStar();
$entry->updateStar($entry->isStarred());
$this->entityManager->flush();
2015-01-23 12:45:24 +01:00
2016-03-11 14:48:46 +01:00
$message = 'flashes.entry.notice.entry_unstarred';
if ($entry->isStarred()) {
$message = 'flashes.entry.notice.entry_starred';
}
$this->addFlash(
2015-01-23 12:45:24 +01:00
'notice',
2016-03-11 14:48:46 +01:00
$message
2015-01-23 12:45:24 +01:00
);
2023-12-25 21:42:08 +01:00
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'));
return $this->redirect($redirectUrl);
2015-01-23 12:45:24 +01:00
}
/**
* Deletes entry and redirect to the homepage or the last viewed page.
2015-01-23 12:45:24 +01:00
*
2022-08-28 16:59:43 +02:00
* @return RedirectResponse
2015-01-23 12:45:24 +01:00
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/delete/{id}', name: 'delete_entry', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('DELETE', subject: 'entry')]
2015-10-21 15:26:37 +02:00
public function deleteEntryAction(Request $request, Entry $entry)
2015-01-23 12:45:24 +01:00
{
2025-03-23 03:09:42 +01:00
if (!$this->isCsrfTokenValid('delete-entry', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
// generates the view url for this entry to check for redirection later
// to avoid redirecting to the deleted entry. Ugh.
$url = $this->generateUrl(
'view',
2023-12-25 21:42:08 +01:00
['id' => $entry->getId()]
);
// entry deleted, dispatch event about it!
$this->eventDispatcher->dispatch(new EntryDeletedEvent($entry), EntryDeletedEvent::NAME);
$this->entityManager->remove($entry);
$this->entityManager->flush();
2015-01-23 12:45:24 +01:00
$this->addFlash(
2015-01-23 12:45:24 +01:00
'notice',
2016-03-11 14:48:46 +01:00
'flashes.entry.notice.entry_deleted'
2015-01-23 12:45:24 +01:00
);
2015-01-22 21:11:22 +01:00
// don't redirect user to the deleted entry (check that the referer doesn't end with the same url)
2023-12-25 21:42:08 +01:00
$prev = $request->query->get('redirect', '');
$to = (1 !== preg_match('#' . $url . '$#i', $prev) ? $prev : null);
$redirectUrl = $this->redirectHelper->to($to);
return $this->redirect($redirectUrl);
2015-01-22 21:11:22 +01:00
}
2016-08-23 16:49:12 +02:00
/**
* Get public URL for entry (and generate it if necessary).
*
2022-08-28 16:59:43 +02:00
* @return Response
2016-08-23 16:49:12 +02:00
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/share/{id}', name: 'share', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('SHARE', subject: 'entry')]
2025-03-23 12:52:16 +01:00
public function shareAction(Request $request, Entry $entry)
2016-08-23 16:49:12 +02:00
{
2025-03-23 12:52:16 +01:00
if (!$this->isCsrfTokenValid('share-entry', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
2016-12-29 10:09:44 +01:00
if (null === $entry->getUid()) {
$entry->generateUid();
2016-08-24 22:29:36 +02:00
$this->entityManager->persist($entry);
$this->entityManager->flush();
2016-08-23 16:49:12 +02:00
}
return $this->redirect($this->generateUrl('share_entry', [
2016-12-29 10:09:44 +01:00
'uid' => $entry->getUid(),
2016-08-23 16:49:12 +02:00
]));
}
/**
* Disable public sharing for an entry.
*
2022-08-28 16:59:43 +02:00
* @return Response
2016-08-23 16:49:12 +02:00
*/
2025-04-10 01:29:49 +02:00
#[Route(path: '/share/delete/{id}', name: 'delete_share', methods: ['POST'], requirements: ['id' => '\d+'])]
2025-04-05 15:21:29 +02:00
#[IsGranted('UNSHARE', subject: 'entry')]
2025-03-23 13:13:11 +01:00
public function deleteShareAction(Request $request, Entry $entry)
2016-08-23 16:49:12 +02:00
{
2025-03-23 13:13:11 +01:00
if (!$this->isCsrfTokenValid('delete-share', $request->request->get('token'))) {
throw new BadRequestHttpException('Bad CSRF token.');
}
2016-12-29 10:09:44 +01:00
$entry->cleanUid();
2016-08-24 22:29:36 +02:00
$this->entityManager->persist($entry);
$this->entityManager->flush();
2016-08-23 16:49:12 +02:00
return $this->redirect($this->generateUrl('view', [
'id' => $entry->getId(),
]));
}
2016-04-10 21:48:11 +02:00
/**
2016-08-24 22:29:36 +02:00
* Ability to view a content publicly.
2016-04-10 17:33:15 +02:00
*
2022-08-28 16:59:43 +02:00
* @return Response
2016-04-10 17:33:15 +02:00
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/share/{uid}', name: 'share_entry', methods: ['GET'], requirements: ['uid' => '.+'])]
2025-04-05 15:21:29 +02:00
#[Cache(maxage: 25200, smaxage: 25200, public: true)]
#[IsGranted('PUBLIC_ACCESS')]
public function shareEntryAction(Entry $entry, Config $craueConfig)
2016-04-10 17:33:15 +02:00
{
if (!$craueConfig->get('share_public')) {
2016-08-24 22:29:36 +02:00
throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
}
2016-04-10 17:33:15 +02:00
return $this->render(
2024-02-19 00:03:14 +01:00
'Entry/share.html.twig',
2016-08-24 22:29:36 +02:00
['entry' => $entry]
2016-04-10 17:33:15 +02:00
);
}
2016-08-26 16:55:41 +02:00
/**
* List the entries with the same domain as the current one.
*
* @param int $page
*
2022-08-28 16:59:43 +02:00
* @return Response
*/
2025-04-05 15:06:57 +02:00
#[Route(path: '/domain/{id}/{page}', name: 'same_domain', methods: ['GET'], requirements: ['id' => '\d+'], defaults: ['page' => 1])]
2025-04-05 15:21:29 +02:00
#[IsGranted('LIST_ENTRIES')]
public function getSameDomainEntries(Request $request, $page = 1)
{
return $this->showEntries('same-domain', $request, $page);
}
2017-07-01 09:52:38 +02:00
/**
* Global method to retrieve entries depending on the given type
* It returns the response to be send.
*
* @param string $type Entries type: unread, starred or archive
* @param int $page
2017-07-01 09:52:38 +02:00
*
2022-08-28 16:59:43 +02:00
* @return Response
2017-07-01 09:52:38 +02:00
*/
private function showEntries($type, Request $request, $page)
{
2025-04-05 14:01:48 +02:00
$searchTerm = (isset($request->query->all('search_entry')['term']) ? trim((string) $request->query->all('search_entry')['term']) : '');
2025-04-05 13:14:32 +02:00
$currentRoute = $request->query->get('currentRoute') ?? '';
2025-01-19 01:22:14 +01:00
$currentEntryId = $request->attributes->getInt('id');
2017-07-01 09:52:38 +02:00
$formOptions = [];
2017-07-01 09:52:38 +02:00
switch ($type) {
case 'search':
$qb = $this->entryRepository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm, $currentRoute);
2017-07-01 09:52:38 +02:00
break;
case 'untagged':
$qb = $this->entryRepository->getBuilderForUntaggedByUser($this->getUser()->getId());
2017-07-01 09:52:38 +02:00
break;
case 'starred':
$qb = $this->entryRepository->getBuilderForStarredByUser($this->getUser()->getId());
$formOptions['filter_starred'] = true;
2017-07-01 09:52:38 +02:00
break;
case 'archive':
$qb = $this->entryRepository->getBuilderForArchiveByUser($this->getUser()->getId());
$formOptions['filter_archived'] = true;
2017-07-01 09:52:38 +02:00
break;
2020-04-26 14:09:16 +02:00
case 'annotated':
$qb = $this->entryRepository->getBuilderForAnnotationsByUser($this->getUser()->getId());
break;
2017-07-01 09:52:38 +02:00
case 'unread':
$qb = $this->entryRepository->getBuilderForUnreadByUser($this->getUser()->getId());
$formOptions['filter_unread'] = true;
2017-07-01 09:52:38 +02:00
break;
case 'same-domain':
2025-01-19 01:22:14 +01:00
$qb = $this->entryRepository->getBuilderForSameDomainByUser($this->getUser()->getId(), $currentEntryId);
break;
2017-07-01 09:52:38 +02:00
case 'all':
$qb = $this->entryRepository->getBuilderForAllByUser($this->getUser()->getId());
2017-07-01 09:52:38 +02:00
break;
default:
2024-08-14 16:39:36 +02:00
throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type));
2017-07-01 09:52:38 +02:00
}
$form = $this->createForm(EntryFilterType::class, [], $formOptions);
2017-07-01 09:52:38 +02:00
if ($request->query->has($form->getName())) {
// manually bind values from the request
2025-01-19 01:22:14 +01:00
$form->submit($request->query->all($form->getName()));
2017-07-01 09:52:38 +02:00
// build the query from the given form object
$this->filterBuilderUpdater->addFilterConditions($form, $qb);
2017-07-01 09:52:38 +02:00
}
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
$entries = $this->preparePagerForEntriesHelper->prepare($pagerAdapter);
2017-07-01 09:52:38 +02:00
try {
$entries->setCurrentPage($page);
2025-04-05 13:56:56 +02:00
} catch (OutOfRangeCurrentPageException) {
2017-07-01 09:52:38 +02:00
if ($page > 1) {
return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
}
}
return $this->render(
2024-02-19 00:03:14 +01:00
'Entry/entries.html.twig', [
2017-07-01 09:52:38 +02:00
'form' => $form->createView(),
'entries' => $entries,
'currentPage' => $page,
'searchTerm' => $searchTerm,
'isFiltered' => $form->isSubmitted(),
2017-07-01 09:52:38 +02:00
]
);
}
2018-10-12 15:01:19 +02:00
/**
* Fetch content and update entry.
* In case it fails, $entry->getContent will return an error message.
*
* @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
*/
private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved')
{
$message = 'flashes.entry.notice.' . $prefixMessage;
try {
$this->contentProxy->updateEntry($entry, $entry->getUrl());
2025-04-05 13:56:56 +02:00
} catch (\Exception) {
// $this->logger->error('Error while saving an entry', [
// 'exception' => $e,
// 'entry' => $entry,
// ]);
2018-10-12 15:01:19 +02:00
$message = 'flashes.entry.notice.' . $prefixMessage . '_failed';
}
if (empty($entry->getDomainName())) {
$this->contentProxy->setEntryDomainName($entry);
2018-10-12 15:01:19 +02:00
}
if (empty($entry->getTitle())) {
$this->contentProxy->setDefaultEntryTitle($entry);
2018-10-12 15:01:19 +02:00
}
$this->addFlash('notice', $message);
2018-10-12 15:01:19 +02:00
}
2017-07-01 09:52:38 +02:00
/**
* Check for existing entry, if it exists, redirect to it with a message.
*
* @return Entry|bool
*/
private function checkIfEntryAlreadyExists(Entry $entry)
{
return $this->entryRepository->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
2017-07-01 09:52:38 +02:00
}
2015-01-22 17:18:56 +01:00
}