2021-04-23 00:30:23 +02:00
< ? php
require_once ( " shoppingListConfig.php " );
$justCreatedProductId ;
class DBLink
{
static $sqlLink = null ;
public static function getDbLink ()
{
if ( DBLink :: $sqlLink == null )
{
DBLink :: $sqlLink = new mysqli ( Configuration :: $mysqlserver , Configuration :: $mysqluser , Configuration :: $mysqlpw , Configuration :: $mysqldb , Configuration :: $mysqlPort );
// Verbindung überprüfen
if ( mysqli_connect_errno ())
{
printf ( " Database connection error:%s \n " , mysqli_connect_error ());
exit ();
}
}
return DBLink :: $sqlLink ;
}
}
2021-04-29 15:47:47 +02:00
if ( isset ( $_GET [ 'command' ]))
2021-04-23 00:30:23 +02:00
{
2021-04-29 15:47:47 +02:00
if ( $_GET [ 'command' ] == " printProductList " || $_GET [ 'command' ] == " printRhasspyProductList " )
{
displayProductList ();
exit ( 0 );
}
else if ( $_GET [ 'command' ] == " printUnitList " )
{
displayUnitList ();
exit ( 0 );
}
2021-04-23 00:30:23 +02:00
}
?>
< html >
< head >
< title > Shopping list </ title >
< style type = " text/css " >
body
{
font - family : Arial , Geneva , Helvetica , sans - serif ;
}
table
{
border - collapse : separate ;
border - spacing : 10 px 10 px ;
}
. smallButton
{
font - size : 10 px ;;
height : 5000 em ;
width : 5000 em ;
}
</ style >
< script language = " javascript " type = " text/javascript " >
function checkCreateProduct ()
{
var productToCreate = document . forms [ " createProductForm " ][ " productToCreateName " ] . value ;
if ( productToCreate == " " )
{
alert ( " Product name must be filled out " );
return false ;
}
var storeTypeId = document . forms [ " createProductForm " ][ " storeType " ] . value ;
if ( storeTypeId == " " || storeTypeId == " 0 " )
{
alert ( " Store type must be selected " );
return false ;
}
return true ;
}
</ script >
</ head >
< body >
< ? php
$command = $_POST [ 'command' ];
$listId ;
if ( isset ( $command ))
{
switch ( $command )
{
case " addToList " :
$productId = $_POST [ 'productToAdd' ];
2021-04-29 15:47:47 +02:00
$amount = $_POST [ 'amountToAdd' ];
$unitId = $_POST [ 'unitToAdd' ];
addToList ( $productId , $amount , $unitId );
2021-04-23 00:30:23 +02:00
break ;
case " removeFromList " :
$productId = $_POST [ 'productToRemove' ];
$listId = $_POST [ 'listId' ];
2021-04-29 15:47:47 +02:00
$amount = $_POST [ 'productAmountToRemove' ];
$unit = $_POST [ 'productUnitToRemove' ];
removeFromList ( $productId , $listId , $amount , $unit );
2021-04-23 00:30:23 +02:00
break ;
case " displayShoppingList " :
displayShoppingList ();
break ;
case " createNewShoppingList " :
createNewShoppingList ();
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 ( $listId , $justCreatedProductId );
showDataMaintenance ();
echo " </body></html> " ;
mysqli_close ( DBLink :: getDbLink ());
2021-04-29 15:47:47 +02:00
function createList ()
2021-04-23 00:30:23 +02:00
{
2021-04-29 15:47:47 +02:00
$SQL_command = " INSERT INTO `lists` (creationTime) VALUES ( " . round ( microtime ( true ) * 1000 ) . " ) " ;
if ( DBLink :: getDbLink () -> query ( $SQL_command ))
2021-04-23 00:30:23 +02:00
{
2021-04-29 15:47:47 +02:00
echo " List created.<br> " ;
return true ;
}
return false ;
}
2021-04-23 00:30:23 +02:00
2021-04-29 15:47:47 +02:00
function addToList ( $productId , $amount , $unitId )
{
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> " ;
}
2021-04-23 00:30:23 +02:00
else
2021-04-29 15:47:47 +02:00
{
/*
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> " ;
}
}
}
2021-04-23 00:30:23 +02:00
}
else
2021-04-29 15:47:47 +02:00
echo " Specifiy product, amount and unit. " ;
2021-04-23 00:30:23 +02:00
}
2021-04-29 15:47:47 +02:00
function removeFromList ( $productId , $listId , $amount , $unitId )
2021-04-23 00:30:23 +02:00
{
2021-04-29 15:47:47 +02:00
if ( $productId > 0 && $amount != null && $unitId > 0 )
2021-04-23 00:30:23 +02:00
{
if ( oneListExists ())
{
2021-04-29 15:47:47 +02:00
$SQL_command = " DELETE FROM listEntries WHERE listId= " . $listId . " AND productId= " . $productId . " AND amount= " . $amount . " AND unit= " . $unitId ;
2021-04-23 00:30:23 +02:00
if ( DBLink :: getDbLink () -> query ( $SQL_command ))
echo " Product removed from list.<br> " ;
else
echo " Product could not be removed from list.<br> " ;
}
}
else
echo " Select a product. " ;
}
2021-04-29 15:47:47 +02:00
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 ;
}
2021-04-23 00:30:23 +02:00
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 ;
}
function displayShoppingList ( $listId , $justCreatedProductId )
{
2021-04-29 15:47:47 +02:00
echo " <h2>Shopping list</h2>
< form action = \ " " . $_SERVER [ 'PHP_SELF' ] . " \" method= \" POST \" >
2021-04-23 00:30:23 +02:00
< input type = \ " hidden \" id= \" command \" name= \" command \" value= \" addToList \" />
< select name = \ " productToAdd \" id= \" productToAdd \" >
< option value = \ " 0 \" >Select product to add</option>
" ;
$SQL_command = " SELECT id, name FROM `products` ORDER BY name ASC " ;
$mysqli_result = DBLink :: getDbLink () -> query ( $SQL_command );
while ( $row = mysqli_fetch_object ( $mysqli_result ))
{
$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> " ;
}
2021-04-29 15:47:47 +02:00
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> " ;
}
2021-04-23 00:30:23 +02:00
echo " </select>
< input type = \ " submit \" value= \" add \" />
</ form > " ;
if ( oneListExists ())
{
$list_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 ))
{
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 /> " ;
}
2021-04-29 15:47:47 +02:00
$SQL_command = " SELECT * FROM listEntries
INNER JOIN products ON listEntries . productId = products . id
INNER JOIN storeTypes ON products . storeTypeId = storeTypes . id
INNER JOIN units ON units . id = listEntries . unit
WHERE listEntries . listId = " . $listId . " ORDER BY storeTypes . name , products . name ASC " ;
2021-04-23 00:30:23 +02:00
$mysqli_result = DBLink :: getDbLink () -> query ( $SQL_command );
$lastShop = " " ;
2021-04-29 15:47:47 +02:00
$found = false ;
2021-04-23 00:30:23 +02:00
while ( $row = mysqli_fetch_array ( $mysqli_result ))
{
2021-04-29 15:47:47 +02:00
if ( ! $found )
$found = true ;
if ( isset ( $lastShop ) && $lastShop != " " && $row [ 9 ] != $lastShop )
2021-04-23 00:30:23 +02:00
echo " </table> " ;
2021-04-29 15:47:47 +02:00
if ( ! isset ( $lastShop ) || $row [ 9 ] != $lastShop )
2021-04-23 00:30:23 +02:00
{
2021-04-29 15:47:47 +02:00
$lastShop = $row [ 9 ];
2021-04-23 00:30:23 +02:00
echo $lastShop . " <br>================ " ;
echo " <table border= \" 0 \" > " ;
}
2021-04-29 15:47:47 +02:00
echo " <tr> " ;
if ( $row [ 14 ] == 1 )
echo " <td style= \" vertical-align:top; \" colspan= \" 3 \" > " . $row [ 5 ] . " </td> " ;
else
echo "
< td style = \ " vertical-align:top; \" > " . $row [ 2 ] . " </td>
< td style = \ " vertical-align:top; \" > " . $row [ 12 ] . " </td>
< td style = \ " vertical-align:top; \" > " . $row [ 5 ] . " </td> " ;
echo "
2021-04-23 00:30:23 +02:00
< td style = \ " vertical-align:top; \" >
< form action = \ " " . $_SERVER [ 'PHP_SELF' ] . " \" method= \" POST \" >
< input type = \ " hidden \" id= \" command \" name= \" command \" value= \" removeFromList \" />
2021-04-29 15:47:47 +02:00
< input type = \ " hidden \" id= \" listId \" name= \" listId \" value= \" " . $listId . " \" />
2021-04-23 00:30:23 +02:00
< input type = \ " hidden \" id= \" productToRemove \" name= \" productToRemove \" value= \" " . $row [ 1 ] . " \" />
2021-04-29 15:47:47 +02:00
< input type = \ " hidden \" id= \" productAmountToRemove \" name= \" productAmountToRemove \" value= \" " . $row [ 2 ] . " \" />
< input type = \ " hidden \" id= \" productUnitToRemove \" name= \" productUnitToRemove \" value= \" " . $row [ 10 ] . " \" />
2021-04-23 00:30:23 +02:00
< input class = \ " smallButton \" type= \" submit \" value= \" remove \" />
</ form >
</ td >
</ tr > " ;
}
2021-04-29 15:47:47 +02:00
if ( $found )
echo " </table> " ;
else
echo " <i>No entries in this list, yet.</i> " ;
2021-04-23 00:30:23 +02:00
}
else
2021-04-29 15:47:47 +02:00
echo " <i>No list exists, yet.</i> " ;
2021-04-23 00:30:23 +02:00
}
function showDataMaintenance ()
{
echo " <hr /> " ;
2021-04-29 15:47:47 +02:00
echo " <h2>Data maintenance</h2>
2021-04-23 00:30:23 +02:00
< 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 " ;
}
}
2021-04-29 15:47:47 +02:00
function displayUnitList ()
{
$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 " ;
}
}
}
2021-04-23 00:30:23 +02:00
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 ;
}
2021-02-03 23:20:13 +01:00
?>