]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/TagRestController.php
Add client_credentials as grant_type
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / TagRestController.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Controller;
4
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;
11
12 class TagRestController extends WallabagRestController
13 {
14 /**
15 * Retrieve all tags.
16 *
17 * @ApiDoc()
18 * @Security("has_role('ROLE_READ')")
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 /**
35 * Permanently remove one tag from **every** entry by passing the Tag label.
36 *
37 * @ApiDoc(
38 * requirements={
39 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
40 * }
41 * )
42 * @Security("has_role('ROLE_WRITE')")
43 * @return JsonResponse
44 */
45 public function deleteTagLabelAction(Request $request)
46 {
47 $this->validateAuthentication();
48 $label = $request->get('tag', '');
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 * )
75 * @Security("has_role('ROLE_WRITE')")
76 * @return JsonResponse
77 */
78 public function deleteTagsLabelAction(Request $request)
79 {
80 $this->validateAuthentication();
81
82 $tagsLabels = $request->get('tags', '');
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 /**
110 * Permanently remove one tag from **every** entry by passing the Tag ID.
111 *
112 * @ApiDoc(
113 * requirements={
114 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
115 * }
116 * )
117 * @Security("has_role('ROLE_WRITE')")
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
135 /**
136 * Remove orphan tag in case no entries are associated to it.
137 * @Security("has_role('ROLE_WRITE')")
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 }