-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
154 lines (143 loc) · 6.69 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
<?php
// boilerplate index
require_once('functions.php');
require_once('db.php');
$page = $_GET['page'] ?? 'home';
$flash = [];
if (isset($_SESSION['flash'])) {
$flash = $_SESSION['flash'];
unset($_SESSION['flash']);
}
// debug($flash);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Лаптопи</title>
<!-- Bootstrap 5.3 CSS CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<!-- jquery CDN -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</head>
<body>
<script>
$(function() {
// добавяне
$(document).on('click', '.add-favorite', function() {
let btn = $(this);
let productId = btn.data('product');
$.ajax({
url: './ajax/add_favorite.php',
method: 'POST',
data: {
product_id: productId
},
success: function(response) {
let res = JSON.parse(response);
if (res.success) {
alert('Продуктът беше добавен в любими');
let removeBtn = $(`<button type="button" class="btn btn-sm btn-danger remove-favorite" data-product="${productId}">Премахни от любими</button>`);
btn.replaceWith(removeBtn);
} else {
alert(res.error);
}
},
error: function(error) {
console.error(error);
}
});
});
// премахване
$(document).on('click', '.remove-favorite', function() {
let btn = $(this);
let productId = btn.data('product');
$.ajax({
url: './ajax/remove_favorite.php',
method: 'POST',
data: {
product_id: productId
},
success: function(response) {
let res = JSON.parse(response);
if (res.success) {
alert('Продуктът беше премахнат от любими');
let addBtn = $(`<button type="button" class="btn btn-sm btn-primary add-favorite" data-product="${productId}">Добави в любими</button>`);
btn.replaceWith(addBtn);
} else {
alert(res.error);
}
},
error: function(error) {
console.error(error);
}
});
});
});
</script>
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark py-3">
<div class="container-fluid">
<a class="navbar-brand fw-bold" href="#">Лаптопи</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link <?php echo($page == 'home' ? 'active' : '') ?>" aria-current="page" href="?page=home">Начало</a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo($page == 'products' ? 'active' : '') ?>" href="?page=products">Продукти</a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo($page == 'contacts' ? 'active' : '') ?>" href="?page=contacts">Контакти</a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo($page == 'add_product' ? 'active' : '') ?>" href="?page=add_product">Добави продукт</a>
</li>
</ul>
<div class="d-flex flex-row align-items-center gap-3">
<?php
if (isset($_SESSION['username'])) {
echo '<span class="text-light me-3">Здравей, ' . htmlspecialchars($_SESSION['username']) . '</span>';
echo '
<form method="POST" action="./handlers/handle_logout.php" class="m-0">
<button type="submit" class="btn btn-outline-light">Изход</button>
</form>
';
} else {
echo '<a href="?page=login" class="btn btn-outline-light">Вход</a>';
echo '<a href="?page=register" class="btn btn-outline-light">Регистрация</a>';
}
?>
</div>
</div>
</div>
</nav>
</header>
<main class="container py-4" style="min-height:80vh;">
<?php
if (isset($flash['message'])) {
echo '
<div class="alert alert-' . $flash['message']['type'] . '">
' . $flash['message']['text'] . '
</div>
';
}
if (file_exists("pages/$page.php")) {
require_once("pages/$page.php");
} else {
require_once("pages/not_found.php");
}
?>
</main>
<footer class="bg-dark text-center py-5 mt-auto">
<div class="container">
<span class="text-light">© 2024 All rights reserved</span>
</div>
</footer>
</body>
</html>