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