fix typo and cleanup

This commit is contained in:
d-buchmann 2025-05-23 13:28:37 +02:00
parent 2e15f87f6e
commit c86f04a50a

View file

@ -69,13 +69,11 @@ final class PartsTableActionHandler
*/
public function handleAction(string $action, array $selected_parts, ?string $target_id, ?string $redirect_url = null, array &$errors = []): ?RedirectResponse
{
// sanitize target_id
if (!str_contains($action, 'tag') && $target_id !== null)
{
if (!is_numeric($target_id)
// 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 ($action === 'add_to_project') {
return new RedirectResponse(
$this->urlGenerator->generate('project_add_parts', [
@ -145,10 +143,10 @@ implode(',', array_map(static fn (PartLot $lot) => $lot->getID(), $part->getPart
break;
case "remove_tag":
$this->denyAccessUnlessGranted('edit', $part);
// remove tag at start or end
$tags = preg_replace("/(^".$target_id.",|,".$target_id."$)/", '', $part->getTags());
// remove tag in the middle, retaining one comma
$tags = str_replace(','.$target_id.',', ',' $tags);
// 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);