mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Added simple redirect to info page, when scanning an QR code.
This commit is contained in:
parent
2768eb1745
commit
c200b650a0
4 changed files with 95 additions and 10 deletions
|
@ -3,10 +3,15 @@
|
|||
# controller: App\Controller\DefaultController::index
|
||||
|
||||
# Redirect every url without an locale to the locale of the user/the global base locale
|
||||
redirector:
|
||||
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})?\\\\//')"
|
||||
|
||||
scan_qr:
|
||||
path: /scan/{type}/{id}
|
||||
controller: App\Controller\ScanController:scanQRCode
|
||||
|
||||
#redirector:
|
||||
# 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})?\\\\//')"
|
|
@ -21,6 +21,8 @@
|
|||
namespace App\Controller;
|
||||
|
||||
|
||||
use App\Services\LabelSystem\BarcodeParser;
|
||||
use Doctrine\ORM\EntityNotFoundException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
|
@ -30,14 +32,26 @@ use Symfony\Component\Routing\Annotation\Route;
|
|||
*/
|
||||
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 int $id
|
||||
*/
|
||||
public function scanQRCode(string $type, int $id)
|
||||
{
|
||||
$this->addFlash('success', 'QR Code scanned!');
|
||||
return $this->redirectToRoute('homepage');
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
65
src/Services/LabelSystem/BarcodeParser.php
Normal file
65
src/Services/LabelSystem/BarcodeParser.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,6 +63,7 @@ class BarcodeContentGenerator
|
|||
return $this->urlGenerator->generate('scan_qr', [
|
||||
'type' => $type,
|
||||
'id' => $target->getID(),
|
||||
'_locale' => null,
|
||||
], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue