add_contract.php (5017B)
1 <!DOCTYPE html> 2 <html lang="nl"> 3 <head> 4 <title>Add contract</title> 5 <!-- Bootstrap CSS --> 6 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> 7 <link rel="stylesheet" type="text/css" href="/css/general.css"> 8 </head> 9 <?php 10 include "views/navbar.php"; 11 include "utils/autoloader.php"; 12 if(!in_array(1, $_SESSION['user_permissions'])){ 13 header('Location: dashboard'); 14 exit; 15 } 16 $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); 17 $customer_id = null; 18 if($_SERVER['REQUEST_METHOD'] == "GET"){ 19 if(isset($_GET['email'])){ 20 $email = $_GET['email']; 21 $c = $db->get(Customer::class); 22 $c->where('email', $email); 23 $customer_id = $c->customer_id; 24 } 25 } 26 $available_subsciptions = []; 27 $all_p = $db->all(Subscription::class); 28 foreach($all_p as $tmp){ 29 $available_permissions[$tmp->sub_id] = $tmp->sub_name; 30 } 31 32 $msg = ""; 33 if ($_SERVER["REQUEST_METHOD"] == "POST") { 34 $errors = array(); // initialize an empty array to store errors 35 36 // Check if subscription type is set and not empty 37 if (isset($_POST['subscription']) && !empty($_POST['subscription'])) { 38 $sub_type = $_POST['subscription']; 39 } else { 40 $errors[] = "subscription type is required"; 41 } 42 43 // Check if customer_id is set and not empty 44 if (isset($_POST['customer_id']) && !empty($_POST['customer_id'])) { 45 $customer_id = $_POST['customer_id']; 46 } else { 47 $errors[] = "customer_id is required"; 48 } 49 50 // Check if start-date is set and not empty 51 if (isset($_POST['start-date']) && !empty($_POST['start-date'])) { 52 $start_date = $_POST['start-date']; 53 } else { 54 $errors[] = "start date is required"; 55 } 56 57 // Check if end-date is set and not empty 58 if (isset($_POST['end-date']) && !empty($_POST['end-date'])) { 59 $end_date = $_POST['end-date']; 60 } else { 61 $errors[] = "end date is required"; 62 } 63 64 // Check if permissions is set 65 if (isset($_POST['tariff']) && !empty($_POST['tariff'])) { 66 $tariff = $_POST['tariff']; 67 } else { 68 $errors[] = "tarif is required"; 69 } 70 // 71 if (isset($_POST['addition']) && !empty($_POST['addition'])) { 72 $addition = $_POST['addition']; 73 } else { 74 $errors[] = "additional information is required"; 75 } 76 77 // Check if there are any errors 78 if (count($errors) > 0) { 79 // Print out the errors 80 foreach ($errors as $error) { 81 $msg.= $error . "<br>"; 82 } 83 } else { 84 $c = $db->get(Contract::class); 85 $c->sub_id = (int) $sub_type; 86 $c->customer_id = (int) $customer_id; 87 $c->start_date = $start_date; 88 $c->end_date = $end_date; 89 $token = bin2hex(random_bytes(32)); 90 $c->token = $token; 91 $c->tariff = (double) $tariff; 92 $c->standards = $addition; 93 94 if($c->add()) 95 $msg = "succes!!!"; 96 } 97 } 98 ?> 99 <body> 100 <div class="container"> 101 <h1>Add contract</h1> 102 <form action="add_contract" method="post"> 103 <div class="mb-3"> 104 <label for="subscription">Choose subscription:</label> 105 <select name="subscription" id="subscription"> 106 <?php 107 foreach($available_permissions as $key => $value){ 108 echo "<option value=\"{$key}\">{$value}</option>"; 109 } 110 ?> 111 </select> 112 </div> 113 <div class="mb-3"> 114 <label for="customer" class="form-label"><b>Customer ID:</b></label> 115 <input type="text" class="form-control" name="customer_id" id="customer_id" placeholder="Customer ID" value="<?php echo $customer_id?>"> 116 </div> 117 <div class="mb-3"> 118 <label for="start-date" class="form-label"><b>Start Date:</b></label> 119 <input class="form-control me-2" type="text" name="start-date" placeholder="Start date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> 120 </div> 121 <div class="mb-3"> 122 <label for="end-date" class="form-label"><b>End Date:</b></label> 123 <input class="form-control me-2" type="text" name="end-date" placeholder="End date (YYYY-MM-DD)" pattern="\d{4}-\d{2}-\d{2}"> 124 </div> 125 <!-- <label for="token" class="form-label"><b>*token*:</b></label>--> 126 <div class="form-group"> 127 <label for="tariff">Tariff:</label> 128 <a>€</a><input type="number" class="form-control" name="tariff" id="tariff" placeholder="Tariff" style="display: inline-block; width: auto;"> 129 </div> 130 <div class="mb-3"> 131 <label for="addition" class="form-label"><b>Additional information:</b></label> 132 <input type="text" class="form-control" name="addition" id="addition" placeholder="Additional information" style="height: 200px;"> 133 </div> 134 <button type="submit" class="btn btn-primary" name="submit">Voeg toe</button> 135 </form> 136 <?php echo $msg;?> 137 </div> 138 </body> 139 </html>