From aa4675589ce4b87264e7ffe2a9651e9761c84a92 Mon Sep 17 00:00:00 2001 From: Katharina Bogad Date: Sun, 6 Jun 2021 23:41:45 +0200 Subject: [PATCH] Grocycode: Datamatrix generation --- .vscode/settings.json | 16 ---------- composer.json | 3 +- composer.lock | 56 +++++++++++++++++++++++++++++++-- controllers/StockController.php | 18 ++++++++++- helpers/Grocycode.php | 30 +++++++++--------- routes.php | 17 +++++----- 6 files changed, 98 insertions(+), 42 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1a8875b2..e5ed3bdc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,20 +14,4 @@ "php-cs-fixer.formatHtml": true, "php-cs-fixer.autoFixBySemicolon": true, "php-cs-fixer.onsave": true, - "phpfmt.passes": [ - "PSR2KeywordsLowerCase", - "PSR2LnAfterNamespace", - "PSR2CurlyOpenNextLine", - "PSR2ModifierVisibilityStaticOrder", - "PSR2SingleEmptyLineAndStripClosingTag", - "ReindentSwitchBlocks", - "AllmanStyleBraces", - "StripExtraCommaInArray" - ], - "phpfmt.exclude": [ - "ReindentComments", - "StripNewlineWithinClassBody" - ], - "phpfmt.psr2": false, - "phpfmt.indent_with_space": false, } \ No newline at end of file diff --git a/composer.json b/composer.json index 22b5f6fa..33b0a597 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "eluceo/ical": "^0.16.0", "erusev/parsedown": "^1.7", "gumlet/php-image-resize": "^1.9", - "ezyang/htmlpurifier": "^4.13" + "ezyang/htmlpurifier": "^4.13", + "jucksearm/php-barcode": "^1.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 2fd1ca61..9480cbc3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6155c0eb959bd8118ce2aabce7fd8a6a", + "content-hash": "2f15993a36a55cfc2018969a144c73b0", "packages": [ { "name": "doctrine/inflector", @@ -828,6 +828,58 @@ }, "time": "2019-06-20T13:13:59+00:00" }, + { + "name": "jucksearm/php-barcode", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/jucksearm/php-barcode.git", + "reference": "066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jucksearm/php-barcode/zipball/066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b", + "reference": "066a58776ec9e94dd6d5843c0fb9a3ff95e74d8b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "jucksearm\\barcode\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPLv3" + ], + "authors": [ + { + "name": "jucksearm", + "email": "jucksearm.bkk@gmail.com" + } + ], + "description": "Barcode Generation Package inspired by Nicola Asuni.", + "keywords": [ + "CODE 128", + "barcode", + "datamatrix", + "pdf417", + "php barcode", + "qr code", + "qrcode" + ], + "support": { + "issues": "https://github.com/jucksearm/php-barcode/issues", + "source": "https://github.com/jucksearm/php-barcode/tree/1.0.0" + }, + "time": "2017-06-05T04:41:51+00:00" + }, { "name": "morris/lessql", "version": "v0.4.1", @@ -2528,5 +2580,5 @@ "php": ">=7.4" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.1.0" } diff --git a/controllers/StockController.php b/controllers/StockController.php index ddb785e2..6e3a33a1 100644 --- a/controllers/StockController.php +++ b/controllers/StockController.php @@ -2,7 +2,9 @@ namespace Grocy\Controllers; +use Grocy\Helpers\Grocycode; use Grocy\Services\RecipesService; +use jucksearm\barcode\lib\DatamatrixFactory; class StockController extends BaseController { @@ -39,7 +41,7 @@ class StockController extends BaseController 'products' => $this->getDatabase()->products()->where('active = 1')->orderBy('name', 'COLLATE NOCASE'), 'locations' => $this->getDatabase()->locations()->orderBy('name', 'COLLATE NOCASE'), 'users' => $usersService->GetUsersAsDto(), - 'transactionTypes' => GetClassConstants('\Grocy\Services\StockService', 'TRANSACTION_TYPE_'), + 'transactionTypes' => GetClassConstants('\Grocy\Services\StockService', 'TRANSACTION_TYPE_') ]); } @@ -428,6 +430,20 @@ class StockController extends BaseController ]); } + public function StockEntryGrocycodeImage(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) + { + $stockEntry = $this->getDatabase()->stock()->where('id', $args['entryId'])->fetch(); + $gc = new Grocycode(Grocycode::PRODUCT, $stockEntry->product_id, [$stockEntry->stock_id]); + $png = (new DatamatrixFactory())->setCode((string) $gc)->getDatamatrixPngData(); + + $response = $response->withHeader('Content-Type', 'image/png') + ->withHeader('Content-Length', strlen($png)) + ->withHeader('Cache-Control', 'no-cache') + ->withHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT'); + $response->getBody()->write($png); + return $response; + } + public function StockSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { return $this->renderPage($response, 'stocksettings', [ diff --git a/helpers/Grocycode.php b/helpers/Grocycode.php index beb6a143..bee3190e 100644 --- a/helpers/Grocycode.php +++ b/helpers/Grocycode.php @@ -17,19 +17,20 @@ namespace Grocy\Helpers; */ class Grocycode { + public const PRODUCT = 'p'; + public const BATTERY = 'b'; + public const CHORE = 'c'; - public const PRODUCT = "p"; - public const BATTERY = "b"; - public const CHORE = "c"; - - public const MAGIC = "grcy"; + public const MAGIC = 'grcy'; /** * An array that registers all valid grocycode types. Register yours here by appending to this array. */ public static $Items = [self::PRODUCT, self::BATTERY, self::CHORE]; - private $type, $id, $extra_data = []; + private $type; + private $id; + private $extra_data = []; /** * Validates a grocycode. @@ -65,7 +66,7 @@ class Grocycode $this->setFromCode($args[0]); return; } - else if ($argc == 2 || $argc == 3) + elseif ($argc == 2 || $argc == 3) { if ($argc == 2) { @@ -75,7 +76,7 @@ class Grocycode return; } - throw new \Exception("No suitable overload found."); + throw new \Exception('No suitable overload found.'); } /** @@ -83,20 +84,19 @@ class Grocycode */ private function setFromCode($code) { - $parts = array_reverse(explode(":", $barcode)); + $parts = array_reverse(explode(':', $barcode)); if (array_pop($parts) != self::MAGIC) { - throw new \Exception("Not a grocycode"); + throw new \Exception('Not a grocycode'); } if (!in_array($this->type = array_pop($parts), self::$Items)) { - throw new \Exception("Unknown grocycode type"); + throw new \Exception('Unknown grocycode type'); } $this->id = array_pop($parts); $this->extra_data = array_reverse($parse); - } /** @@ -106,11 +106,11 @@ class Grocycode { if (!is_array($extra_data)) { - throw new \Exception("Extra data must be array of string"); + throw new \Exception('Extra data must be array of string'); } if (!in_array($type, self::$Items)) { - throw new \Exception("Unknown grocycode type"); + throw new \Exception('Unknown grocycode type'); } $this->type = $type; @@ -137,6 +137,6 @@ class Grocycode { $arr = array_merge([self::MAGIC, $this->type, $this->id], $this->extra_data); - return implode(":", $arr); + return implode(':', $arr); } } diff --git a/routes.php b/routes.php index 4ce72312..8b5dcc23 100644 --- a/routes.php +++ b/routes.php @@ -1,12 +1,12 @@ group('', function (RouteCollectorProxy $group) { +$app->group('', function (RouteCollectorProxy $group) +{ // System routes $group->get('/', '\Grocy\Controllers\SystemController:Root')->setName('root'); $group->get('/about', '\Grocy\Controllers\SystemController:About'); @@ -60,6 +60,7 @@ $app->group('', function (RouteCollectorProxy $group) { $group->get('/quantityunitpluraltesting', '\Grocy\Controllers\StockController:QuantityUnitPluralFormTesting'); $group->get('/stockjournal/summary', '\Grocy\Controllers\StockController:JournalSummary'); $group->get('/productbarcodes/{productBarcodeId}', '\Grocy\Controllers\StockController:ProductBarcodesEditForm'); + $group->get('/stockentry/{entryId}/grocycode', '\Grocy\Controllers\StockController:StockEntryGrocycodeImage'); } // Stock price tracking @@ -139,7 +140,8 @@ $app->group('', function (RouteCollectorProxy $group) { $group->get('/manageapikeys/new', '\Grocy\Controllers\OpenApiController:CreateNewApiKey'); }); -$app->group('/api', function (RouteCollectorProxy $group) { +$app->group('/api', function (RouteCollectorProxy $group) +{ // OpenAPI $group->get('/openapi/specification', '\Grocy\Controllers\OpenApiController:DocumentationSpec'); @@ -245,6 +247,7 @@ $app->group('/api', function (RouteCollectorProxy $group) { })->add(JsonMiddleware::class); // Handle CORS preflight OPTIONS requests -$app->options('/api/{routes:.+}', function (Request $request, Response $response): Response { +$app->options('/api/{routes:.+}', function (Request $request, Response $response): Response +{ return $response->withStatus(204); });