mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-01 04:49:36 +00:00
Use symfony native functions to generate the routes for part creation
This commit is contained in:
parent
0010ee8de1
commit
5100469751
1 changed files with 22 additions and 24 deletions
|
|
@ -42,8 +42,10 @@ declare(strict_types=1);
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Form\LabelSystem\ScanDialogType;
|
use App\Form\LabelSystem\ScanDialogType;
|
||||||
|
use App\Services\InfoProviderSystem\Providers\LCSCProvider;
|
||||||
use App\Services\LabelSystem\BarcodeScanner\BarcodeRedirector;
|
use App\Services\LabelSystem\BarcodeScanner\BarcodeRedirector;
|
||||||
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanHelper;
|
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanHelper;
|
||||||
|
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanResultInterface;
|
||||||
use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
|
use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
|
||||||
use App\Services\LabelSystem\BarcodeScanner\LocalBarcodeScanResult;
|
use App\Services\LabelSystem\BarcodeScanner\LocalBarcodeScanResult;
|
||||||
use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult;
|
use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult;
|
||||||
|
|
@ -105,7 +107,7 @@ class ScanController extends AbstractController
|
||||||
return $this->redirect($url);
|
return $this->redirect($url);
|
||||||
} catch (EntityNotFoundException) {
|
} catch (EntityNotFoundException) {
|
||||||
// Decoded OK, but no part is found. If it’s a vendor code, redirect to create.
|
// Decoded OK, but no part is found. If it’s a vendor code, redirect to create.
|
||||||
$createUrl = $this->buildCreateUrlForScanResult($scan, $request->getLocale());
|
$createUrl = $this->buildCreateUrlForScanResult($scan);
|
||||||
if ($createUrl !== null) {
|
if ($createUrl !== null) {
|
||||||
return $this->redirect($createUrl);
|
return $this->redirect($createUrl);
|
||||||
}
|
}
|
||||||
|
|
@ -161,21 +163,19 @@ class ScanController extends AbstractController
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a URL for creating a new part based on the barcode data
|
* Builds a URL for creating a new part based on the barcode data
|
||||||
* @param object $scanResult
|
* @param BarcodeScanResultInterface $scanResult
|
||||||
* @param string $locale
|
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
private function buildCreateUrlForScanResult(object $scanResult, string $locale): ?string
|
private function buildCreateUrlForScanResult(BarcodeScanResultInterface $scanResult): ?string
|
||||||
{
|
{
|
||||||
// LCSC
|
// LCSC
|
||||||
if ($scanResult instanceof LCSCBarcodeScanResult) {
|
if ($scanResult instanceof LCSCBarcodeScanResult) {
|
||||||
$lcscCode = $scanResult->getPC();
|
$lcscCode = $scanResult->getPC();
|
||||||
if (is_string($lcscCode) && $lcscCode !== '') {
|
if ($lcscCode !== null && $lcscCode !== '') {
|
||||||
return '/'
|
return $this->generateUrl('info_providers_create_part', [
|
||||||
. rawurlencode($locale)
|
'providerKey' => 'lcsc',
|
||||||
. '/part/from_info_provider/lcsc/'
|
'providerId' => $lcscCode,
|
||||||
. rawurlencode($lcscCode)
|
]);
|
||||||
. '/create';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -185,7 +185,7 @@ class ScanController extends AbstractController
|
||||||
|
|
||||||
// Mouser: use supplierPartNumber -> search provider -> provider_id
|
// Mouser: use supplierPartNumber -> search provider -> provider_id
|
||||||
if ($vendor === 'mouser'
|
if ($vendor === 'mouser'
|
||||||
&& is_string($scanResult->supplierPartNumber)
|
&& $scanResult->supplierPartNumber !== null
|
||||||
&& $scanResult->supplierPartNumber !== ''
|
&& $scanResult->supplierPartNumber !== ''
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -204,12 +204,12 @@ class ScanController extends AbstractController
|
||||||
// If there are results, provider_id is MouserPartNumber (per MouserProvider.php)
|
// If there are results, provider_id is MouserPartNumber (per MouserProvider.php)
|
||||||
$best = $dtos[0] ?? null;
|
$best = $dtos[0] ?? null;
|
||||||
|
|
||||||
if ($best !== null && is_string($best->provider_id) && $best->provider_id !== '') {
|
if ($best !== null && $best->provider_id !== '') {
|
||||||
return '/'
|
|
||||||
. rawurlencode($locale)
|
return $this->generateUrl('info_providers_create_part', [
|
||||||
. '/part/from_info_provider/mouser/'
|
'providerKey' => 'mouser',
|
||||||
. rawurlencode($best->provider_id)
|
'providerId' => $best->provider_id,
|
||||||
. '/create';
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addFlash('warning', 'No Mouser match found for this MPN.');
|
$this->addFlash('warning', 'No Mouser match found for this MPN.');
|
||||||
|
|
@ -238,11 +238,10 @@ class ScanController extends AbstractController
|
||||||
$id = $scanResult->customerPartNumber ?: $scanResult->supplierPartNumber;
|
$id = $scanResult->customerPartNumber ?: $scanResult->supplierPartNumber;
|
||||||
|
|
||||||
if (is_string($id) && $id !== '') {
|
if (is_string($id) && $id !== '') {
|
||||||
return '/'
|
return $this->generateUrl('info_providers_create_part', [
|
||||||
. rawurlencode($locale)
|
'providerKey' => 'digikey',
|
||||||
. '/part/from_info_provider/digikey/'
|
'providerId' => $id,
|
||||||
. rawurlencode($id)
|
]);
|
||||||
. '/create';
|
|
||||||
}
|
}
|
||||||
} catch (\InvalidArgumentException) {
|
} catch (\InvalidArgumentException) {
|
||||||
$this->addFlash('warning', 'Digi-Key provider is not installed/enabled');
|
$this->addFlash('warning', 'Digi-Key provider is not installed/enabled');
|
||||||
|
|
@ -312,7 +311,6 @@ class ScanController extends AbstractController
|
||||||
$input = trim((string) $request->request->get('input', ''));
|
$input = trim((string) $request->request->get('input', ''));
|
||||||
$mode = (string) ($request->request->get('mode') ?? '');
|
$mode = (string) ($request->request->get('mode') ?? '');
|
||||||
$infoMode = (bool) filter_var($request->request->get('info_mode', false), FILTER_VALIDATE_BOOL);
|
$infoMode = (bool) filter_var($request->request->get('info_mode', false), FILTER_VALIDATE_BOOL);
|
||||||
$locale = $request->getLocale();
|
|
||||||
|
|
||||||
if ($input === '') {
|
if ($input === '') {
|
||||||
return new JsonResponse(['ok' => false], 200);
|
return new JsonResponse(['ok' => false], 200);
|
||||||
|
|
@ -364,7 +362,7 @@ class ScanController extends AbstractController
|
||||||
// Create link only when NOT found (vendor codes)
|
// Create link only when NOT found (vendor codes)
|
||||||
$createUrl = null;
|
$createUrl = null;
|
||||||
if (!$targetFound) {
|
if (!$targetFound) {
|
||||||
$createUrl = $this->buildCreateUrlForScanResult($scan, $locale);
|
$createUrl = $this->buildCreateUrlForScanResult($scan);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render fragment (use openUrl for universal "Open" link)
|
// Render fragment (use openUrl for universal "Open" link)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue