diff --git a/rest/index.php b/rest/index.php index 19e7f8f07242e76ee7c010de175f44f91afa45a4..cabb20a0a5b607c4702ca4d958b9e0613453f70f 100755 --- a/rest/index.php +++ b/rest/index.php @@ -460,8 +460,6 @@ $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'); -$app->delete('/tags/{tagId}/link/{id}', \Tag\controllers\TagController::class . ':unLink'); //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 f7c8bc8e3897e2dc33a3df3b7f88d1cb2bdd687d..796107bdcb97772f41c845036757296395c9d1e5 100644 --- a/src/app/tag/controllers/TagController.php +++ b/src/app/tag/controllers/TagController.php @@ -210,12 +210,49 @@ class TagController } } + $links = []; + + $tag = TagModel::getById(['id' => $args['id']]); + $tag['links'] = json_decode($tag['links']); + + if (!empty($body['links'])) { + if (!Validator::arrayType()->validate($body['links'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body links is not an array']); + } elseif (in_array($args['id'], $body['links'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body links contains tag']); + } + + foreach ($body['links'] as $link) { + $links[] = (string)$link; + } + + $newLinks = array_diff($links, $tag['links']); + TagModel::update([ + 'postSet' => ['links' => "jsonb_insert(links, '{0}', '\"{$args['id']}\"')"], + 'where' => ['id in (?)', "(links @> ?) = false"], + 'data' => [$newLinks, "\"{$args['id']}\""] + ]); + } + + if (!empty($tag['links'])) { + $linksToDelete = array_diff($tag['links'], $links); + + if (!empty($linksToDelete)) { + TagModel::update([ + 'postSet' => ['links' => "links - '{$tag['id']}'"], + 'where' => ['id in (?)'], + 'data' => [$linksToDelete] + ]); + } + } + TagModel::update([ 'set' => [ 'label' => $body['label'], 'description' => $body['description'] ?? null, 'parent_id' => $parent, - 'usage' => $body['usage'] ?? null + 'usage' => $body['usage'] ?? null, + 'links' => json_encode($links) ], 'where' => ['id = ?'], 'data' => [$args['id']] @@ -378,116 +415,6 @@ class TagController return $response->withStatus(204); } - public static function link(Request $request, Response $response, array $args) - { - if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_tag', 'userId' => $GLOBALS['id']])) { - return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); - } - - if (!Validator::intVal()->validate($args['id'])) { - return $response->withStatus(400)->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['links'] as $value) { - HistoryController::add([ - 'tableName' => 'tags', - 'recordId' => $args['id'], - 'eventType' => 'UP', - 'info' => _LINK_ADDED_TAG . " : {$linkedTagsInfo[$value]}", - 'eventId' => 'tagModification' - ]); - HistoryController::add([ - 'tableName' => 'tags', - 'recordId' => $value, - 'eventType' => 'UP', - 'info' => _LINK_ADDED_TAG . " : {$tag['label']}", - 'eventId' => 'tagModification' - ]); - } - - return $response->withStatus(204); - } - - public static function unLink(Request $request, Response $response, array $args) - { - if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_tag', 'userId' => $GLOBALS['id']])) { - return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); - } - - if (!Validator::intVal()->validate($args['tagId']) || !Validator::intVal()->validate($args['id'])) { - return $response->withStatus(400)->withJson(['errors' => 'Route tagId or id is not an integer']); - } - - TagModel::update([ - 'postSet' => ['links' => "links - '{$args['id']}'"], - 'where' => ['id = ?'], - 'data' => [$args['tagId']] - ]); - TagModel::update([ - 'postSet' => ['links' => "links - '{$args['tagId']}'"], - 'where' => ['id = ?'], - 'data' => [$args['id']] - ]); - - $linkedTagsInfo = TagModel::get([ - 'select' => ['label', 'id'], - 'where' => ['id in (?)'], - 'data' => [[$args['tagId'], $args['id']]] - ]); - $linkedTagsInfo = array_column($linkedTagsInfo, 'label', 'id'); - - HistoryController::add([ - 'tableName' => 'tags', - 'recordId' => $args['tagId'], - 'eventType' => 'UP', - 'info' => _LINK_DELETED_TAG . " : {$linkedTagsInfo[$args['id']]}", - 'eventId' => 'tagModification' - ]); - HistoryController::add([ - 'tableName' => 'tags', - 'recordId' => $args['id'], - 'eventType' => 'UP', - 'info' => _LINK_DELETED_TAG . " : {$linkedTagsInfo[$args['tagId']]}", - 'eventId' => 'tagModification' - ]); - - return $response->withStatus(204); - } - private static function tagIsInChildren(array $args) { ValidatorModel::notEmpty($args, ['idToFind', 'parentId']);