-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_people.php
69 lines (62 loc) · 2.72 KB
/
add_people.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
<?php
include 'db.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$grant_id = $_POST['grant_id'];
$user_id = $_POST['user_id'];
$role = $_POST['role'];
$status = 'pending';
$checkQuery = $conn->prepare("SELECT * FROM grant_users WHERE grant_id = ? AND user_id = ?");
if (!$checkQuery) {
die("Error preparing statement for checking member: " . $conn->error);
}
$checkQuery->bind_param('ii', $grant_id, $user_id);
$checkQuery->execute();
$result = $checkQuery->get_result();
if ($result->num_rows > 0) {
$existingMember = $result->fetch_assoc();
if ($existingMember['status'] === 'rejected') {
$updateStmt = $conn->prepare("UPDATE grant_users SET status = ?, role = ? WHERE grant_id = ? AND user_id = ?");
if (!$updateStmt) {
die("Error preparing statement for updating member: " . $conn->error);
}
$updateStmt->bind_param('ssii', $status, $role, $grant_id, $user_id);
$updateStmt->execute();
$message = "You have been re-invited to join the grant: {$grant_id} as a $role.";
$stmtNotification = $conn->prepare("INSERT INTO notifications (user_id, message, grant_id) VALUES (?, ?, ?)");
if (!$stmtNotification) {
die("Error preparing statement for adding notification: " . $conn->error);
}
$stmtNotification->bind_param('isi', $user_id, $message, $grant_id);
$stmtNotification->execute();
header("Location: manage_people.php?grant_id=$grant_id&reinvited=1");
exit();
} else {
echo "<p>User is already a member of this grant.</p>";
exit();
}
} else {
$stmt = $conn->prepare("INSERT INTO grant_users (grant_id, user_id, role, status) VALUES (?, ?, ?, ?)");
if (!$stmt) {
die("Error preparing statement for inserting member: " . $conn->error);
}
$stmt->bind_param('iiss', $grant_id, $user_id, $role, $status);
$stmt->execute();
$message = "You have been invited to join the grant: {$grant_id} as a $role.";
$stmtNotification = $conn->prepare("INSERT INTO notifications (user_id, message, grant_id) VALUES (?, ?, ?)");
if (!$stmtNotification) {
die("Error preparing statement for adding notification: " . $conn->error);
}
$stmtNotification->bind_param('isi', $user_id, $message, $grant_id);
$stmtNotification->execute();
header("Location: manage_people.php?grant_id=$grant_id&added=1");
exit();
}
} else {
echo "<p>Invalid request method.</p>";
exit();
}
?>