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