From 07dd5f9c7eb4fabb668fbe4c6805b66de7959bbf Mon Sep 17 00:00:00 2001 From: Jens Date: Sun, 2 May 2021 21:22:07 +0200 Subject: [PATCH] Heavily improved PHP interface. --- phpInterface/shoppingList.php | 1743 +++++++++++++++++++++++++++++---- 1 file changed, 1564 insertions(+), 179 deletions(-) diff --git a/phpInterface/shoppingList.php b/phpInterface/shoppingList.php index 6803929..f92ce5f 100644 --- a/phpInterface/shoppingList.php +++ b/phpInterface/shoppingList.php @@ -1,9 +1,38 @@ id = $id; + } + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + } + public function getName() + { + return $this->name; + } + + public function setAbbreviation($abbreviation) + { + $this->abbreviation = $abbreviation; + } + public function getAbbreviation() + { + return $this->abbreviation; + } + + public function setDefault($isDefault) + { + $this->isDefault = $isDefault; + } + public function isDefault() + { + return $this->isDefault; + } + + public function setDummy($isDummy) + { + $this->isDummy = $isDummy; + } + public function isDummy() + { + return $this->isDummy; + } + + public function setPiece($isPiece) + { + $this->isPiece = $isPiece; + } + public function isPiece() + { + return $this->isPiece; + } + + public function getMainName() + { + $unitNamesArray = explode(";", $name); + return $unitNamesArray[0]; + } + + private function ensurePlausibility() + { + $SQL_commands = ""; + + if($this->isDefault()) + $SQL_commands .= "UPDATE units SET isDefault=0 WHERE NOT id=".$this->getID().";"; + + if($this->isDummy()) + $SQL_commands .= "UPDATE units SET isDummy=0 WHERE NOT id=".$this->getID().";"; + + if(strlen($SQL_commands) > 0) + { + $mysqli_result = DBLink::getDbLink()->query($SQL_commands); + + if ($mysqli_result) + return true; + + return false; + } + else + return true; + } + + public static function getById($unitId) + { + $allUnits = Unit::getAllUnits(); + + foreach($allUnits as $unit) + { + if($unit->getID() === $unitId) + { + return $unit; + } + } + + return null; + } + + public function create() + { + $SQL_command = "INSERT INTO units (name, abbreviation, isDefault, isDummy, isPiece) VALUES (\"".$this->getName()."\", \"".$this->getAbbreviation()."\", "; + + if($this->isDefault()) + $SQL_command .= "1, "; + else + $SQL_command .= "0, "; + + if($this->isDummy()) + $SQL_command .= "1, "; + else + $SQL_command .= "0, "; + + if($this->isPiece()) + $SQL_command .= "1)"; + else + $SQL_command .= "0)"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + $this->ensurePlausibility(); + return true; + } + + return false; + } + + public function change() + { + $SQL_command = "UPDATE units SET name=\"".$this->getName()."\", abbreviation=\"".$this->getAbbreviation()."\", "; + + if($this->isDefault()) + $SQL_command .= "isDefault=1, "; + else + $SQL_command .= "isDefault=0, "; + + if($this->isDummy()) + $SQL_command .= "isDummy=1, "; + else + $SQL_command .= "isDummy=0, "; + + if($this->isPiece()) + $SQL_command .= "isPiece=1"; + else + $SQL_command .= "isPiece=0"; + + $SQL_command .= " WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + $this->ensurePlausibility(); + return true; + } + + return false; + } + + public function delete() + { + $SQL_command = "DELETE FROM units WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function getUsage() + { + $SQL_command = "SELECT COUNT(listId) as entryAmount FROM listEntries WHERE unit=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($row = mysqli_fetch_object($mysqli_result)) + return $row->entryAmount; + + return 0; + } + + public static function getAllUnits() + { + $SQL_command = "SELECT * FROM units ORDER BY units.name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + $returnList = array(); + + while ($row = mysqli_fetch_object($mysqli_result)) + { + $newUnit = new Unit(); + + $newUnit->setID($row->id); + $newUnit->setName($row->name); + $newUnit->setAbbreviation($row->abbreviation); + $newUnit->setDefault($row->isDefault == 1); + $newUnit->setDummy($row->isDummy == 1); + $newUnit->setPiece($row->isPiece == 1); + + $returnList[] = $newUnit; + } + + return $returnList; + } + } + + class StoreType + { + private $id; + private $name; + + private static $storeTypesCache = null; + + public function setID($id) + { + $this->id = $id; + } + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + } + public function getName() + { + return $this->name; + } + + public static function getById($storeTypeId) + { + $allStoreTypes = StoreType::getAllStoreTypes(); + + foreach($allStoreTypes as $storeType) + { + if($storeType->getID() === $storeTypeId) + { + return $storeType; + } + } + + return null; + } + + public function create() + { + $SQL_command = "INSERT INTO storeTypes (name) VALUES (\"".$this->getName()."\")"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function change() + { + $SQL_command = "UPDATE storeTypes SET name=\"".$this->getName()."\" WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function delete() + { + $SQL_command = "DELETE FROM storeTypes WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function getUsage() + { + $SQL_command = "SELECT COUNT(name) as entryAmount FROM products WHERE storeTypeId=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($row = mysqli_fetch_object($mysqli_result)) + return $row->entryAmount; + + return 0; + } + + public static function getAllStoreTypes() + { + if(StoreType::$storeTypesCache == null) + { + $SQL_command = "SELECT * FROM storeTypes ORDER BY storeTypes.name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + StoreType::$storeTypesCache = array(); + + while ($row = mysqli_fetch_object($mysqli_result)) + { + $newStoreType = new StoreType(); + + $newStoreType->setID($row->id); + $newStoreType->setName($row->name); + + StoreType::$storeTypesCache[] = $newStoreType; + } + } + + return StoreType::$storeTypesCache; + } + } + + class Product + { + private $id; + private $name; + private $synonyms; + private $storeType; + + private static $productsCache = null; + + public function setID($id) + { + $this->id = $id; + } + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + } + public function getName() + { + return $this->name; + } + + public function setSynonyms($synonyms) + { + $this->synonyms = $synonyms; + } + public function getSynonyms() + { + return $this->synonyms; + } + + public function setStoreType(StoreType $storeType) + { + $this->storeType = $storeType; + } + public function getStoreType() + { + return $this->storeType; + } + + public static function getById($productId) + { + $allProducts = Product::getAllProducts(); + + foreach($allProducts as $product) + { + if($product->getID() === $productId) + { + return $product; + } + } + + return null; + } + + public function create() + { + $syns = "NULL"; + if($this->getSynonyms() != null || strlen($this->getSynonyms()) > 0) + $syns = "\"".$this->getSynonyms()."\""; + + $SQL_command = "INSERT INTO products (name, synonyms, storeTypeId) VALUES (\"".$this->getName()."\", ".$syns.", ".$this->getStoreType()->getID().")"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function change() + { + $syns = "NULL"; + if($this->getSynonyms() != null || strlen($this->getSynonyms()) > 0) + $syns = "\"".$this->getSynonyms()."\""; + + $SQL_command = "UPDATE products SET name=\"".$this->getName()."\", synonyms=".$syns.", storeTypeId=".$this->getStoreType()->getID()." WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function delete() + { + $SQL_command = "DELETE FROM products WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function getUsage() + { + $SQL_command = "SELECT COUNT(listId) as entryAmount FROM listEntries WHERE productId=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($row = mysqli_fetch_object($mysqli_result)) + return $row->entryAmount; + + return 0; + } + + public static function getAllProducts() + { + if(Product::$productsCache == null) + { +/* $productsAmount = 500; + $SQL_command = "SELECT COUNT(name) as productsAmount FROM products"; + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + if($row = mysqli_fetch_object($mysqli_result)) + $amount = $row->productsAmount; +*/ + $SQL_command = "SELECT * FROM products ORDER BY products.name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + +// Product::$productsCache = new SplFixedArray($productsAmount); + Product::$productsCache = array(); + +// $index = 0; + while ($row = mysqli_fetch_object($mysqli_result)) + { + $newProduct = new Product(); + + $newProduct->setID($row->id); + $newProduct->setName($row->name); + $newProduct->setSynonyms($row->synonyms); + $newProduct->setStoreType(StoreType::getById($row->storeTypeId)); + + Product::$productsCache[] = $newProduct; +// Product::$productsCache[$index] = $newProduct; +// $index++; + } + } + + return Product::$productsCache; + } + } + + class ShoppingListEntry + { + private $parentList; + private $product; + private $amount; + private $unit; + + public function setParentList($parentList) + { + $this->parentList = $parentList; + } + public function getParentList() + { + return $this->parentList; + } + + public function setProduct(Product $product) + { + $this->product = $product; + } + public function getProduct() + { + return $this->product; + } + + public function setAmount($amount) + { + $this->amount = $amount; + } + public function getAmount() + { + return $this->amount; + } + + public function setUnit(Unit $unit) + { + $this->unit = $unit; + } + public function getUnit() + { + return $this->unit; + } + + public function create() + { + $syns = "NULL"; + if($this->getSynonyms() != null || strlen($this->getSynonyms()) > 0) + $syns = "\"".$this->getSynonyms()."\""; + + $SQL_command = "INSERT INTO products (name, synonyms, storeTypeId) VALUES (\"".$this->getName()."\", ".$syns.", ".$this->getStoreType()->getID().")"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function change() + { + $syns = "NULL"; + if($this->getSynonyms() != null || strlen($this->getSynonyms()) > 0) + $syns = "\"".$this->getSynonyms()."\""; + + $SQL_command = "UPDATE products SET name=\"".$this->getName()."\", synonyms=".$syns.", storeTypeId=".$this->getStoreType()->getID()." WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function delete() + { + $SQL_command = "DELETE FROM products WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public function getUsage() + { + $SQL_command = "SELECT COUNT(listId) as entryAmount FROM listEntries WHERE productId=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($row = mysqli_fetch_object($mysqli_result)) + return $row->entryAmount; + + return 0; + } + + public static function getAllProducts() + { + if(Product::$productsCache == null) + { +/* $productsAmount = 500; + $SQL_command = "SELECT COUNT(name) as productsAmount FROM products"; + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + if($row = mysqli_fetch_object($mysqli_result)) + $amount = $row->productsAmount; +*/ + $SQL_command = "SELECT * FROM products ORDER BY products.name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + +// Product::$productsCache = new SplFixedArray($productsAmount); + Product::$productsCache = array(); + +// $index = 0; + while ($row = mysqli_fetch_object($mysqli_result)) + { + $newProduct = new Product(); + + $newProduct->setID($row->id); + $newProduct->setName($row->name); + $newProduct->setSynonyms($row->synonyms); + $newProduct->setStoreType(StoreType::getById($row->storeTypeId)); + + Product::$productsCache[] = $newProduct; +// Product::$productsCache[$index] = $newProduct; +// $index++; + } + } + + return Product::$productsCache; + } + + public static 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; + } + } + + class ShoppingList + { + private $id; + private $creationTime; + private $comment; + private $entries = array(); + + private static $listsCache = null; + + public function setID($id) + { + $this->id = $id; + } + public function getID() + { + return $this->id; + } + + public function setCreationTime($cTime) + { + $this->creationTime = $cTime; + } + public function getCreationTime() + { + return $this->creationTime; + } + + public function setComment($comment) + { + $this->comment = $comment; + } + public function getComment() + { + return $this->comment; + } + + public function setEntries($entries) + { + $this->entries = $entries; + } + public function getEntries() + { + return $this->entries; + } + + public function create() + { + if($this->getCreationTime() == null || $this->getCreationTime() == 0 || strlen($this->getCreationTime()) == 0) + $this->setCreationTime(round(microtime(true) * 1000)); + + $comment = "NULL"; + if($this->getComment() != null || strlen($this->getComment()) > 0) + $comment = "\"".$this->getComment()."\""; + + $SQL_command = "INSERT INTO lists (creationTime, comment) VALUES (".$this->getCreationTime().", ".$comment.")"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function change() + { + $comment = "NULL"; + if($this->getComment() != null || strlen($this->getComment()) > 0) + $comment = "\"".$this->getComment()."\""; + + $SQL_command = "UPDATE lists SET creationTime=\"".$this->getCreationTime()."\", comment=".$comment." WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + { + return true; + } + + return false; + } + + public function delete() + { + $SQL_command = "DELETE FROM lists WHERE id=".$this->getID(); + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + if ($mysqli_result) + return true; + + return false; + } + + public static function getAllLists() + { + if(ShoppingList::$listsCache == null) + { + $SQL_command = "SELECT * FROM lists ORDER BY lists.creationTime DESC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + ShoppingList::$listsCache = array(); + + while ($row = mysqli_fetch_object($mysqli_result)) + { + $newList = new ShoppingList(); + + $newList->setID($row->id); + $newList->setCreationTime($row->creationTime); + $newList->setComment($row->comment); + + $SQL_command_inner = "SELECT * FROM listEntries WHERE listId=".$newList->getID(); + $mysqli_result_inner = DBLink::getDbLink()->query($SQL_command_inner); + while ($row_inner = mysqli_fetch_object($mysqli_result_inner)) + { + $newListEntry = new ShoppingListEntry(); + $newListEntry->setParentList($newList); + $newListEntry->setProduct(Product::getById($row_inner->productId)); + $newListEntry->setAmount($row_inner->amount); + $newListEntry->setUnit(Unit::getById($row_inner->unit)); + $newList->getEntries()[] = $newListEntry; + } + + ShoppingList::$listsCache[] = $newList; + } + } + + return ShoppingList::$listsCache; + } + + public static 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; + } + + public static 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; + } } ?> + + Shopping list @@ -43,6 +910,12 @@ font-family: Arial, Geneva, Helvetica, sans-serif; } + details + { + padding-top: 15px; + padding-left: 15px; + } + table { border-collapse:separate; @@ -59,16 +932,11 @@ @@ -94,36 +1011,194 @@ switch($command) { case "addToList": - $productId = $_POST['productToAdd']; - addToList($productId); + $productId = $_POST['productId']; + $amount = $_POST['amountToAdd']; + $unitId = $_POST['unitToAdd']; + addToList($productId, $amount, $unitId); break; + case "removeFromList": $productId = $_POST['productToRemove']; $listId = $_POST['listId']; - removeFromList($productId, $listId); + $amount = $_POST['productAmountToRemove']; + $unit = $_POST['productUnitToRemove']; + removeFromList($productId, $listId, $amount, $unit); break; + case "displayShoppingList": displayShoppingList(); break; + case "createNewShoppingList": - createNewShoppingList(); + $newList = new ShoppingList(); + if($newList->create()) + echo "New shopping list has been created."; + else + echo "New shopping list could not be created: ".mysqli_error(DBLink::getDbLink()); break; - case "createProduct": - $productNames = $_POST['productToCreateName']; - $storeTypeId = $_POST['storeType']; - $rc = createProduct($productNames, $storeTypeId); - if($rc > 0) - $justCreatedProductId = $rc; - break; - case "oldList": + + case "manageList": $listId = $_POST['oldListId']; $subCommand = $_POST['bSubmit']; - if($subCommand == "remove") + GLOBAL $iconDelete; + if($subCommand == $iconDelete) { removeShoppingList($listId); $listId = null; } break; + + case "createProduct": + $p = new Product(); + $p->setID($_POST['oldProductId']); + + if(strpos($_POST['productName'], ";") !== false) + { + $terms = explode(";", $_POST['productName']); + $p->setName = $terms[0]; + + $syns = ""; + for($i=1; $i 0) + $syns .= ";".$currentWord; + } + + $syns = ltrim($syns, ';'); + $p->setSynonyms($syns); + } + else + $p->setName($_POST['productName']); + + if(isset($_POST['productStoreTypeId']) && (int)$_POST['productStoreTypeId'] > 0) + { + $p->setStoreType(StoreType::getById($_POST['productStoreTypeId'])); + + if($p->create()) + echo "Product has been created."; + else + echo "Product could not be created: ".mysqli_error(DBLink::getDbLink()); + } + else + echo "Store type was not set."; + + break; + case "manageProduct": + $p = new Product(); + $p->setID($_POST['oldProductId']); + $p->setName($_POST['productName']); + $p->setSynonyms($_POST['productSynonyms']); + $p->setStoreType(StoreType::getById($_POST['productStoreTypeId'])); + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + $subCommand = $_POST['bSubmit']; + if($subCommand == $iconDelete) + { + if($p->delete()) + echo "Product has been deleted."; + else + echo "Product could not be deleted: ".mysqli_error(DBLink::getDbLink()); + } + else if($subCommand == $iconSave) + { + if($p->change()) + echo "Product has been changed."; + else + echo "Product could not be changed: ".mysqli_error(DBLink::getDbLink()); + } + else + echo "Invalid command."; + break; + + + case "createStoreType": + $st = new StoreType(); + $st->setName($_POST['storeTypeName']); + + if($st->create()) + echo "Store type has been created."; + else + echo "Store type could not be created: ".mysqli_error(DBLink::getDbLink()); + + break; + case "manageStoreType": + $st = new StoreType(); + $st->setID($_POST['oldStoreTypeId']); + $st->setName($_POST['storeTypeName']); + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + $subCommand = $_POST['bSubmit']; + if($subCommand == $iconDelete) + { + if($st->delete()) + echo "Store type has been deleted."; + else + echo "Store type could not be deleted: ".mysqli_error(DBLink::getDbLink()); + } + else if($subCommand == $iconSave) + { + if($st->change()) + echo "Store type has been changed."; + else + echo "Store type could not be changed: ".mysqli_error(DBLink::getDbLink()); + } + else + echo "Invalid command."; + break; + + + case "createUnit": + + $u = new Unit(); + $u->setName($_POST['unitName']); + $u->setAbbreviation($_POST['unitAbbreviation']); + $u->setDefault(isset($_POST['unitIsDefault'])); + $u->setDummy(isset($_POST['unitIsDummy'])); + $u->setPiece(isset($_POST['unitIsPiece'])); + + if($u->create()) + echo "Unit has been created."; + else + echo "Unit could not be created."; + + break; + case "manageUnit": + + $u = new Unit(); + $u->setID($_POST['oldUnitId']); + $u->setName($_POST['unitName']); + $u->setAbbreviation($_POST['unitAbbreviation']); + $u->setDefault(isset($_POST['unitIsDefault'])); + $u->setDummy(isset($_POST['unitIsDummy'])); + $u->setPiece(isset($_POST['unitIsPiece'])); + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + $subCommand = $_POST['bSubmit']; + if($subCommand == $iconDelete) + { + if($u->delete()) + echo "Unit has been deleted."; + else + echo "Unit could not be deleted."; + } + else if($subCommand == $iconSave) + { + if($u->change()) + echo "Unit has been changed."; + else + echo "Unit could not be changed."; + } + else + echo "Invalid command."; + break; case "trainRhasspy": echo "Triggering training in Rhasspy has not been implemented, yet."; break; @@ -139,34 +1214,66 @@ mysqli_close(DBLink::getDbLink()); - function addToList($productId) + function addToList($productId, $amount, $unitId) { - if($productId > 0) + if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0) { - $SQL_command; + if(!ShoppingList::oneListExists()) + createList(); - 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(!ShoppingListEntry::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.
"; + if (DBLink::getDbLink()->query($SQL_command)) + echo "Product added to list.
"; + else + echo "Product could not be added to list.
"; + } else - echo "Product could not be added to list.
"; + { + /* + 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.
"; + else + echo "Product could not be added to existing entry on list.
"; + } + 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.
"; + else + echo "Product could not be removed from list (too many pieces of this item have been removed).
"; + } + } + } } else - echo "Select a product."; + echo "Specifiy product, amount and unit."; } - function removeFromList($productId, $listId) + function removeFromList($productId, $listId, $amount, $unitId) { - if($productId > 0) + if($productId > 0 && $amount != null && $unitId > 0) { - if(oneListExists()) + if(ShoppingList::oneListExists()) { - $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId; + $SQL_command = "DELETE FROM listEntries WHERE listId=".$listId." AND productId=".$productId." AND amount=".$amount." AND unit=".$unitId; if (DBLink::getDbLink()->query($SQL_command)) echo "Product removed from list.
"; @@ -178,41 +1285,16 @@ 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 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) { - echo "
+ echo "

Shopping list

+ + - "; - +/* $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)) @@ -220,16 +1302,48 @@ $currentId = (int)$row->id; if(isset($justCreatedProductId) && $justCreatedProductId === $currentId) - echo ""; + echo ""; else echo ""; } +*/ + foreach(Product::getAllProducts() as $product) + { + $currentId = (int)$product->getID(); + + if(isset($justCreatedProductId) && $justCreatedProductId === $currentId) + echo ""; + else + echo ""; + } + + + echo " + + + +
"; - if(oneListExists()) + if(ShoppingList::oneListExists()) { $list_SQL_command; @@ -252,7 +1366,7 @@ 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)."
"; + echo "This list was 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 "".$row->comment."
"; @@ -260,79 +1374,102 @@ echo "
"; } - $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"; + $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"; $mysqli_result = DBLink::getDbLink()->query($SQL_command); $lastShop=""; + $found = false; while ($row = mysqli_fetch_array($mysqli_result)) { - if(isset($lastShop) && $lastShop != "" && $row[7] != $lastShop) + if(!$found) + $found = true; + + if(isset($lastShop) && $lastShop != "" && $row[9] != $lastShop) echo ""; - if(!isset($lastShop) || $row[7] != $lastShop) + if(!isset($lastShop) || $row[9] != $lastShop) { - $lastShop = $row[7]; - echo $lastShop."
================"; + $lastShop = $row[9]; + echo "

".$lastShop."

"; echo ""; } - echo " - + echo ""; + + if($row[14] == 1) + echo ""; + else + echo " + + + "; + + GLOBAL $iconDelete; + + echo " "; } - echo "
".$row[3]."
".$row[5]."".$row[2]."".$row[12]."".$row[5]." -
+ - + - + + +
"; + + if($found) + echo ""; + else + echo "No entries in this list, yet."; } else - echo "No list exists, yet."; + echo "No list exists, yet."; } function showDataMaintenance() { echo "
"; - echo "

Data maintenance

+ $detailsName = "detailsMaintenance"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + echo "
+ + Data maintenance + "; + + echo "

Data maintenance

"; + + + $detailsName = "detailsLists"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + echo "
+ + Manage shopping lists +

Create new list

-
+ - -
+ + "; -

Create new product

- -
- - - - - - -
- You can enter synonyms. Separate all terms with semikolons."; - - if(twoListsExist()) + if(ShoppingList::twoListsExist()) { $SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC"; @@ -340,42 +1477,293 @@ echo "

Show specific list

"; - echo "
- + echo " + - - + GLOBAL $iconDelete; + echo " + + + -
"; + "; + } + + echo "
"; + + $detailsName = "detailsRhasspy"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + echo "
+ + Manage Rhasspy + + +

Train Rhasspy

+ +
+ + +
+
"; + + $detailsName = "detailsManageProducts"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + GLOBAL $iconSave; + + echo " +
+ + Manage products + + +

Create new product

+ +
+ + + + + + +
+ You can enter synonyms. Separate all terms with semikolons. + "; + + $allProducts = Product::getAllProducts(); + + if(count($allProducts) > 0) + echo "

Manage existing products

+ + + + + + "; + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + foreach($allProducts as $p) + { + echo " + + + + + + getID()."\" /> + "; + + $usageAmount = $p->getUsage(); + $deletionWarning = "Are you sure you want to delete this product?"; + if($usageAmount > 0) + $deletionWarning = "Are you sure you want to delete this product? It is still in use by ".$usageAmount." list entries. If you delete the product it will be removed from all shopping lists as well."; + + echo " + + + + "; } - echo "

