checkIfAuthenticated(true);
$result = true;
//If BB is already setup and for some reason an internal check failed
//(eg. missing php module), the use is redirected to setup.php
//If the problem is fixed and he clicks "Refresh", redirect him to index.php
//instead of showing the setup again
if (isset($_POST["was_internal_check"])) {
if (BBConfig::getInstance()["GROCY_API_URL"] != null && BBConfig::getInstance()["GROCY_API_KEY"] != null) {
header("Location: ./index.php");
die();
}
}
//If settings were submitted, save and check them
if (isset($_POST["GROCY_API_URL"])) {
$apiWithTrailingSlash = rtrim($_POST["GROCY_API_URL"], '/') . '/';
$result = API::checkApiConnection($apiWithTrailingSlash, $_POST["GROCY_API_KEY"]);
if ($result === true) {
DatabaseConnection::getInstance()->updateConfig("GROCY_API_URL", sanitizeString($apiWithTrailingSlash));
DatabaseConnection::getInstance()->updateConfig("GROCY_API_KEY", sanitizeString($_POST["GROCY_API_KEY"]));
header("Location: ./index.php");
die();
}
}
$webUi = new WebUiGenerator(MENU_SETUP);
$webUi->addHeader();
if (checkExtensionsInstalled()["result"] == RESULT_REQ_MISSING) {
$webUi->addCard("Setup", getHtmlSetupExtMissing());
} else {
$webUi->addCard("Setup", getHtmlSetupTable($result));
}
$webUi->addFooter();
$webUi->printHtml();
/**
* @param String | true $result
* @return string
* @throws DbConnectionDuringEstablishException
*/
function getHtmlSetupTable($result): string {
$config = BBConfig::getInstance();
$html = new UiEditor();
$html->addHtml('Welcome to Barcode Buddy! Please enter your Grocy API details below. For more information, please visit the documentation.');
$html->addLineBreak(3);
$editValue = "";
if (isset($_POST["GROCY_API_URL"]))
$editValue = $_POST["GROCY_API_URL"];
else if ($config["GROCY_API_URL"] != null)
$editValue = $config["GROCY_API_URL"];
$html->buildEditField('GROCY_API_URL', 'Grocy API URL', $editValue)
->pattern('https://.*/api/|http://.*/api/|https://.*/api|http://.*/api')
->setPlaceholder('e.g. https://your.grocy.com/api/')
->generate();
$html->addLineBreak();
$editValue = "";
if (isset($_POST["GROCY_API_KEY"]))
$editValue = $_POST["GROCY_API_KEY"];
$html->buildEditField('GROCY_API_KEY', 'Grocy API Key', $editValue)
->pattern('[A-Za-z0-9]{48,50}')
->generate();
if ($result !== true) {
$html->addHtml('
Unable to connect to API: ' . $result . '');
}
$html->addLineBreak(2);
$html->buildButton("button_save", "Save")
->setSubmit()
->setRaised()
->setIsAccent()
->generate();
return $html->getHtml();
}
function getHtmlSetupExtMissing(): string {
$html = new UiEditor();
$html->addHtml('Welcome to Barcode Buddy! Please make sure the following extensions are installed and enabled:');
$html->addLineBreak(2);
$extensions = checkExtensionsInstalled();
foreach ($extensions["req"] as $ext => $installed) {
$html->addHtml(formatExtentioncolor($ext, $installed, true));
}
foreach ($extensions["opt"] as $ext => $installed) {
$html->addHtml(formatExtentioncolor($ext, $installed, false));
}
$html->addLineBreak(2);
$html->addHiddenField("was_internal_check", "1");
$html->buildButton("button_refresh", "Refresh")
->setSubmit()
->setRaised()
->setIsAccent()
->generate();
return $html->getHtml();
}
function formatExtentioncolor(string $name, bool $installed, bool $req): string {
if ($installed) {
return 'Installed: ' . $name . '
';
} else {
if ($req) {
return 'Not installed: ' . $name . ' (required)
';
} else {
return 'Not installed: ' . $name . ' (optional)
';
}
}
}