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

@ -2,6 +2,8 @@
require_once("shoppingListConfig.php"); require_once("shoppingListConfig.php");
$justCreatedProductId;
class DBLink class DBLink
{ {
static $sqlLink = null; static $sqlLink = null;
@ -30,27 +32,62 @@
exit(0); exit(0);
} }
echo "<html> ?>
<head>
<title>Shopping list</title>
<style type=\"text/css\">
table
{
border-collapse:separate;
border-spacing:10px 10px;
}
.smallButton <html>
<head>
<title>Shopping list</title>
<style type="text/css">
body
{
font-family: Arial, Geneva, Helvetica, sans-serif;
}
table
{
border-collapse:separate;
border-spacing:10px 10px;
}
.smallButton
{
font-size:10px;;
height:5000em;
width:5000em;
}
</style>
<script language="javascript" type="text/javascript">
function checkCreateProduct()
{
var productToCreate = document.forms["createProductForm"]["productToCreateName"].value;
if (productToCreate == "")
{ {
font-size:10px;; alert("Product name must be filled out");
height:5000em; return false;
width:5000em;
} }
</style>
</head> var storeTypeId = document.forms["createProductForm"]["storeType"].value;
<body>"; if (storeTypeId == "" || storeTypeId == "0")
{
alert("Store type must be selected");
return false;
}
return true;
}
</script>
</head>
<body>
<?php
$command = $_POST['command']; $command = $_POST['command'];
$listId;
if(isset($command)) if(isset($command))
{ {
@ -62,7 +99,8 @@
break; break;
case "removeFromList": case "removeFromList":
$productId = $_POST['productToRemove']; $productId = $_POST['productToRemove'];
removeFromList($productId); $listId = $_POST['listId'];
removeFromList($productId, $listId);
break; break;
case "displayShoppingList": case "displayShoppingList":
displayShoppingList(); displayShoppingList();
@ -70,17 +108,37 @@
case "createNewShoppingList": case "createNewShoppingList":
createNewShoppingList(); createNewShoppingList();
break; break;
case "createProduct":
$productNames = $_POST['productToCreateName'];
$storeTypeId = $_POST['storeType'];
$rc = createProduct($productNames, $storeTypeId);
if($rc > 0)
$justCreatedProductId = $rc;
break;
case "oldList":
$listId = $_POST['oldListId'];
$subCommand = $_POST['bSubmit'];
if($subCommand == "remove")
{
removeShoppingList($listId);
$listId = null;
}
break;
case "trainRhasspy":
echo "Triggering training in Rhasspy has not been implemented, yet.";
break;
} }
} }
displayShoppingList(); displayShoppingList($listId, $justCreatedProductId);
showDataMaintenance();
echo "</body></html>"; echo "</body></html>";
mysqli_close(DBLink::getDbLink()); mysqli_close(DBLink::getDbLink());
function addToList($productId) function addToList($productId)
{ {
if($productId > 0) if($productId > 0)
@ -102,13 +160,13 @@
} }
function removeFromList($productId) function removeFromList($productId, $listId)
{ {
if($productId > 0) if($productId > 0)
{ {
if(oneListExists()) if(oneListExists())
{ {
$SQL_command = "DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId; $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId;
if (DBLink::getDbLink()->query($SQL_command)) if (DBLink::getDbLink()->query($SQL_command))
echo "Product removed from list.<br>"; echo "Product removed from list.<br>";
@ -134,15 +192,21 @@
return false; return false;
} }
function twoListsExist()
function displayShoppingList()
{ {
echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"if(!confirm('Are you sure you want to create a new list?')){return false;}\"> $SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"createNewShoppingList\" /> $mysqli_result = DBLink::getDbLink()->query($SQL_command);
<input type=\"submit\" value=\"Create new list\" /> if ($row = mysqli_fetch_object($mysqli_result))
</form>"; {
if($row->listAmount > 1)
return true;
}
return false;
}
function displayShoppingList($listId, $justCreatedProductId)
{
echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"> echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" /> <input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" />
<select name=\"productToAdd\" id=\"productToAdd\"> <select name=\"productToAdd\" id=\"productToAdd\">
@ -153,7 +217,12 @@
$mysqli_result = DBLink::getDbLink()->query($SQL_command); $mysqli_result = DBLink::getDbLink()->query($SQL_command);
while ($row = mysqli_fetch_object($mysqli_result)) while ($row = mysqli_fetch_object($mysqli_result))
{ {
echo "<option value=\"".$row->id."\">".$row->name."</option>"; $currentId = (int)$row->id;
if(isset($justCreatedProductId) && $justCreatedProductId === $currentId)
echo "<option value=\"".$row->id."\" selected>".$row->name."</option>";
else
echo "<option value=\"".$row->id."\">".$row->name."</option>";
} }
echo " </select> echo " </select>
@ -162,9 +231,24 @@
if(oneListExists()) if(oneListExists())
{ {
$SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC LIMIT 0,1"; $list_SQL_command;
$mysqli_result = DBLink::getDbLink()->query($SQL_command); if(isset($listId))
{
$list_SQL_command = "SELECT * FROM lists WHERE id=".$listId;
}
else
{
$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)) if ($row = mysqli_fetch_object($mysqli_result))
{ {
@ -176,7 +260,7 @@
echo "<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=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) ORDER BY storeTypes.name, products.name ASC"; $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); $mysqli_result = DBLink::getDbLink()->query($SQL_command);
@ -199,6 +283,7 @@
<td style=\"vertical-align:top;\"> <td style=\"vertical-align:top;\">
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\"> <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"removeFromList\" /> <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 type=\"hidden\" id=\"productToRemove\" name=\"productToRemove\" value=\"".$row[1]."\" />
<input class=\"smallButton\" type=\"submit\" value=\"remove\" /> <input class=\"smallButton\" type=\"submit\" value=\"remove\" />
</form> </form>
@ -211,6 +296,80 @@
echo "No list exists, yet."; 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() function displayProductList()
{ {
@ -242,4 +401,57 @@
echo "Error creating new list.<br>"; 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;
}
?> ?>