Train Rhasspy

+ if(count($allProducts) > 0) + echo "
Product nameSynonymsStore type
getName()."\" required />getSynonyms()."\" /> +
"; -
- - -
"; + echo " +
"; + + $detailsName = "detailsManageUnits"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + echo " +
+ + Manage units + + +

Create new unit

+ +
+ + + + + + +
Unit name
Unit abbreviation
Is default?
Is dummy?
Is piece?
+ + +
"; + + $allUnits = Unit::getAllUnits(); + + if(count($allUnits) > 0) + echo "

Manage existing units

+ + + + + + + + "; + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + foreach($allUnits as $u) + { + echo " + + + + + + + + getID()."\" /> + "; + + $usageAmount = $u->getUsage(); + $deletionWarning = "Are you sure you want to delete this unit?"; + if($usageAmount > 0) + $deletionWarning = "Are you sure you want to delete this unit? It is still in use by ".$usageAmount." list entries. If you delete the unit those entries will all be removed as well."; + + echo " + + + + "; + } + + if(count($allUnits) > 0) + echo "
Unit nameAbbreviationis default?is dummy?is piece?
getName()."\" required />getAbbreviation()."\" />isDefault()) echo " checked"; echo " />isDummy()) echo " checked"; echo " />isPiece()) echo " checked"; echo " />
"; + + echo " +
"; + + + $detailsName = "detailsManageStoreTypes"; + $openString = ""; + if(in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + GLOBAL $iconSave; + + echo " +
+ + Manage store types + + +

