2021-02-03 23:20:13 +01:00
< ? php
2021-02-05 21:46:17 +01:00
require_once ( " shoppingListConfig.php " );
2021-02-03 23:20:13 +01:00
class DBLink
{
static $sqlLink = null ;
public static function getDbLink ()
{
if ( DBLink :: $sqlLink == null )
{
2021-02-05 21:46:17 +01:00
DBLink :: $sqlLink = new mysqli ( Configuration :: $mysqlserver , Configuration :: $mysqluser , Configuration :: $mysqlpw , Configuration :: $mysqldb , Configuration :: $mysqlPort );
2021-02-03 23:20:13 +01:00
// Verbindung überprüfen
if ( mysqli_connect_errno ())
{
printf ( " Database connection error:%s \n " , mysqli_connect_error ());
exit ();
}
}
return DBLink :: $sqlLink ;
}
}
2021-02-27 00:48:17 +01:00
if ( isset ( $_GET [ 'command' ]) && $_GET [ 'command' ] == " printProductList " || $_GET [ 'command' ] == " printRhasspyProductList " )
2021-02-03 23:20:13 +01:00
{
2021-02-22 15:36:21 +01:00
displayProductList ();
2021-02-03 23:20:13 +01:00
exit ( 0 );
}
echo " <html>
< head >
< title > Shopping list </ title >
< style type = \ " text/css \" >
table
{
border - collapse : separate ;
border - spacing : 10 px 10 px ;
}
. smallButton
{
font - size : 10 px ;;
height : 5000 em ;
width : 5000 em ;
}
</ style >
</ head >
< body > " ;
$command = $_POST [ 'command' ];
if ( isset ( $command ))
{
switch ( $command )
{
case " addToList " :
$productId = $_POST [ 'productToAdd' ];
addToList ( $productId );
break ;
case " removeFromList " :
$productId = $_POST [ 'productToRemove' ];
removeFromList ( $productId );
break ;
2021-02-22 15:36:21 +01:00
case " displayShoppingList " :
displayShoppingList ();
break ;
case " createNewShoppingList " :
createNewShoppingList ();
2021-02-03 23:20:13 +01:00
break ;
}
}
2021-02-22 15:36:21 +01:00
displayShoppingList ();
2021-02-03 23:20:13 +01:00
echo " </body></html> " ;
mysqli_close ( DBLink :: getDbLink ());
function addToList ( $productId )
{
if ( $productId > 0 )
{
$SQL_command ;
if ( oneListExists ())
$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 . " )) " ;
if ( DBLink :: getDbLink () -> query ( $SQL_command ))
echo " Product added to list.<br> " ;
else
echo " Product could not be added to list.<br> " ;
}
else
echo " Select a product. " ;
}
function removeFromList ( $productId )
{
if ( $productId > 0 )
{
if ( oneListExists ())
{
$SQL_command = " DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId= " . $productId ;
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. " ;
}
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 ;
}
2021-02-22 15:36:21 +01:00
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;} \" >
< input type = \ " hidden \" id= \" command \" name= \" command \" value= \" createNewShoppingList \" />
< input type = \ " submit \" value= \" Create new list \" />
</ form > " ;
2021-02-03 23:20:13 +01:00
echo " <form action= \" " . $_SERVER [ 'PHP_SELF' ] . " \" method= \" POST \" >
< 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 ))
{
echo " <option value= \" " . $row -> id . " \" > " . $row -> name . " </option> " ;
}
echo " </select>
< input type = \ " submit \" value= \" add \" />
</ form > " ;
if ( oneListExists ())
{
2021-02-26 15:26:01 +01:00
$SQL_command = " SELECT * FROM lists ORDER BY creationTime DESC LIMIT 0,1 " ;
$mysqli_result = DBLink :: getDbLink () -> query ( $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-02-03 23:20:13 +01:00
$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 );
$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= \" productToRemove \" name= \" productToRemove \" value= \" " . $row [ 1 ] . " \" />
< input class = \ " smallButton \" type= \" submit \" value= \" remove \" />
</ form >
</ td >
</ tr > " ;
}
echo " </table> " ;
}
else
echo " No list exists, yet. " ;
}
2021-02-22 15:36:21 +01:00
function displayProductList ()
2021-02-03 23:20:13 +01:00
{
$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-02-22 15:36:21 +01: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> " ;
}
2021-02-03 23:20:13 +01:00
?>