Skip to content

Commit

Permalink
feat(home) displaying cached stations
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmrval committed Jun 14, 2024
1 parent cf5f96f commit 387bc0a
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 35 deletions.
31 changes: 3 additions & 28 deletions components/admin/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,6 @@ function getUsers() {
}
}

function updateUserDetails($userId, $email, $firstName, $lastName, $is_admin, $password = null) {
global $conn;
try {
if ($password) {
$query = $conn->prepare("UPDATE users SET email = ?, firstName = ?, lastName = ?, password = ?, is_admin = ? WHERE id = ?");
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query->execute([$email, $firstName, $lastName, $hashedPassword, $is_admin, $userId]);
} else {
$query = $conn->prepare("UPDATE users SET email = ?, firstName = ?, lastName = ?, is_admin = ? WHERE id = ?");
$query->execute([$email, $firstName, $lastName, $is_admin, $userId]);
}
return true;
} catch(PDOException $e) {
return false;
}
}

function deleteUser($userId) {
global $conn;
try {
$query = $conn->prepare("DELETE FROM users WHERE id = ?");
$query->execute([$userId]);
return true;
} catch(PDOException $e) {
return false;
}
}

$users = getUsers();

include 'post.php';
Expand All @@ -56,6 +28,9 @@ function deleteUser($userId) {
unset($_SESSION['message']);
?>
<h2 class="mb-4">Administration</h2>
<form method="POST">
<button type="submit" name="refreshData" class="btn btn-primary">Refresh Data</button>
</form>
<?php include 'users_list.php'; ?>
</div>

Expand Down
78 changes: 77 additions & 1 deletion components/admin/post.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,84 @@
<?php


function updateUserDetails($userId, $email, $firstName, $lastName, $is_admin, $password = null) {
global $conn;
try {
if ($password) {
$query = $conn->prepare("UPDATE users SET email = ?, firstName = ?, lastName = ?, password = ?, is_admin = ? WHERE id = ?");
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query->execute([$email, $firstName, $lastName, $hashedPassword, $is_admin, $userId]);
} else {
$query = $conn->prepare("UPDATE users SET email = ?, firstName = ?, lastName = ?, is_admin = ? WHERE id = ?");
$query->execute([$email, $firstName, $lastName, $is_admin, $userId]);
}
return true;
} catch(PDOException $e) {
return false;
}
}

function deleteUser($userId) {
global $conn;
try {
$query = $conn->prepare("DELETE FROM users WHERE id = ?");
$query->execute([$userId]);
return true;
} catch(PDOException $e) {
return false;
}
}

function refreshData() {
global $conn;
try {

$conn->exec("TRUNCATE TABLE stops");

$json = @file_get_contents(__DIR__ .'/../../data/stops.json');
$data = json_decode($json, true);

$filteredData = array_filter($data, function($item) {
return isset($item['fields']['mode']) && $item['fields']['mode'] === 'METRO';
});

$query = $conn->prepare("INSERT INTO stops (stopId, name, lineId) VALUES (?, ?, ?)");
$conn->beginTransaction();
foreach ($filteredData as $item) {
$fields = $item['fields'];
if (isset($fields['id_ref_zda'], $fields['nom_zda'], $fields['indice_lig'])) {
try {
$query->execute([$fields['id_ref_zda'], $fields['nom_zda'], $fields['indice_lig']]);
} catch (PDOException $e) {
}
}
}

$conn->commit();

return true;
} catch (Exception $e) {
return false;
}
}


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['refreshData'])) {
$success = refreshData();
if ($success) {
$_SESSION['message'] = '<div class="alert alert-success text-center" role="alert">Data refreshed successfully.</div>';
} else {
$_SESSION['message'] = '<div class="alert alert-danger text-center" role="alert">Failed to refresh data.</div>';
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}

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();
Expand All @@ -17,6 +92,7 @@
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}


$password = !empty($_POST['password']) ? $_POST['password'] : null;
$_POST['is_admin'] = isset($_POST['is_admin']) ? 1 : 0;
Expand Down
2 changes: 1 addition & 1 deletion components/homepage/stop.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if (isset($data['Siri']['ServiceDelivery']['StopMonitoringDelivery'][0]['MonitoredStopVisit'])) {
foreach ($data['Siri']['ServiceDelivery']['StopMonitoringDelivery'][0]['MonitoredStopVisit'] as $visit) {
$vehicleJourney = $visit['MonitoredVehicleJourney'];
if (strpos($vehicleJourney['OperatorRef']['value'], '.' . $lineId . '.' . $lineId) !== false) {
if (isset($vehicleJourney['MonitoredCall']['ExpectedArrivalTime'])) {
$direction = $vehicleJourney['DirectionName'][0]['value'];
$expectedArrival = $vehicleJourney['MonitoredCall']['ExpectedArrivalTime'];
$expectedDeparture = $vehicleJourney['MonitoredCall']['ExpectedDepartureTime'];
Expand Down
6 changes: 3 additions & 3 deletions components/navigate/stop_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<tr>
<td><?php echo htmlspecialchars($station['name']); ?></td>
<td class="text-end">
<?php if (isFavorite($_SESSION['user_id'], $station['id'], $i)): ?>
<button class="btn btn-danger remove-stop" data-station-id="<?= $station['id'] ?>" data-line-id="<?= $i ?>">Revoke</button>
<?php if (isFavorite($_SESSION['user_id'], $station['stopId'], $i)): ?>
<button class="btn btn-danger remove-stop" data-station-id="<?= $station['stopId'] ?>" data-line-id="<?= $i ?>">Revoke</button>
<?php else: ?>
<button class="btn btn-success add-stop" data-station-id="<?= $station['id'] ?>" data-line-id="<?= $i ?>">Add</button>
<button class="btn btn-success add-stop" data-station-id="<?= $station['stopId'] ?>" data-line-id="<?= $i ?>">Add</button>
<?php endif; ?>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion components/structure/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
}
?>
</head>
<body style="display:block;">
<body style="display:none;">
Loading

0 comments on commit 387bc0a

Please sign in to comment.