2016-10-28 12:46:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ApiBundle\Controller;
|
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use JMS\Serializer\SerializerInterface;
|
2022-11-06 12:00:41 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
|
|
|
use Swagger\Annotations as SWG;
|
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;
|
|
|
|
|
|
|
|
class TagRestController extends WallabagRestController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Retrieve all tags.
|
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Tags"},
|
|
|
|
* summary="Retrieve all tags.",
|
|
|
|
* @SWG\Response(
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function getTagsAction()
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$tags = $this->get('doctrine')
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Tag::class)
|
2022-09-23 13:16:38 +00:00
|
|
|
->findAllFlatTagsWithNbEntries($this->getUser()->getId());
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$json = $this->get(SerializerInterface::class)->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.",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="tag",
|
|
|
|
* in="body",
|
|
|
|
* description="Tag as a string",
|
|
|
|
* required=true,
|
|
|
|
* pattern="\w+",
|
|
|
|
* @SWG\Schema(type="string")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function deleteTagLabelAction(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
2017-05-09 21:15:25 +00:00
|
|
|
$label = $request->get('tag', '');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$tags = $this->get('doctrine')->getRepository(Tag::class)->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-11-23 14:51:33 +00:00
|
|
|
$this->get('doctrine')
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2016-10-28 12:46:30 +00:00
|
|
|
->removeTag($this->getUser()->getId(), $tag);
|
|
|
|
|
|
|
|
$this->cleanOrphanTag($tag);
|
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$json = $this->get(SerializerInterface::class)->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.",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="tags",
|
|
|
|
* in="body",
|
|
|
|
* description="Tags as strings (comma splitted)",
|
|
|
|
* required=true,
|
|
|
|
* @SWG\Schema(
|
|
|
|
* type="string",
|
|
|
|
* example="tag1,tag2",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function deleteTagsLabelAction(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2017-05-09 21:15:25 +00:00
|
|
|
$tagsLabels = $request->get('tags', '');
|
2016-10-28 12:46:30 +00:00
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$tags = $this->get('doctrine')->getRepository(Tag::class)->findByLabelsAndUser(explode(',', $tagsLabels), $this->getUser()->getId());
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
if (empty($tags)) {
|
|
|
|
throw $this->createNotFoundException('Tags not found');
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$this->get('doctrine')
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2016-10-28 12:46:30 +00:00
|
|
|
->removeTags($this->getUser()->getId(), $tags);
|
|
|
|
|
|
|
|
$this->cleanOrphanTag($tags);
|
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$json = $this->get(SerializerInterface::class)->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.",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="tag",
|
|
|
|
* in="body",
|
|
|
|
* description="The tag",
|
|
|
|
* required=true,
|
|
|
|
* pattern="\w+",
|
|
|
|
* @SWG\Schema(type="integer")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function deleteTagAction(Tag $tag)
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$tagFromDb = $this->get('doctrine')->getRepository(Tag::class)->findByLabelsAndUser([$tag->getLabel()], $this->getUser()->getId());
|
2018-12-29 19:42:41 +00:00
|
|
|
|
|
|
|
if (empty($tagFromDb)) {
|
|
|
|
throw $this->createNotFoundException('Tag not found');
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$this->get('doctrine')
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2016-10-28 12:46:30 +00:00
|
|
|
->removeTag($this->getUser()->getId(), $tag);
|
|
|
|
|
|
|
|
$this->cleanOrphanTag($tag);
|
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$json = $this->get(SerializerInterface::class)->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];
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$em = $this->get('doctrine')->getManager();
|
2016-10-28 12:46:30 +00:00
|
|
|
|
|
|
|
foreach ($tags as $tag) {
|
2018-09-05 12:25:32 +00:00
|
|
|
if (0 === \count($tag->getEntries())) {
|
2016-10-28 12:46:30 +00:00
|
|
|
$em->remove($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$em->flush();
|
|
|
|
}
|
|
|
|
}
|