3 namespace Wallabag\ApiBundle\Controller
;
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
6 use Symfony\Component\HttpFoundation\Request
;
7 use Symfony\Component\HttpFoundation\JsonResponse
;
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('serializer')->serialize($tags, 'json');
30 return (new JsonResponse())->setJson($json);
34 * Permanently remove one tag from **every** entry.
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->request
->get('tag', '');
49 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
52 throw $this->createNotFoundException('Tag not found');
56 ->getRepository('WallabagCoreBundle:Entry')
57 ->removeTag($this->getUser()->getId(), $tag);
59 $this->cleanOrphanTag($tag);
61 $json = $this->get('serializer')->serialize($tag, 'json');
63 return (new JsonResponse())->setJson($json);
67 * Permanently remove some tags from **every** entry.
71 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
75 * @return JsonResponse
77 public function deleteTagsLabelAction(Request
$request)
79 $this->validateAuthentication();
81 $tagsLabels = $request->request
->get('tags', '');
85 foreach (explode(',', $tagsLabels) as $tagLabel) {
86 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
88 if (!empty($tagEntity)) {
94 throw $this->createNotFoundException('Tags not found');
98 ->getRepository('WallabagCoreBundle:Entry')
99 ->removeTags($this->getUser()->getId(), $tags);
101 $this->cleanOrphanTag($tags);
103 $json = $this->get('serializer')->serialize($tags, 'json');
105 return (new JsonResponse())->setJson($json);
109 * Permanently remove one tag from **every** entry.
113 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
117 * @return JsonResponse
119 public function deleteTagAction(Tag
$tag)
121 $this->validateAuthentication();
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->removeTag($this->getUser()->getId(), $tag);
127 $this->cleanOrphanTag($tag);
129 $json = $this->get('serializer')->serialize($tag, 'json');
131 return (new JsonResponse())->setJson($json);
135 * Retrieve version number.
139 * @return JsonResponse
141 public function getVersionAction()
143 $version = $this->container
->getParameter('wallabag_core.version');
145 $json = $this->get('serializer')->serialize($version, 'json');
147 return (new JsonResponse())->setJson($json);
151 * Remove orphan tag in case no entries are associated to it.
153 * @param Tag|array $tags
155 private function cleanOrphanTag($tags)
157 if (!is_array($tags)) {
161 $em = $this->getDoctrine()->getManager();
163 foreach ($tags as $tag) {
164 if (count($tag->getEntries()) === 0) {