feat(stops) caching stops

main
Valentin 2024-06-14 16:59:36 +02:00
parent 1d20fd7815
commit cf5f96f47b
7 changed files with 79 additions and 57 deletions

View File

@ -16,8 +16,6 @@ function getUsers() {
} }
} }
$users = getUsers();
function updateUserDetails($userId, $email, $firstName, $lastName, $is_admin, $password = null) { function updateUserDetails($userId, $email, $firstName, $lastName, $is_admin, $password = null) {
global $conn; global $conn;
try { try {
@ -46,39 +44,9 @@ function deleteUser($userId) {
} }
} }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['userId'])) { $users = getUsers();
if (isset($_POST['delete'])) {
if ($_POST['userId'] == $_SESSION['user_id']) {
echo 222;
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">You cannot delete yourself.</div>';
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
$success = deleteUser($_POST['userId']);
if ($success) {
$_SESSION['message'] = '<div class="alert alert-success text-center" role="alert">User deleted successfully.</div>';
} else {
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">Failed to delete user.</div>';
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
$password = !empty($_POST['password']) ? $_POST['password'] : null; include 'post.php';
$_POST['is_admin'] = isset($_POST['is_admin']) ? 1 : 0;
$success = updateUserDetails($_POST['userId'], $_POST['email'], $_POST['firstName'], $_POST['lastName'], $_POST['is_admin'], $password);
if ($success) {
$_SESSION['message'] = '<div class="alert alert-success text-center" role="alert">User updated successfully.</div>';
} else {
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">Failed to update user.</div>';
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
?> ?>

37
components/admin/post.php Normal file
View File

@ -0,0 +1,37 @@
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['userId'])) {
if (isset($_POST['delete'])) {
if ($_POST['userId'] == $_SESSION['user_id']) {
echo 222;
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">You cannot delete yourself.</div>';
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
$success = deleteUser($_POST['userId']);
if ($success) {
$_SESSION['message'] = '<div class="alert alert-success text-center" role="alert">User deleted successfully.</div>';
} else {
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">Failed to delete user.</div>';
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
$password = !empty($_POST['password']) ? $_POST['password'] : null;
$_POST['is_admin'] = isset($_POST['is_admin']) ? 1 : 0;
$success = updateUserDetails($_POST['userId'], $_POST['email'], $_POST['firstName'], $_POST['lastName'], $_POST['is_admin'], $password);
if ($success) {
$_SESSION['message'] = '<div class="alert alert-success text-center" role="alert">User updated successfully.</div>';
} else {
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">Failed to update user.</div>';
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
?>

View File

@ -29,7 +29,7 @@ $lineIds = $query->fetchAll(PDO::FETCH_COLUMN);
?> ?>
<div class="px-4 my-5 text-center"> <div class="px-4 my-5 text-center">
<h1 class="display-5 fw-bold">Subway Schedule</h1> <h1 class="display-5 fw-bold">Subways</h1>
<div class="col-lg-6 mx-auto"> <div class="col-lg-6 mx-auto">
<p class="lead mb-4">Displaying your favorite stations and lines below</p> <p class="lead mb-4">Displaying your favorite stations and lines below</p>
@ -46,7 +46,6 @@ if (empty($lineIds)) {
} }
?> ?>
<script> <script>
function removeFavorite(stopId, lineId) { function removeFavorite(stopId, lineId) {

View File

@ -12,6 +12,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['first_name'] = $user['first_name']; $_SESSION['first_name'] = $user['first_name'];
$_SESSION['is_admin'] = $user['is_admin']; $_SESSION['is_admin'] = $user['is_admin'];
header("Location: /"); header("Location: /");
$log = $conn->prepare("INSERT INTO logs (userId, date) VALUES (:userId, :date)");
$log->bindParam(':userId', $user['id']);
$log->bindParam(':date', date('Y-m-d-H-i-s'));
$log->execute();
} else { } else {
$errorMessage = "Invalid email or password."; $errorMessage = "Invalid email or password.";
} }

View File

@ -1,12 +1,10 @@
<?php <?php
function getStops($line) { function getStops($lineId) {
$json = file_get_contents(__DIR__ . '/../../data/stops.json'); global $conn;
$data = json_decode($json, true); $stmt = $conn->prepare("SELECT * FROM stops WHERE lineId = ?");
$result = array_filter($data, function($item) use ($line) { $stmt->execute([$lineId]);
return $item['fields']['mode'] === 'METRO' && $item['fields']['indice_lig'] === "$line"; return $stmt->fetchAll();
});
return $result;
} }
function isFavorite($userId, $stopId, $lineId) { function isFavorite($userId, $stopId, $lineId) {
@ -16,6 +14,7 @@ function isFavorite($userId, $stopId, $lineId) {
return $stmt->rowCount() > 0; return $stmt->rowCount() > 0;
} }
?> ?>
<div class="px-4 my-5 text-center"> <div class="px-4 my-5 text-center">
@ -29,7 +28,11 @@ function isFavorite($userId, $stopId, $lineId) {
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
<?php for ($i = 1; $i <= 14; $i++): ?> <?php
for ($i = 1; $i <= 14; $i++):
$stops = getStops($i);
if (!empty($stops)):
?>
<div class="col-2 mb-3"> <div class="col-2 mb-3">
<div class="card h-100"> <div class="card h-100">
<div class="card-body"> <div class="card-body">
@ -44,9 +47,12 @@ function isFavorite($userId, $stopId, $lineId) {
<button type="button" class="btn-close" data-bs-dismiss="modal"></button> <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<?php $stations = getStops($i); ?>
<div class="row"> <div class="row">
<?php include 'stop_list.php'; ?> <?php
$stops = getStops($i);
include 'stop_list.php';
?>
</div> </div>
</div> </div>
</div> </div>
@ -58,7 +64,7 @@ function isFavorite($userId, $stopId, $lineId) {
<?php if ($i % 6 === 0): ?> <?php if ($i % 6 === 0): ?>
</div><div class="row"> </div><div class="row">
<?php endif; ?> <?php endif; ?>
<?php endfor; ?> <?php endif; endfor; ?>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,10 +1,8 @@
<div class="row"> <div class="row">
<?php <?php
$half = ceil(count($stations) / 2); $chunks = array_chunk($stops, ceil(count($stops) / 2), true);
$chunks = array_chunk($stations, $half, true);
?>
<?php foreach ($chunks as $chunk): ?> foreach ($chunks as $chunk): ?>
<div class="col-md-6"> <div class="col-md-6">
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
@ -16,12 +14,12 @@
<tbody> <tbody>
<?php foreach ($chunk as $station): ?> <?php foreach ($chunk as $station): ?>
<tr> <tr>
<td><?php echo htmlspecialchars($station['fields']['nom_zda']); ?></td> <td><?php echo htmlspecialchars($station['name']); ?></td>
<td class="text-end"> <td class="text-end">
<?php if (isFavorite($_SESSION['user_id'], $station['fields']['id_ref_zda'], $i)): ?> <?php if (isFavorite($_SESSION['user_id'], $station['id'], $i)): ?>
<button class="btn btn-danger remove-stop" data-station-id="<?= $station['fields']['id_ref_zda'] ?>" data-line-id="<?= $i ?>">Retirer</button> <button class="btn btn-danger remove-stop" data-station-id="<?= $station['id'] ?>" data-line-id="<?= $i ?>">Revoke</button>
<?php else: ?> <?php else: ?>
<button class="btn btn-success add-stop" data-station-id="<?= $station['fields']['id_ref_zda'] ?>" data-line-id="<?= $i ?>">Ajouter</button> <button class="btn btn-success add-stop" data-station-id="<?= $station['id'] ?>" data-line-id="<?= $i ?>">Add</button>
<?php endif; ?> <?php endif; ?>
</td> </td>
</tr> </tr>

View File

@ -15,14 +15,23 @@ try {
)"); )");
$conn->exec("CREATE TABLE IF NOT EXISTS favorites ( $conn->exec("CREATE TABLE IF NOT EXISTS favorites (
id INT AUTO_INCREMENT PRIMARY KEY,
userId INT NOT NULL, userId INT NOT NULL,
stopId INT NOT NULL, stopId INT NOT NULL,
lineId INT NOT NULL, lineId INT NOT NULL,
FOREIGN KEY (userId) REFERENCES users(id) FOREIGN KEY (userId) REFERENCES users(id)
)"); )");
$conn->exec("CREATE TABLE IF NOT EXISTS logs (
userId INT NOT NULL,
date DATETIME NOT NULL,
FOREIGN KEY (userId) REFERENCES users(id)
)");
$conn->exec("CREATE TABLE IF NOT EXISTS stops (
id INT NOT NULL PRIMARY KEY,
lineId INT NOT NULL,
name VARCHAR(255) NOT NULL
)");
} catch(PDOException $e) { } catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage(); echo "Connection failed: " . $e->getMessage();