iwa-panda1

Manage Weather Data by International Weather Agency (Version 1)
Log | Files | Refs

commit 642585d98dfcd7a1528426a88e0dfc4b61a8be95
parent 63fc2d94ff72438677072dc100c0d723afda368c
Author: MoiBaguette <[email protected]>
Date:   Mon, 17 Apr 2023 01:05:33 +0200

customers and contracts

Diffstat:
MLollipop/DatabaseObject.php | 42+++++++++++++++++++++++++++++++++++++-----
DaddContract.php | 124-------------------------------------------------------------------------------
Aadd_contract.php | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aadd_customer.php | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Madd_user.php | 82++++++++++++++++++++++++++++++++++++++++----------------------------------------
Aalter_contract.php | 166+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aalter_customer.php | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Malter_user.php | 24++++++++++++------------
Aclasses/Contract.php | 15+++++++++++++++
Aclasses/Customer.php | 15+++++++++++++++
Aclasses/Subscription.php | 15+++++++++++++++
Mindex.php | 2+-
Mnavbar.php | 16++++++++--------
Asearch_contract.php | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asearch_customer.php | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asearch_data.php | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsearchdata.php | 99-------------------------------------------------------------------------------
17 files changed, 923 insertions(+), 290 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -26,13 +26,41 @@ namespace Lollipop { { $this->data = $data; } - public function where(string $key, string $value){ + public function where(string $key, string $value) : bool + { $sql = "SELECT * FROM {$this->table} WHERE $key = ?"; $value = array($value); $stmt = $this->db->conn->prepare($sql); $stmt->execute($value); $result = $stmt->get_result(); + if ($result->num_rows == 0) { + return false; + } $this->data = $result->fetch_assoc(); + return true; + } + public function where_array(array $values) : bool + { + $sql = "SELECT * FROM {$this->table} WHERE "; + $params = []; + $i = 0; + foreach($values as $key => $param){ + if($i > 0) + $sql .= " and "; + $sql .= "{$key} = ?"; + $params[] = $param; + } + + $stmt = $this->db->conn->prepare($sql); + $stmt->execute($params); + $result = $stmt->get_result(); + + if ($result->num_rows == 0) { + return false; + } + + $this->data = $result->fetch_assoc(); + return true; } public function load(string $id): bool { @@ -54,10 +82,10 @@ namespace Lollipop { return true; } - public function save() + public function save() : bool { if (!$this->changed_keys) - return; + return false; $sql = "UPDATE {$this->table} SET "; @@ -73,9 +101,13 @@ namespace Lollipop { $values[] = $this->data[$this->primary]; $stmt = $this->db->conn->prepare($sql); - $stmt->execute($values); - + $this->changed_keys = []; + + if($stmt->execute($values)) + return true; + else + return false; } public function add() : bool diff --git a/addContract.php b/addContract.php @@ -1,124 +0,0 @@ -<!DOCTYPE html> -<html lang="nl"> -<head> - <title>Add contract</title> - <!-- Bootstrap CSS --> - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> -</head> -<?php -include "navbar.php"; -include "Connect.php"; -if(!in_array(1, $_SESSION['permissions'])){ - header('Location: dashboard.php'); - exit; -} -?> -<body> -<div class="container"> - <h1>Add contract</h1> - - <form action="addContract.php" method="post"> - <div class="mb-3"> - <label for="subscription">Choose subscription:</label> - <select name="subscription" id="subscription"> - <option value="realtime">Realtime</option> - <option value="weekly">Weekly</option> - <option value="fortnightly">Fortnightly</option> - </select> - </div> - <div class="mb-3"> - <label for="customer" class="form-label"><b>Customer ID:</b></label> - <input type="text" class="form-control" name="customer" id="customer" placeholder="Customer ID"> - </div> - <div class="mb-3"> - <label for="start-date" class="form-label"><b>Start Date:</b></label> - <input class="form-control me-2" type="text" name="start-date" placeholder="Start date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> - </div> - <div class="mb-3"> - <label for="start-date" class="form-label"><b>End Date:</b></label> - <input class="form-control me-2" type="text" name="end-date" placeholder="End date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> - </div> - <label for="token" class="form-label"><b>*token*:</b></label> - <div class="form-group"> - <label for="tariff">Tariff:</label> - <a>€</a><input type="number" class="form-control" name="tariff" id="tariff" placeholder="Tariff" style="display: inline-block; width: auto;"> - </div> - <div class="mb-3"> - <label for="addition" class="form-label"><b>Additional information:</b></label> - <input type="text" class="form-control" name="addition" id="addition" placeholder="Additional information" style="height: 200px;"> - </div> - <button type="submit" class="btn btn-primary" name="submit">Voeg toe</button> - </form> -</div> -<?php -$connect = new Connect; -$conn = $connect->getConn(); - -if ($_SERVER["REQUEST_METHOD"] == "POST") { - $errors = array(); // initialize an empty array to store errors - - // Check if voornaam is set and not empty - if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { - $fname = $_POST['voornaam']; - } else { - $errors[] = "Voornaam is required"; - } - - // Check if achternaam is set and not empty - if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { - $lname = $_POST['achternaam']; - } else { - $errors[] = "Achternaam is required"; - } - - // Check if email is set and not empty - if (isset($_POST['email']) && !empty($_POST['email'])) { - $email = $_POST['email']; - } else { - $errors[] = "E-mail is required"; - } - - // Check if password is set and not empty - if (isset($_POST['password']) && !empty($_POST['password'])) { - $password = $_POST['password']; - } else { - $errors[] = "Wachtwoord is required"; - } - - // Check if permissions is set - if (isset($_POST['permissions'])) { - $permissions = $_POST['permissions']; - } else { - $errors[] = "Permissies zijn vereist"; - } - - // Check if there are any errors - if (count($errors) > 0) { - // Print out the errors - foreach ($errors as $error) { - echo $error . "<br>"; - } - } else { - // Pass the password through a hashing function - $hash = password_hash($password, PASSWORD_DEFAULT); - - // Making a sql statement to add user to the database, preparing it and excuting - $sql = "INSERT INTO medewerkers (email, voornaam, achternaam, wachtwoord) VALUES(?, ?, ?, ?)"; - $stmt= $conn->prepare($sql); - $stmt->bind_param("ssss", $email, $fname, $lname, $hash); - $stmt->execute(); - - //Excecuting a sql statement for all the user permissions - foreach($permissions as $perm){ - $sql = "INSERT INTO medewerkers_permissie (email, permissie_id) VALUES (?, ?);"; - $stmt= $conn->prepare($sql); - $stmt->bind_param("si", $email, $perm); - $stmt->execute(); - } - } -} -// closing the connection -mysqli_close($conn); -?> -</body> -</html> diff --git a/add_contract.php b/add_contract.php @@ -0,0 +1,138 @@ +<!DOCTYPE html> +<html lang="nl"> +<head> + <title>Add contract</title> + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> +</head> +<?php +include "navbar.php"; +include "utils/autoloader.php"; +if(!in_array(1, $_SESSION['permissions'])){ + header('Location: dashboard.php'); + exit; +} +$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); +$customer_id = null; +if($_SERVER['REQUEST_METHOD'] == "GET"){ + if(isset($_GET['email'])){ + $email = $_GET['email']; + $c = $db->get(Customer::class); + $c->where('email', $email); + $customer_id = $c->customer_id; + } +} +$available_subsciptions = []; +$all_p = $db->all(Subscription::class); +foreach($all_p as $tmp){ + $available_permissions[$tmp->sub_id] = $tmp->sub_name; +} + +$msg = ""; +if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if subscription type is set and not empty + if (isset($_POST['subscription']) && !empty($_POST['subscription'])) { + $sub_type = $_POST['subscription']; + } else { + $errors[] = "subscription type is required"; + } + + // Check if customer_id is set and not empty + if (isset($_POST['customer_id']) && !empty($_POST['customer_id'])) { + $customer_id = $_POST['customer_id']; + } else { + $errors[] = "customer_id is required"; + } + + // Check if start-date is set and not empty + if (isset($_POST['start-date']) && !empty($_POST['start-date'])) { + $start_date = $_POST['start-date']; + } else { + $errors[] = "start date is required"; + } + + // Check if end-date is set and not empty + if (isset($_POST['end-date']) && !empty($_POST['end-date'])) { + $end_date = $_POST['end-date']; + } else { + $errors[] = "end date is required"; + } + + // Check if permissions is set + if (isset($_POST['tariff']) && !empty($_POST['tariff'])) { + $tariff = $_POST['tariff']; + } else { + $errors[] = "tarif is required"; + } + // + if (isset($_POST['addition']) && !empty($_POST['addition'])) { + $addition = $_POST['addition']; + } else { + $errors[] = "additional information is required"; + } + + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + $msg.= $error . "<br>"; + } + } else { + $c = $db->get(Contract::class); + $c->sub_id = (int) $sub_type; + $c->customer_id = (int) $customer_id; + $c->start_date = $start_date; + $c->end_date = $end_date; + $token = bin2hex(random_bytes(32)); + $c->token = $token; + $c->tariff = (double) $tariff; + $c->standards = $addition; + + if($c->add()) + $msg = "succes!!!"; + } + } +?> +<body> +<div class="container"> + <h1>Add contract</h1> + <form action="add_contract.php" method="post"> + <div class="mb-3"> + <label for="subscription">Choose subscription:</label> + <select name="subscription" id="subscription"> + <?php + foreach($available_permissions as $key => $value){ + echo "<option value=\"{$key}\">{$value}</option>"; + } + ?> + </select> + </div> + <div class="mb-3"> + <label for="customer" class="form-label"><b>Customer ID:</b></label> + <input type="text" class="form-control" name="customer_id" id="customer_id" placeholder="Customer ID" value="<?php echo $customer_id?>"> + </div> + <div class="mb-3"> + <label for="start-date" class="form-label"><b>Start Date:</b></label> + <input class="form-control me-2" type="text" name="start-date" placeholder="Start date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> + </div> + <div class="mb-3"> + <label for="end-date" class="form-label"><b>End Date:</b></label> + <input class="form-control me-2" type="text" name="end-date" placeholder="End date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> + </div> + <label for="token" class="form-label"><b>*token*:</b></label> + <div class="form-group"> + <label for="tariff">Tariff:</label> + <a>€</a><input type="number" class="form-control" name="tariff" id="tariff" placeholder="Tariff" style="display: inline-block; width: auto;"> + </div> + <div class="mb-3"> + <label for="addition" class="form-label"><b>Additional information:</b></label> + <input type="text" class="form-control" name="addition" id="addition" placeholder="Additional information" style="height: 200px;"> + </div> + <button type="submit" class="btn btn-primary" name="submit">Voeg toe</button> + </form> + <?php echo $msg;?> +</div> +</body> +</html> diff --git a/add_customer.php b/add_customer.php @@ -0,0 +1,87 @@ +<!DOCTYPE html> +<html lang="eng"> + <head> + <title>Add user</title> + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> + </head> + <?php + include "navbar.php"; + include "utils/autoloader.php"; + if(!in_array(1, $_SESSION['user_permissions'])){ + header('Location: /dashboard'); + exit; + } + $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); + $msg = ""; + if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if voornaam is set and not empty + if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { + $fname = $_POST['voornaam']; + } else { + $errors[] = "Voornaam is required"; + } + + // Check if achternaam is set and not empty + if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { + $lname = $_POST['achternaam']; + } else { + $errors[] = "Achternaam is required"; + } + // Check if email is set and not empty + if (isset($_POST['email']) && !empty($_POST['email'])) { + $email = $_POST['email']; + } else { + $errors[] = "E-mail is required"; + } + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + $msg .= $error . "<br>"; + } + } else { + //create a database object with table customer + $c = $db->get(Customer::class); + //check if customer already exists + if($c->where("email", $email)){ + $msg = "this user already exists: " . $c->email . " " . $c->first_name . " " . $c->last_name; + }else{ + $c = $db->get(Customer::class); + //set new user data + $c->first_name = $fname; + $c->last_name = $lname; + $c->email = $email; + //add user with the add function + if($c->add()){ + $msg = "added to the db this info:<br> email: {$c->email}<br> firstname: {$c->first_name}<br> lastname: {$c->last_name}"; + }; + } + } + } + ?> + <body> + <div class="container"> + <h1>Add customer</h1> + + <form action="add_customer.php" method="post"> + <div class="mb-3"> + <label for="voornaam" class="form-label"><b>Voornaam:</b></label> + <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> + </div> + <div class="mb-3"> + <label for="achternaam" class="form-label"><b>Achternaam:</b></label> + <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> + </div> + <div class="mb-3"> + <label for="email" class="form-label"><b>Email:</b></label> + <input type="text" class="form-control" name="email" id="email" placeholder="Email"> + </div> + <button type="submit" class="btn btn-primary" name="submit">Add customer</button> + </form> + <?php echo $msg;?> + </div> + </body> +</html> diff --git a/add_user.php b/add_user.php @@ -19,41 +19,7 @@ foreach($all_p as $tmp){ $available_permissions[] = ['id' => $tmp->permission_id, 'name' => $tmp->permission_name]; } - ?> - <body> - <div class="container"> - <h1>Add user</h1> - - <form action="add_user.php" method="post"> - <div class="mb-3"> - <label for="voornaam" class="form-label"><b>Voornaam:</b></label> - <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> - </div> - <div class="mb-3"> - <label for="achternaam" class="form-label"><b>Achternaam:</b></label> - <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> - </div> - <div class="mb-3"> - <label for="email" class="form-label"><b>Email:</b></label> - <input type="text" class="form-control" name="email" id="email" placeholder="Email"> - </div> - <div class="mb-3"> - <label for="password" class="form-label"><b>Wachtwoord:</b></label> - <input type="password" class="form-control" name="password" id="password" placeholder="******"> - </div> - <p>Please select the user permissions:</p> - <?php - foreach($available_permissions as $db_permission){ - echo "<div class=\"mb-3 form-check\"> - <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\"> - <label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label> - </div>"; - } - ?> - <button type="submit" class="btn btn-primary" name="submit">Add user</button> - </form> - </div> - <?php + $msg = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $errors = array(); // initialize an empty array to store errors @@ -89,14 +55,14 @@ if (isset($_POST['permissions'])) { $permissions = $_POST['permissions']; } else { - $errors[] = "Permissies zijn vereist"; + $errors[] = "Permissions are required"; } // Check if there are any errors if (count($errors) > 0) { // Print out the errors foreach ($errors as $error) { - echo $error . "<br>"; + $msg .= $error . "<br>"; } } else { // Pass the password through a hashing function @@ -104,10 +70,9 @@ //create a database object with table user $u = $db->get(User::class); - $u->where("email", $email); //check if email already exists - if($u->email != null){ - echo"this email address is taken: " . $email; + if($u->where("email", $email)){ + $msg = "this email address is taken: " . $email; }else{ $u = $db->get(User::class); $succes = false; @@ -121,6 +86,7 @@ if($u->add()){ $succes = true; }; + $u = $db->get(User::class); $u->where("email", $email); //create a database object with table permission for each permission //set the data and execute the add function @@ -134,11 +100,45 @@ } } if($succes){ - echo"succes!"; + $msg = "succes! user with email: {$email} was added to the db"; } } } } ?> + <body> + <div class="container"> + <h1>Add user</h1> + + <form action="add_user.php" method="post"> + <div class="mb-3"> + <label for="voornaam" class="form-label"><b>Voornaam:</b></label> + <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> + </div> + <div class="mb-3"> + <label for="achternaam" class="form-label"><b>Achternaam:</b></label> + <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> + </div> + <div class="mb-3"> + <label for="email" class="form-label"><b>Email:</b></label> + <input type="text" class="form-control" name="email" id="email" placeholder="Email"> + </div> + <div class="mb-3"> + <label for="password" class="form-label"><b>Wachtwoord:</b></label> + <input type="password" class="form-control" name="password" id="password" placeholder="******"> + </div> + <p>Please select the user permissions:</p> + <?php + foreach($available_permissions as $db_permission){ + echo "<div class=\"mb-3 form-check\"> + <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\"> + <label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label> + </div>"; + } + ?> + <button type="submit" class="btn btn-primary" name="submit">Add user</button> + </form> + <?php echo $msg;?> + </div> </body> </html> diff --git a/alter_contract.php b/alter_contract.php @@ -0,0 +1,166 @@ +<!DOCTYPE html> +<html lang="nl"> +<head> + <title>Add contract</title> + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> +</head> +<?php +include "navbar.php"; +include "utils/autoloader.php"; +if(!in_array(1, $_SESSION['permissions'])){ + header('Location: dashboard.php'); + exit; +} +$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); +$customer_id = null; +if($_SERVER['REQUEST_METHOD'] == "GET"){ + if(isset($_GET['contract_id'])){ + $c = $db->get(Contract::class); + $c->where('contract_id', $_GET['contract_id']); + $customer_id = $c->customer_id; + $contract_id = $c->contract_id; + $sub_type = $c->sub_id; + $start_date = $c->start_date; + $end_date = $c->end_date; + $tariff = $c->tariff; + $addition = $c->standards; + }else{ + $customer_id = ""; + $selected = ""; + $start_date = ""; + $end_date = ""; + $tariff = ""; + $addition = ""; + } +} +$msg = ""; +$available_subsciptions = []; +$all_p = $db->all(Subscription::class); +foreach($all_p as $tmp){ + $available_permissions[$tmp->sub_id] = $tmp->sub_name; +} +if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if subscription type is set and not empty + if (isset($_POST['subscription']) && !empty($_POST['subscription'])) { + $sub_type = $_POST['subscription']; + } else { + $errors[] = "subscription type is required"; + } + + // Check if customer_id is set and not empty + if (isset($_POST['customer_id']) && !empty($_POST['customer_id'])) { + $customer_id = $_POST['customer_id']; + } else { + $errors[] = "customer_id is required"; + } + // Check if contract_id is set and not empty + if (isset($_POST['contract_id']) && !empty($_POST['contract_id'])) { + $contract_id = $_POST['contract_id']; + } else { + $errors[] = "contract_id is required"; + } + + // Check if start-date is set and not empty + if (isset($_POST['start-date']) && !empty($_POST['start-date'])) { + $start_date = $_POST['start-date']; + } else { + $errors[] = "start date is required"; + } + + // Check if end-date is set and not empty + if (isset($_POST['end-date']) && !empty($_POST['end-date'])) { + $end_date = $_POST['end-date']; + } else { + $errors[] = "end date is required"; + } + + // Check if permissions is set + if (isset($_POST['tariff']) && !empty($_POST['tariff'])) { + $tariff = $_POST['tariff']; + } else { + $errors[] = "tarif is required"; + } + // + if (isset($_POST['addition']) && !empty($_POST['addition'])) { + $addition = $_POST['addition']; + } else { + $errors[] = "additional information is required"; + } + + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + echo $error . "<br>"; + } + } else { + $c = $db->get(Contract::class); + if(!$c->where('contract_id', (int) $contract_id)){ + $msg = "this contract does not exist"; + }else{ + $c->sub_id = (int) $sub_type; + $c->customer_id = (int) $customer_id; + $c->start_date = $start_date; + $c->end_date = $end_date; + $token = bin2hex(random_bytes(32)); + $c->token = $token; + $c->tariff = (double) $tariff; + $c->standards = $addition; + + if($c->save()){ + $msg = "update to the db this info:<br> contract id: {$c->customer_id}<br> start date: {$c->start_date}<br> end date: {$c->end_date} addition: {$c->standards}"; + } + } + } +} +?> +<body> +<div class="container"> + <h1>Add contract</h1> + <form action="alter_contract.php" method="post"> + <div class="mb-3"> + <label for="subscription">Choose subscription:</label> + <select name="subscription" id="subscription"> + <?php + $tmp = ""; + foreach($available_permissions as $key => $value){ + if($sub_type == $value) + $tmp = "selected"; + else + $tmp = "selected"; + echo "<option value=\"{$key} {$tmp}\">{$value}</option>"; + } + ?> + </select> + </div> + <div class="mb-3"> + <label for="customer" class="form-label"><b>Customer ID:</b></label> + <input type="text" class="form-control" name="customer_id" id="customer_id" placeholder="Customer ID" value="<?php echo $customer_id;?>"> + </div> + <div class="mb-3"> + <label for="start-date" class="form-label"><b>Start Date:</b></label> + <input class="form-control me-2" type="text" name="start-date" placeholder="Start date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$start_date;?>> + </div> + <div class="mb-3"> + <label for="end-date" class="form-label"><b>End Date:</b></label> + <input class="form-control me-2" type="text" name="end-date" placeholder="End date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$end_date;?>> + </div> + <label for="token" class="form-label"><b>*token*:</b></label> + <div class="form-group"> + <label for="tariff">Tariff:</label> + <a>€</a><input type="number" class="form-control" name="tariff" id="tariff" placeholder="Tariff" style="display: inline-block; width: auto;" value=<?php echo$tariff;?>> + </div> + <div class="mb-3"> + <label for="addition" class="form-label"><b>Additional information:</b></label> + <input type="text" class="form-control" name="addition" id="addition" placeholder="Additional information" style="height: 200px;" value=<?php echo$addition;?>> + </div> + <input type="hidden" name="contract_id" value=<?php echo$contract_id;?>> + <button type="submit" class="btn btn-primary" name="submit">Alter contract</button> + </form> + <?php echo $msg;?> +</div> +</body> +</html> diff --git a/alter_customer.php b/alter_customer.php @@ -0,0 +1,108 @@ +<!DOCTYPE html> +<!--Deltron 3030 - The mastermind --> +<html lang="eng"> + <head> + <title>Add user</title> + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> + </head> +<?php +include "navbar.php"; +include "utils/autoloader.php"; +if(!in_array(1, $_SESSION['user_permissions'])){ + header('Location: /dashboard'); + exit; +} +$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); + //if not found set to empty if not GET +$fname = ""; +$lname = ""; +$email = ""; +$msg = ""; +if($_SERVER["REQUEST_METHOD"] == "GET"){ + //if the get var isset and user is found in the database load data into forms + if(!isset($_GET['email'])){ + echo""; + }else{ + $get_email = $_GET['email']; + $c = $db->get(Customer::class); + if($c->where("email", $get_email)){ + $fname = $c->first_name; + $lname = $c->last_name; + $email = $c->email; + $customer_id = $c->customer_id; + } + } +} +if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if voornaam is set and not empty + if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { + $fname = $_POST['voornaam']; + } else { + $errors[] = "Voornaam is required"; + } + + // Check if achternaam is set and not empty + if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { + $lname = $_POST['achternaam']; + } else { + $errors[] = "Achternaam is required"; + } + // Check if email is set and not empty + if (isset($_POST['email']) && !empty($_POST['email'])) { + $email = $_POST['email']; + } else { + $errors[] = "E-mail is required"; + } + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + $msg .= $error . "<br>"; + } + } else { + //create a database object with table customer + $c = $db->get(Customer::class); + //check if customer already exists + if(!$c->where("email", $email)){ + $msg = "this user does not exist: " . $email . " " . $fname . " " . $lname; + }else{ + $c = $db->get(Customer::class); + $c->where('email', $email); + //set new user data + $c->first_name = $fname; + $c->last_name = $lname; + $c->email = $email; + //add user with the add function + if($c->save()){ + $msg = "update to the db this info:<br> email: {$c->email}<br> firstname: {$c->first_name}<br> lastname: {$c->last_name}"; + }; + } + } +} +?> + <body> + <div class="container"> + <h1>Alter customer</h1> + + <form action="alter_customer.php" method="post"> + <div class="mb-3"> + <label for="voornaam" class="form-label"><b>Voornaam:</b></label> + <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam" value=<?php echo$fname?>> + </div> + <div class="mb-3"> + <label for="achternaam" class="form-label"><b>Achternaam:</b></label> + <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam" value=<?php echo$lname?>> + </div> + <div class="mb-3"> + <label for="email" class="form-label"><b>Email:</b></label> + <input type="text" class="form-control" name="email" id="email" placeholder="Email" value=<?php echo$email?>> + </div> + <button type="submit" class="btn btn-primary" name="submit">Alter customer</button> + </form> + <?php echo $msg;?> + </div> + </body> +</html> diff --git a/alter_user.php b/alter_user.php @@ -16,7 +16,7 @@ use Lollipop\SQLDatabase; exit; } $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); - + $msg = ""; //select the available permissions from the database $all_p = $db->all(Permissions::class); $available_permissions = []; @@ -55,14 +55,14 @@ use Lollipop\SQLDatabase; if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { $fname = $_POST['voornaam']; } else { - $errors[] = "Voornaam is required"; + $errors[] = "First name is required"; } // Check if achternaam is set and not empty if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { $lname = $_POST['achternaam']; } else { - $errors[] = "Achternaam is required"; + $errors[] = "Last name is required"; } // Check if email is set and not empty @@ -81,7 +81,7 @@ use Lollipop\SQLDatabase; if (count($errors) > 0) { // Print out the errors foreach ($errors as $error) { - echo $error . "<br>"; + $msg .= $error . "<br>"; } } else { //create a database object with table user @@ -90,7 +90,7 @@ use Lollipop\SQLDatabase; $user_id = $u->user_id; //check if email already exists if($u->email == null){ - echo"this user does not exist " . $email; + $msg = "this user does not exist " . $email; }else{ $succes = false; //set new user data @@ -98,9 +98,8 @@ use Lollipop\SQLDatabase; $u->user_id = $user_id; $u->first_name = $fname; $u->last_name = $lname; - echo $u->save(); //add user with the add function - if(true){ + if( $u->save()){ $succes = true; }; @@ -121,7 +120,7 @@ use Lollipop\SQLDatabase; } } if($succes){ - echo"succes!"; + $msg = "succes! changes saved to the database"; } } } @@ -150,12 +149,12 @@ use Lollipop\SQLDatabase; <h1>Alter user</h1> <form action="alter_user.php?email=<?php echo $email;?>" method="post"> <div class="mb-3"> - <label for="voornaam" class="form-label"><b>Voornaam:</b></label> - <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam" value=<?php echo$fname?>> + <label for="voornaam" class="form-label"><b>First name:</b></label> + <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="First name" value=<?php echo$fname?>> </div> <div class="mb-3"> - <label for="achternaam" class="form-label"><b>Achternaam:</b></label> - <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam" value=<?php echo$lname?>> + <label for="achternaam" class="form-label"><b>Last name:</b></label> + <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Last name" value=<?php echo$lname?>> </div> <div class="mb-3"> <label for="email" class="form-label"><b>Email:</b></label> @@ -175,6 +174,7 @@ use Lollipop\SQLDatabase; ?> <button type="submit" class="btn btn-primary" name="submit">Alter user</button> </form> + <?php echo $msg;?> </div> </body> </html> \ No newline at end of file diff --git a/classes/Contract.php b/classes/Contract.php @@ -0,0 +1,14 @@ +<?php +class Contract extends Lollipop\DatabaseObject +{ + static function get_table(): string + { + return "contract"; + } + + static function get_primary(): string + { + return "contract_id"; + } +} +?> +\ No newline at end of file diff --git a/classes/Customer.php b/classes/Customer.php @@ -0,0 +1,14 @@ +<?php +class Customer extends Lollipop\DatabaseObject +{ + static function get_table(): string + { + return "customer"; + } + + static function get_primary(): string + { + return "customer_id"; + } +} +?> +\ No newline at end of file diff --git a/classes/Subscription.php b/classes/Subscription.php @@ -0,0 +1,14 @@ +<?php +class Subscription extends Lollipop\DatabaseObject +{ + static function get_table(): string + { + return "subscription"; + } + + static function get_primary(): string + { + return "sub_id"; + } +} +?> +\ No newline at end of file diff --git a/index.php b/index.php @@ -41,7 +41,7 @@ $router->get('/alter_user', function () { include 'search_user.php'; }); $router->get('/dashboard', function () { - include 'searchdata.php'; + include 'search_data.php'; }); $router->post('/logout', function () { diff --git a/navbar.php b/navbar.php @@ -26,9 +26,12 @@ if (in_array(2, $permission_levels)) { // Administrative employee links $admin_employee_links = array( - array('url' => 'admin_employee_page_1.php', 'title' => 'Admin Employee Page 1'), - array('url' => 'admin_employee_page_2.php', 'title' => 'Admin Employee Page 2'), - array('url' => 'admin_employee_page_3.php', 'title' => 'Admin Employee Page 3') + array('url' => '/add_customer.php', 'title' => 'Add customer'), + array('url' => '/search_customer.php', 'title' => 'Search customer'), + array('url' => '/alter_customer.php', 'title' => 'Alter customer data'), + array('url' => '/add_contract.php', 'title' => 'Add contract'), + array('url' => '/search_contract.php', 'title' => 'Search contract'), + array('url' => '/alter_contract.php', 'title' => 'Alter contract') ); $links[] = array('name' => 'Administrative Employee', 'links' => $admin_employee_links); } @@ -36,7 +39,7 @@ if (in_array(3, $permission_levels)) { // Scientific employee links $scientific_employee_links = array( - array('url' => 'scientific_employee_page_1.php', 'title' => 'Scientific Employee Page 1'), + array('url' => 'search_data.php', 'title' => 'Search data'), array('url' => 'scientific_employee_page_2.php', 'title' => 'Scientific Employee Page 2'), array('url' => 'scientific_employee_page_3.php', 'title' => 'Scientific Employee Page 3') ); @@ -72,12 +75,9 @@ </ul> </li> <?php } ?> - <li class="nav-item"> - <a class="nav-link" href="/addcontract">Add Subscription</a> - </li> </ul> <form method="post" action="/logout"> - <button type="submit" id='logout' class="btn btn-primary">log out</button> + <button type="submit" id='logout' class="btn btn-primary">Log out</button> </form> </div> </nav> diff --git a/search_contract.php b/search_contract.php @@ -0,0 +1,88 @@ +<!DOCTYPE html> +<html lang="eng"> + <head> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> + <?php + include "navbar.php"; + include "utils/autoloader.php"; + if(!in_array(1, $_SESSION['user_permissions'])){ + header('Location: /dashboard'); + exit; + } + $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); + ?> + </head> +<body> + <form class="d-flex" action="search_contract.php" method="post"> + <input class="form-control me-2" type="search" name="search_id" placeholder="ID" aria-label="Search"> + <button class="btn btn-outline-success" type="submit">Search</button> +</form> + <?php + if ($_SERVER["REQUEST_METHOD"] == "POST"){ + $query = []; + if(isset($_POST['search_id'])) { + $query["customer_id"] = "%{$_POST['search_id']}%"; + } + if(isset($_POST['delete'])){ + $c = $db->get(Contract::class); + $c->where("customer_id", $_POST['delete']); + $c->delete(); + } + if($query == null){ + $query['customer_id'] = "%"; + } + display_results($db, $query); + } + function display_results($db, $query){ + //create a User orm class and load all the records where user like query + $results = $db->all_where(Contract::class, $query); + + // display results + if($results != null){ + echo "<table class=\"table table-striped\"> + <thead> + <tr> + <th>Contract id</th> + <th>Customer id </th> + <th>Sub id </th> + <th>Tariff</th> + <th>Start date</th> + <th>End date</th> + <th>Standards</th> + <th>Token</th> + <th>Alter contract data</th> + <th>Delete</th> + </tr> + </thead> + <tbody>"; + foreach($results as $data) { + $link_alter = "/alter_contract.php?contract_id=" . $data->contract_id; + echo "<tr>"; + echo "<td>" . $data->contract_id . "</td>"; + echo "<td>" . $data->customer_id . "</td>"; + echo "<td>" . $data->sub_id . "</td>"; + echo "<td>" . $data->tariff . "</td>"; + echo "<td>" . $data->start_date . "</td>"; + echo "<td>" . $data->end_date . "</td>"; + echo "<td>" . $data->standards . "</td>"; + echo "<td>" . $data->token . "</td>"; + echo "<td><a href='" . $link_alter . "'>Alter</a></td>"; + echo " + <td> + <form method=\"post\" action=\"search_contract.php\"> + <button type=\"submit\" name='delete' value=" . $data->customer_id ." ' class=\"btn btn-primary\">Delete</button> + </form> + </td>"; + echo "</tr>"; + } + echo" + </tbody> + </table>"; + }else{ + echo "No contracts were found."; + } + } + ?> + </body> +</html> + diff --git a/search_customer.php b/search_customer.php @@ -0,0 +1,91 @@ +<!DOCTYPE html> +<html lang="eng"> + <head> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> + <?php + include "navbar.php"; + include "utils/autoloader.php"; + if(!in_array(1, $_SESSION['user_permissions'])){ + header('Location: /dashboard'); + exit; + } + $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); + ?> + </head> +<body> + <form class="d-flex" action="search_customer.php" method="post"> + <input class="form-control me-2" type="search" name="search_email" placeholder="Email" aria-label="Search"> + <input class="form-control me-2" type="search" name="search_first_name" placeholder="First name" aria-label="Search"> + <input class="form-control me-2" type="search" name="search_last_name" placeholder="Last name" aria-label="Search"> + <button class="btn btn-outline-success" type="submit">Search</button> +</form> + <?php + if ($_SERVER["REQUEST_METHOD"] == "POST"){ + $query = []; + if(isset($_POST['search_email'])) { + $query["email"] = "%{$_POST['search_email']}%"; + } + if(isset($_POST['search_last_name'])) { + $query["first_name"] = "%{$_POST['search_first_name']}%"; + } + if(isset($_POST['search_first_name'])) { + $query["last_name"] = "%{$_POST['search_last_name']}%"; + } + if(isset($_POST['delete'])){ + $c = $db->get(Customer::class); + $c->where("email", $_POST['delete']); + $c->delete(); + } + if($query == []){ + $query['email'] = "%"; + } + display_results($db, $query); + } + function display_results($db, $query){ + //create a User orm class and load all the records where user like query + $results = $db->all_where(Customer::class, $query); + + // display results + if($results != null){ + echo "<table class=\"table table-striped\"> + <thead> + <tr> + <th>Id</th> + <th>E-mail</th> + <th>First name</th> + <th>Last name</th> + <th>Alter customer data</th> + <th>Add conctract</th> + <th>Delete</th> + </tr> + </thead> + <tbody>"; + foreach($results as $data) { + $link_alter = "/alter_customer.php?email=" . $data->email; + $link_add_contract = "/add_contract.php?email=" . $data->email; + echo "<tr>"; + echo "<td>" . $data->customer_id . "</td>"; + echo "<td>" . $data->email . "</td>"; + echo "<td>" . $data->first_name . "</td>"; + echo "<td>" . $data->last_name . "</td>"; + echo "<td><a href='" . $link_alter . "'>Alter</a></td>"; + echo "<td><a href='" . $link_add_contract . "'>Add Contract</a></td>"; + echo " + <td> + <form method=\"post\" action=\"search_customer.php\"> + <button type=\"submit\" name='delete' value=" . $data->email ." ' class=\"btn btn-primary\">delete</button> + </form> + </td>"; + echo "</tr>"; + } + echo" + </tbody> + </table>"; + }else{ + echo "No customers were found."; + } + } + ?> + </body> +</html> + diff --git a/search_data.php b/search_data.php @@ -0,0 +1,100 @@ +<?php +error_reporting(0); + + include "navbar.php"; + include "Connect.php"; +?> + + <?php + $connect = new Connect; + $conn = $connect->getConn(); + $params = []; + + $dateEnd = ($_GET['date-end']) . "%"; + $dateBegin = ($_GET['date-begin'] . "%"); + + $startDate = $_GET['date-begin']; + $endDate = $_GET['date-end']; + $station = $_GET['station']; + + $sql = "SELECT station_name, date_time, validated, temperature FROM weather_data"; + if(!isset($_GET['date-begin']) && !isset($_GET['date-end']) && !isset($_GET['station'])){ + $sql .= " LIMIT 250"; + } + if (isset($_GET['date-begin']) && $_GET['date-begin']) { + $sql .= " WHERE date_time >= ?"; + $params[] = $dateBegin; + if (isset($_GET['date-end']) && $_GET['date-end']) { + $sql .= " AND date_time <= ?"; + $params[] = $dateEnd; + } + } elseif (isset($_GET['date-end']) && $_GET['date-end']) { + $sql .= " WHERE date_time <= ?"; + $params[] = $dateEnd; + } + if (isset($_GET['station']) && $_GET['station']) { + if (isset($_GET['date-begin']) || isset($_GET['date-end'])){ + $sql .= " AND "; + } + else{ + $sql .= " WHERE "; + } + $sql .= "station_name = ?"; + $params[] = $_GET['station']; + } + + $stmt = $conn->prepare($sql); +// if (isset($_GET['date-begin'])) +// $stmt->bind_param('s', $dateBegin); +// if (isset($_GET['date-end'])) +// $stmt->bind_param('s', $dateEnd); +// if (isset($_GET['station'])) { +// $stmt->bind_param('d', $_GET['station']); +// } + + $stmt->execute($params); + $result = $stmt->get_result(); + + ?> +<html> +<head> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> +</head> +<body> +<form class="d-flex" action="search_data.php" method="get"> + <input class="form-control me-2" type="text" name="date-begin" placeholder="Date begin (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$startDate?>> + <input class="form-control me-2" type="text" name="date-end" placeholder="Date end (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$endDate?>> + <input class="form-control me-2" type="text" name="station" placeholder="Search" aria-label="Search" value=<?php echo$station?>> + <button class="btn btn-outline-success" type="submit">Search</button> +</form> +<?php + // verification logic and $_SESSION start + if ($result->num_rows > 0) { + echo "<table class=\"table table-striped\"> + <thead> + <tr> + <th>Station</th> + <th>Date</th> + <th>Validated</th> + <th>Temperature</th> + </tr> + </thead> + <tbody>"; + while ($row = mysqli_fetch_assoc($result)) { + $link = "/search_data.php?station=" . $row['station_name']; + echo "<tr>"; + echo "<td><a href='" . $link . "'>" . $row['station_name'] . "</a></td>"; + echo "<td>" . $row['date_time'] . "</td>"; + echo "<td>" . $row['validated'] . "</td>"; + echo "<td>" . $row['temperature'] . "</td>"; + echo "</tr>"; + } + echo " + </tbody> + </table>"; + } else { + echo "No data found."; + } + ?> +</body> +</html> +\ No newline at end of file diff --git a/searchdata.php b/searchdata.php @@ -1,98 +0,0 @@ -<?php -error_reporting(0); - - include "navbar.php"; - include "Connect.php"; -?> - - <?php - $connect = new Connect; - $conn = $connect->getConn(); - $params = []; - - $dateEnd = ($_GET['date-end']) . "%"; - $dateBegin = ($_GET['date-begin'] . "%"); - - $startDate = $_GET['date-begin']; - $endDate = $_GET['date-end']; - $station = $_GET['station']; - - $sql = "SELECT station_name, date_time, validated, temperature FROM weather_data"; - if (isset($_GET['date-begin']) && $_GET['date-begin']) { - $sql .= " WHERE date_time >= ?"; - $params[] = $dateBegin; - if (isset($_GET['date-end']) && $_GET['date-end']) { - $sql .= " AND date_time <= ?"; - $params[] = $dateEnd; - } - } elseif (isset($_GET['date-end']) && $_GET['date-end']) { - $sql .= " WHERE date_time <= ?"; - $params[] = $dateEnd; - } - if (isset($_GET['station']) && $_GET['station']) { - if (isset($_GET['date-begin']) || isset($_GET['date-end'])){ - $sql .= " AND "; - } - else{ - $sql .= " WHERE "; - } - $sql .= "station_name = ?"; - $params[] = $_GET['station']; - } - - - $stmt = $conn->prepare($sql); -// if (isset($_GET['date-begin'])) -// $stmt->bind_param('s', $dateBegin); -// if (isset($_GET['date-end'])) -// $stmt->bind_param('s', $dateEnd); -// if (isset($_GET['station'])) { -// $stmt->bind_param('d', $_GET['station']); -// } - - $stmt->execute($params); - $result = $stmt->get_result(); - - ?> -<html> -<head> - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> -</head> -<body> -<form class="d-flex" action="searchdata.php" method="get"> - <input class="form-control me-2" type="text" name="date-begin" placeholder="Date begin (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$startDate?>> - <input class="form-control me-2" type="text" name="date-end" placeholder="Date end (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}" value=<?php echo$endDate?>> - <input class="form-control me-2" type="text" name="station" placeholder="Search" aria-label="Search" value=<?php echo$station?>> - <button class="btn btn-outline-success" type="submit">Search</button> -</form> -<?php - // verification logic and $_SESSION start - if ($result->num_rows > 0) { - echo "<table class=\"table table-striped\"> - <thead> - <tr> - <th>Station</th> - <th>Date</th> - <th>Validated</th> - <th>Temperature</th> - </tr> - </thead> - <tbody>"; - while ($row = mysqli_fetch_assoc($result)) { - $link = "/searchdata.php?station=" . $row['station_name']; - echo "<tr>"; - echo "<td><a href='" . $link . "'>" . $row['station_name'] . "</a></td>"; - echo "<td>" . $row['date_time'] . "</td>"; - echo "<td>" . $row['validated'] . "</td>"; - echo "<td>" . $row['temperature'] . "</td>"; - echo "</tr>"; - } - echo " - </tbody> - </table>"; - } else { - echo "No data found."; - } - ?> -</body> -</html> -\ No newline at end of file