IPN-Vorschlagslogik um Konfiguration erweitert

This commit is contained in:
Marcel Diegelmann 2025-09-25 10:26:34 +02:00
parent 3100c83246
commit a2f53290f4
14 changed files with 163 additions and 51 deletions

View file

@ -47,6 +47,7 @@ use App\Services\Parts\PartLotWithdrawAddHelper;
use App\Services\Parts\PricedetailHelper;
use App\Services\ProjectSystem\ProjectBuildPartHelper;
use App\Settings\BehaviorSettings\PartInfoSettings;
use App\Settings\MiscSettings\IpnSuggestSettings;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
@ -74,7 +75,7 @@ final class PartController extends AbstractController
private readonly EntityManagerInterface $em,
private readonly EventCommentHelper $commentHelper,
private readonly PartInfoSettings $partInfoSettings,
private readonly int $autocompletePartDigits,
private readonly IpnSuggestSettings $ipnSuggestSettings,
) {
}
@ -451,7 +452,7 @@ final class PartController extends AbstractController
$template,
[
'part' => $new_part,
'ipnSuggestions' => $partRepository->autoCompleteIpn($data, base64_encode($data->getDescription()), $this->autocompletePartDigits),
'ipnSuggestions' => $partRepository->autoCompleteIpn($data, base64_encode($data->getDescription()), $this->ipnSuggestSettings->suggestPartDigits),
'form' => $form,
'merge_old_name' => $merge_infos['tname_before'] ?? null,
'merge_other' => $merge_infos['other_part'] ?? null,

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Entity\Parameters\AbstractParameter;
use App\Settings\MiscSettings\IpnSuggestSettings;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Attachments\Attachment;
use App\Entity\Parts\Category;
@ -63,7 +64,7 @@ class TypeaheadController extends AbstractController
public function __construct(
protected AttachmentURLGenerator $urlGenerator,
protected Packages $assets,
protected int $autocompletePartDigits
protected IpnSuggestSettings $ipnSuggestSettings,
) {
}
@ -207,7 +208,7 @@ class TypeaheadController extends AbstractController
$clonedPart->setCategory($category);
$partRepository = $entityManager->getRepository(Part::class);
$ipnSuggestions = $partRepository->autoCompleteIpn($clonedPart, $description, $this->autocompletePartDigits);
$ipnSuggestions = $partRepository->autoCompleteIpn($clonedPart, $description, $this->ipnSuggestSettings->suggestPartDigits);
return new JsonResponse($ipnSuggestions);
}

View file

@ -3,6 +3,7 @@
namespace App\EventSubscriber\UserSystem;
use App\Entity\Parts\Part;
use App\Settings\MiscSettings\IpnSuggestSettings;
use Doctrine\Common\EventSubscriber;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
@ -12,7 +13,7 @@ class PartUniqueIpnSubscriber implements EventSubscriber
{
public function __construct(
private EntityManagerInterface $entityManager,
private readonly bool $enforceUniqueIpn = false
private IpnSuggestSettings $ipnSuggestSettings
) {
}
@ -53,7 +54,7 @@ class PartUniqueIpnSubscriber implements EventSubscriber
->findOneBy(['ipn' => $part->getIpn()]);
if ($existingPart && $existingPart->getId() !== $part->getId()) {
if ($this->enforceUniqueIpn) {
if ($this->ipnSuggestSettings->enableUniqueCheck) {
return;
}
@ -70,4 +71,4 @@ class PartUniqueIpnSubscriber implements EventSubscriber
$part->setIpn($originalIpn . "_$increment");
}
}
}
}

View file

