2015-08-07 16:17:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
|
|
|
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2015-08-19 09:46:21 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
|
|
|
use Wallabag\CoreBundle\Form\Type\NewTagType;
|
2015-08-07 16:17:23 +00:00
|
|
|
|
|
|
|
class TagController extends Controller
|
|
|
|
{
|
2015-08-19 09:46:21 +00:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag")
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function addTagFormAction(Request $request, Entry $entry)
|
|
|
|
{
|
2015-12-29 13:50:52 +00:00
|
|
|
$tag = new Tag();
|
2015-12-22 12:00:37 +00:00
|
|
|
$form = $this->createForm(NewTagType::class, $tag);
|
2015-08-19 09:46:21 +00:00
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isValid()) {
|
|
|
|
$existingTag = $this->getDoctrine()
|
|
|
|
->getRepository('WallabagCoreBundle:Tag')
|
2015-12-29 13:50:52 +00:00
|
|
|
->findOneByLabel($tag->getLabel());
|
2015-08-19 09:46:21 +00:00
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
|
|
|
|
if (is_null($existingTag)) {
|
2016-03-21 13:06:07 +00:00
|
|
|
$this->get('wallabag_core.content_proxy')->assignTagsToEntry(
|
|
|
|
$entry,
|
|
|
|
$tag
|
|
|
|
);
|
2015-08-19 09:46:21 +00:00
|
|
|
$em->persist($tag);
|
2015-12-29 13:50:52 +00:00
|
|
|
} elseif (!$existingTag->hasEntry($entry)) {
|
2016-03-21 13:06:07 +00:00
|
|
|
$this->get('wallabag_core.content_proxy')->assignTagsToEntry(
|
|
|
|
$entry,
|
|
|
|
$existingTag
|
|
|
|
);
|
2015-12-29 13:50:52 +00:00
|
|
|
$em->persist($existingTag);
|
2015-08-19 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
$this->get('session')->getFlashBag()->add(
|
|
|
|
'notice',
|
2016-03-11 13:48:46 +00:00
|
|
|
'flashes.tag.notice.tag_added'
|
2015-08-19 09:46:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', array(
|
|
|
|
'form' => $form->createView(),
|
|
|
|
'entry' => $entry,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:41:28 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
if (count($tag->getEntries()) == 0) {
|
|
|
|
$em->remove($tag);
|
|
|
|
}
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->redirect($request->headers->get('referer'));
|
|
|
|
}
|
|
|
|
|
2015-08-07 16:17:23 +00:00
|
|
|
/**
|
|
|
|
* Shows tags for current user.
|
|
|
|
*
|
|
|
|
* @Route("/tag/list", name="tag")
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function showTagAction()
|
|
|
|
{
|
|
|
|
$tags = $this->getDoctrine()
|
|
|
|
->getRepository('WallabagCoreBundle:Tag')
|
|
|
|
->findTags($this->getUser()->getId());
|
|
|
|
|
|
|
|
return $this->render(
|
|
|
|
'WallabagCoreBundle:Tag:tags.html.twig',
|
|
|
|
array(
|
2015-08-20 05:53:55 +00:00
|
|
|
'tags' => $tags,
|
2015-08-07 16:17:23 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|