-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_settings.php
More file actions
executable file
·106 lines (87 loc) · 3.63 KB
/
Copy pathnotification_settings.php
File metadata and controls
executable file
·106 lines (87 loc) · 3.63 KB
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
<?php
$page_title = "Notification Settings";
include 'includes/header.php';
// Redirect if not logged in
if (!is_logged_in()) {
header("Location: login.php");
exit;
}
$user_id = $_SESSION['user_id'];
// Get current preferences
$query = "SELECT * FROM notification_preferences WHERE user_id = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
// If no preferences set, create default
if (mysqli_num_rows($result) == 0) {
$insert = "INSERT INTO notification_preferences (user_id) VALUES (?)";
$stmt = mysqli_prepare($conn, $insert);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
// Get default preferences
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
}
$prefs = mysqli_fetch_assoc($result);
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$comment_notifications = isset($_POST['comment_notifications']) ? 1 : 0;
$upvote_notifications = isset($_POST['upvote_notifications']) ? 1 : 0;
$downvote_notifications = isset($_POST['downvote_notifications']) ? 1 : 0;
$update = "UPDATE notification_preferences SET
comment_notifications = ?,
upvote_notifications = ?,
downvote_notifications = ?
WHERE user_id = ?";
$stmt = mysqli_prepare($conn, $update);
mysqli_stmt_bind_param($stmt, "iiii", $comment_notifications, $upvote_notifications, $downvote_notifications, $user_id);
if (mysqli_stmt_execute($stmt)) {
$success = "Notification settings updated successfully";
// Refresh preferences
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$prefs = mysqli_fetch_assoc($result);
} else {
$error = "Error updating settings";
}
}
?>
<div class="form-container">
<h2 class="form-title">Notification Settings</h2>
<?php if (isset($success)): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form method="post" action="">
<div class="form-group">
<label class="checkbox-label">
<input type="checkbox" name="comment_notifications" <?php echo $prefs['comment_notifications'] ? 'checked' : ''; ?>>
Notify me when someone comments on my posts
</label>
</div>
<div class="form-group">
<label class="checkbox-label">
<input type="checkbox" name="upvote_notifications" <?php echo $prefs['upvote_notifications'] ? 'checked' : ''; ?>>
Notify me when someone upvotes my posts or comments
</label>
</div>
<div class="form-group">
<label class="checkbox-label">
<input type="checkbox" name="downvote_notifications" <?php echo $prefs['downvote_notifications'] ? 'checked' : ''; ?>>
Notify me when someone downvotes my posts or comments
</label>
</div>
<button type="submit" class="btn btn-block">Save Settings</button>
<div class="form-footer">
<a href="profile.php">Back to Profile</a>
</div>
</form>
</div>
<?php include 'includes/footer.php'; ?>