]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
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; | |
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 | ||
28 | $json = $this->get('serializer')->serialize($tags, 'json'); | |
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(); | |
47 | $label = $request->request->get('tag', ''); | |
48 | ||
49 | $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); | |
50 | ||
51 | if (empty($tag)) { | |
52 | throw $this->createNotFoundException('Tag not found'); | |
53 | } | |
54 | ||
55 | $this->getDoctrine() | |
56 | ->getRepository('WallabagCoreBundle:Entry') | |
57 | ->removeTag($this->getUser()->getId(), $tag); | |
58 | ||
59 | $this->cleanOrphanTag($tag); | |
60 | ||
61 | $json = $this->get('serializer')->serialize($tag, 'json'); | |
62 | ||
63 | return (new JsonResponse())->setJson($json); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Permanently remove some tags from **every** entry. | |
68 | * | |
69 | * @ApiDoc( | |
70 | * requirements={ | |
71 | * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"} | |
72 | * } | |
73 | * ) | |
74 | * | |
75 | * @return JsonResponse | |
76 | */ | |
77 | public function deleteTagsLabelAction(Request $request) | |
78 | { | |
79 | $this->validateAuthentication(); | |
80 | ||
81 | $tagsLabels = $request->request->get('tags', ''); | |
82 | ||
83 | $tags = []; | |
84 | ||
85 | foreach (explode(',', $tagsLabels) as $tagLabel) { | |
86 | $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); | |
87 | ||
88 | if (!empty($tagEntity)) { | |
89 | $tags[] = $tagEntity; | |
90 | } | |
91 | } | |
92 | ||
93 | if (empty($tags)) { | |
94 | throw $this->createNotFoundException('Tags not found'); | |
95 | } | |
96 | ||
97 | $this->getDoctrine() | |
98 | ->getRepository('WallabagCoreBundle:Entry') | |
99 | ->removeTags($this->getUser()->getId(), $tags); | |
100 | ||
101 | $this->cleanOrphanTag($tags); | |
102 | ||
103 | $json = $this->get('serializer')->serialize($tags, 'json'); | |
104 | ||
105 | return (new JsonResponse())->setJson($json); | |
106 | } | |
107 | ||
108 | /** | |
80299ed2 | 109 | * Permanently remove one tag from **every** entry by passing the Tag ID. |
900c8448 NL |
110 | * |
111 | * @ApiDoc( | |
112 | * requirements={ | |
113 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"} | |
114 | * } | |
115 | * ) | |
116 | * | |
117 | * @return JsonResponse | |
118 | */ | |
119 | public function deleteTagAction(Tag $tag) | |
120 | { | |
121 | $this->validateAuthentication(); | |
122 | ||
123 | $this->getDoctrine() | |
124 | ->getRepository('WallabagCoreBundle:Entry') | |
125 | ->removeTag($this->getUser()->getId(), $tag); | |
126 | ||
127 | $this->cleanOrphanTag($tag); | |
128 | ||
129 | $json = $this->get('serializer')->serialize($tag, 'json'); | |
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 | { | |
141 | if (!is_array($tags)) { | |
142 | $tags = [$tags]; | |
143 | } | |
144 | ||
145 | $em = $this->getDoctrine()->getManager(); | |
146 | ||
147 | foreach ($tags as $tag) { | |
148 | if (count($tag->getEntries()) === 0) { | |
149 | $em->remove($tag); | |
150 | } | |
151 | } | |
152 | ||
153 | $em->flush(); | |
154 | } | |
155 | } |