]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Symfony\Component\HttpFoundation\JsonResponse; | |
9 | use Symfony\Component\HttpFoundation\Response; | |
10 | use Wallabag\CoreBundle\Entity\Entry; | |
11 | use Wallabag\CoreBundle\Entity\Tag; | |
12 | use Wallabag\CoreBundle\Service\Extractor; | |
13 | ||
14 | class WallabagRestController extends Controller | |
15 | { | |
16 | /** | |
17 | * @param Entry $entry | |
18 | * @param string $tags | |
19 | */ | |
20 | private function assignTagsToEntry(Entry $entry, $tags) | |
21 | { | |
22 | foreach (explode(',', $tags) as $label) { | |
23 | $label = trim($label); | |
24 | $tagEntity = $this | |
25 | ->getDoctrine() | |
26 | ->getRepository('WallabagCoreBundle:Tag') | |
27 | ->findOneByLabel($label); | |
28 | ||
29 | if (is_null($tagEntity)) { | |
30 | $tagEntity = new Tag($this->getUser()); | |
31 | $tagEntity->setLabel($label); | |
32 | } | |
33 | ||
34 | // only add the tag on the entry if the relation doesn't exist | |
35 | if (!$entry->getTags()->contains($tagEntity)) { | |
36 | $entry->addTag($tagEntity); | |
37 | } | |
38 | } | |
39 | } | |
40 | ||
41 | /** | |
42 | * Retrieve salt for a giver user. | |
43 | * | |
44 | * @ApiDoc() | |
45 | * @return array | |
46 | */ | |
47 | public function getSaltAction($username) | |
48 | { | |
49 | $user = $this | |
50 | ->getDoctrine() | |
51 | ->getRepository('WallabagCoreBundle:User') | |
52 | ->findOneByUsername($username); | |
53 | ||
54 | if (is_null($user)) { | |
55 | throw $this->createNotFoundException(); | |
56 | } | |
57 | ||
58 | return array($user->getSalt() ?: null); | |
59 | } | |
60 | /** | |
61 | * Retrieve all entries. It could be filtered by many options. | |
62 | * | |
63 | * @ApiDoc( | |
64 | * parameters={ | |
65 | * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."}, | |
66 | * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."}, | |
67 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | |
68 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | |
69 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
70 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
71 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
72 | * } | |
73 | * ) | |
74 | * @return Entry | |
75 | */ | |
76 | public function getEntriesAction(Request $request) | |
77 | { | |
78 | $isArchived = $request->query->get('archive'); | |
79 | $isStarred = $request->query->get('star'); | |
80 | $sort = $request->query->get('sort', 'created'); | |
81 | $order = $request->query->get('order', 'desc'); | |
82 | $page = $request->query->get('page', 1); | |
83 | $perPage = $request->query->get('perPage', 30); | |
84 | $tags = $request->query->get('tags', array()); | |
85 | ||
86 | $entries = $this | |
87 | ->getDoctrine() | |
88 | ->getRepository('WallabagCoreBundle:Entry') | |
89 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); | |
90 | ||
91 | if (!($entries)) { | |
92 | throw $this->createNotFoundException(); | |
93 | } | |
94 | ||
95 | $json = $this->get('serializer')->serialize($entries, 'json'); | |
96 | ||
97 | return new JsonResponse($json, 200); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Retrieve a single entry | |
102 | * | |
103 | * @ApiDoc( | |
104 | * requirements={ | |
105 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
106 | * } | |
107 | * ) | |
108 | * @return Entry | |
109 | */ | |
110 | public function getEntryAction(Entry $entry) | |
111 | { | |
112 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
113 | throw $this->createAccessDeniedException(); | |
114 | } | |
115 | ||
116 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
117 | ||
118 | return new JsonResponse($json, 200); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Create an entry | |
123 | * | |
124 | * @ApiDoc( | |
125 | * parameters={ | |
126 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
127 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
128 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
129 | * } | |
130 | * ) | |
131 | * @return Entry | |
132 | */ | |
133 | public function postEntriesAction(Request $request) | |
134 | { | |
135 | $url = $request->request->get('url'); | |
136 | ||
137 | $content = Extractor::extract($url); | |
138 | $entry = new Entry($this->getUser()); | |
139 | $entry->setUrl($url); | |
140 | $entry->setTitle($request->request->get('title') ?: $content->getTitle()); | |
141 | $entry->setContent($content->getBody()); | |
142 | ||
143 | $tags = $request->request->get('tags', ''); | |
144 | if (!empty($tags)) { | |
145 | $this->assignTagsToEntry($entry, $tags); | |
146 | } | |
147 | ||
148 | $em = $this->getDoctrine()->getManager(); | |
149 | $em->persist($entry); | |
150 | $em->flush(); | |
151 | ||
152 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
153 | ||
154 | return new JsonResponse($json, 200); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Change several properties of an entry | |
159 | * | |
160 | * @ApiDoc( | |
161 | * requirements={ | |
162 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
163 | * }, | |
164 | * parameters={ | |
165 | * {"name"="title", "dataType"="string", "required"=false}, | |
166 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
167 | * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."}, | |
168 | * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."}, | |
169 | * } | |
170 | * ) | |
171 | * @return Entry | |
172 | */ | |
173 | public function patchEntriesAction(Entry $entry, Request $request) | |
174 | { | |
175 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
176 | throw $this->createAccessDeniedException(); | |
177 | } | |
178 | ||
179 | $title = $request->request->get("title"); | |
180 | $isArchived = $request->request->get("archive"); | |
181 | $isStarred = $request->request->get("star"); | |
182 | ||
183 | if (!is_null($title)) { | |
184 | $entry->setTitle($title); | |
185 | } | |
186 | ||
187 | if (!is_null($isArchived)) { | |
188 | $entry->setArchived($isArchived); | |
189 | } | |
190 | ||
191 | if (!is_null($isStarred)) { | |
192 | $entry->setStarred($isStarred); | |
193 | } | |
194 | ||
195 | $tags = $request->request->get('tags', ''); | |
196 | if (!empty($tags)) { | |
197 | $this->assignTagsToEntry($entry, $tags); | |
198 | } | |
199 | ||
200 | $em = $this->getDoctrine()->getManager(); | |
201 | $em->flush(); | |
202 | ||
203 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
204 | ||
205 | return new JsonResponse($json, 200); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Delete **permanently** an entry | |
210 | * | |
211 | * @ApiDoc( | |
212 | * requirements={ | |
213 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
214 | * } | |
215 | * ) | |
216 | * @return Entry | |
217 | */ | |
218 | public function deleteEntriesAction(Entry $entry) | |
219 | { | |
220 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
221 | throw $this->createAccessDeniedException(); | |
222 | } | |
223 | ||
224 | $em = $this->getDoctrine()->getManager(); | |
225 | $em->remove($entry); | |
226 | $em->flush(); | |
227 | ||
228 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
229 | ||
230 | return new JsonResponse($json, 200); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Retrieve all tags for an entry | |
235 | * | |
236 | * @ApiDoc( | |
237 | * requirements={ | |
238 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
239 | * } | |
240 | * ) | |
241 | */ | |
242 | public function getEntriesTagsAction(Entry $entry) | |
243 | { | |
244 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
245 | throw $this->createAccessDeniedException(); | |
246 | } | |
247 | ||
248 | $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); | |
249 | ||
250 | return new Response($json, 200, array('application/json')); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Add one or more tags to an entry | |
255 | * | |
256 | * @ApiDoc( | |
257 | * requirements={ | |
258 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
259 | * }, | |
260 | * parameters={ | |
261 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
262 | * } | |
263 | * ) | |
264 | */ | |
265 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
266 | { | |
267 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
268 | throw $this->createAccessDeniedException(); | |
269 | } | |
270 | ||
271 | $tags = $request->request->get('tags', ''); | |
272 | if (!empty($tags)) { | |
273 | $this->assignTagsToEntry($entry, $tags); | |
274 | } | |
275 | ||
276 | $em = $this->getDoctrine()->getManager(); | |
277 | $em->persist($entry); | |
278 | $em->flush(); | |
279 | ||
280 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
281 | ||
282 | return new JsonResponse($json, 200); | |
283 | } | |
284 | ||
285 | /** | |
286 | * Permanently remove one tag for an entry | |
287 | * | |
288 | * @ApiDoc( | |
289 | * requirements={ | |
290 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}, | |
291 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
292 | * } | |
293 | * ) | |
294 | */ | |
295 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
296 | { | |
297 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { | |
298 | throw $this->createAccessDeniedException(); | |
299 | } | |
300 | ||
301 | $entry->removeTag($tag); | |
302 | $em = $this->getDoctrine()->getManager(); | |
303 | $em->persist($entry); | |
304 | $em->flush(); | |
305 | ||
306 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
307 | ||
308 | return new JsonResponse($json, 200); | |
309 | } | |
310 | ||
311 | /** | |
312 | * Retrieve all tags | |
313 | * | |
314 | * @ApiDoc() | |
315 | */ | |
316 | public function getTagsAction() | |
317 | { | |
318 | $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json'); | |
319 | ||
320 | return new JsonResponse($json, 200); | |
321 | } | |
322 | ||
323 | /** | |
324 | * Permanently remove one tag from **every** entry | |
325 | * | |
326 | * @ApiDoc( | |
327 | * requirements={ | |
328 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"} | |
329 | * } | |
330 | * ) | |
331 | */ | |
332 | public function deleteTagAction(Tag $tag) | |
333 | { | |
334 | if ($tag->getUser()->getId() != $this->getUser()->getId()) { | |
335 | throw $this->createAccessDeniedException(); | |
336 | } | |
337 | ||
338 | $em = $this->getDoctrine()->getManager(); | |
339 | $em->remove($tag); | |
340 | $em->flush(); | |
341 | ||
342 | $json = $this->get('serializer')->serialize($tag, 'json'); | |
343 | ||
344 | return new JsonResponse($json, 200); | |
345 | } | |
346 | } |