diff --git a/phpInterface/shoppingList.php b/phpInterface/shoppingList.php index 4080900..64d3e7b 100644 --- a/phpInterface/shoppingList.php +++ b/phpInterface/shoppingList.php @@ -1,1896 +1,1903 @@ -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(); - $this->setID(DBLink::getDbLink()->insert_id); - 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) - { - $this->setID(DBLink::getDbLink()->insert_id); - 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) - { - $this->setID(DBLink::getDbLink()->insert_id); - 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) - { - $this->setID(DBLink::getDbLink()->insert_id); - 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) - { - $this->setID(DBLink::getDbLink()->insert_id); - 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 - - - - - - - - -create()) - echo "New shopping list has been created."; - else - echo "New shopping list could not be created: ".mysqli_error(DBLink::getDbLink()); - break; - - case "manageList": - $listId = $_POST['oldListId']; - $subCommand = $_POST['bSubmit']; - GLOBAL $iconDelete; - if($subCommand == $iconDelete) - { - removeShoppingList($listId); - $listId = null; - } - break; - - case "createProduct": - $p = new Product(); - - if(strpos($_POST['productName'], ";") !== false) - { - $terms = explode(";", $_POST['productName']); - $p->setName(trim($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."; - $justCreatedProductId = $p->getID(); - } - 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']); - if(strpos($_POST['productName'], ";") !== false) - { - $terms = explode(";", $_POST['productName']); - $p->setName(trim($terms[0])); - - $syns = ""; - for($i=1; $i 0) - $syns .= ";".$currentWord; - } - - $syns = ltrim($syns, ';'); - $p->setSynonyms($syns); - } - else - $p->setName($_POST['productName']); - $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": - if(triggerRhasspyTraining()) - echo "Rhasspy training has completed successfully."; - else - echo "Rhasspy training has failed."; - break; - } - } - - displayShoppingList($listId, $justCreatedProductId); - - showDataMaintenance(); - - echo ""; - - mysqli_close(DBLink::getDbLink()); - - function addToList($productId, $amount, $unitId) - { - if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0) - { - if(!ShoppingList::oneListExists()) - createList(); - - 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.
"; - else - echo "Product could not be added to list.
"; - } - 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.
"; - 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 "Specifiy product, amount and unit."; - } - - function removeFromList($productId, $listId, $amount, $unitId) - { - if($productId > 0 && $amount != null && $unitId > 0) - { - if(ShoppingList::oneListExists()) - { - $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.
"; - else - echo "Product could not be removed from list.
"; - } - } - else - echo "Select a product."; - } - - function displayShoppingList($listId, $justCreatedProductId) - { - echo "

Shopping list

- -
- - - - - - - -
"; - - if(ShoppingList::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 "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."
"; - - echo "
"; - } - - $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(!$found) - $found = true; - - if(isset($lastShop) && $lastShop != "" && $row[9] != $lastShop) - echo ""; - - if(!isset($lastShop) || $row[9] != $lastShop) - { - $lastShop = $row[9]; - echo "

".$lastShop."

"; - echo ""; - } - - echo ""; - - if($row[14] == 1) - echo ""; - else - echo " - - - "; - - GLOBAL $iconDelete; - - echo " - - "; - } - - if($found) - echo "
".$row[5]."".$row[2]."".$row[12]."".$row[5]." -
- - - - - - -
-
"; - else - echo "No entries in this list, yet."; - } - else - echo "No list exists, yet."; - } - - function showDataMaintenance() - { - echo "
"; - - $detailsName = "detailsMaintenance"; - $openString = ""; - if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements']))) - $openString = " open=\"\""; - - echo "
- - Data maintenance - "; - - echo "

Data maintenance

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

Create new list

- -
- - -
"; - - if(ShoppingList::twoListsExist()) - { - $SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC"; - - $mysqli_result = DBLink::getDbLink()->query($SQL_command); - - echo "

Show specific list

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

Train Rhasspy

- - WARNING: This can take up to 2 minutes.

- -
- - -
-
"; - - $detailsName = "detailsManageProducts"; - $openString = ""; - if(isset($_POST['openDetailsElements']) && 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 " - - - - "; - } - - if(count($allProducts) > 0) - echo "
Product nameSynonymsStore type
getName()."\" required />getSynonyms()."\" /> -
"; - - echo " -
"; - - $detailsName = "detailsManageUnits"; - $openString = ""; - if(isset($_POST['openDetailsElements']) && 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(isset($_POST['openDetailsElements']) && 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_command = "SELECT name, synonyms FROM products ORDER BY products.name ASC"; - - $mysqli_result = DBLink::getDbLink()->query($SQL_command); - - $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"; - }*/ - - 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 displayUnitList() - { - /*$SQL_command = "SELECT name FROM units ORDER BY name ASC"; - - $mysqli_result = DBLink::getDbLink()->query($SQL_command); - - while ($row = mysqli_fetch_object($mysqli_result)) - { - $unitArray = explode(";", $row->name); - if(count($unitArray) == 1) - echo $unitArray[0]."\n"; - else - { - $tbp = ""; - for($i=1; $igetName()); - if(count($unitArray) == 1) - echo $unitArray[0]."\n"; - else - { - $tbp = ""; - for($i=1; $iquery($SQL_command)) - echo "List has been deleted.
"; - else - echo "Error deleting list.
"; - } - - - - 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; - } - +options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true); + DBLink::$sqlLink->ssl_set(NULL, NULL, Configuration::$mysqlSslCertificatePath, NULL, NULL); + } + DBLink::$sqlLink->real_connect(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; + } + } + + if(isset($_GET['command'])) + { + if($_GET['command'] == "printProductList" || $_GET['command'] == "printRhasspyProductList") + { + displayProductList(); + exit(0); + } + else if($_GET['command'] == "printUnitList") + { + displayUnitList(); + exit(0); + } + } + + class Unit + { + private $id; + private $name; + private $abbreviation; + private $isDefault; + private $isDummy; + private $isPiece; + + 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 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(); + $this->setID(DBLink::getDbLink()->insert_id); + 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) + { + $this->setID(DBLink::getDbLink()->insert_id); + 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) + { + $this->setID(DBLink::getDbLink()->insert_id); + 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) + { + $this->setID(DBLink::getDbLink()->insert_id); + 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) + { + $this->setID(DBLink::getDbLink()->insert_id); + 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 + + + + + + + + +create()) + echo "New shopping list has been created."; + else + echo "New shopping list could not be created: ".mysqli_error(DBLink::getDbLink()); + break; + + case "manageList": + $listId = $_POST['oldListId']; + $subCommand = $_POST['bSubmit']; + GLOBAL $iconDelete; + if($subCommand == $iconDelete) + { + removeShoppingList($listId); + $listId = null; + } + break; + + case "createProduct": + $p = new Product(); + + if(strpos($_POST['productName'], ";") !== false) + { + $terms = explode(";", $_POST['productName']); + $p->setName(trim($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."; + $justCreatedProductId = $p->getID(); + } + 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']); + if(strpos($_POST['productName'], ";") !== false) + { + $terms = explode(";", $_POST['productName']); + $p->setName(trim($terms[0])); + + $syns = ""; + for($i=1; $i 0) + $syns .= ";".$currentWord; + } + + $syns = ltrim($syns, ';'); + $p->setSynonyms($syns); + } + else + $p->setName($_POST['productName']); + $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": + if(triggerRhasspyTraining()) + echo "Rhasspy training has completed successfully."; + else + echo "Rhasspy training has failed."; + break; + } + } + + displayShoppingList($listId, $justCreatedProductId); + + showDataMaintenance(); + + echo ""; + + mysqli_close(DBLink::getDbLink()); + + function addToList($productId, $amount, $unitId) + { + if($productId > 0 && $amount != 0 && isset($unitId) && strlen($unitId) > 0) + { + if(!ShoppingList::oneListExists()) + createList(); + + 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.
"; + else + echo "Product could not be added to list.
"; + } + 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.
"; + 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 "Specifiy product, amount and unit."; + } + + function removeFromList($productId, $listId, $amount, $unitId) + { + if($productId > 0 && $amount != null && $unitId > 0) + { + if(ShoppingList::oneListExists()) + { + $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.
"; + else + echo "Product could not be removed from list.
"; + } + } + else + echo "Select a product."; + } + + function displayShoppingList($listId, $justCreatedProductId) + { + echo "

Shopping list

+ +
+ + + + + + + +
"; + + if(ShoppingList::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 "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."
"; + + echo "
"; + } + + $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(!$found) + $found = true; + + if(isset($lastShop) && $lastShop != "" && $row[9] != $lastShop) + echo ""; + + if(!isset($lastShop) || $row[9] != $lastShop) + { + $lastShop = $row[9]; + echo "

".$lastShop."

"; + echo ""; + } + + echo ""; + + if($row[14] == 1) + echo ""; + else + echo " + + + "; + + GLOBAL $iconDelete; + + echo " + + "; + } + + if($found) + echo "
".$row[5]."".$row[2]."".$row[12]."".$row[5]." +
+ + + + + + +
+
"; + else + echo "No entries in this list, yet."; + } + else + echo "No list exists, yet."; + } + + function showDataMaintenance() + { + echo "
"; + + $detailsName = "detailsMaintenance"; + $openString = ""; + if(isset($_POST['openDetailsElements']) && in_array($detailsName, explode(";", $_POST['openDetailsElements']))) + $openString = " open=\"\""; + + echo "
+ + Data maintenance + "; + + echo "

Data maintenance

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

Create new list

+ +
+ + +
"; + + if(ShoppingList::twoListsExist()) + { + $SQL_command = "SELECT * FROM lists ORDER BY creationTime DESC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + echo "

Show specific list

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

Train Rhasspy

+ + WARNING: This can take up to 2 minutes.

+ +
+ + +
+
"; + + $detailsName = "detailsManageProducts"; + $openString = ""; + if(isset($_POST['openDetailsElements']) && 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 " + + + + "; + } + + if(count($allProducts) > 0) + echo "
Product nameSynonymsStore type
getName()."\" required />getSynonyms()."\" /> +
"; + + echo " +
"; + + $detailsName = "detailsManageUnits"; + $openString = ""; + if(isset($_POST['openDetailsElements']) && 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(isset($_POST['openDetailsElements']) && 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_command = "SELECT name, synonyms FROM products ORDER BY products.name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + $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"; + }*/ + + 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 displayUnitList() + { + /*$SQL_command = "SELECT name FROM units ORDER BY name ASC"; + + $mysqli_result = DBLink::getDbLink()->query($SQL_command); + + while ($row = mysqli_fetch_object($mysqli_result)) + { + $unitArray = explode(";", $row->name); + if(count($unitArray) == 1) + echo $unitArray[0]."\n"; + else + { + $tbp = ""; + for($i=1; $igetName()); + if(count($unitArray) == 1) + echo $unitArray[0]."\n"; + else + { + $tbp = ""; + for($i=1; $iquery($SQL_command)) + echo "List has been deleted.
"; + else + echo "Error deleting list.
"; + } + + + + 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; + } + ?> \ No newline at end of file