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