-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
556 lines (469 loc) · 20.9 KB
/
index.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
session_start();
include_once ("php/connect.php");
@$_SESSION['message'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['form_type'] == 'form_add') {
@$name=$_POST['nameOfElement'];
@$parent=$_POST['selectAdd'];
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
//run the store proc
$sql= "CALL dodajWezel('{$name}',$parent);";
$wynik = $polaczenie->query($sql);
$_SESSION['message']="Dodano nowy wezeł";
$polaczenie->close();
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['form_type'] == 'form_rename') {
@$name=$_POST['newName'];
@$id=$_POST['selectRename'];
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "CALL zmienNazwe('{$name}',$id);";
$wynik = $polaczenie->query($sql);
$_SESSION['message']="Zmieniono nazwe węzła";
$polaczenie->close();
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['form_type'] == 'form_move') {
$id_wezla = $_POST['selectSource'];
$id_nowego_rodzica = $_POST['selectDestination'];
setDestination($id_wezla,$id_nowego_rodzica);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['form_type'] == 'form_delete') {
$id=$_POST['selectDelete'];
deleteTree($id);
}
function deleteTree($id){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$query = "SELECT * FROM countries WHERE parent_id='{$id}';";
$result = $polaczenie->query($query);
while($row = $result->fetch_assoc()) {
deleteTree($row['id']);
}
$query = "DELETE FROM countries WHERE id='$id';";
$result = $polaczenie->query($query);
$_SESSION['message']="Usunieto wezeł";
$polaczenie->close();
}
}
function setDestination($id_wezla,$id_nowego_rodzica)
{
$nazwa_tabeli = "countries";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$query = "SELECT id, parent_id FROM countries WHERE id = '{$id_wezla}';";
$result = $polaczenie->query( $query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id_rodzica_wezla = $row['parent_id'];
if ($id_wezla == $id_nowego_rodzica) {
$_SESSION['message']= "Wybrano ten sam węzeł";
} elseif ($id_rodzica_wezla == $id_nowego_rodzica) {
$_SESSION['message']="Węzeł jest już dzieckiem tego rodzica";
} else {
$query = "SELECT GROUP_CONCAT(lv SEPARATOR ',') AS potomkowie FROM (SELECT @pv:=(SELECT GROUP_CONCAT(id SEPARATOR ',')" .
" FROM countries WHERE parent_id IN (@pv)) AS lv FROM countries" .
" JOIN (SELECT @pv:={$id_wezla})tmp WHERE parent_id IN (@pv)) a;";
$result = $polaczenie->query( $query);
if ($result) {
while ($row = $result->fetch_assoc()) {
break;
}
$potomkowie = $row['potomkowie'];
$potomkowie_tablica = explode(",", $potomkowie);
if (in_array($id_nowego_rodzica, $potomkowie_tablica)) {
$_SESSION['message']="Nie można przenieść węzła do niższego poziomu w tej samej gałęzi";
} else {
// Zamienia w bazie danych wartość pola 'parent_id' wybranego węzła na ID nowego rodzica.
$query = "UPDATE {$nazwa_tabeli} SET parent_id = '{$id_nowego_rodzica}' WHERE id = '{$id_wezla}';";
$result = $polaczenie->query($query);
if ($result) {
$_SESSION['message']= "Węzeł został przeniesiony";
} else {
$_SESSION['message']= "Wystąpił błąd podczas przenoszenia węzła";
}
}
} else {
$query = "UPDATE {$nazwa_tabeli} SET parent_id = '{$id_nowego_rodzica}' WHERE id = '{$id_wezla}';";
$result = $polaczenie->query($query);
if ($result) {
$_SESSION['message']="Węzeł został przeniesiony";
} else {
$_SESSION['message']= "Wystąpił błąd podczas przenoszenia węzła";
}
}
$result =$polaczenie->query($query);
}
break;
}
} else {
$_SESSION['message']= "Wystąpił błąd podczas przenoszenia węzła";
}
}
}
function childs($id){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries where parent_id={$id}";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
$checking=checkIfChilds($r['id']);
if($checking>0){
echo "<button class=\"collapsible\">" . $r['text'] . "</button>";
echo "<div class=\"content\">";
$rs = $r['id'];
childs($rs);
echo "</div>";
}else{
echo "<div class=\"content2\">".$r['text']."</div>";
}
}
$polaczenie->close();
}
}
function childs1($id){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries where parent_id={$id} order by text asc";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
$checking=checkIfChilds($r['id']);
if($checking>0){
echo "<button class=\"collapsible\">" . $r['text'] . "</button>";
echo "<div class=\"content\">";
$rs = $r['id'];
childs1($rs);
echo "</div>";
}else{
echo "<div class=\"content2\">".$r['text']."</div>";
}
}
$polaczenie->close();
}
}
function childs2($id){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries where parent_id={$id} order by text desc";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
$checking=checkIfChilds($r['id']);
if($checking>0){
echo "<button class=\"collapsible\">" . $r['text'] . "</button>";
echo "<div class=\"content\">";
$rs = $r['id'];
childs2($rs);
echo "</div>";
}else{
echo "<div class=\"content2\">".$r['text']."</div>";
}
}
$polaczenie->close();
}
}
function checkIfChilds($id){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "countries";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql = "select * from countries where parent_id={$id}";
$wynik = $polaczenie->query($sql);
$row = mysqli_num_rows($wynik);
$polaczenie->close();
return $row;
}
}
function komunikat($tresc){
echo '<script>
alert("' . $tresc . '");
</script>';
}
if(isset($_SESSION['message'])) {
komunikat($_SESSION['message']);
unset($_SESSION['message']);
};
?>
<html>
<head>
<script>
function validateFormMove() {
var y = document.forms["formMove"]["selectSource"].value;
var x = document.forms["formMove"]["selectDestination"].value;
if (x == y) {
alert("Nie jest mozliwe w to samo miejsce");
return false;
}
}
</script>
<script>
function validateFormAdd() {
var regexp;
var x = document.forms["form_add"]["nameOfElement"].value;
if (x == "") {
alert("Nazwa jest pusta, wypełnij.");
return false;
}else{
regexp = /^[A-Za-z \t\r\n\f]*$/;
if (!regexp.test(x))
{
alert("Nazwa może zawierać litery i spacje");
return false;
}
}
}
</script>
<script>
function validateFormRename() {
var regexp;
var x = document.forms["formRename"]["newName"].value;
if (x == "") {
alert("Nazwa jest pusta, wypełnij.");
return false;
}else{
regexp = /^[A-Za-z \t\r\n\f]*$/;
if (!regexp.test(x))
{
alert("Nazwa może zawierać litery i spacje");
return false;
}
}
}
</script>
<link href="styles/style.css?v=<?php echo time(); ?>" rel="stylesheet" type="text/css">
</head>
<body>
<div class="main">
<div class="left">
<div class="left_element">
<form name ='form_add' action="./index.php" onsubmit="return validateFormAdd();" method="post">
<input type="hidden" name="form_type" value="form_add">
<div class="left_element_info">
<p>Dodaj nowy węzeł</p>
</div>
<div class="left_element_first">
<span><p>Nazwa węzła: </p></span>
<input type="text" name="nameOfElement">
</div>
<div class="left_element_second">
<p>Miejsce docelowe:</p>
<select name="selectAdd" id="selectAdd">
<option value="0">Dodaj jako nowy węzeł</option>
<?php
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
echo "<option value=\"".$r['id']."\">".$r['text']."</option>";
}
$polaczenie->close();
}
?>
</select>
</div>
<div class="left_element_button">
<input type="submit" name="button_add" value="Dodaj węzeł" class="left_button">
</div>
</form>
</div>
<div class="left_element">
<form name="formMove" action="./index.php" method="post" onsubmit="return validateFormMove();">
<input type="hidden" name="form_type" value="form_move">
<div class="left_element_info">
<p>Przenieś węzeł</p>
</div>
<div class="left_element_first">
<p>Wybierz węzeł:</p>
<select name="selectSource" id="selectSource">
<?php
require_once "php/connect.php";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0){
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
echo "<option value=\"".$r['id']."\">".$r['text']."</option>";
}
$polaczenie->close();
}
?>
</select>
</div>
<div class="left_element_second">
<p>Wybierz nowego rodzica:</p>
<select name="selectDestination" id="selectDestination">
<?php
require_once "php/connect.php";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
echo "<option value=\"".$r['id']."\">".$r['text']."</option>";
}
$polaczenie->close();
}
?>
</select>
</div>
<div class="left_element_button">
<input type="submit" name="button_move" value="Przenieś" class="left_button">
</div>
</form>
</div>
<div class="left_element">
<form name="formRename" action="./index.php" onsubmit="return validateFormRename();" method="post">
<input type="hidden" name="form_type" value="form_rename">
<div class="left_element_info"><p>Zmień nazwe węzła</p></div>
<div class="left_element_first">
<p>Wybierz węzeł:</p>
<select name="selectRename" id="selectRename">
<?php
require_once "php/connect.php";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
echo "<option value=\"".$r['id']."\">".$r['text']."</option>";
}
$polaczenie->close();
}
?>
</select>
</div>
<div class="left_element_second">
<p>Nowa nazwa węzła: </p>
<div class="left_element_first">
<input type="text" name="newName">
</div>
</div>
<div class="left_element_button">
<input type="submit" name="button_rename" value="Zatwierdź zmianę" class="left_button">
</div>
</form>
</div>
<div class="left_element2">
<form name="deleteForm" action="./index.php" method="post">
<input type="hidden" name="form_type" value="form_delete">
<div class="left_element_info"><p>Usuń wezęł</p></div>
<div class="left_element_first2">
<p>Wybierz węzeł: </p>
<select name="selectDelete" id="selectDelete">
<?php
require_once "php/connect.php";
$polaczenie = @new mysqli($servername, $username, $password, $dbname);
if ($polaczenie->connect_errno != 0) {
echo "Error" . $polaczenie->connect_errno . " Opis: " . $polaczenie->connect_error;
} else {
$sql= "select * from countries";
$wynik = $polaczenie->query($sql);
while ($r = $wynik->fetch_assoc()) {
echo "<option value=\"".$r['id']."\">".$r['text']."</option>";
}
$polaczenie->close();
}
?>
</select>
</div>
<div class="left_element_button2">
<input type="submit" name="button_delete" value="Usuń" class="left_button">
</div>
</form>
</div>
<div class="left_element2">
<form action="./index.php" method="post">
<div class="left_element_info"><p>Sortowanie nazwe węzła</p></div>
<div class="left_element_button2">
<input type="submit" default name="button_change" value="Brak sortowania" class="left_button">
</div>
<div class="left_element_button2">
<input type="submit" name="button_change" value="Rosnąco" class="left_button">
</div>
<div class="left_element_button2">
<input type="submit" name="button_change" value="Malejąco" class="left_button">
</div>
</form>
</div>
</div>
<div class="right">
<div class="right_element">
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['button_change'] == 'Brak sortowania') {
childs(0);
}else if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['button_change'] == 'Rosnąco') {
childs1(0);
}else if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['button_change'] == 'Malejąco') {
childs2(0);
}else{
childs1(0);
}
?>
</div>
</div>
</div>
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = 20*content.scrollHeight + "px";
}
});
}
</script>
</body>
</html>