alter_contract.php (6330B) download
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/homepage.css">
8</head>
9<?php
10include "views/navbar.php";
11include "utils/autoloader.php";
12if(!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;
18if($_SERVER['REQUEST_METHOD'] == "GET"){
19 if(isset($_GET['contract_id'])){
20 $c = $db->get(Contract::class);
21 $c->where('contract_id', $_GET['contract_id']);
22 $customer_id = $c->customer_id;
23 $contract_id = $c->contract_id;
24 $sub_type = $c->sub_id;
25 $start_date = $c->start_date;
26 $end_date = $c->end_date;
27 $tariff = $c->tariff;
28 $addition = $c->standards;
29 }else{
30 $customer_id = "";
31 $selected = "";
32 $start_date = "";
33 $end_date = "";
34 $tariff = "";
35 $addition = "";
36 }
37}
38$msg = "";
39$available_subsciptions = [];
40$all_p = $db->all(Subscription::class);
41foreach($all_p as $tmp){
42 $available_permissions[$tmp->sub_id] = $tmp->sub_name;
43}
44if ($_SERVER["REQUEST_METHOD"] == "POST") {
45 $errors = array(); // initialize an empty array to store errors
46
47 // Check if subscription type is set and not empty
48 if (isset($_POST['subscription']) && !empty($_POST['subscription'])) {
49 $sub_type = $_POST['subscription'];
50 } else {
51 $errors[] = "subscription type is required";
52 }
53
54 // Check if customer_id is set and not empty
55 if (isset($_POST['customer_id']) && !empty($_POST['customer_id'])) {
56 $customer_id = $_POST['customer_id'];
57 } else {
58 $errors[] = "customer_id is required";
59 }
60 // Check if contract_id is set and not empty
61 if (isset($_POST['contract_id']) && !empty($_POST['contract_id'])) {
62 $contract_id = $_POST['contract_id'];
63 } else {
64 $errors[] = "contract_id is required";
65 }
66
67 // Check if start-date is set and not empty
68 if (isset($_POST['start-date']) && !empty($_POST['start-date'])) {
69 $start_date = $_POST['start-date'];
70 } else {
71 $errors[] = "start date is required";
72 }
73
74 // Check if end-date is set and not empty
75 if (isset($_POST['end-date']) && !empty($_POST['end-date'])) {
76 $end_date = $_POST['end-date'];
77 } else {
78 $errors[] = "end date is required";
79 }
80
81 // Check if permissions is set
82 if (isset($_POST['tariff']) && !empty($_POST['tariff'])) {
83 $tariff = $_POST['tariff'];
84 } else {
85 $errors[] = "tarif is required";
86 }
87 //
88 if (isset($_POST['addition']) && !empty($_POST['addition'])) {
89 $addition = $_POST['addition'];
90 } else {
91 $errors[] = "additional information is required";
92 }
93
94 // Check if there are any errors
95 if (count($errors) > 0) {
96 // Print out the errors
97 foreach ($errors as $error) {
98 echo $error . "<br>";
99 }
100 } else {
101 $c = $db->get(Contract::class);
102 if(!$c->where('contract_id', (int) $contract_id)){
103 $msg = "this contract does not exist";
104 }else{
105 $c->sub_id = (int) $sub_type;
106 $c->customer_id = (int) $customer_id;
107 $c->start_date = $start_date;
108 $c->end_date = $end_date;
109 $token = bin2hex(random_bytes(32));
110 $c->token = $token;
111 $c->tariff = (double) $tariff;
112 $c->standards = $addition;
113
114 if($c->save()){
115 $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}";
116 }
117 }
118 }
119}
120?>
121<body>
122<div class="container">
123 <h1>Add contract</h1>
124 <form action="alter_contract" method="post">
125 <div class="mb-3">
126 <label for="subscription">Choose subscription:</label>
127 <select name="subscription" id="subscription">
128 <?php
129 $tmp = "";
130 foreach($available_permissions as $key => $value){
131 if($sub_type == $value)
132 $tmp = "selected";
133 else
134 $tmp = "selected";
135 echo "<option value=\"{$key} {$tmp}\">{$value}</option>";
136 }
137 ?>
138 </select>
139 </div>
140 <div class="mb-3">
141 <label for="customer" class="form-label"><b>Customer ID:</b></label>
142 <input type="text" class="form-control" name="customer_id" id="customer_id" placeholder="Customer ID" value="<?php echo $customer_id;?>">
143 </div>
144 <div class="mb-3">
145 <label for="start-date" class="form-label"><b>Start Date:</b></label>
146 <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;?>>
147 </div>
148 <div class="mb-3">
149 <label for="end-date" class="form-label"><b>End Date:</b></label>
150 <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;?>>
151 </div>
152<!-- <label for="token" class="form-label"><b>*token*:</b></label>-->
153 <div class="form-group">
154 <label for="tariff">Tariff:</label>
155 <a>€</a><input type="number" class="form-control" name="tariff" id="tariff" placeholder="Tariff" style="display: inline-block; width: auto;" value=<?php echo$tariff;?>>
156 </div>
157 <div class="mb-3">
158 <label for="addition" class="form-label"><b>Additional information:</b></label>
159 <input type="text" class="form-control" name="addition" id="addition" placeholder="Additional information" style="height: 200px;" value=<?php echo$addition;?>>
160 </div>
161 <input type="hidden" name="contract_id" value=<?php echo$contract_id;?>>
162 <button type="submit" class="btn btn-primary" name="submit">Alter contract</button>
163 </form>
164 <?php echo $msg;?>
165</div>
166</body>
167</html>