Added basic scan dialog.

This commit is contained in:
Jan Böhmer 2020-04-26 21:26:11 +02:00
parent 35c788fabc
commit 85821ec673
9 changed files with 1113 additions and 800 deletions

View file

@ -21,9 +21,14 @@
namespace App\Controller;
use App\Form\LabelSystem\ScanDialogType;
use App\Services\LabelSystem\BarcodeParser;
use App\Services\LabelSystem\Barcodes\BarcodeNormalizer;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
@ -33,10 +38,39 @@ use Symfony\Component\Routing\Annotation\Route;
class ScanController extends AbstractController
{
protected $barcodeParser;
protected $barcodeNormalizer;
public function __construct(BarcodeParser $barcodeParser)
public function __construct(BarcodeParser $barcodeParser, BarcodeNormalizer $barcodeNormalizer)
{
$this->barcodeParser = $barcodeParser;
$this->barcodeNormalizer = $barcodeNormalizer;
}
/**
* @Route("/", name="scan_dialog")
*/
public function dialog(Request $request): Response
{
$form = $this->createForm(ScanDialogType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$input = $form['input']->getData();
try {
[$type, $id] = $this->barcodeNormalizer->normalizeBarcodeContent($input);
try {
return $this->redirect($this->barcodeParser->getQRRedirectTarget($type, $id));
} catch (EntityNotFoundException $exception) {
$this->addFlash('success', 'scan.qr_not_found');
}
} catch (\InvalidArgumentException $exception) {
$this->addFlash('error', 'scan.format_unknown');
}
}
return $this->render('LabelSystem/Scanner/dialog.html.twig', [
'form' => $form->createView(),
]);
}
/**
@ -44,7 +78,7 @@ class ScanController extends AbstractController
* @param string $type
* @param int $id
*/
public function scanQRCode(string $type, int $id)
public function scanQRCode(string $type, int $id): Response
{
try {
$this->addFlash('success', 'scan.qr_success');

View file

@ -0,0 +1,49 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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/>.
*/
namespace App\Form\LabelSystem;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ScanDialogType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('input', TextType::class, [
'attr' => [
'autofocus' => true,
]
]);
$builder->add('submit', SubmitType::class, [
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('mapped', false);
}
}

View file

@ -31,6 +31,7 @@ class BarcodeParser
protected $urlGenerator;
protected $em;
public function __construct(UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager)
{
$this->urlGenerator = $urlGenerator;
@ -58,6 +59,9 @@ class BarcodeParser
return $this->urlGenerator->generate('app_part_show', ['id' => $lot->getPart()->getID()]);
case 'location':
return $this->urlGenerator->generate('part_list_store_location', ['id' => $id]);
default:
throw new \InvalidArgumentException('Unknown $type: ' . $type);
}

View file

@ -0,0 +1,75 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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/>.
*/
namespace App\Services\LabelSystem\Barcodes;
class BarcodeNormalizer
{
protected const PREFIX_TYPE_MAP = [
'L' => 'lot',
'P' => 'part',
'S' => 'location',
];
/**
* Parses barcode content and normalizes it.
* Returns an array in the format ['part', 1]: First entry contains element type, second the ID of the element
* @param string $input
* @return array
*/
public function normalizeBarcodeContent(string $input): array
{
$input = trim($input);
$matches = [];
//Some scanner output '-' as ß, so replace it (ß is never used, so we can replace it safely)
$input = str_replace('ß', '-', $input);
//Extract parts from QR code's URL
if (preg_match('#^https?://.*/scan/(\w+)/(\d+)/?$#', $input, $matches)) {
return [$matches[1], (int) $matches[2]];
}
//New Code39 barcodes use L-000001 format
if (preg_match('#^(\w)-(\d{6,})$#', $input, $matches)) {
$prefix = $matches[1];
$id = (int) $matches[2];
if (!isset(self::PREFIX_TYPE_MAP[$prefix])) {
throw new \InvalidArgumentException('Unknown prefix ' . $prefix);
}
return [self::PREFIX_TYPE_MAP[$prefix], $id];
}
//Legacy Part-DB location labels used $L00336 format
if (preg_match('#^\$L(\d{5,})$#', $input, $matches)) {
return ['location', (int) $matches[1]];
}
//Legacy Part-DB used EAN8 barcodes for part labels. Format 0000001(2) (note the optional 8th digit => checksum)
if (preg_match('#^(\d{7})\d?$#', $input, $matches)) {
return ['part', (int) $matches[1]];
}
throw new \InvalidArgumentException('Unknown barcode format!');
}
}

View file

@ -0,0 +1,9 @@
{% extends 'main_card.html.twig' %}
{% block card_title %}<i class="fas fa-atom fa-fw"></i> {% trans %}label_scanner.title{% endtrans %}{% endblock %}
{% block card_content %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}

View file

@ -16,6 +16,12 @@
<div class="collapse navbar-collapse" id="navbarContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{{ url("scan_dialog") }}">{% trans %}navbar.scanner.link{% endtrans %}</a>
</li>
</ul>
<div class="form-inline my-2 my-lg-0 ml-auto" id="searchbar">
<!-- Searchbar -->
{% include "_navbar_search.html.twig" %}

View file

@ -0,0 +1,39 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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/>.
*/
namespace App\Tests\Services\LabelSystem;
use App\Services\LabelSystem\BarcodeParser;
use PHPUnit\Framework\TestCase;
class BarcodeParserTest extends TestCase
{
public function testGetQRRedirectTarget()
{
}
public function testNormalizeBarcodeContent()
{
}
}

View file

@ -0,0 +1,91 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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/>.
*/
namespace App\Tests\Services\LabelSystem\Barcodes;
use App\Services\LabelSystem\Barcodes\BarcodeNormalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class BarcodeNormalizerTest extends WebTestCase
{
/** @var BarcodeNormalizer */
protected $service;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(BarcodeNormalizer::class);
}
public function dataProvider(): array
{
return [
//QR URL content:
[['lot', 1], 'https://localhost:8000/scan/lot/1'],
[['part', 123], 'https://localhost:8000/scan/part/123'],
[['location', 4], 'http://foo.bar/part-db/scan/location/4'],
[['under_score', 10], 'http://test/part-db/sub/scan/under_score/10/'],
//New Code39 barcodes:
[['lot', 10], 'L-000010'],
[['lot', 10], 'Lß000010'],
[['part', 123], 'P-000123'],
[['location', 123], 'S-000123'],
[['lot', 12345678], 'L-12345678'],
//Legacy storelocation format
[['location', 336], '$L00336'],
[['location', 12345678], '$L12345678'],
//Legacy Part format
[['part', 123], '0000123'],
[['part', 123], '00001236'],
[['part', 1234567], '12345678'],
];
}
public function invalidDataProvider(): array
{
return [
['https://localhost/part/1'], //Without scan
['L-'], //Without number
['L-123'], //Too short
['X-123456'], //Unknown prefix
['XXXWADSDF sdf'], //Garbage
[''], //Empty
];
}
/**
* @dataProvider dataProvider
*/
public function testNormalizeBarcodeContent(array $expected, string $input)
{
$this->assertSame($expected, $this->service->normalizeBarcodeContent($input));
}
/**
* @dataProvider invalidDataProvider
*/
public function testInvalidFormats(string $input)
{
$this->expectException(\InvalidArgumentException::class);
$this->service->normalizeBarcodeContent($input);
}
}

File diff suppressed because it is too large Load diff