Added simple redirect to info page, when scanning an QR code.

This commit is contained in:
Jan Böhmer 2020-04-26 18:59:49 +02:00
parent 2768eb1745
commit c200b650a0
4 changed files with 95 additions and 10 deletions

View file

@ -3,10 +3,15 @@
# controller: App\Controller\DefaultController::index # controller: App\Controller\DefaultController::index
# Redirect every url without an locale to the locale of the user/the global base locale # Redirect every url without an locale to the locale of the user/the global base locale
redirector:
path: /{url} scan_qr:
requirements: path: /scan/{type}/{id}
url: ".*" controller: App\Controller\ScanController:scanQRCode
controller: App\Controller\RedirectController:addLocalePart
# Dont match localized routes (no redirection loop, if no root with that name exists) #redirector:
condition: "not (request.getPathInfo() matches '/^\\\\/[a-z]{2}(_[A-Z]{2})?\\\\//')" # path: /{url}
# requirements:
# url: ".*"
# controller: App\Controller\RedirectController:addLocalePart
# # Dont match localized routes (no redirection loop, if no root with that name exists)
# condition: "not (request.getPathInfo() matches '/^\\\\/[a-z]{2}(_[A-Z]{2})?\\\\//')"

View file

@ -21,6 +21,8 @@
namespace App\Controller; namespace App\Controller;
use App\Services\LabelSystem\BarcodeParser;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@ -30,14 +32,26 @@ use Symfony\Component\Routing\Annotation\Route;
*/ */
class ScanController extends AbstractController class ScanController extends AbstractController
{ {
protected $barcodeParser;
public function __construct(BarcodeParser $barcodeParser)
{
$this->barcodeParser = $barcodeParser;
}
/** /**
* @Route("/{type}/{id}", name="scan_qr") * The route definition for this action is done in routes.yaml, as it does not use the _locale prefix as the other routes
* @param string $type * @param string $type
* @param int $id * @param int $id
*/ */
public function scanQRCode(string $type, int $id) public function scanQRCode(string $type, int $id)
{ {
$this->addFlash('success', 'QR Code scanned!'); try {
$this->addFlash('success', 'scan.qr_success');
return $this->redirect($this->barcodeParser->getQRRedirectTarget($type, $id));
} catch (EntityNotFoundException $exception) {
$this->addFlash('success', 'scan.qr_not_found');
return $this->redirectToRoute('homepage'); return $this->redirectToRoute('homepage');
} }
}
} }

View file

@ -0,0 +1,65 @@
<?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;
use App\Entity\Parts\PartLot;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class BarcodeParser
{
protected $urlGenerator;
protected $em;
public function __construct(UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager)
{
$this->urlGenerator = $urlGenerator;
$this->em = $entityManager;
}
/**
* Determines the URL to which the user should be redirected, when scanning a QR code
* @param string $type The type of the element that was scanned (e.g. 'part', 'lot', etc.)
* @param int $id The ID of the element that was scanned
* @return string The URL to which should be redirected.
* @throws EntityNotFoundException
*/
public function getQRRedirectTarget(string $type, int $id): string
{
switch ($type) {
case 'part':
return $this->urlGenerator->generate('app_part_show', ['id' => $id]);
case 'lot':
//Try to determine the part to the given lot
$lot = $this->em->find(PartLot::class, $id);
if ($lot === null) {
throw new EntityNotFoundException();
}
return $this->urlGenerator->generate('app_part_show', ['id' => $lot->getPart()->getID()]);
default:
throw new \InvalidArgumentException('Unknown $type: ' . $type);
}
}
}

View file

@ -63,6 +63,7 @@ class BarcodeContentGenerator
return $this->urlGenerator->generate('scan_qr', [ return $this->urlGenerator->generate('scan_qr', [
'type' => $type, 'type' => $type,
'id' => $target->getID(), 'id' => $target->getID(),
'_locale' => null,
], UrlGeneratorInterface::ABSOLUTE_URL); ], UrlGeneratorInterface::ABSOLUTE_URL);
} }