]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Hateoas\Configuration\Route; | |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | |
7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
efd351c9 | 8 | use Symfony\Component\Config\Definition\Exception\Exception; |
900c8448 NL |
9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | |
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
12 | use Wallabag\CoreBundle\Entity\Entry; | |
13 | use Wallabag\CoreBundle\Entity\Tag; | |
5a619812 JB |
14 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
900c8448 NL |
16 | |
17 | class EntryRestController extends WallabagRestController | |
18 | { | |
19 | /** | |
20 | * Check if an entry exist by url. | |
21 | * | |
22 | * @ApiDoc( | |
23 | * parameters={ | |
24 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | |
25 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} | |
26 | * } | |
27 | * ) | |
28 | * | |
29 | * @return JsonResponse | |
30 | */ | |
31 | public function getEntriesExistsAction(Request $request) | |
32 | { | |
33 | $this->validateAuthentication(); | |
34 | ||
35 | $urls = $request->query->get('urls', []); | |
36 | ||
37 | // handle multiple urls first | |
38 | if (!empty($urls)) { | |
39 | $results = []; | |
40 | foreach ($urls as $url) { | |
41 | $res = $this->getDoctrine() | |
42 | ->getRepository('WallabagCoreBundle:Entry') | |
43 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
44 | ||
ca9a83ee | 45 | $results[$url] = $res instanceof Entry ? $res->getId() : false; |
900c8448 NL |
46 | } |
47 | ||
48 | $json = $this->get('serializer')->serialize($results, 'json'); | |
49 | ||
50 | return (new JsonResponse())->setJson($json); | |
51 | } | |
52 | ||
53 | // let's see if it is a simple url? | |
54 | $url = $request->query->get('url', ''); | |
55 | ||
56 | if (empty($url)) { | |
57 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); | |
58 | } | |
59 | ||
60 | $res = $this->getDoctrine() | |
61 | ->getRepository('WallabagCoreBundle:Entry') | |
62 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
63 | ||
ca9a83ee | 64 | $exists = $res instanceof Entry ? $res->getId() : false; |
900c8448 NL |
65 | |
66 | $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); | |
67 | ||
68 | return (new JsonResponse())->setJson($json); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Retrieve all entries. It could be filtered by many options. | |
73 | * | |
74 | * @ApiDoc( | |
75 | * parameters={ | |
76 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | |
77 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | |
78 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | |
79 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | |
80 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
81 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
82 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
83 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, | |
84 | * } | |
85 | * ) | |
86 | * | |
87 | * @return JsonResponse | |
88 | */ | |
89 | public function getEntriesAction(Request $request) | |
90 | { | |
91 | $this->validateAuthentication(); | |
92 | ||
93 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); | |
94 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); | |
95 | $sort = $request->query->get('sort', 'created'); | |
96 | $order = $request->query->get('order', 'desc'); | |
97 | $page = (int) $request->query->get('page', 1); | |
98 | $perPage = (int) $request->query->get('perPage', 30); | |
99 | $tags = $request->query->get('tags', ''); | |
100 | $since = $request->query->get('since', 0); | |
101 | ||
102 | $pager = $this->getDoctrine() | |
103 | ->getRepository('WallabagCoreBundle:Entry') | |
104 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); | |
105 | ||
106 | $pager->setCurrentPage($page); | |
107 | $pager->setMaxPerPage($perPage); | |
108 | ||
109 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | |
110 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | |
111 | $pager, | |
112 | new Route( | |
113 | 'api_get_entries', | |
114 | [ | |
115 | 'archive' => $isArchived, | |
116 | 'starred' => $isStarred, | |
117 | 'sort' => $sort, | |
118 | 'order' => $order, | |
119 | 'page' => $page, | |
120 | 'perPage' => $perPage, | |
121 | 'tags' => $tags, | |
122 | 'since' => $since, | |
123 | ], | |
124 | UrlGeneratorInterface::ABSOLUTE_URL | |
125 | ) | |
126 | ); | |
127 | ||
128 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); | |
129 | ||
130 | return (new JsonResponse())->setJson($json); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Retrieve a single entry. | |
135 | * | |
136 | * @ApiDoc( | |
137 | * requirements={ | |
138 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
139 | * } | |
140 | * ) | |
141 | * | |
142 | * @return JsonResponse | |
143 | */ | |
144 | public function getEntryAction(Entry $entry) | |
145 | { | |
146 | $this->validateAuthentication(); | |
147 | $this->validateUserAccess($entry->getUser()->getId()); | |
148 | ||
149 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
150 | ||
151 | return (new JsonResponse())->setJson($json); | |
152 | } | |
153 | ||
864c1dd2 JB |
154 | /** |
155 | * Retrieve a single entry as a predefined format. | |
156 | * | |
157 | * @ApiDoc( | |
158 | * requirements={ | |
159 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
160 | * } | |
161 | * ) | |
162 | * | |
163 | * @return Response | |
164 | */ | |
165 | public function getEntryExportAction(Entry $entry, Request $request) | |
166 | { | |
167 | $this->validateAuthentication(); | |
168 | $this->validateUserAccess($entry->getUser()->getId()); | |
169 | ||
170 | return $this->get('wallabag_core.helper.entries_export') | |
171 | ->setEntries($entry) | |
172 | ->updateTitle('entry') | |
173 | ->exportAs($request->attributes->get('_format')); | |
174 | } | |
175 | ||
1eca7831 | 176 | /** |
a7abcc7b | 177 | * Handles an entries list and delete URL. |
1eca7831 NL |
178 | * |
179 | * @ApiDoc( | |
180 | * parameters={ | |
a7abcc7b | 181 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} |
1eca7831 NL |
182 | * } |
183 | * ) | |
184 | * | |
185 | * @return JsonResponse | |
186 | */ | |
a7abcc7b | 187 | public function deleteEntriesListAction(Request $request) |
1eca7831 NL |
188 | { |
189 | $this->validateAuthentication(); | |
190 | ||
a7abcc7b | 191 | $urls = json_decode($request->query->get('urls', [])); |
1eca7831 NL |
192 | $results = []; |
193 | ||
194 | // handle multiple urls | |
a7abcc7b | 195 | if (!empty($urls)) { |
1eca7831 | 196 | $results = []; |
a7abcc7b | 197 | foreach ($urls as $key => $url) { |
1eca7831 | 198 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
a7abcc7b | 199 | $url, |
1eca7831 NL |
200 | $this->getUser()->getId() |
201 | ); | |
202 | ||
a7abcc7b NL |
203 | $results[$key]['url'] = $url; |
204 | ||
205 | if (false !== $entry) { | |
206 | $em = $this->getDoctrine()->getManager(); | |
207 | $em->remove($entry); | |
208 | $em->flush(); | |
209 | ||
210 | // entry deleted, dispatch event about it! | |
211 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
212 | } | |
1eca7831 | 213 | |
a7abcc7b NL |
214 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; |
215 | } | |
216 | } | |
1eca7831 | 217 | |
a7abcc7b | 218 | $json = $this->get('serializer')->serialize($results, 'json'); |
1eca7831 | 219 | |
a7abcc7b NL |
220 | return (new JsonResponse())->setJson($json); |
221 | } | |
1eca7831 | 222 | |
a7abcc7b NL |
223 | /** |
224 | * Handles an entries list and create URL. | |
225 | * | |
226 | * @ApiDoc( | |
227 | * parameters={ | |
228 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | |
229 | * } | |
230 | * ) | |
231 | * | |
232 | * @return JsonResponse | |
efd351c9 NL |
233 | * |
234 | * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached | |
a7abcc7b NL |
235 | */ |
236 | public function postEntriesListAction(Request $request) | |
237 | { | |
238 | $this->validateAuthentication(); | |
1eca7831 | 239 | |
a7abcc7b NL |
240 | $urls = json_decode($request->query->get('urls', [])); |
241 | $results = []; | |
1eca7831 | 242 | |
efd351c9 NL |
243 | $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); |
244 | ||
245 | if (count($urls) > $limit) { | |
246 | throw new Exception('API limit reached'); | |
247 | } | |
248 | ||
a7abcc7b NL |
249 | // handle multiple urls |
250 | if (!empty($urls)) { | |
a7abcc7b NL |
251 | foreach ($urls as $key => $url) { |
252 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
253 | $url, | |
254 | $this->getUser()->getId() | |
255 | ); | |
1eca7831 | 256 | |
a7abcc7b | 257 | $results[$key]['url'] = $url; |
1eca7831 | 258 | |
a7abcc7b NL |
259 | if (false === $entry) { |
260 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | |
261 | new Entry($this->getUser()), | |
262 | $url | |
263 | ); | |
1eca7831 | 264 | } |
a7abcc7b NL |
265 | |
266 | $em = $this->getDoctrine()->getManager(); | |
267 | $em->persist($entry); | |
268 | $em->flush(); | |
269 | ||
270 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
271 | ||
272 | // entry saved, dispatch event about it! | |
273 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
1eca7831 NL |
274 | } |
275 | } | |
276 | ||
277 | $json = $this->get('serializer')->serialize($results, 'json'); | |
278 | ||
279 | return (new JsonResponse())->setJson($json); | |
280 | } | |
281 | ||
900c8448 NL |
282 | /** |
283 | * Create an entry. | |
284 | * | |
285 | * @ApiDoc( | |
286 | * parameters={ | |
287 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
288 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
289 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
290 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, | |
291 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, | |
292 | * } | |
293 | * ) | |
294 | * | |
295 | * @return JsonResponse | |
296 | */ | |
297 | public function postEntriesAction(Request $request) | |
298 | { | |
299 | $this->validateAuthentication(); | |
300 | ||
301 | $url = $request->request->get('url'); | |
302 | $title = $request->request->get('title'); | |
303 | $isArchived = $request->request->get('archive'); | |
304 | $isStarred = $request->request->get('starred'); | |
305 | ||
306 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); | |
307 | ||
308 | if (false === $entry) { | |
309 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | |
310 | new Entry($this->getUser()), | |
311 | $url | |
312 | ); | |
313 | } | |
314 | ||
315 | if (!is_null($title)) { | |
316 | $entry->setTitle($title); | |
317 | } | |
318 | ||
319 | $tags = $request->request->get('tags', ''); | |
320 | if (!empty($tags)) { | |
321 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
322 | } | |
323 | ||
324 | if (!is_null($isStarred)) { | |
325 | $entry->setStarred((bool) $isStarred); | |
326 | } | |
327 | ||
328 | if (!is_null($isArchived)) { | |
329 | $entry->setArchived((bool) $isArchived); | |
330 | } | |
331 | ||
332 | $em = $this->getDoctrine()->getManager(); | |
333 | $em->persist($entry); | |
900c8448 NL |
334 | $em->flush(); |
335 | ||
5a619812 JB |
336 | // entry saved, dispatch event about it! |
337 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
338 | ||
900c8448 NL |
339 | $json = $this->get('serializer')->serialize($entry, 'json'); |
340 | ||
341 | return (new JsonResponse())->setJson($json); | |
342 | } | |
343 | ||
344 | /** | |
345 | * Change several properties of an entry. | |
346 | * | |
347 | * @ApiDoc( | |
348 | * requirements={ | |
349 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
350 | * }, | |
351 | * parameters={ | |
352 | * {"name"="title", "dataType"="string", "required"=false}, | |
353 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
354 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | |
355 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | |
356 | * } | |
357 | * ) | |
358 | * | |
359 | * @return JsonResponse | |
360 | */ | |
361 | public function patchEntriesAction(Entry $entry, Request $request) | |
362 | { | |
363 | $this->validateAuthentication(); | |
364 | $this->validateUserAccess($entry->getUser()->getId()); | |
365 | ||
366 | $title = $request->request->get('title'); | |
367 | $isArchived = $request->request->get('archive'); | |
368 | $isStarred = $request->request->get('starred'); | |
369 | ||
370 | if (!is_null($title)) { | |
371 | $entry->setTitle($title); | |
372 | } | |
373 | ||
374 | if (!is_null($isArchived)) { | |
375 | $entry->setArchived((bool) $isArchived); | |
376 | } | |
377 | ||
378 | if (!is_null($isStarred)) { | |
379 | $entry->setStarred((bool) $isStarred); | |
380 | } | |
381 | ||
382 | $tags = $request->request->get('tags', ''); | |
383 | if (!empty($tags)) { | |
384 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
385 | } | |
386 | ||
387 | $em = $this->getDoctrine()->getManager(); | |
388 | $em->flush(); | |
389 | ||
390 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
391 | ||
392 | return (new JsonResponse())->setJson($json); | |
393 | } | |
394 | ||
0a6f4568 JB |
395 | /** |
396 | * Reload an entry. | |
5cd0857e | 397 | * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). |
0a6f4568 JB |
398 | * |
399 | * @ApiDoc( | |
400 | * requirements={ | |
401 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
402 | * } | |
403 | * ) | |
404 | * | |
405 | * @return JsonResponse | |
406 | */ | |
407 | public function patchEntriesReloadAction(Entry $entry) | |
408 | { | |
409 | $this->validateAuthentication(); | |
410 | $this->validateUserAccess($entry->getUser()->getId()); | |
411 | ||
0a6f4568 JB |
412 | try { |
413 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
414 | } catch (\Exception $e) { | |
415 | $this->get('logger')->error('Error while saving an entry', [ | |
416 | 'exception' => $e, | |
417 | 'entry' => $entry, | |
418 | ]); | |
419 | ||
5cd0857e | 420 | return new JsonResponse([], 304); |
0a6f4568 JB |
421 | } |
422 | ||
423 | // if refreshing entry failed, don't save it | |
424 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
5cd0857e | 425 | return new JsonResponse([], 304); |
0a6f4568 JB |
426 | } |
427 | ||
428 | $em = $this->getDoctrine()->getManager(); | |
429 | $em->persist($entry); | |
430 | $em->flush(); | |
431 | ||
432 | // entry saved, dispatch event about it! | |
433 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
434 | ||
435 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
436 | ||
437 | return (new JsonResponse())->setJson($json); | |
438 | } | |
439 | ||
900c8448 NL |
440 | /** |
441 | * Delete **permanently** an entry. | |
442 | * | |
443 | * @ApiDoc( | |
444 | * requirements={ | |
445 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
446 | * } | |
447 | * ) | |
448 | * | |
449 | * @return JsonResponse | |
450 | */ | |
451 | public function deleteEntriesAction(Entry $entry) | |
452 | { | |
453 | $this->validateAuthentication(); | |
454 | $this->validateUserAccess($entry->getUser()->getId()); | |
455 | ||
456 | $em = $this->getDoctrine()->getManager(); | |
457 | $em->remove($entry); | |
458 | $em->flush(); | |
459 | ||
5a619812 JB |
460 | // entry deleted, dispatch event about it! |
461 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
462 | ||
900c8448 NL |
463 | $json = $this->get('serializer')->serialize($entry, 'json'); |
464 | ||
465 | return (new JsonResponse())->setJson($json); | |
466 | } | |
467 | ||
468 | /** | |
469 | * Retrieve all tags for an entry. | |
470 | * | |
471 | * @ApiDoc( | |
472 | * requirements={ | |
473 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
474 | * } | |
475 | * ) | |
476 | * | |
477 | * @return JsonResponse | |
478 | */ | |
479 | public function getEntriesTagsAction(Entry $entry) | |
480 | { | |
481 | $this->validateAuthentication(); | |
482 | $this->validateUserAccess($entry->getUser()->getId()); | |
483 | ||
484 | $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); | |
485 | ||
486 | return (new JsonResponse())->setJson($json); | |
487 | } | |
488 | ||
489 | /** | |
490 | * Add one or more tags to an entry. | |
491 | * | |
492 | * @ApiDoc( | |
493 | * requirements={ | |
494 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
495 | * }, | |
496 | * parameters={ | |
497 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
498 | * } | |
499 | * ) | |
500 | * | |
501 | * @return JsonResponse | |
502 | */ | |
503 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
504 | { | |
505 | $this->validateAuthentication(); | |
506 | $this->validateUserAccess($entry->getUser()->getId()); | |
507 | ||
508 | $tags = $request->request->get('tags', ''); | |
509 | if (!empty($tags)) { | |
510 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
511 | } | |
512 | ||
513 | $em = $this->getDoctrine()->getManager(); | |
514 | $em->persist($entry); | |
515 | $em->flush(); | |
516 | ||
517 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
518 | ||
519 | return (new JsonResponse())->setJson($json); | |
520 | } | |
521 | ||
522 | /** | |
523 | * Permanently remove one tag for an entry. | |
524 | * | |
525 | * @ApiDoc( | |
526 | * requirements={ | |
527 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
528 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
529 | * } | |
530 | * ) | |
531 | * | |
532 | * @return JsonResponse | |
533 | */ | |
534 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
535 | { | |
536 | $this->validateAuthentication(); | |
537 | $this->validateUserAccess($entry->getUser()->getId()); | |
538 | ||
539 | $entry->removeTag($tag); | |
540 | $em = $this->getDoctrine()->getManager(); | |
541 | $em->persist($entry); | |
542 | $em->flush(); | |
543 | ||
544 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
545 | ||
546 | return (new JsonResponse())->setJson($json); | |
547 | } | |
d1fc5902 NL |
548 | |
549 | /** | |
80299ed2 | 550 | * Handles an entries list delete tags from them. |
d1fc5902 NL |
551 | * |
552 | * @ApiDoc( | |
553 | * parameters={ | |
80299ed2 | 554 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."} |
d1fc5902 NL |
555 | * } |
556 | * ) | |
557 | * | |
558 | * @return JsonResponse | |
559 | */ | |
80299ed2 | 560 | public function deleteEntriesTagsListAction(Request $request) |
d1fc5902 NL |
561 | { |
562 | $this->validateAuthentication(); | |
563 | ||
564 | $list = json_decode($request->query->get('list', [])); | |
565 | $results = []; | |
566 | ||
567 | // handle multiple urls | |
568 | if (!empty($list)) { | |
d1fc5902 NL |
569 | foreach ($list as $key => $element) { |
570 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
571 | $element->url, | |
572 | $this->getUser()->getId() | |
573 | ); | |
574 | ||
575 | $results[$key]['url'] = $element->url; | |
d1fc5902 NL |
576 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; |
577 | ||
578 | $tags = $element->tags; | |
579 | ||
580 | if (false !== $entry && !(empty($tags))) { | |
80299ed2 NL |
581 | $tags = explode(',', $tags); |
582 | foreach ($tags as $label) { | |
583 | $label = trim($label); | |
584 | ||
585 | $tag = $this->getDoctrine() | |
586 | ->getRepository('WallabagCoreBundle:Tag') | |
587 | ->findOneByLabel($label); | |
588 | ||
589 | if (false !== $tag) { | |
590 | $entry->removeTag($tag); | |
591 | } | |
d1fc5902 NL |
592 | } |
593 | ||
594 | $em = $this->getDoctrine()->getManager(); | |
595 | $em->persist($entry); | |
596 | $em->flush(); | |
597 | } | |
598 | } | |
599 | } | |
600 | ||
601 | $json = $this->get('serializer')->serialize($results, 'json'); | |
602 | ||
603 | return (new JsonResponse())->setJson($json); | |
604 | } | |
80299ed2 NL |
605 | |
606 | /** | |
607 | * Handles an entries list and add tags to them. | |
608 | * | |
609 | * @ApiDoc( | |
610 | * parameters={ | |
611 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."} | |
612 | * } | |
613 | * ) | |
614 | * | |
615 | * @return JsonResponse | |
616 | */ | |
617 | public function postEntriesTagsListAction(Request $request) | |
618 | { | |
619 | $this->validateAuthentication(); | |
620 | ||
621 | $list = json_decode($request->query->get('list', [])); | |
622 | $results = []; | |
623 | ||
624 | // handle multiple urls | |
625 | if (!empty($list)) { | |
80299ed2 NL |
626 | foreach ($list as $key => $element) { |
627 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
628 | $element->url, | |
629 | $this->getUser()->getId() | |
630 | ); | |
631 | ||
632 | $results[$key]['url'] = $element->url; | |
633 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
634 | ||
635 | $tags = $element->tags; | |
636 | ||
637 | if (false !== $entry && !(empty($tags))) { | |
638 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
639 | ||
640 | $em = $this->getDoctrine()->getManager(); | |
641 | $em->persist($entry); | |
642 | $em->flush(); | |
643 | } | |
644 | } | |
645 | } | |
646 | ||
647 | $json = $this->get('serializer')->serialize($results, 'json'); | |
648 | ||
649 | return (new JsonResponse())->setJson($json); | |
650 | } | |
900c8448 | 651 | } |