Create new store type

+ +
+ + +
Store type name
+ +
"; + + $allStoreTypes = StoreType::getAllStoreTypes(); + + if(count($allStoreTypes) > 0) + { + echo "

Manage existing store types

+ + + + "; + } + + GLOBAL $iconDelete; + GLOBAL $iconSave; + + foreach($allStoreTypes as $st) + { + echo " + + + + getID()."\" /> + "; + + $usageAmount = $st->getUsage(); + $deletionFunction = "if(!confirm('Are you sure you want to delete this store type?')){return false;}"; + if($usageAmount > 0) + $deletionFunction = "alert('You cannot currently delete this store type. This is still your place to buy ".$usageAmount." products.'); return false;"; + + echo " + + + + "; + } + + if(count($allStoreTypes) > 0) + echo "
Store type name
getName()."\" required />
"; + + echo " +
"; + + + + echo " +
"; } function displayProductList() { - $SQL_Befehl = "SELECT name, synonyms FROM products ORDER BY products.name ASC"; +/* $SQL_command = "SELECT name, synonyms FROM products ORDER BY products.name ASC"; - $mysqli_result = DBLink::getDbLink()->query($SQL_Befehl); + $mysqli_result = DBLink::getDbLink()->query($SQL_command); $lastShop=""; @@ -385,20 +1773,58 @@ echo $row[0]."\n"; else echo "(".str_replace(";", "|", $row[1])."|".$row[0]."):".$row[0]."\n"; + }*/ + + foreach(Product::getAllProducts() as $product) + { + if($product->getSynonyms() == null && strlen($product->getSynonyms()) == 0) + echo $product->getName()."\n"; + else +// echo "(".str_replace(";", "|", $product->getSynonyms())."):".$product->getName()."\n"; + echo "(".str_replace(";", "|", $product->getSynonyms())."|".$product->getName()."):".$product->getName()."\n"; } } - function createNewShoppingList() + function displayUnitList() { - $date = new DateTime(); + /*$SQL_command = "SELECT name FROM units ORDER BY name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); - $SQL_command = "INSERT INTO lists (creationTime) VALUES (".($date->getTimestamp() * 1000).")"; + while ($row = mysqli_fetch_object($mysqli_result)) + { + $unitArray = explode(";", $row->name); + if(count($unitArray) == 1) + echo $unitArray[0]."\n"; + else + { + $tbp = ""; + for($i=1; $iquery($SQL_command)) - echo "New list has been created.
"; - else - echo "Error creating new list.
"; + $tbp = trim($tbp, "|"); + + echo "(".$tbp."):".$unitArray[0]."\n"; + } + }*/ + + foreach(Unit::getAllUnits() as $unit) + { + $unitArray = explode(";", $unit->getName()); + if(count($unitArray) == 1) + echo $unitArray[0]."\n"; + else + { + $tbp = ""; + for($i=1; $i"; } - - function createProduct($productNames, $storeTypeId) - { - $date = new DateTime(); - - $SQL_command; - - if(strpos($productNames, ";") !== false) - { - $terms = explode(";", $productNames); - $mainName = $terms[0]; - - $syns = ""; - for($i=1; $i 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.
"; - return DBLink::getDbLink()->insert_id; - } - else - echo "Error creating new product.
"; - - return -1; - } - ?> \ No newline at end of file