diff --git a/config/routes.yaml b/config/routes.yaml index a8c925b7..b99a4ebf 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -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})?\\\\//')" \ No newline at end of file + +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})?\\\\//')" \ No newline at end of file diff --git a/src/Controller/ScanController.php b/src/Controller/ScanController.php index 5c1a8b35..bc088a46 100644 --- a/src/Controller/ScanController.php +++ b/src/Controller/ScanController.php @@ -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'); + } } } \ No newline at end of file diff --git a/src/Services/LabelSystem/BarcodeParser.php b/src/Services/LabelSystem/BarcodeParser.php new file mode 100644 index 00000000..fd2a0b5d --- /dev/null +++ b/src/Services/LabelSystem/BarcodeParser.php @@ -0,0 +1,65 @@ +. + */ + +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); + } + } +} \ No newline at end of file diff --git a/src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php b/src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php index d50fb36c..2389c6e2 100644 --- a/src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php +++ b/src/Services/LabelSystem/Barcodes/BarcodeContentGenerator.php @@ -63,6 +63,7 @@ class BarcodeContentGenerator return $this->urlGenerator->generate('scan_qr', [ 'type' => $type, 'id' => $target->getID(), + '_locale' => null, ], UrlGeneratorInterface::ABSOLUTE_URL); }