Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ data/config.php
data/users.db
/vendor/
.idea
/.devDocker/
/.run/Dev Docker.run.xml
/data/logs/
25 changes: 13 additions & 12 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
$api->checkIfAuthorized();
$api->execute($requestedUrl);


class BBuddyApi {

private $routes = array();
Expand Down Expand Up @@ -115,7 +114,6 @@ function addRoute(ApiRoute $route): void {
}

private function initRoutes(): void {

$this->addRoute(new ApiRoute("/action/scan", function () {
$barcode = "";
if (isset($_GET["text"]))
Expand All @@ -124,22 +122,24 @@ private function initRoutes(): void {
$barcode = $_GET["add"];
if (isset($_POST["barcode"]))
$barcode = $_POST["barcode"];
if ($barcode == "")
if ($barcode == "") {
return self::createResultArray(null, "No barcode supplied", 400);
else {
} else {
$bestBefore = null;
$price = null;
if (isset($_POST["bestBeforeInDays"]) && $_POST["bestBeforeInDays"] != null) {
if (is_numeric($_POST["bestBeforeInDays"]))
if (is_numeric($_POST["bestBeforeInDays"])) {
$bestBefore = $_POST["bestBeforeInDays"];
else
} else {
return self::createResultArray(null, "Invalid parameter bestBeforeInDays: needs to be type int", 400);
}
}
if (isset($_POST["price"]) && $_POST["price"] != null) {
if (is_numeric($_POST["price"]))
if (is_numeric($_POST["price"])) {
$price = $_POST["price"];
else
} else {
return self::createResultArray(null, "Invalid parameter price: needs to be type float", 400);
}
}
$result = processNewBarcode(sanitizeString($barcode), $bestBefore, $price);
return self::createResultArray(array("result" => sanitizeString($result)));
Expand All @@ -159,10 +159,10 @@ private function initRoutes(): void {
else if (isset($_POST["state"]))
$state = $_POST["state"];

//Also check if value is a valid range (STATE_CONSUME the lowest and STATE_CONSUME_ALL the highest value)
if (!is_numeric($state) || $state < STATE_CONSUME || $state > STATE_CONSUME_ALL)
//Also check if value is a valid range (STATE_CONSUME the lowest and STATE_TXFR the highest value)
if (!is_numeric($state) || $state < STATE_CONSUME || $state > STATE_TXFR) {
return self::createResultArray(null, "Invalid state provided", 400);
else {
} else {
DatabaseConnection::getInstance()->setTransactionState(intval($state));
return self::createResultArray();
}
Expand All @@ -178,7 +178,8 @@ private function initRoutes(): void {
"BARCODE_GS" => $config["BARCODE_GS"],
"BARCODE_Q" => $config["BARCODE_Q"],
"BARCODE_AS" => $config["BARCODE_AS"],
"BARCODE_CA" => $config["BARCODE_CA"]
"BARCODE_CA" => $config["BARCODE_CA"],
"BARCODE_TXFR" => $config["BARCODE_TXFR"],
));
}));

Expand Down
136 changes: 136 additions & 0 deletions barcodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* Barcode Buddy for Grocy
*
* PHP version 8
*
* LICENSE: This source file is subject to version 3.0 of the GNU General
* Public License v3.0 that is attached to this project.
*
* @author Marc Ole Bulling
* @copyright 2019 Marc Ole Bulling
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU GPL v3.0
* @since File available since Release 1.8.1.9
*/

require_once __DIR__ . "/incl/configProcessing.inc.php";
require_once __DIR__ . "/incl/db.inc.php";
require_once __DIR__ . "/incl/webui.inc.php";
require_once __DIR__ . "/incl/api.inc.php";

const MODE_ACTION = 'act';
const MODE_QUANTITY = 'qty';
const MODE_LOCATION = 'loc';

$CONFIG->checkIfAuthenticated(true, true);

// Get mode and validate it
$mode = MODE_ACTION;
if (isset($_GET)) {
if (isset($_GET["mode"])) {
$mode = $_GET["mode"];
}
}
if (!in_array($mode, [MODE_ACTION ,MODE_QUANTITY, MODE_LOCATION])) {
die("Invalid mode");
}

// Generate the page
$webUi = new WebUiGenerator(MENU_GENERIC);

$webUi->addBaseHeader(
null,
false,
true,
"<script src=\"/incl/js/JsBarcode.all.min.js\"></script>\n<script src=\"/incl/js/scripts_barcodes.js\"></script>");

switch ($mode) {
case MODE_ACTION:
getHtmlActionTable($webUi);
break;
case MODE_QUANTITY:
getHtmlQuantityTable($webUi);
break;
case MODE_LOCATION:
getHtmlLocationTable($webUi);
break;
}

$webUi->printHtml();

function getHtmlActionTable(WebUiGenerator $webUi): void
{
$config = BBConfig::getInstance();

$actions = [
1 => ['barcode' => $config['BARCODE_C'], 'name' => 'Consume'],
2 => ['barcode' => $config['BARCODE_CS'], 'name' => 'Consume (spoiled)'],
3 => ['barcode' => $config['BARCODE_CA'], 'name' => 'Consume All'],
4 => ['barcode' => $config['BARCODE_P'], 'name' => 'Purchase'],
5 => ['barcode' => $config['BARCODE_O'], 'name' => 'Open'],
6 => ['barcode' => $config['BARCODE_GS'], 'name' => 'Inventory'],
7 => ['barcode' => $config['BARCODE_AS'], 'name' => 'Add to Shopping List'],
];


// Generate the HTML
$html = new UiEditor(true, null, "barcodes");
$html->addHtml("<div id=\"action-barcodes\" class=\"flex-settings\" data-actions='" . json_encode($actions) . "'>");

foreach ($actions as $key => $action) {
$html->addDiv("<a href=\"#\" onclick=\"downloadBarcode('action-$key'); return false;\"><img id=\"action-$key\" alt=\"$key\" data-name=\"{$action['name']}\"/></a>", null, "flex-settings-child");

}

$html->addHtml('</div>');
$webUi->addHtml($html->getHtml());

// Generate the JS
$webUi->addScript("generateActionBarcodes();");
}

function getHtmlLocationTable(WebUiGenerator $webUi): void
{
$config = BBConfig::getInstance();
$locations = API::getLocations();

// Generate the HTML
$html = new UiEditor(true, null, "barcodes");
$html->addHtml("<div id=\"location-barcodes\" class=\"flex-settings\" data-locations='" . json_encode($locations) . "' data-barcode='" . $config['BARCODE_TXFR'] . "'>");

foreach ($locations as $location) {
$html->addDiv("<a href=\"#\" onclick=\"downloadBarcode('location-$location->id'); return false;\"><img id=\"location-$location->id\" alt=\"$location->id\" data-name=\"{$location->name}\"/></a>", null, "flex-settings-child");

}

$html->addHtml('</div>');
$webUi->addHtml($html->getHtml());

// Generate the JS
$webUi->addScript("generateLocationBarcodes();");
}


function getHtmlQuantityTable(WebUiGenerator $webUi): void
{
$config = BBConfig::getInstance();

// Get quantity start and end
$startQty = isset($_GET['startQty']) ? intval($_GET['startQty']) : 1;
$endQty = isset($_GET['endQty']) ? intval($_GET['endQty']) : 10;

// Generate the HTML
$html = new UiEditor(true, null, "barcodes");
$html->addHtml("<div id=\"quantity-barcodes\" class=\"flex-settings\" data-start-qty='$startQty' data-end-qty='$endQty' data-barcode='" . $config['BARCODE_Q'] . "'>");

for ($i = $startQty; $i <= $endQty; $i++) {
$html->addDiv("<a href=\"#\" onclick=\"downloadBarcode('quantity-$i'); return false;\"><img id=\"quantity-$i\" alt=\"$i\" data-name=\"Quantity $i\"/></a>", null, "flex-settings-child");
}

$html->addHtml('</div>');
$webUi->addHtml($html->getHtml());

// Generate the JS
$webUi->addScript("generateQuantityBarcodes();");
}
Loading