diff --git a/phpInterface/README.md b/phpInterface/README.md new file mode 100644 index 0000000..c1e56a9 --- /dev/null +++ b/phpInterface/README.md @@ -0,0 +1,7 @@ +# ShoppingListPHP + +This is a PHP interface for the shopping list I originally intended for use with the voice assistant Rhasspy only. + +Throw the files onto a webserver that has PHP and network access to your MySQL/MariaDB server and edit shoppingListConfig.php. + +Afterwards simply access shoppingList.php with your browser. diff --git a/phpInterface/shoppingList.php b/phpInterface/shoppingList.php new file mode 100644 index 0000000..bd66ded --- /dev/null +++ b/phpInterface/shoppingList.php @@ -0,0 +1,212 @@ + + + Shopping list + + + "; + + $command = $_POST['command']; + + if(isset($command)) + { + switch($command) + { + case "addToList": + $productId = $_POST['productToAdd']; + addToList($productId); + break; + case "removeFromList": + $productId = $_POST['productToRemove']; + removeFromList($productId); + break; + case "printShoppingList": + printShoppingList(); + break; + } + } + + printShoppingList(); + + echo ""; + + 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.
"; + else + echo "Product could not be added to list.
"; + } + 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.
"; + else + echo "Product could not be removed from list.
"; + } + } + 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; + } + + + + + function printShoppingList() + { + echo "
+ + + +
"; + + if(oneListExists()) + { + $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 ""; + + if(!isset($lastShop) || $row[7] != $lastShop) + { + $lastShop = $row[7]; + echo $lastShop."
================"; + echo ""; + } + + echo " + + + "; + } + echo "
".$row[3]." +
+ + + +
+
"; + } + else + echo "No list exists, yet."; + } + + + function printRhasspyProductList() + { + $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"; + } + } + +?> \ No newline at end of file diff --git a/phpInterface/shoppingListConfig.php b/phpInterface/shoppingListConfig.php new file mode 100644 index 0000000..818c6c8 --- /dev/null +++ b/phpInterface/shoppingListConfig.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/shellScripts/ShoppingListAdd.sh b/shellScripts/ShoppingListAdd.sh new file mode 100644 index 0000000..9b3e326 --- /dev/null +++ b/shellScripts/ShoppingListAdd.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +SHOPPINGITEM="" +SITEID="" +PARAMS="" +SESSIONID="" + +while (( "$#" )); do + case "$1" in + --shoppingProduct) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SHOPPINGITEM=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + --siteId) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SITEID=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + --sessionId) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SESSIONID=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + -*|--*=) # unsupported flags + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + *) # preserve positional arguments + PARAMS="$PARAMS $1" + shift + ;; + esac +done + +# set positional arguments in their proper place +eval set -- "$PARAMS" + +if [ "$SHOPPINGITEM" = "" ] +then + echo "Was soll ich auf die Liste setzen?" + exit 1 +else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM + if [ "$?" -eq "0" ] + then + exit 0 + else + exit 1 + fi +fi diff --git a/shellScripts/ShoppingListRemove.sh b/shellScripts/ShoppingListRemove.sh new file mode 100644 index 0000000..24286f4 --- /dev/null +++ b/shellScripts/ShoppingListRemove.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +SHOPPINGITEM="" +SITEID="" +PARAMS="" +SESSIONID="" + +while (( "$#" )); do + case "$1" in + --shoppingProduct) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SHOPPINGITEM=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + --siteId) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SITEID=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + --sessionId) + if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then + SESSIONID=$2 + shift 2 + else + echo "Error: Argument for $1 is missing" >&2 + exit 1 + fi + ;; + -*|--*=) # unsupported flags + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + *) # preserve positional arguments + PARAMS="$PARAMS $1" + shift + ;; + esac +done + +# set positional arguments in their proper place +eval set -- "$PARAMS" + +if [ "$SHOPPINGITEM" = "" ] +then + echo "Was soll ich von der Liste streichen?" + exit 1 +else + java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM + if [ "$?" -eq "0" ] + then + exit 0 + else + exit 1 + fi +fi diff --git a/shellScripts/ShoppingListReset.sh b/shellScripts/ShoppingListReset.sh new file mode 100644 index 0000000..3d3284b --- /dev/null +++ b/shellScripts/ShoppingListReset.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +java -jar /home/pi/hc_scripts/ShoppingList.jar --action resetList +if [ "$?" -eq "0" ] +then + exit 0 +else + exit 1 +fi diff --git a/shellScripts/ShoppingListSend.sh b/shellScripts/ShoppingListSend.sh new file mode 100644 index 0000000..5655498 --- /dev/null +++ b/shellScripts/ShoppingListSend.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +java -jar /home/pi/hc_scripts/ShoppingList.jar --action sendList +if [ "$?" -eq "0" ] +then + exit 0 +else + exit 1 +fi