Upload files to 'phpInterface'

Several new features for PHP interface.
This commit is contained in:
jens 2021-04-23 00:30:23 +02:00
parent eb5a21b64a
commit 38547ed9dd

View File

@ -1,245 +1,457 @@
<?php <?php
require_once("shoppingListConfig.php"); require_once("shoppingListConfig.php");
class DBLink $justCreatedProductId;
{
static $sqlLink = null; class DBLink
{
public static function getDbLink() static $sqlLink = null;
{
if(DBLink::$sqlLink == null) public static function getDbLink()
{ {
DBLink::$sqlLink = new mysqli(Configuration::$mysqlserver, Configuration::$mysqluser, Configuration::$mysqlpw, Configuration::$mysqldb, Configuration::$mysqlPort); if(DBLink::$sqlLink == null)
{
// Verbindung überprüfen DBLink::$sqlLink = new mysqli(Configuration::$mysqlserver, Configuration::$mysqluser, Configuration::$mysqlpw, Configuration::$mysqldb, Configuration::$mysqlPort);
if (mysqli_connect_errno())
{ // Verbindung überprüfen
printf("Database connection error:%s\n", mysqli_connect_error()); if (mysqli_connect_errno())
exit(); {
} printf("Database connection error:%s\n", mysqli_connect_error());
} exit();
}
return DBLink::$sqlLink; }
}
} return DBLink::$sqlLink;
}
if(isset($_GET['command']) && $_GET['command'] == "printProductList" || $_GET['command'] == "printRhasspyProductList") }
{
displayProductList(); if(isset($_GET['command']) && $_GET['command'] == "printProductList" || $_GET['command'] == "printRhasspyProductList")
exit(0); {
} displayProductList();
exit(0);
echo "<html> }
<head>
<title>Shopping list</title> ?>
<style type=\"text/css\">
table <html>
{ <head>
border-collapse:separate; <title>Shopping list</title>
border-spacing:10px 10px; <style type="text/css">
} body
{
.smallButton font-family: Arial, Geneva, Helvetica, sans-serif;
{ }
font-size:10px;;
height:5000em; table
width:5000em; {
} border-collapse:separate;
</style> border-spacing:10px 10px;
</head> }
<body>";
.smallButton
$command = $_POST['command']; {
font-size:10px;;
if(isset($command)) height:5000em;
{ width:5000em;
switch($command) }
{ </style>
case "addToList":
$productId = $_POST['productToAdd']; <script language="javascript" type="text/javascript">
addToList($productId);
break; function checkCreateProduct()
case "removeFromList": {
$productId = $_POST['productToRemove']; var productToCreate = document.forms["createProductForm"]["productToCreateName"].value;
removeFromList($productId); if (productToCreate == "")
break; {
case "displayShoppingList": alert("Product name must be filled out");
displayShoppingList(); return false;
break; }
case "createNewShoppingList":
createNewShoppingList(); var storeTypeId = document.forms["createProductForm"]["storeType"].value;
break; if (storeTypeId == "" || storeTypeId == "0")
} {
} alert("Store type must be selected");
return false;
displayShoppingList(); }
echo "</body></html>"; return true;
}
mysqli_close(DBLink::getDbLink());
</script>
</head>
function addToList($productId)
{ <body>
if($productId > 0)
{ <?php
$SQL_command;
$command = $_POST['command'];
if(oneListExists()) $listId;
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.")";
else if(isset($command))
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).") RETURN @id, ".$productId."))"; {
switch($command)
if (DBLink::getDbLink()->query($SQL_command)) {
echo "Product added to list.<br>"; case "addToList":
else $productId = $_POST['productToAdd'];
echo "Product could not be added to list.<br>"; addToList($productId);
} break;
else case "removeFromList":
echo "Select a product."; $productId = $_POST['productToRemove'];
} $listId = $_POST['listId'];
removeFromList($productId, $listId);
break;
function removeFromList($productId) case "displayShoppingList":
{ displayShoppingList();
if($productId > 0) break;
{ case "createNewShoppingList":
if(oneListExists()) createNewShoppingList();
{ break;
$SQL_command = "DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId; case "createProduct":
$productNames = $_POST['productToCreateName'];
if (DBLink::getDbLink()->query($SQL_command)) $storeTypeId = $_POST['storeType'];
echo "Product removed from list.<br>"; $rc = createProduct($productNames, $storeTypeId);
else if($rc > 0)
echo "Product could not be removed from list.<br>"; $justCreatedProductId = $rc;
} break;
} case "oldList":
else $listId = $_POST['oldListId'];
echo "Select a product."; $subCommand = $_POST['bSubmit'];
} if($subCommand == "remove")
{
removeShoppingList($listId);
function oneListExists() $listId = null;
{ }
$SQL_command = "SELECT COUNT(id) as listAmount FROM lists"; break;
$mysqli_result = DBLink::getDbLink()->query($SQL_command); case "trainRhasspy":
if ($row = mysqli_fetch_object($mysqli_result)) echo "Triggering training in Rhasspy has not been implemented, yet.";
{ break;
if($row->listAmount > 0) }
return true; }
}
displayShoppingList($listId, $justCreatedProductId);
return false;
} showDataMaintenance();
echo "</body></html>";
function displayShoppingList()
{ mysqli_close(DBLink::getDbLink());
echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"if(!confirm('Are you sure you want to create a new list?')){return false;}\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"createNewShoppingList\" />
<input type=\"submit\" value=\"Create new list\" /> function addToList($productId)
</form>"; {
if($productId > 0)
{
echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"> $SQL_command;
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" />
<select name=\"productToAdd\" id=\"productToAdd\"> if(oneListExists())
<option value=\"0\">Select product to add</option> $SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.")";
"; else
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).") RETURN @id, ".$productId."))";
$SQL_command = "SELECT id, name FROM `products` ORDER BY name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command); if (DBLink::getDbLink()->query($SQL_command))
while ($row = mysqli_fetch_object($mysqli_result)) echo "Product added to list.<br>";
{ else
echo "<option value=\"".$row->id."\">".$row->name."</option>"; echo "Product could not be added to list.<br>";
} }
else
echo " </select> echo "Select a product.";
<input type=\"submit\" value=\"add\" /> }
</form>";
if(oneListExists()) function removeFromList($productId, $listId)
{ {
$SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC LIMIT 0,1"; if($productId > 0)
{
$mysqli_result = DBLink::getDbLink()->query($SQL_command); if(oneListExists())
{
if ($row = mysqli_fetch_object($mysqli_result)) $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId;
{
echo "List created on ".date("l, d.m.Y", ($row->creationTime)/1000)." at ".date("G:i:s", ($row->creationTime)/1000)."<br />"; if (DBLink::getDbLink()->query($SQL_command))
echo "Product removed from list.<br>";
if(isset($row->comment) && strlen($row->comment) > 0) else
echo "<i>".$row->comment."</i><br />"; echo "Product could not be removed from list.<br>";
}
echo "<br />"; }
} else
echo "Select a product.";
$SQL_command = "SELECT * FROM listEntries INNER JOIN products ON listEntries.productId=products.id INNER JOIN storeTypes ON products.storeTypeId = storeTypes.id WHERE listEntries.listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) ORDER BY storeTypes.name, products.name ASC"; }
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
function oneListExists()
$lastShop=""; {
$SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
while ($row = mysqli_fetch_array($mysqli_result)) $mysqli_result = DBLink::getDbLink()->query($SQL_command);
{ if ($row = mysqli_fetch_object($mysqli_result))
if(isset($lastShop) && $lastShop != "" && $row[7] != $lastShop) {
echo "</table>"; if($row->listAmount > 0)
return true;
if(!isset($lastShop) || $row[7] != $lastShop) }
{
$lastShop = $row[7]; return false;
echo $lastShop."<br>================"; }
echo "<table border=\"0\">";
} function twoListsExist()
{
echo "<tr> $SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
<td style=\"vertical-align:top;\">".$row[3]."</td> $mysqli_result = DBLink::getDbLink()->query($SQL_command);
<td style=\"vertical-align:top;\"> if ($row = mysqli_fetch_object($mysqli_result))
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"> {
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"removeFromList\" /> if($row->listAmount > 1)
<input type=\"hidden\" id=\"productToRemove\" name=\"productToRemove\" value=\"".$row[1]."\" /> return true;
<input class=\"smallButton\" type=\"submit\" value=\"remove\" /> }
</form>
</td> return false;
</tr>"; }
}
echo "</table>"; function displayShoppingList($listId, $justCreatedProductId)
} {
else echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
echo "No list exists, yet."; <input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" />
} <select name=\"productToAdd\" id=\"productToAdd\">
<option value=\"0\">Select product to add</option>
";
function displayProductList()
{ $SQL_command = "SELECT id, name FROM `products` ORDER BY name ASC";
$SQL_Befehl = "SELECT name, synonyms FROM products ORDER BY products.name ASC"; $mysqli_result = DBLink::getDbLink()->query($SQL_command);
while ($row = mysqli_fetch_object($mysqli_result))
$mysqli_result = DBLink::getDbLink()->query($SQL_Befehl); {
$currentId = (int)$row->id;
$lastShop="";
if(isset($justCreatedProductId) && $justCreatedProductId === $currentId)
while ($row = mysqli_fetch_array($mysqli_result)) echo "<option value=\"".$row->id."\" selected>".$row->name."</option>";
{ else
if(is_null($row[1])) echo "<option value=\"".$row->id."\">".$row->name."</option>";
echo $row[0]."\n"; }
else
echo "(".str_replace(";", "|", $row[1])."|".$row[0]."):".$row[0]."\n"; echo " </select>
} <input type=\"submit\" value=\"add\" />
} </form>";
if(oneListExists())
function createNewShoppingList() {
{ $list_SQL_command;
$date = new DateTime();
if(isset($listId))
$SQL_command = "INSERT INTO lists (creationTime) VALUES (".($date->getTimestamp() * 1000).")"; {
$list_SQL_command = "SELECT * FROM lists WHERE id=".$listId;
if (DBLink::getDbLink()->query($SQL_command)) }
echo "New list has been created.<br>"; else
else {
echo "Error creating new list.<br>"; $list_SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC LIMIT 0,1";
}
$SQL_command_listId = "SELECT id FROM lists ORDER BY creationTime DESC LIMIT 0,1";
$mysqli_result = DBLink::getDbLink()->query($SQL_command_listId);
if ($row = mysqli_fetch_object($mysqli_result))
$listId = $row->id;
}
$mysqli_result = DBLink::getDbLink()->query($list_SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
echo "List created on ".date("l, d.m.Y", ($row->creationTime)/1000)." at ".date("G:i:s", ($row->creationTime)/1000)."<br />";
if(isset($row->comment) && strlen($row->comment) > 0)
echo "<i>".$row->comment."</i><br />";
echo "<br />";
}
$SQL_command = "SELECT * FROM listEntries INNER JOIN products ON listEntries.productId=products.id INNER JOIN storeTypes ON products.storeTypeId = storeTypes.id WHERE listEntries.listId=".$listId." ORDER BY storeTypes.name, products.name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
$lastShop="";
while ($row = mysqli_fetch_array($mysqli_result))
{
if(isset($lastShop) && $lastShop != "" && $row[7] != $lastShop)
echo "</table>";
if(!isset($lastShop) || $row[7] != $lastShop)
{
$lastShop = $row[7];
echo $lastShop."<br>================";
echo "<table border=\"0\">";
}
echo "<tr>
<td style=\"vertical-align:top;\">".$row[3]."</td>
<td style=\"vertical-align:top;\">
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"removeFromList\" />
<input type=\"hidden\" id=\"listId\" name=\"listId\" value=\"".$listId."\" />
<input type=\"hidden\" id=\"productToRemove\" name=\"productToRemove\" value=\"".$row[1]."\" />
<input class=\"smallButton\" type=\"submit\" value=\"remove\" />
</form>
</td>
</tr>";
}
echo "</table>";
}
else
echo "No list exists, yet.";
}
function showDataMaintenance()
{
echo "<hr />";
echo " <h2>Data maintenance</h3>
<h3>Create new list</h3>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"if(!confirm('Are you sure you want to create a new list?')){return false;}\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"createNewShoppingList\" />
<input type=\"submit\" value=\"Create new list\" />
</form>
<h3>Create new product</h3>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"return checkCreateProduct()\" id=\"createProductForm\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"createProduct\" />
<input type=\"text\" id=\"productToCreateName\" name=\"productToCreateName\" />
<select name=\"storeType\" id=\"storeType\">
<option value=\"0\">Select store type</option>
";
$SQL_command = "SELECT id, name FROM storeTypes ORDER BY name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
while ($row = mysqli_fetch_object($mysqli_result))
{
echo "<option value=\"".$row->id."\">".$row->name."</option>";
}
echo " </select>
<input type=\"submit\" value=\"Create new product\" />
</form>
<font size=\"2\">You can enter synonyms. Separate all terms with semikolons.</font>";
if(twoListsExist())
{
$SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
echo "<h3>Show specific list</h3>";
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" id=\"oldListForm\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"oldList\" />
<select name=\"oldListId\" id=\"oldListId\">";
while ($row = mysqli_fetch_object($mysqli_result))
{
echo "<option value=\"".$row->id."\">List created on ".date("l, d.m.Y", ($row->creationTime)/1000)." at ".date("G:i:s", ($row->creationTime)/1000);
if(isset($row->comment) && strlen($row->comment) > 0)
echo "<i>(".$row->comment.")</i>";
echo "</option>";
}
echo "
</select>
<input type=\"submit\" name=\"bSubmit\" value=\"show\" />
<input type=\"submit\" name=\"bSubmit\" value=\"remove\" onClick=\"if(!confirm('Are you sure you want to delete that list?')){return false;}\"/>
</form>";
}
echo " <h3>Train Rhasspy</h3>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"trainRhasspy\" />
<input type=\"submit\" value=\"Train Rhasspy\" />
</form>";
}
function displayProductList()
{
$SQL_Befehl = "SELECT name, synonyms FROM products ORDER BY products.name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_Befehl);
$lastShop="";
while ($row = mysqli_fetch_array($mysqli_result))
{
if(is_null($row[1]))
echo $row[0]."\n";
else
echo "(".str_replace(";", "|", $row[1])."|".$row[0]."):".$row[0]."\n";
}
}
function createNewShoppingList()
{
$date = new DateTime();
$SQL_command = "INSERT INTO lists (creationTime) VALUES (".($date->getTimestamp() * 1000).")";
if (DBLink::getDbLink()->query($SQL_command))
echo "New list has been created.<br>";
else
echo "Error creating new list.<br>";
}
function removeShoppingList($listId)
{
$date = new DateTime();
$SQL_command = "DELETE FROM lists WHERE id=".$listId;
if (DBLink::getDbLink()->query($SQL_command))
echo "List has been deleted.<br>";
else
echo "Error deleting list.<br>";
}
function createProduct($productNames, $storeTypeId)
{
$date = new DateTime();
$SQL_command;
if(strpos($productNames, ";") !== false)
{
$terms = explode(";", $productNames);
$mainName = $terms[0];
$syns = "";
for($i=1; $i<count($terms); $i++)
{
$currentWord = trim($terms[$i]);
if(strlen($currentWord) > 0)
$syns .= ";".$currentWord;
}
$syns = ltrim($syns, ';');
}
if(strlen($syns) > 0)
$SQL_command = "INSERT INTO products (name, synonyms, storeTypeId) VALUES (\"".$mainName."\", \"".$syns."\",".$storeTypeId.")";
else
$SQL_command = "INSERT INTO products (name, storeTypeId) VALUES (\"".$productNames."\", ".$storeTypeId.")";
if (DBLink::getDbLink()->query($SQL_command))
{
echo "New product has been created.<br>";
return DBLink::getDbLink()->insert_id;
}
else
echo "Error creating new product.<br>";
return -1;
}
?> ?>