|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Helpers\Helper; |
| 6 | +use App\Models\Setting; |
| 7 | +use Com\Tecnick\Barcode\Barcode; |
| 8 | +use Illuminate\Http\Response; |
| 9 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 10 | + |
| 11 | +class QrCodeController extends Controller |
| 12 | +{ |
| 13 | + public static $map_show_route = [ |
| 14 | + 'accessories' => 'accessories.show', |
| 15 | + 'assets' => 'hardware.show', |
| 16 | + 'companies' => 'companies.show', |
| 17 | + 'components' => 'components.show', |
| 18 | + 'consumables' => 'consumables.show', |
| 19 | + 'hardware' => 'hardware.show', |
| 20 | + 'licenses' => 'licenses.show', |
| 21 | + 'locations' => 'locations.show', |
| 22 | + 'models' => 'models.show', |
| 23 | + 'users' => 'users.show', |
| 24 | + ]; |
| 25 | + |
| 26 | + public function show($object_type, $id): Response|BinaryFileResponse|string|bool |
| 27 | + { |
| 28 | + $settings = Setting::getSettings(); |
| 29 | + |
| 30 | + if ($settings->label2_2d_type === 'none') { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + if (! array_key_exists($object_type, self::$map_show_route)) { |
| 35 | + return $object_type.' is not a valid type.'; |
| 36 | + } |
| 37 | + |
| 38 | + $object = self::$map_object_type[$object_type]::withTrashed()->find($id); |
| 39 | + |
| 40 | + if (! $object) { |
| 41 | + return 'That item is invalid'; |
| 42 | + } |
| 43 | + |
| 44 | + $this->authorize('view', $object); |
| 45 | + |
| 46 | + $size = Helper::barcodeDimensions($settings->label2_2d_type); |
| 47 | + $qr_file = public_path().'/uploads/barcodes/qr-'.str_slug($object_type).'-'.str_slug($id).'.png'; |
| 48 | + |
| 49 | + if (file_exists($qr_file)) { |
| 50 | + return response()->file($qr_file, ['Content-type' => 'image/png']); |
| 51 | + } |
| 52 | + |
| 53 | + $barcode = new Barcode; |
| 54 | + $barcode_obj = $barcode->getBarcodeObj( |
| 55 | + $settings->label2_2d_type, |
| 56 | + route(self::$map_show_route[$object_type], $id), |
| 57 | + $size['height'], |
| 58 | + $size['width'], |
| 59 | + 'black', |
| 60 | + [-2, -2, -2, -2] |
| 61 | + ); |
| 62 | + file_put_contents($qr_file, $barcode_obj->getPngData()); |
| 63 | + |
| 64 | + return response($barcode_obj->getPngData())->header('Content-type', 'image/png'); |
| 65 | + } |
| 66 | +} |
0 commit comments