]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/TagRestController.php
Add client_credentials as grant_type
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / TagRestController.php
CommitLineData
900c8448
NL
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Nelmio\ApiDocBundle\Annotation\ApiDoc;
0f8268c9 6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
900c8448
NL
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpFoundation\JsonResponse;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag;
11
12class TagRestController extends WallabagRestController
13{
14 /**
15 * Retrieve all tags.
16 *
17 * @ApiDoc()
0f8268c9 18 * @Security("has_role('ROLE_READ')")
900c8448
NL
19 * @return JsonResponse
20 */
21 public function getTagsAction()
22 {
23 $this->validateAuthentication();
24
25 $tags = $this->getDoctrine()
26 ->getRepository('WallabagCoreBundle:Tag')
27 ->findAllTags($this->getUser()->getId());
28
29 $json = $this->get('serializer')->serialize($tags, 'json');
30
31 return (new JsonResponse())->setJson($json);
32 }
33
34 /**
80299ed2 35 * Permanently remove one tag from **every** entry by passing the Tag label.
900c8448
NL
36 *
37 * @ApiDoc(
38 * requirements={
39 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
40 * }
41 * )
0f8268c9 42 * @Security("has_role('ROLE_WRITE')")
900c8448
NL
43 * @return JsonResponse
44 */
45 public function deleteTagLabelAction(Request $request)
46 {
47 $this->validateAuthentication();
1594a79f 48 $label = $request->get('tag', '');
900c8448
NL
49
50 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
51
52 if (empty($tag)) {
53 throw $this->createNotFoundException('Tag not found');
54 }
55
56 $this->getDoctrine()
57 ->getRepository('WallabagCoreBundle:Entry')
58 ->removeTag($this->getUser()->getId(), $tag);
59
60 $this->cleanOrphanTag($tag);
61
62 $json = $this->get('serializer')->serialize($tag, 'json');
63
64 return (new JsonResponse())->setJson($json);
65 }
66
67 /**
68 * Permanently remove some tags from **every** entry.
69 *
70 * @ApiDoc(
71 * requirements={
72 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
73 * }
74 * )
0f8268c9 75 * @Security("has_role('ROLE_WRITE')")
900c8448
NL
76 * @return JsonResponse
77 */
78 public function deleteTagsLabelAction(Request $request)
79 {
80 $this->validateAuthentication();
81
1594a79f 82 $tagsLabels = $request->get('tags', '');
900c8448
NL
83
84 $tags = [];
85
86 foreach (explode(',', $tagsLabels) as $tagLabel) {
87 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
88
89 if (!empty($tagEntity)) {
90 $tags[] = $tagEntity;
91 }
92 }
93
94 if (empty($tags)) {
95 throw $this->createNotFoundException('Tags not found');
96 }
97
98 $this->getDoctrine()
99 ->getRepository('WallabagCoreBundle:Entry')
100 ->removeTags($this->getUser()->getId(), $tags);
101
102 $this->cleanOrphanTag($tags);
103
104 $json = $this->get('serializer')->serialize($tags, 'json');
105
106 return (new JsonResponse())->setJson($json);
107 }
108
109 /**
80299ed2 110 * Permanently remove one tag from **every** entry by passing the Tag ID.
900c8448
NL
111 *
112 * @ApiDoc(
113 * requirements={
114 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
115 * }
116 * )
0f8268c9 117 * @Security("has_role('ROLE_WRITE')")
900c8448
NL
118 * @return JsonResponse
119 */
120 public function deleteTagAction(Tag $tag)
121 {
122 $this->validateAuthentication();
123
124 $this->getDoctrine()
125 ->getRepository('WallabagCoreBundle:Entry')
126 ->removeTag($this->getUser()->getId(), $tag);
127
128 $this->cleanOrphanTag($tag);
129
130 $json = $this->get('serializer')->serialize($tag, 'json');
131
132 return (new JsonResponse())->setJson($json);
133 }
134
900c8448
NL
135 /**
136 * Remove orphan tag in case no entries are associated to it.
0f8268c9 137 * @Security("has_role('ROLE_WRITE')")
900c8448
NL
138 * @param Tag|array $tags
139 */
140 private function cleanOrphanTag($tags)
141 {
142 if (!is_array($tags)) {
143 $tags = [$tags];
144 }
145
146 $em = $this->getDoctrine()->getManager();
147
148 foreach ($tags as $tag) {
149 if (count($tag->getEntries()) === 0) {
150 $em->remove($tag);
151 }
152 }
153
154 $em->flush();
155 }
156}