From 8b2d45c5b13b65215f3be3394905e47a5c83ce1a Mon Sep 17 00:00:00 2001 From: buchmann Date: Tue, 7 Oct 2025 08:50:51 +0200 Subject: [PATCH] Update PartsTableActionHandler.php --- .../Parts/PartsTableActionHandler.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Services/Parts/PartsTableActionHandler.php b/src/Services/Parts/PartsTableActionHandler.php index 584c41e2..7a737c7d 100644 --- a/src/Services/Parts/PartsTableActionHandler.php +++ b/src/Services/Parts/PartsTableActionHandler.php @@ -70,8 +70,8 @@ final class PartsTableActionHandler public function handleAction(string $action, array $selected_parts, ?string $target_id, ?string $redirect_url = null, array &$errors = []): ?RedirectResponse { // validate target_id - if (!str_contains($action, 'tag') && $target_id !== null && !is_numeric($target_id)) - throw new InvalidArgumentException('$target_id must be an integer for action'. $action.'!'); + if (!str_contains($action, 'tag') && $target_id !== null && !is_numeric($target_id)) { + throw new InvalidArgumentException('$target_id must be an integer for action '. $action.'!'); } if ($action === 'add_to_project') { @@ -136,17 +136,24 @@ implode(',', array_map(static fn (PartLot $lot) => $lot->getID(), $part->getPart switch ($action) { case "add_tag": - $this->denyAccessUnlessGranted('edit', $part); - $tags = $part->getTags(); - // simple append - $part->setTags($tags.','.$target_id); + if ($target_id !== null) + { + $this->denyAccessUnlessGranted('edit', $part); + $tags = $part->getTags(); + // simply append the tag but and avoid duplicates + if (!str_contains($tags, $target_id)) + $part->setTags($tags.','.$target_id); + } break; case "remove_tag": - $this->denyAccessUnlessGranted('edit', $part); - // remove any matching tag at start or end - $tags = preg_replace('/(^'.$target_id.',|,'.$target_id.'$)/', '', $part->getTags()); - // remove any matching tags in the middle, retaining one comma, and commit - $part->setTags(str_replace(','.$target_id.',', ',' $tags); + if ($target_id !== null) + { + $this->denyAccessUnlessGranted('edit', $part); + // remove any matching tag at start or end + $tags = preg_replace('/(^'.$target_id.',|,'.$target_id.'$)/', '', $part->getTags()); + // remove any matching tags in the middle, retaining one comma, and commit + $part->setTags(str_replace(','.$target_id.',', ',', $tags)); + } break; case 'favorite': $this->denyAccessUnlessGranted('change_favorite', $part);