@ -118,13 +118,13 @@ class PartRepository extends NamedDBElementRepository
*
* @param Part $part The part for which autocomplete suggestions are generated.
* @param string $description Base64-encoded description to assist in generating suggestions.
* @param int $autocompletePartDigits The number of digits used in autocomplete increments.
* @param int $suggestPartDigits The number of digits used in autocomplete increments.
*
* @return array An associative array containing the following keys:
* - 'commonPrefixes': List of common prefixes found for the part.
* - 'prefixesPartIncrement': Increments for the generated prefixes, including hierarchical prefixes.
*/
public function autoCompleteIpn(Part $part, string $description, int $autocompletePartDigits): array
public function autoCompleteIpn(Part $part, string $description, int $suggestPartDigits): array
{
$category = $part->getCategory();
$ipnSuggestions = ['commonPrefixes' => [], 'prefixesPartIncrement' => []];
@ -140,7 +140,7 @@ class PartRepository extends NamedDBElementRepository
$directIpnPrefixEmpty = $category->getPartIpnPrefix() === '';
$currentPath = $currentPath === '' ? 'n.a.' : $currentPath;
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $autocompletePartDigits);
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $suggestPartDigits);
$ipnSuggestions['commonPrefixes'][] = [
'title' => $currentPath . '-',
@ -181,7 +181,7 @@ class PartRepository extends NamedDBElementRepository
'description' => $this->translator->trans('part.edit.tab.advanced.ipn.prefix.hierarchical.no_increment')
];
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $autocompletePartDigits);
$increment = $this->generateNextPossiblePartIncrement($currentPath, $part, $suggestPartDigits);
$ipnSuggestions['prefixesPartIncrement'][] = [
'title' => $currentPath . '-' . $increment,
@ -249,18 +249,18 @@ class PartRepository extends NamedDBElementRepository
*
* @param string $currentPath The base path or prefix for the part's identifier.
* @param Part $currentPart The part entity for which the increment is being generated.
* @param int $autocompletePartDigits The number of digits reserved for the increment.
* @param int $suggestPartDigits The number of digits reserved for the increment.
*
* @return string|null The next possible increment as a zero-padded string, or null if it cannot be generated.
*
* @throws NonUniqueResultException If the query returns non-unique results.
* @throws NoResultException If the query fails to return a result.
*/
private function generateNextPossiblePartIncrement(string $currentPath, Part $currentPart, int $autocompletePartDigits): ?string
private function generateNextPossiblePartIncrement(string $currentPath, Part $currentPart, int $suggestPartDigits): ?string
{
$qb = $this->createQueryBuilder('part');
$expectedLength = strlen($currentPath) + 1 + $autocompletePartDigits; // Path + '-' + $autocompletePartDigits digits
$expectedLength = strlen($currentPath) + 1 + $suggestPartDigits; // Path + '-' + $suggestPartDigits digits
// Fetch all parts in the given category, sorted by their ID in ascending order
$qb->select('part')
@ -281,14 +281,14 @@ class PartRepository extends NamedDBElementRepository
if ($part->getId() === $currentPart->getId()) {
// Extract and return the current part's increment directly
$incrementPart = substr($part->getIpn(), -$autocompletePartDigits);
$incrementPart = substr($part->getIpn(), -$suggestPartDigits);
if (is_numeric($incrementPart)) {
return str_pad((string) $incrementPart, $autocompletePartDigits, '0', STR_PAD_LEFT);
return str_pad((string) $incrementPart, $suggestPartDigits, '0', STR_PAD_LEFT);
}
}
// Extract last $autocompletePartDigits digits for possible available part increment
$incrementPart = substr($part->getIpn(), -$autocompletePartDigits);
$incrementPart = substr($part->getIpn(), -$suggestPartDigits);
if (is_numeric($incrementPart)) {
$usedIncrements[] = (int) $incrementPart;
}
@ -302,7 +302,7 @@ class PartRepository extends NamedDBElementRepository
$nextIncrement++;
}
return str_pad((string) $nextIncrement, $autocompletePartDigits, '0', STR_PAD_LEFT);
return str_pad((string) $nextIncrement, $suggestPartDigits, '0', STR_PAD_LEFT);
}
/**

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Settings\MiscSettings;
use App\Settings\SettingsIcon;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
use Symfony\Component\Translation\TranslatableMessage as TM;
use Symfony\Component\Validator\Constraints as Assert;
#[Settings(label: new TM("settings.misc.ipn_suggest"))]
#[SettingsIcon("fa-list")]
class IpnSuggestSettings
{
use SettingsTrait;
#[SettingsParameter(
label: new TM("settings.misc.ipn_suggest.enableUniqueCheck"),
envVar: "bool:IPN_ENABLE_UNIQUE_CHECK", envVarMode: EnvVarMode::OVERWRITE,
)]
public bool $enableUniqueCheck = true;
#[SettingsParameter(label: new TM("settings.misc.ipn_suggest.suggestPartDigits"),
description: new TM("settings.misc.ipn_suggest.suggestPartDigits.help"),
formOptions: ['attr' => ['min' => 1, 'max' => 100]],
envVar: "int:IPN_SUGGEST_PART_DIGITS", envVarMode: EnvVarMode::OVERWRITE
)]
#[Assert\Range(min: 1, max: 6)]
public int $suggestPartDigits = 4;
}

View file

@ -34,4 +34,7 @@ class MiscSettings
#[EmbeddedSettings]
public ?ExchangeRateSettings $exchangeRate = null;
}
#[EmbeddedSettings]
public ?IpnSuggestSettings $ipnSuggestSettings = null;
}

View file

@ -3,6 +3,7 @@
namespace App\Validator\Constraints;
use App\Entity\Parts\Part;
use App\Settings\MiscSettings\IpnSuggestSettings;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\ORM\EntityManagerInterface;
@ -10,12 +11,12 @@ use Doctrine\ORM\EntityManagerInterface;
class UniquePartIpnValidator extends ConstraintValidator
{
private EntityManagerInterface $entityManager;
private bool $enforceUniqueIpn;
private IpnSuggestSettings $ipnSuggestSettings;
public function __construct(EntityManagerInterface $entityManager, bool $enforceUniqueIpn)
public function __construct(EntityManagerInterface $entityManager, IpnSuggestSettings $ipnSuggestSettings)
{
$this->entityManager = $entityManager;
$this->enforceUniqueIpn = $enforceUniqueIpn;
$this->ipnSuggestSettings = $ipnSuggestSettings;
}
public function validate($value, Constraint $constraint)
@ -24,7 +25,7 @@ class UniquePartIpnValidator extends ConstraintValidator
return;
}
if (!$this->enforceUniqueIpn) {
if (!$this->ipnSuggestSettings->enableUniqueCheck) {
return;
}
@ -40,12 +41,10 @@ class UniquePartIpnValidator extends ConstraintValidator
foreach ($existingParts as $existingPart) {
if ($currentPart->getId() !== $existingPart->getId()) {
if ($this->enforceUniqueIpn) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();
}
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();
}
}
}
}
}