3 namespace Wallabag\ApiBundle\Controller
;
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\HttpFoundation\JsonResponse
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
12 class TagRestController
extends WallabagRestController
18 * @Security("has_role('ROLE_READ')")
19 * @return JsonResponse
21 public function getTagsAction()
23 $this->validateAuthentication();
25 $tags = $this->getDoctrine()
26 ->getRepository('WallabagCoreBundle:Tag')
27 ->findAllTags($this->getUser()->getId());
29 $json = $this->get('serializer')->serialize($tags, 'json');
31 return (new JsonResponse())->setJson($json);
35 * Permanently remove one tag from **every** entry by passing the Tag label.
39 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
42 * @Security("has_role('ROLE_WRITE')")
43 * @return JsonResponse
45 public function deleteTagLabelAction(Request
$request)
47 $this->validateAuthentication();
48 $label = $request->get('tag', '');
50 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
53 throw $this->createNotFoundException('Tag not found');
57 ->getRepository('WallabagCoreBundle:Entry')
58 ->removeTag($this->getUser()->getId(), $tag);
60 $this->cleanOrphanTag($tag);
62 $json = $this->get('serializer')->serialize($tag, 'json');
64 return (new JsonResponse())->setJson($json);
68 * Permanently remove some tags from **every** entry.
72 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
75 * @Security("has_role('ROLE_WRITE')")
76 * @return JsonResponse
78 public function deleteTagsLabelAction(Request
$request)
80 $this->validateAuthentication();
82 $tagsLabels = $request->get('tags', '');
86 foreach (explode(',', $tagsLabels) as $tagLabel) {
87 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
89 if (!empty($tagEntity)) {
95 throw $this->createNotFoundException('Tags not found');
99 ->getRepository('WallabagCoreBundle:Entry')
100 ->removeTags($this->getUser()->getId(), $tags);
102 $this->cleanOrphanTag($tags);
104 $json = $this->get('serializer')->serialize($tags, 'json');
106 return (new JsonResponse())->setJson($json);
110 * Permanently remove one tag from **every** entry by passing the Tag ID.
114 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
117 * @Security("has_role('ROLE_WRITE')")
118 * @return JsonResponse
120 public function deleteTagAction(Tag
$tag)
122 $this->validateAuthentication();
125 ->getRepository('WallabagCoreBundle:Entry')
126 ->removeTag($this->getUser()->getId(), $tag);
128 $this->cleanOrphanTag($tag);
130 $json = $this->get('serializer')->serialize($tag, 'json');
132 return (new JsonResponse())->setJson($json);
136 * Remove orphan tag in case no entries are associated to it.
137 * @Security("has_role('ROLE_WRITE')")
138 * @param Tag|array $tags
140 private function cleanOrphanTag($tags)
142 if (!is_array($tags)) {
146 $em = $this->getDoctrine()->getManager();
148 foreach ($tags as $tag) {
149 if (count($tag->getEntries()) === 0) {