Compare commits

...

4 Commits

Author SHA1 Message Date
0971e3cfaf Errors fixed when creating/managing products 2021-05-08 15:07:52 +02:00
84e73a9ec5 Added rhasspyMasterUrl to php config. 2021-05-04 11:46:05 +02:00
89f51dab28 Updates 2021-05-02 21:45:32 +02:00
39db9470cc - 2021-05-02 21:24:30 +02:00
5 changed files with 271 additions and 216 deletions

View File

@ -0,0 +1,196 @@
-- phpMyAdmin SQL Dump
-- version 5.0.4
-- https://www.phpmyadmin.net/
--
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `shoppingList`
--
-- --------------------------------------------------------
--
-- Table structure for table `listEntries`
--
CREATE TABLE `listEntries` (
`listId` int(11) NOT NULL,
`productId` int(11) NOT NULL,
`amount` float NOT NULL DEFAULT 1,
`unit` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `lists`
--
CREATE TABLE `lists` (
`id` int(11) NOT NULL,
`creationTime` bigint(20) NOT NULL,
`comment` varchar(200) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `products`
--
CREATE TABLE `products` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`synonyms` varchar(500) DEFAULT NULL COMMENT 'Separate entries with semicolons.',
`storeTypeId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `settings`
--
CREATE TABLE `settings` (
`settingName` varchar(100) NOT NULL,
`settingValue` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `settings`
--
INSERT INTO `settings` (`settingName`, `settingValue`) VALUES
('databaseVersion', '3');
-- --------------------------------------------------------
--
-- Table structure for table `storeTypes`
--
CREATE TABLE `storeTypes` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Table structure for table `units`
--
CREATE TABLE `units` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`abbreviation` varchar(10) NOT NULL,
`isDefault` tinyint(2) NOT NULL DEFAULT 0,
`isDummy` tinyint(2) NOT NULL DEFAULT 0 COMMENT 'This unit will not actually be displayed. Instead an item will just be shown without amount or unit.',
`isPiece` tinyint(2) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `listEntries`
--
ALTER TABLE `listEntries`
ADD PRIMARY KEY (`listId`,`productId`,`unit`),
ADD KEY `listEntries_ibfk_3` (`unit`),
ADD KEY `listEntries_ibfk_2` (`productId`);
--
-- Indexes for table `lists`
--
ALTER TABLE `lists`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `products`
--
ALTER TABLE `products`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`),
ADD KEY `storeTypeId` (`storeTypeId`);
--
-- Indexes for table `settings`
--
ALTER TABLE `settings`
ADD PRIMARY KEY (`settingName`);
--
-- Indexes for table `storeTypes`
--
ALTER TABLE `storeTypes`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`);
--
-- Indexes for table `units`
--
ALTER TABLE `units`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `lists`
--
ALTER TABLE `lists`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `storeTypes`
--
ALTER TABLE `storeTypes`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `units`
--
ALTER TABLE `units`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `listEntries`
--
ALTER TABLE `listEntries`
ADD CONSTRAINT `listEntries_ibfk_1` FOREIGN KEY (`listId`) REFERENCES `lists` (`id`),
ADD CONSTRAINT `listEntries_ibfk_2` FOREIGN KEY (`productId`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `listEntries_ibfk_3` FOREIGN KEY (`unit`) REFERENCES `units` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `products`
--
ALTER TABLE `products`
ADD CONSTRAINT `products_ibfk_1` FOREIGN KEY (`storeTypeId`) REFERENCES `storeTypes` (`id`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

View File

@ -5,9 +5,15 @@
See https://server47.de for more information.
*/
// ini_set('display_errors', 0);
// error_reporting(0);
error_reporting(E_ALL & E_NOTICE);
ini_set("display_errors","on");
require_once("shoppingListConfig.php");
$justCreatedProductId;
$justCreatedProductId = null;
$iconDelete = "";
$iconSave = "💾";
@ -67,7 +73,6 @@
displayUnitList();
exit(0);
}
<<<<<<< HEAD
}
class Unit
@ -871,8 +876,6 @@
return false;
}
=======
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
}
?>
@ -1006,19 +1009,16 @@
<?php
$command = $_POST['command'];
$listId;
if(isset($_POST['command']))
$command = $_POST['command'];
$listId = null;
if(isset($command))
{
switch($command)
{
case "addToList":
<<<<<<< HEAD
$productId = $_POST['productId'];
=======
$productId = $_POST['productToAdd'];
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
$amount = $_POST['amountToAdd'];
$unitId = $_POST['unitToAdd'];
addToList($productId, $amount, $unitId);
@ -1057,12 +1057,11 @@
case "createProduct":
$p = new Product();
$p->setID($_POST['oldProductId']);
if(strpos($_POST['productName'], ";") !== false)
{
$terms = explode(";", $_POST['productName']);
$p->setName = $terms[0];
$p->setName(trim($terms[0]));
$syns = "";
for($i=1; $i<count($terms); $i++)
@ -1095,8 +1094,25 @@
case "manageProduct":
$p = new Product();
$p->setID($_POST['oldProductId']);
$p->setName($_POST['productName']);
$p->setSynonyms($_POST['productSynonyms']);
if(strpos($_POST['productName'], ";") !== false)
{
$terms = explode(";", $_POST['productName']);
$p->setName(trim($terms[0]));
$syns = "";
for($i=1; $i<count($terms); $i++)
{
$currentWord = trim($terms[$i]);
if(strlen($currentWord) > 0)
$syns .= ";".$currentWord;
}
$syns = ltrim($syns, ';');
$p->setSynonyms($syns);
}
else
$p->setName($_POST['productName']);
$p->setStoreType(StoreType::getById($_POST['productStoreTypeId']));
GLOBAL $iconDelete;
@ -1207,7 +1223,10 @@
echo "Invalid command.";
break;
case "trainRhasspy":
echo "Triggering training in Rhasspy has not been implemented, yet.";
if(triggerRhasspyTraining())
echo "Rhasspy training has completed successfully.";
else
echo "Rhasspy training has failed.";
break;
}
}
@ -1221,20 +1240,10 @@
mysqli_close(DBLink::getDbLink());
<<<<<<< HEAD
function addToList($productId, $amount, $unitId)
=======
function createList()
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
{
<<<<<<< HEAD
if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0)
=======
$SQL_command = "INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).")";
if (DBLink::getDbLink()->query($SQL_command))
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
{
<<<<<<< HEAD
if(!ShoppingList::oneListExists())
createList();
@ -1278,74 +1287,6 @@
}
}
}
=======
echo "List created.<br>";
return true;
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
}
<<<<<<< HEAD
else
echo "Specifiy product, amount and unit.";
=======
return false;
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
}
<<<<<<< HEAD
function removeFromList($productId, $listId, $amount, $unitId)
=======
function addToList($productId, $amount, $unitId)
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
{
<<<<<<< HEAD
=======
if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0)
{
if(!oneListExists())
createList();
if(!entryCombinationExists($productId, $unitId))
{
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId, amount, unit) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.", ".$amount.", ".$unitId.")";
if (DBLink::getDbLink()->query($SQL_command))
echo "Product added to list.<br>";
else
echo "Product could not be added to list.<br>";
}
else
{
/*
update command that adds the amount to the already existing amount
if amount is negative -> subtract from existing amount. if resulting amount <= 0 ->
*/
$SQL_command = "SELECT amount FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId;
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
if($row->amount + $amount > 0)
{
$SQL_command = "UPDATE listEntries set amount=".($row->amount + $amount)." WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId;
if (DBLink::getDbLink()->query($SQL_command))
echo "Product added to existing entry on list.<br>";
else
echo "Product could not be added to existing entry on list.<br>";
}
else
{
$SQL_command = "DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId;
if (DBLink::getDbLink()->query($SQL_command))
echo "Product removed from list as too many pieces of this item have been removed.<br>";
else
echo "Product could not be removed from list (too many pieces of this itme have been removed).<br>";
}
}
}
}
else
echo "Specifiy product, amount and unit.";
@ -1354,7 +1295,6 @@
function removeFromList($productId, $listId, $amount, $unitId)
{
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
if($productId > 0 && $amount != null && $unitId > 0)
{
if(ShoppingList::oneListExists())
@ -1371,60 +1311,11 @@
echo "Select a product.";
}
<<<<<<< HEAD
=======
function entryCombinationExists($productId, $unitId)
{
$SQL_command = "SELECT COUNT(listId) as entryAmount FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId." AND unit=".$unitId;
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
if($row->entryAmount > 0)
return true;
}
return false;
}
function oneListExists()
{
$SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
if($row->listAmount > 0)
return true;
}
return false;
}
function twoListsExist()
{
$SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
if($row->listAmount > 1)
return true;
}
return false;
}
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
function displayShoppingList($listId, $justCreatedProductId)
{
echo " <h2>Shopping list</h2>
<<<<<<< HEAD
<form id=\"addProductToListForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"addToggleStates(this); return checkAddProductToList();\">
=======
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" />
<select name=\"productId\" id=\"productId\">
<option value=\"0\">Select product to add</option>
@ -1446,7 +1337,7 @@
{
$currentId = (int)$product->getID();
if(isset($justCreatedProductId) && $justCreatedProductId === $currentId)
if(isset($justCreatedProductId) && $justCreatedProductId != null && $justCreatedProductId === $currentId)
echo "<option value=\"".$product->getID()."\" name=\"".$product->getName()."\" selected=\"selected\">".$product->getName()."</option>";
else
echo "<option value=\"".$product->getID()."\" name=\"".$product->getName()."\">".$product->getName()."</option>";
@ -1474,27 +1365,6 @@
echo "<option value=\"".$row->id."\">".$unitArray[0]."</option>";
}
echo " </select>
<input type=\"number\" step=\"any\" id=\"amountToAdd\" name=\"amountToAdd\" value=\"1.0\" />
<select name=\"unitToAdd\" id=\"unitToAdd\">
<option value=\"0\">Select unit to add</option>
";
$SQL_command = "SELECT * FROM `units` ORDER BY name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
while ($row = mysqli_fetch_object($mysqli_result))
{
$currentId = (int)$row->id;
$unitArray = explode(";", $row->name);
if($row->isDefault == 1 || (isset($justCreatedUnitId) && $justCreatedUnitId === $currentId))
echo "<option value=\"".$row->id."\" selected>".$unitArray[0]."</option>";
else
echo "<option value=\"".$row->id."\">".$unitArray[0]."</option>";
}
echo " </select>
<input type=\"submit\" value=\"add\" />
</form>";
@ -1552,11 +1422,7 @@
if(!isset($lastShop) || $row[9] != $lastShop)
{
$lastShop = $row[9];
<<<<<<< HEAD
echo "<h3>".$lastShop."</h3>";
=======
echo $lastShop."<br>================";
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
echo "<table border=\"0\">";
}
@ -1570,11 +1436,8 @@
<td style=\"vertical-align:top;\">".$row[12]."</td>
<td style=\"vertical-align:top;\">".$row[5]."</td>";
<<<<<<< HEAD
GLOBAL $iconDelete;
=======
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
echo "
<td style=\"vertical-align:top;\">
<form id=\"removeItemFromListForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"addToggleStates(this)\">
@ -1583,11 +1446,7 @@
<input type=\"hidden\" id=\"productToRemove\" name=\"productToRemove\" value=\"".$row[1]."\" />
<input type=\"hidden\" id=\"productAmountToRemove\" name=\"productAmountToRemove\" value=\"".$row[2]."\" />
<input type=\"hidden\" id=\"productUnitToRemove\" name=\"productUnitToRemove\" value=\"".$row[10]."\" />
<<<<<<< HEAD
<input class=\"smallButton\" type=\"submit\" value=\"".$iconDelete."\" />
=======
<input class=\"smallButton\" type=\"submit\" value=\"remove\" />
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
</form>
</td>
</tr>";
@ -1606,10 +1465,9 @@
{
echo "<hr />";
<<<<<<< HEAD
$detailsName = "detailsMaintenance";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
echo "<details id=\"".$detailsName."\"".$openString." ontoggle=\"rememberToggleState(this)\">
@ -1622,16 +1480,13 @@
$detailsName = "detailsLists";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
echo " <details id=\"".$detailsName."\"".$openString." ontoggle=\"rememberToggleState(this)\">
<summary>
<strong>Manage shopping lists</strong>
</summary>
=======
echo " <h2>Data maintenance</h2>
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
<h3>Create new list</h3>
@ -1679,7 +1534,7 @@
$detailsName = "detailsRhasspy";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
echo " <details id=\"".$detailsName."\"".$openString." ontoggle=\"rememberToggleState(this)\">
@ -1689,6 +1544,8 @@
<h3>Train Rhasspy</h3>
WARNING: This can take up to 2 minutes.<br><br>
<form id=\"trainRhasspyForm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" onSubmit=\"addToggleStates(this)\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"trainRhasspy\" />
<input type=\"submit\" value=\"🚂\" style=\"font-size : 25px;\" />
@ -1697,7 +1554,7 @@
$detailsName = "detailsManageProducts";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
GLOBAL $iconSave;
@ -1789,7 +1646,7 @@
$detailsName = "detailsManageUnits";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
echo "
@ -1862,7 +1719,7 @@
$detailsName = "detailsManageStoreTypes";
$openString = "";
if(in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements'])))
$openString = " open=\"\"";
GLOBAL $iconSave;
@ -1958,36 +1815,6 @@
function displayUnitList()
<<<<<<< HEAD
=======
{
$SQL_Befehl = "SELECT name FROM units ORDER BY name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_Befehl);
$lastShop="";
while ($row = mysqli_fetch_object($mysqli_result))
{
$unitArray = explode(";", $row->name);
if(count($unitArray) == 1)
echo $unitArray[0]."\n";
else
{
$tbp = "";
for($i=1; $i<count($unitArray); $i++)
$tbp .= $unitArray[$i]."|";
$tbp = trim($tbp, "|");
echo "(".$tbp."):".$unitArray[0]."\n";
}
}
}
function createNewShoppingList()
>>>>>>> branch 'development' of https://git.server47.de/jens/ShoppingList.git
{
/*$SQL_command = "SELECT name FROM units ORDER BY name ASC";
@ -2040,4 +1867,35 @@
echo "Error deleting list.<br>";
}
function triggerRhasspyTraining()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Configuration::$rhasspyMasterUrl."/api/train");
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_POSTFIELDS, ""); // Send empty data to force POST
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// Further processing ...
if (strpos($server_output, 'Training completed') !== false)
{
return true;
}
return false;
}
?>

View File

@ -7,5 +7,6 @@
public static $mysqlpw = 'somePassword'; //Password
public static $mysqldb = 'shoppingList'; //Database
public static $mysqlPort = 3306;
public static $rhasspyMasterUrl = "http://rhasspy-master:12101"; // URL of your Rhasspy master webpage WITHOUT ending slash. It is required if you want to trigger a retraining.
}
?>