-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplates.php
223 lines (203 loc) · 6.85 KB
/
templates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
include ("common.php");
include ("dbconnect.php");
navigation();
if ($user_class < "9")
exit();
$nameErr = $portsErr = $template_idErr = "";
$name = $ports = $template_id = $t_id = "";
print "<p><center>SVR TEMPLATE Configuration</p>";
if (!($_SERVER["REQUEST_METHOD"] == "POST")&&!($_GET)) {
print "<form action=\"templates.php\" method=\"post\">";
print "OLT*:<select id=\"select-olt\" name=\"olt\">";
try {
$result = $db->query("SELECT * from OLT");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($olt == $row{'ID'}) {
print "<option value=\"" . $row{'ID'} ."\" selected>" . $row{'NAME'} . "</option>";
} else {
print "<option value=\"" . $row{'ID'} ."\">" . $row{'NAME'} . "</option>";
}
}
print '</select>';
print "<input type='submit' name='SUBMIT' value='GET'><br><br>";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["SUBMIT"]!="GET") {
if ($_POST["t_id"]) {
$t_id = test_input($_POST["t_id"]);
}
if ($_POST["template_id"]) {
$template_id = test_input($_POST["template_id"]);
}
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["ports"])) {
$portsErr = "Number of Ports Required!";
} else {
$ports = test_input($_POST["ports"]);
}
}
if (empty($_POST["olt"])) {
$oltErr = "OLT is required";
} else {
$olt = test_input($_POST["olt"]);
}
if ($_POST["SUBMIT"]) {
$submit = test_input($_POST["SUBMIT"]);
}
print "<form action=\"templates.php\" method=\"post\">";
print "OLT*:<select id=\"select-olt\" name=\"olt\">";
try {
$result = $db->query("SELECT * from OLT");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($olt == $row{'ID'}) {
print "<option value=\"" . $row{'ID'} ."\" selected>" . $row{'NAME'} . "</option>";
} else {
print "<option value=\"" . $row{'ID'} ."\">" . $row{'NAME'} . "</option>";
}
}
print '</select>';
print "<input type='submit' name='SUBMIT' value='GET'><br><br>";
// ADD TEMPLATE
if ($name !== '' && $ports !== '' && $template_id !== '' && $submit == "ADD") {
try {
$result = $db->query("INSERT INTO SVR_TEMPLATE (NAME, PORTS, OLT, TEMPLATE_ID) VALUES ('$name', '$ports', '$olt', '$template_id')");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
exit("SVR Template added Succesfully");
}
// EDIT SVR TEMPLATE
if ($template_id !== '' && $name !== '' && $ports !== '' && $submit == "EDIT") {
// UPDATE TEMPALTES
try {
$result = $db->query("UPDATE SVR_TEMPLATE SET NAME = '$name', PORTS = '$ports', TEMPLATE_ID = '$template_id' where ID = '$t_id'");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
exit("SVR Template Edited Succesfully");
}
// DELETE SVR TEMPLATE
if ($t_id !== '' && $submit == "DELETE") {
// CHECK IF TEMPLATE IS ASSIGNED TO ANY CUSTOMER
try {
$result = $db->query("SELECT SVR_TEMPLATE from CUSTOMERS where SVR_TEMPLATE = '$t_id'");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if ($row["SVR_TEMPLATE"])
exit ("SVR TEMPLATE IS ASSIGNED TO CUSTOMERS, Please remove SVR_TEMPLATES from customers to Delete it!");
}
try {
$result = $db->query("DELETE FROM SVR_TEMPLATE where ID='$t_id'");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
exit("SVR TEMPLATE Deleted Succesfully");
}
//END OF POST METHOD
}
// GET METHOD
if ($_GET) {
$olt = $_GET['olt'];
$t_id = $_GET['id'];
if (!preg_match('/^[0-9]*$/', $t_id)) {
print "that sux";
exit;
} else {
try {
$result = $db->query("SELECT ID, NAME, PORTS, TEMPLATE_ID from SVR_TEMPLATE where ID='$t_id'");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$t_id = $row["ID"];
$name = $row["NAME"];
$ports = $row["PORTS"];
$template_id = $row["TEMPLATE_ID"];
}
}
print "<form action=\"templates.php\" method=\"post\">";
print "OLT*:<select id=\"select-olt\" name=\"olt\">";
try {
$result = $db->query("SELECT * from OLT");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($olt == $row{'ID'}) {
print "<option value=\"" . $row{'ID'} ."\" selected>" . $row{'NAME'} . "</option>";
} else {
print "<option value=\"" . $row{'ID'} ."\">" . $row{'NAME'} . "</option>";
}
}
print '</select>';
print "<input type='submit' name='SUBMIT' value='GET'><br><br>";
}
if (($_SERVER["REQUEST_METHOD"] == "POST")||($_GET)) {
try {
$result = $db->query("SELECT ID, NAME, PORTS, TEMPLATE_ID from SVR_TEMPLATE where OLT='$olt'");
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage() . "\n";
exit;
}
print "<table border=1 cellpadding=1 cellspacing=1><tr align=center style=font-weight:bold><td>ID</td><td>NAME</td><td>PORTS</td><td>TEMPLATES_ID</td></tr>";
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
print "<tr align=right><td><a href='templates.php?edit=1&id=". $row{'ID'} . "&olt=" . $olt . "'>" . $row{'ID'} . "</a></td><td>" . $row{'NAME'} . "</td><td>" . $row{'PORTS'} . "</td><td>" . $row{'TEMPLATE_ID'} . "</td></tr>";
}
print "</table>";
print "<form action=\"templates.php\" method=\"post\">";
print "<p><table>";
if ($_GET["edit"] == "1") {
print "<tr><td>Template ID*:</td><td>" . $template_id . "</td>";
print "<input type=\"hidden\" name=\"template_id\" value=\"". $template_id ."\">";
print "<input type=\"hidden\" name=\"t_id\" value=\"". $t_id ."\">";
} else {
print "<tr><td>Template ID*:</td><td><input type=\"text\" name=\"template_id\"";
if($template_id)
print "value=\"".$template_id ."\"></td>";
}
if($template_idErr != "")
print "<td style=\"color:red\">" . $template_idErr . "</td>";
print "</tr>";
print "<tr><td>Name*:</td><td><input type=\"text\" name=\"name\"";
if($name)
print "value=\"".$name ."\"";
print "></td>";
if($nameErr != "")
print "<td style=\"color:red\">" . $nameErr . "</td>";
print "</tr><tr><td>Ports*:</td><td><input type=\"text\" name=\"ports\"";
if($ports)
print "value=\"".$ports ."\"";
print "></td>";
if($portsErr != "")
print "<td style=\"color:red\">" . $portsErr . "</td>";
print "</tr></table></p>";
if ($_GET["edit"] == "1" || $t_id) {
print "<input type='submit' name='SUBMIT' value='EDIT'>";
print " <input type='submit' name='SUBMIT' value='DELETE'>";
}else{
print "<input type='submit' name='SUBMIT' value='ADD'>";
}
print "</form>";
}
?>