3 namespace Wallabag\ApiBundle\Controller
;
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
6 use Symfony\Component\HttpFoundation\JsonResponse
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Entity\Tag
;
11 class TagRestController
extends WallabagRestController
18 * @return JsonResponse
20 public function getTagsAction()
22 $this->validateAuthentication();
24 $tags = $this->getDoctrine()
25 ->getRepository('WallabagCoreBundle:Tag')
26 ->findAllTags($this->getUser()->getId());
28 $json = $this->get('jms_serializer')->serialize($tags, 'json');
30 return (new JsonResponse())->setJson($json);
34 * Permanently remove one tag from **every** entry by passing the Tag label.
38 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
42 * @return JsonResponse
44 public function deleteTagLabelAction(Request
$request)
46 $this->validateAuthentication();
47 $label = $request->get('tag', '');
49 $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser([$label], $this->getUser()->getId());
52 throw $this->createNotFoundException('Tag not found');
58 ->getRepository('WallabagCoreBundle:Entry')
59 ->removeTag($this->getUser()->getId(), $tag);
61 $this->cleanOrphanTag($tag);
63 $json = $this->get('jms_serializer')->serialize($tag, 'json');
65 return (new JsonResponse())->setJson($json);
69 * Permanently remove some tags from **every** entry.
73 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
77 * @return JsonResponse
79 public function deleteTagsLabelAction(Request
$request)
81 $this->validateAuthentication();
83 $tagsLabels = $request->get('tags', '');
85 $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser(explode(',', $tagsLabels), $this->getUser()->getId());
88 throw $this->createNotFoundException('Tags not found');
92 ->getRepository('WallabagCoreBundle:Entry')
93 ->removeTags($this->getUser()->getId(), $tags);
95 $this->cleanOrphanTag($tags);
97 $json = $this->get('jms_serializer')->serialize($tags, 'json');
99 return (new JsonResponse())->setJson($json);
103 * Permanently remove one tag from **every** entry by passing the Tag ID.
107 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
111 * @return JsonResponse
113 public function deleteTagAction(Tag
$tag)
115 $this->validateAuthentication();
117 $tagFromDb = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser([$tag->getLabel()], $this->getUser()->getId());
119 if (empty($tagFromDb)) {
120 throw $this->createNotFoundException('Tag not found');
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->removeTag($this->getUser()->getId(), $tag);
127 $this->cleanOrphanTag($tag);
129 $json = $this->get('jms_serializer')->serialize($tag, 'json');
131 return (new JsonResponse())->setJson($json);
135 * Remove orphan tag in case no entries are associated to it.
137 * @param Tag|array $tags
139 private function cleanOrphanTag($tags)
141 if (!\
is_array($tags)) {
145 $em = $this->getDoctrine()->getManager();
147 foreach ($tags as $tag) {
148 if (0 === \
count($tag->getEntries())) {