From 8219f623b465badec2330950d46304050e7c2b22 Mon Sep 17 00:00:00 2001 From: Guillaume Heurtier <guillaume.heurtier@maarch.org> Date: Tue, 11 Feb 2020 11:25:23 +0100 Subject: [PATCH] FEAT #12471 TIME 0:25 added link tags --- rest/index.php | 1 + src/app/tag/controllers/TagController.php | 60 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/rest/index.php b/rest/index.php index fd1d4ddbced..d22809aa39d 100755 --- a/rest/index.php +++ b/rest/index.php @@ -445,6 +445,7 @@ $app->get('/tags/{id}', \Tag\controllers\TagController::class . ':getById'); $app->put('/tags/{id}', \Tag\controllers\TagController::class . ':update'); $app->put('/mergeTags', \Tag\controllers\TagController::class . ':merge'); $app->delete('/tags/{id}', \Tag\controllers\TagController::class . ':delete'); +$app->put('/tags/{id}/link', \Tag\controllers\TagController::class . ':link'); //Templates $app->get('/templates', \Template\controllers\TemplateController::class . ':get'); diff --git a/src/app/tag/controllers/TagController.php b/src/app/tag/controllers/TagController.php index 59b3271d216..2062f36a06c 100644 --- a/src/app/tag/controllers/TagController.php +++ b/src/app/tag/controllers/TagController.php @@ -271,4 +271,64 @@ class TagController return $response->withStatus(204); } + + public static function link(Request $request, Response $response, array $args) + { + if (!Validator::intVal()->validate($args['id'])) { + return $response->withStatus(403)->withJson(['errors' => 'Route id is not an integer']); + } + + $body = $request->getParsedBody(); + + if (!Validator::arrayType()->notEmpty()->validate($body['links'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body links is empty or not an array']); + } elseif (in_array($args['id'], $body['links'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body links contains tag']); + } + + $tag = TagModel::getById(['id' => $args['id']]); + $linkedTags = json_decode($tag['links'], true); + $linkedTags = array_merge($linkedTags, $body['links']); + $linkedTags = array_unique($linkedTags); + foreach ($linkedTags as $key => $linkedTag) { + $linkedTags[$key] = (string)$linkedTag; + } + + TagModel::update([ + 'set' => ['links' => json_encode($linkedTags)], + 'where' => ['id = ?'], + 'data' => [$args['id']] + ]); + TagModel::update([ + 'postSet' => ['links' => "jsonb_insert(links, '{0}', '\"{$args['id']}\"')"], + 'where' => ['id in (?)', "(links @> ?) = false"], + 'data' => [$body['links'], "\"{$args['id']}\""] + ]); + + $linkedTagsInfo = TagModel::get([ + 'select' => ['label', 'id'], + 'where' => ['id in (?)'], + 'data' => [$body['links']] + ]); + $linkedTagsInfo = array_column($linkedTagsInfo, 'label', 'id'); + + foreach ($body['linkedResources'] as $value) { + HistoryController::add([ + 'tableName' => 'tags', + 'recordId' => $args['resId'], + 'eventType' => 'UP', + 'info' => _LINK_ADDED . " : {$linkedTagsInfo[$value]}", + 'eventId' => 'tagModification' + ]); + HistoryController::add([ + 'tableName' => 'tags', + 'recordId' => $value, + 'eventType' => 'UP', + 'info' => _LINK_ADDED . " : {$tag['label']}", + 'eventId' => 'tagModification' + ]); + } + + return $response->withStatus(204); + } } -- GitLab