feat(notifications): add browser push notifications for calendar events with personal profile settings
This commit is contained in:
@@ -80,6 +80,24 @@ function ensure_remember_token_columns(PDO $db): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Garandeert dat de kolom voor notificatie-instellingen aanwezig is in MariaDB
|
||||
*/
|
||||
function ensure_notification_columns(PDO $db): void {
|
||||
static $notifColumnsChecked = false;
|
||||
if ($notifColumnsChecked) return;
|
||||
$notifColumnsChecked = true;
|
||||
|
||||
try {
|
||||
$result = $db->query("SHOW COLUMNS FROM `users` LIKE 'notification_settings'");
|
||||
if (!$result || $result->rowCount() === 0) {
|
||||
$db->exec("ALTER TABLE `users` ADD COLUMN `notification_settings` TEXT NULL AFTER `remember_token_expires_at`");
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// Stil negeren indien al aanwezig
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Genereert en bewaart een persistente 'Onthoud mij' token in MariaDB en plaatst een veilige cookie
|
||||
*/
|
||||
@@ -324,4 +342,61 @@ function verify_oauth_state(string $state): ?int {
|
||||
return (int)$payload['uid'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Haalt de persoonlijke notificatie-instellingen van een gebruiker op
|
||||
*/
|
||||
function get_user_notification_settings(int $userId): array {
|
||||
$db = get_db_connection();
|
||||
ensure_notification_columns($db);
|
||||
|
||||
$defaults = [
|
||||
'enabled' => 1,
|
||||
'lead_time_minutes' => 5, // 0, 2, 5, 10, 15, 30
|
||||
'repeat_count' => 2, // 1, 2, 3, 5
|
||||
'repeat_interval_minutes' => 2, // 1, 2, 3, 5
|
||||
'sound_enabled' => 1, // 0 of 1
|
||||
];
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("SELECT notification_settings FROM users WHERE id = ?");
|
||||
$stmt->execute([$userId]);
|
||||
$json = $stmt->fetchColumn();
|
||||
if (!empty($json)) {
|
||||
$saved = json_decode($json, true);
|
||||
if (is_array($saved)) {
|
||||
return array_merge($defaults, $saved);
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
error_log("get_user_notification_settings error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Slaat de persoonlijke notificatie-instellingen van een gebruiker op
|
||||
*/
|
||||
function update_user_notification_settings(int $userId, array $settings): bool {
|
||||
$db = get_db_connection();
|
||||
ensure_notification_columns($db);
|
||||
|
||||
$current = get_user_notification_settings($userId);
|
||||
$merged = [
|
||||
'enabled' => !empty($settings['enabled']) ? 1 : 0,
|
||||
'lead_time_minutes' => max(0, min(60, (int)($settings['lead_time_minutes'] ?? $current['lead_time_minutes']))),
|
||||
'repeat_count' => max(1, min(10, (int)($settings['repeat_count'] ?? $current['repeat_count']))),
|
||||
'repeat_interval_minutes' => max(1, min(30, (int)($settings['repeat_interval_minutes'] ?? $current['repeat_interval_minutes']))),
|
||||
'sound_enabled' => !empty($settings['sound_enabled']) ? 1 : 0,
|
||||
];
|
||||
|
||||
try {
|
||||
$stmt = $db->prepare("UPDATE users SET notification_settings = ? WHERE id = ?");
|
||||
return $stmt->execute([json_encode($merged), $userId]);
|
||||
} catch (Throwable $e) {
|
||||
error_log("update_user_notification_settings error: " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user