]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
6 | use Symfony\Component\HttpFoundation\JsonResponse; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
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('jms_serializer')->serialize($tags, 'json'); | |
29 | ||
30 | return (new JsonResponse())->setJson($json); | |
31 | } | |
32 | ||
33 | /** | |
34 | * Permanently remove one tag from **every** entry by passing the Tag label. | |
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->get('tag', ''); | |
48 | ||
49 | $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser([$label], $this->getUser()->getId()); | |
50 | ||
51 | if (empty($tags)) { | |
52 | throw $this->createNotFoundException('Tag not found'); | |
53 | } | |
54 | ||
55 | $tag = $tags[0]; | |
56 | ||
57 | $this->getDoctrine() | |
58 | ->getRepository('WallabagCoreBundle:Entry') | |
59 | ->removeTag($this->getUser()->getId(), $tag); | |
60 | ||
61 | $this->cleanOrphanTag($tag); | |
62 | ||
63 | $json = $this->get('jms_serializer')->serialize($tag, 'json'); | |
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 | ||
83 | $tagsLabels = $request->get('tags', ''); | |
84 | ||
85 | $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findByLabelsAndUser(explode(',', $tagsLabels), $this->getUser()->getId()); | |
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 | ||
97 | $json = $this->get('jms_serializer')->serialize($tags, 'json'); | |
98 | ||
99 | return (new JsonResponse())->setJson($json); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Permanently remove one tag from **every** entry by passing the Tag ID. | |
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 | ||
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 | ||
123 | $this->getDoctrine() | |
124 | ->getRepository('WallabagCoreBundle:Entry') | |
125 | ->removeTag($this->getUser()->getId(), $tag); | |
126 | ||
127 | $this->cleanOrphanTag($tag); | |
128 | ||
129 | $json = $this->get('jms_serializer')->serialize($tag, 'json'); | |
130 | ||
131 | return (new JsonResponse())->setJson($json); | |
132 | } | |
133 | ||
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 (0 === \count($tag->getEntries())) { | |
149 | $em->remove($tag); | |
150 | } | |
151 | } | |
152 | ||
153 | $em->flush(); | |
154 | } | |
155 | } |