wallabag/src/Wallabag/CoreBundle/Controller/TagController.php
Jeremy Benoist 8d4ed0df06
Update deps
Also CS (because cs-fixer got an update)

Package operations: 0 installs, 26 updates, 0 removals
  - Updating twig/twig (v2.12.1 => v2.12.2)
  - Updating symfony/symfony (v3.4.33 => v3.4.34)
  - Updating doctrine/event-manager (v1.0.0 => 1.1.0)
  - Updating doctrine/collections (v1.6.2 => 1.6.3)
  - Updating doctrine/cache (v1.8.1 => 1.9.0)
  - Updating doctrine/persistence (1.1.1 => 1.2.0)
  - Updating doctrine/inflector (v1.3.0 => 1.3.1)
  - Updating symfony/mime (v4.3.5 => v4.3.7)
  - Updating swiftmailer/swiftmailer (v6.2.1 => v6.2.3)
  - Updating symfony/swiftmailer-bundle (v3.3.0 => v3.3.1)
  - Updating doctrine/dbal (v2.9.2 => v2.9.3)
  - Updating doctrine/instantiator (1.2.0 => 1.3.0)
  - Updating j0k3r/graby-site-config (1.0.93 => 1.0.94)
  - Updating phpoption/phpoption (1.5.0 => 1.5.2)
  - Updating symfony/http-client-contracts (v1.1.7 => v1.1.8)
  - Updating symfony/http-client (v4.3.5 => v4.3.7)
  - Updating sensiolabs/security-checker (v6.0.2 => v6.0.3)
  - Updating paragonie/constant_time_encoding (v2.2.3 => v2.3.0)
  - Updating scheb/two-factor-bundle (v4.7.1 => v4.8.0)
  - Updating symfony/phpunit-bridge (v4.3.6 => v4.3.7)
  - Updating composer/xdebug-handler (1.3.3 => 1.4.0)
  - Updating friendsofphp/php-cs-fixer (v2.15.3 => v2.16.0)
  - Updating doctrine/data-fixtures (v1.3.2 => 1.3.3)
  - Updating nette/schema (v1.0.0 => v1.0.1)
  - Updating nikic/php-parser (v4.2.4 => v4.3.0)
  - Updating sentry/sentry (2.2.2 => 2.2.4)
2019-11-12 14:18:58 +01:00

181 lines
5.9 KiB
PHP

<?php
namespace Wallabag\CoreBundle\Controller;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Form\Type\NewTagType;
use Wallabag\CoreBundle\Form\Type\RenameTagType;
class TagController extends Controller
{
/**
* @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addTagFormAction(Request $request, Entry $entry)
{
$form = $this->createForm(NewTagType::class, new Tag());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
$entry,
$form->get('label')->getData()
);
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
'flashes.tag.notice.tag_added'
);
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
}
return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', [
'form' => $form->createView(),
'entry' => $entry,
]);
}
/**
* Removes tag from entry.
*
* @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
{
$entry->removeTag($tag);
$em = $this->getDoctrine()->getManager();
$em->flush();
// remove orphan tag in case no entries are associated to it
if (0 === \count($tag->getEntries())) {
$em->remove($tag);
$em->flush();
}
$redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
return $this->redirect($redirectUrl);
}
/**
* Shows tags for current user.
*
* @Route("/tag/list", name="tag")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showTagAction()
{
$tags = $this->get('wallabag_core.tag_repository')
->findAllFlatTagsWithNbEntries($this->getUser()->getId());
$nbEntriesUntagged = $this->get('wallabag_core.entry_repository')
->countUntaggedEntriesByUser($this->getUser()->getId());
$renameForms = [];
foreach ($tags as $tag) {
$renameForms[$tag['id']] = $this->createForm(RenameTagType::class, new Tag())->createView();
}
return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
'tags' => $tags,
'renameForms' => $renameForms,
'nbEntriesUntagged' => $nbEntriesUntagged,
]);
}
/**
* @param int $page
*
* @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"})
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showEntriesForTagAction(Tag $tag, $page, Request $request)
{
$entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
$this->getUser()->getId(),
$tag->getId()
);
$pagerAdapter = new ArrayAdapter($entriesByTag);
$entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter);
try {
$entries->setCurrentPage($page);
} catch (OutOfRangeCurrentPageException $e) {
if ($page > 1) {
return $this->redirect($this->generateUrl($request->get('_route'), [
'slug' => $tag->getSlug(),
'page' => $entries->getNbPages(),
]), 302);
}
}
return $this->render('WallabagCoreBundle:Entry:entries.html.twig', [
'form' => null,
'entries' => $entries,
'currentPage' => $page,
'tag' => $tag,
]);
}
/**
* Rename a given tag with a new label
* Create a new tag with the new name and drop the old one.
*
* @Route("/tag/rename/{slug}", name="tag_rename")
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function renameTagAction(Tag $tag, Request $request)
{
$form = $this->createForm(RenameTagType::class, new Tag());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
$this->getUser()->getId(),
$tag->getId()
);
foreach ($entries as $entry) {
$this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
$entry,
$form->get('label')->getData()
);
$entry->removeTag($tag);
}
$em = $this->getDoctrine()->getManager();
$em->flush();
}
$this->get('session')->getFlashBag()->add(
'notice',
'flashes.tag.notice.tag_renamed'
);
$redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
return $this->redirect($redirectUrl);
}
}