-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgalleries_list.php
117 lines (117 loc) · 5.81 KB
/
galleries_list.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
<!DOCTYPE html>
<html lang="ru">
<head>
<title>Список галлерей</title>
<meta charset='utf-8'>
<link rel="stylesheet" href="../Style/gallery_stylesheet.css"> <!-- Баловался css'ом, так или иначе пригодится ещё. Береги глаза, там пиздец. -->
</head>
<body>
<?php
require_once("connect_db.php");//скрипт с коннектом бд
@$user = $_COOKIE['admin_flag'];
@$username = $_COOKIE['username'];
$query = "SELECT * FROM galleries"; //формируем запрос
$buff = $pdo->query($query);
try{
$catalog = $buff->fetchAll(PDO::FETCH_ASSOC); //получаем ответ
} catch (PDOException $e){
echo "Если Вы это видите - произошла какая-то хрень. Ошибка выполнения запроса: ".$e->getMessage();
}
if (!isset($_REQUEST['Action']))
{
?>
<header>
<h2 class="title" id="title">
<span>Галлереи:</span>
<?php if($username){ ?>
<div class="LogOut">
<input type="text" name="username" value="<?=" Выполнен вход как: ".$username?>" id="LogedAs"><br />
<input type="submit" name="Action" value="Log out" form="Galleries" style="float: right;">
<?php } else {?>
<input type="text" name="username" value="<?=" Вход не выполнен."?>" id="LogedAs" style="float: right;"><br />
<?php } ?>
</div>
</h2>
</header>
<form id="Galleries" action = "<?=$_SERVER['SCRIPT_NAME']?>" method="post">
<?php if($user == '1') // решил таким образом отделить админский функционал от обычного. Топорно, но работает.
{?>
<select name="GalleryList" form="Galleries" multiple>
<?php foreach($catalog as $log):?>
<option><?=$log['title']."<br />";?></option>
<?php endforeach;?>
</select><br />
<p><input type="submit" name="Action" value="Open gallery" form="Galleries" >
<input type="submit" name="Action" value="Create new gallery" form="Galleries"></p>
<input type="submit" name="Action" value="Delete gallery" form="Galleries" >
<!-- ----------------------------------------------------------------------------------------------- -->
<?php } else {?> <!-- Здесь начинается пользовательсткий -->
<input type="submit" name="Action" value="Create new gallery" form="Galleries" >
<ul name="GalleryList" form="Galleries">
<?php foreach($catalog as $log):?> <!-- А вот и ссылочки. Превьюшек для галлерей пока нет, так что там везде alt'ы будут -->
<li><a href="gallery_content.php?gallery_id=<?=$log['id']?>"><img src="<?=$log['image_path']?>" alt="<?=$log['title']?>" height="150" width="200"></a></li>
<?php endforeach;?>
</ul><br />
<a class="rewind_up" href="#title" title="Наверх"> ↑ </a>
<?php } ?>
</form>
<p id="footer">
© 2021, Gallery web app<br>
All trademarks and registered trademarks appearing on this site are
the property of their respective owners.
</p>
<?php
}
else
{
$gallery = @$_REQUEST['GalleryList'];
$query = "SELECT id FROM galleries WHERE title='$gallery'";
$gallery = $pdo->query($query);
$gallery = $gallery->fetchAll(PDO::FETCH_ASSOC); //ответ приходит в виде двумерного массива, не нашёл/не вспомнил,
$gallery = $gallery['0']; //как его адекватно разобрать, поэтому так.
$gallery = $gallery['id'];
switch($_REQUEST['Action'])
{
case 'Open gallery':
if(!$gallery)
echo "Вы не выбрали галлерею для открытия.";
else
{
$dir = dirname($_SERVER['SCRIPT_NAME']);
if ($dir=='\\') $dir='';
header("Location: $dir/gallery_content.php?gallery_id=$gallery");
}
break;
case 'Create new gallery':
$dir = dirname($_SERVER['SCRIPT_NAME']);
if ($dir=='\\') $dir='';
header("Location: $dir/create_new_gallery.php");
break;
case 'Delete gallery':
$value = $_REQUEST['GalleryList'];
$query = "SELECT id FROM pictures where gallery_id='$gallery'";//вытаскиваем id'шик галлереи
$buff = $pdo->query($query);
$catalog = $buff->fetchAll(PDO::FETCH_ASSOC);
foreach($catalog as $picture)//удаляем все комменты под каждой фоткой (иначе всё падает из-за ключей)
{
$id = $picture['id'];
echo $id." ";
$query = "DELETE FROM comments where image_id='$id'";
$pdo->exec($query);
}
$query = "DELETE FROM pictures where gallery_id='$gallery'";
$pdo->exec($query);//удаляем все фотки
$query = "DELETE FROM galleries where title='$value'";
$pdo->exec($query);//удаляем саму галерею
header("location: ". $_SERVER['PHP_SELF']);
break;
case 'Log out':
$dir = dirname($_SERVER['SCRIPT_NAME']);
if ($dir=='\\') $dir='';
header("Location: $dir/index.php");
break;
}
}
?>
</body>
</html>