2016-10-28 12:46:30 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-31 08:28:37 +00:00
|
|
|
namespace Wallabag\CoreBundle\Controller\Api;
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-11-06 12:00:41 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
2022-12-23 13:40:42 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2016-10-28 12:46:30 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-11-23 11:44:55 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2016-10-28 12:46:30 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
2022-12-19 09:37:22 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
|
|
|
use Wallabag\CoreBundle\Repository\TagRepository;
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
class TagRestController extends WallabagRestController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Retrieve all tags.
|
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Tags"},
|
|
|
|
* summary="Retrieve all tags.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
|
|
|
* description="Returned when successful"
|
|
|
|
* )
|
|
|
|
* )
|
2016-10-28 12:46:30 +00:00
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/tags.{_format}", methods={"GET"}, name="api_get_tags", defaults={"_format": "json"})
|
|
|
|
*
|
2016-10-28 12:46:30 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function getTagsAction(TagRepository $tagRepository)
|
2016-10-28 12:46:30 +00:00
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$tags = $tagRepository->findAllFlatTagsWithNbEntries($this->getUser()->getId());
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($tags, 'json');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-24 10:24:17 +00:00
|
|
|
* Permanently remove one tag from **every** entry by passing the Tag label.
|
2016-10-28 12:46:30 +00:00
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Tags"},
|
|
|
|
* summary="Permanently remove one tag from every entry by passing the Tag label.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Parameter(
|
2022-11-06 12:00:41 +00:00
|
|
|
* name="tag",
|
2022-12-23 13:40:42 +00:00
|
|
|
* in="query",
|
2022-11-06 12:00:41 +00:00
|
|
|
* description="Tag as a string",
|
|
|
|
* required=true,
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* pattern="\w+",
|
|
|
|
* )
|
2022-11-06 12:00:41 +00:00
|
|
|
* ),
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
|
|
|
* description="Returned when successful"
|
|
|
|
* )
|
2016-10-28 12:46:30 +00:00
|
|
|
* )
|
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/tag/label.{_format}", methods={"DELETE"}, name="api_delete_tag_label", defaults={"_format": "json"})
|
|
|
|
*
|
2016-10-28 12:46:30 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function deleteTagLabelAction(Request $request, TagRepository $tagRepository, EntryRepository $entryRepository)
|
2016-10-28 12:46:30 +00:00
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
2017-05-09 21:15:25 +00:00
|
|
|
$label = $request->get('tag', '');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$tags = $tagRepository->findByLabelsAndUser([$label], $this->getUser()->getId());
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2018-12-29 19:42:41 +00:00
|
|
|
if (empty($tags)) {
|
2016-10-28 12:46:30 +00:00
|
|
|
throw $this->createNotFoundException('Tag not found');
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:42:41 +00:00
|
|
|
$tag = $tags[0];
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entryRepository->removeTag($this->getUser()->getId(), $tag);
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
$this->cleanOrphanTag($tag);
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($tag, 'json');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permanently remove some tags from **every** entry.
|
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Tags"},
|
|
|
|
* summary="Permanently remove some tags from every entry.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Parameter(
|
2022-11-06 12:00:41 +00:00
|
|
|
* name="tags",
|
2022-12-23 13:40:42 +00:00
|
|
|
* in="query",
|
2022-11-06 12:00:41 +00:00
|
|
|
* description="Tags as strings (comma splitted)",
|
|
|
|
* required=true,
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Schema(
|
2022-11-06 12:00:41 +00:00
|
|
|
* type="string",
|
|
|
|
* example="tag1,tag2",
|
|
|
|
* )
|
|
|
|
* ),
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
|
|
|
* description="Returned when successful"
|
|
|
|
* )
|
2016-10-28 12:46:30 +00:00
|
|
|
* )
|
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/tags/label.{_format}", methods={"DELETE"}, name="api_delete_tags_label", defaults={"_format": "json"})
|
|
|
|
*
|
2016-10-28 12:46:30 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function deleteTagsLabelAction(Request $request, TagRepository $tagRepository, EntryRepository $entryRepository)
|
2016-10-28 12:46:30 +00:00
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2017-05-09 21:15:25 +00:00
|
|
|
$tagsLabels = $request->get('tags', '');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$tags = $tagRepository->findByLabelsAndUser(explode(',', $tagsLabels), $this->getUser()->getId());
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
if (empty($tags)) {
|
|
|
|
throw $this->createNotFoundException('Tags not found');
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entryRepository->removeTags($this->getUser()->getId(), $tags);
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
$this->cleanOrphanTag($tags);
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($tags, 'json');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-24 10:24:17 +00:00
|
|
|
* Permanently remove one tag from **every** entry by passing the Tag ID.
|
2016-10-28 12:46:30 +00:00
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Tags"},
|
|
|
|
* summary="Permanently remove one tag from every entry by passing the Tag ID.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Parameter(
|
2022-11-06 12:00:41 +00:00
|
|
|
* name="tag",
|
2022-12-23 13:40:42 +00:00
|
|
|
* in="path",
|
2022-11-06 12:00:41 +00:00
|
|
|
* description="The tag",
|
|
|
|
* required=true,
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* pattern="\w+",
|
|
|
|
* )
|
2022-11-06 12:00:41 +00:00
|
|
|
* ),
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
|
|
|
* description="Returned when successful"
|
|
|
|
* )
|
2016-10-28 12:46:30 +00:00
|
|
|
* )
|
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/tags/{tag}.{_format}", methods={"DELETE"}, name="api_delete_tag", defaults={"_format": "json"})
|
|
|
|
*
|
2016-10-28 12:46:30 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function deleteTagAction(Tag $tag, TagRepository $tagRepository, EntryRepository $entryRepository)
|
2016-10-28 12:46:30 +00:00
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$tagFromDb = $tagRepository->findByLabelsAndUser([$tag->getLabel()], $this->getUser()->getId());
|
2018-12-29 19:42:41 +00:00
|
|
|
|
|
|
|
if (empty($tagFromDb)) {
|
|
|
|
throw $this->createNotFoundException('Tag not found');
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entryRepository->removeTag($this->getUser()->getId(), $tag);
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
$this->cleanOrphanTag($tag);
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($tag, 'json');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove orphan tag in case no entries are associated to it.
|
|
|
|
*
|
|
|
|
* @param Tag|array $tags
|
|
|
|
*/
|
|
|
|
private function cleanOrphanTag($tags)
|
|
|
|
{
|
2018-09-05 12:25:32 +00:00
|
|
|
if (!\is_array($tags)) {
|
2016-10-28 12:46:30 +00:00
|
|
|
$tags = [$tags];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
2018-09-05 12:25:32 +00:00
|
|
|
if (0 === \count($tag->getEntries())) {
|
2022-12-19 09:37:22 +00:00
|
|
|
$this->entityManager->remove($tag);
|
2016-10-28 12:46:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$this->entityManager->flush();
|
2016-10-28 12:46:30 +00:00
|
|
|
}
|
|
|
|
}
|