-
Notifications
You must be signed in to change notification settings - Fork 0
/
teams.php
305 lines (282 loc) · 10.9 KB
/
teams.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
// Import util functions
require("util.php");
checkSession();
dbConnect();
// Initialize creation validity
$postValid = True;
$name = "";
$description = "";
if (empty($_POST)) {
$postValid = False;
} else {
// Get submitted information
$name = htmlspecialchars($_POST["name"]);
$description = htmlspecialchars($_POST["description"]);
// Validate name
if (empty($name) || ctype_space($name)) {
$postValid = False;
$errorMessage = "<p id=\"error\">Name invalid.</p>";
}
}
if (isset($_SESSION["errorMessage"])) {
$errorMessage = $_SESSION["errorMessage"];
unset($_SESSION["errorMessage"]);
}
$teams = [];
$invitations = [];
// Get teams
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE member = :id AND status = 2");
$stmt->bindParam(":id", $id);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$teams[] = $row["team"];
}
// Get received invitations
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE member = :id AND status = 1");
$stmt->bindParam(":id", $id);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$invitations[] = $row["team"];
}
echoHeader(1);
?>
<div id="content">
<div id="teams">
<?php
if (isset($_SESSION["successMessage"])) {
echo($_SESSION["successMessage"]);
unset($_SESSION["successMessage"]);
} else {
if (!empty($_GET["id"])) {
$team = $_GET["id"];
// Display creation form if submitted details are invalid
if ($team == "new") {
if (!$postValid) { ?>
<div id="form">
<form method="post" action=<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>>
<table style="margin: 0 auto;">
<?php if (!empty($errorMessage)) { echo("<tr><td colspan=\"2\">" . $errorMessage . "</td></tr>"); } ?>
<tr>
<td id="label" align="right">Team Name: </td>
<td id="label"><input type="text" name="name" value="<?php if (!empty($name)) { echo($name); } ?>" /></td>
</tr>
<tr>
<td id="label" align="right">Description: </td>
<td id="label"><input type="text" name="description" value="<?php if (!empty($description)) { echo($description); } ?>" /></td>
</tr>
<tr>
<td id="label" align="right"><input type="submit" name="create" value="Create Team" /></td>
</tr>
</table>
</form>
</div>
<?php }
else {
// Ensure team id is unique
$teamid = uniqid();
$stmt = $conn->prepare("SELECT * FROM teams WHERE id = :teamid");
$stmt->bindParam(":teamid", $teamid);
$stmt->execute();
$result = $stmt->fetchAll();
while (!empty($result)) {
$id = uniqid();
$stmt->execute();
$result = $stmt->fetchAll();
}
// Create team
$stmt = $conn->prepare("INSERT INTO teams (id, name, description, owner) VALUES (:teamid, :name, :description, :id)");
$stmt->bindParam(":teamid", $teamid);
$stmt->bindParam(":name", $name);
$stmt->bindParam(":description", $description);
$stmt->bindParam(":id", $id);
$stmt->execute();
// Add user to team
$s = 2;
$stmt = $conn->prepare("INSERT INTO teammembers (member, team, status) VALUES (:id, :teamid, :status)");
$stmt->bindParam(":id", $id);
$stmt->bindParam(":teamid", $teamid);
$stmt->bindParam(":status", $s);
$stmt->execute();
$_SESSION["successMessage"] = "<p id=\"label\">Team successfully created.</p>";
header("Location: " . $_SERVER['REQUEST_URI']);
}
}
// Display team information
else {
// Get team information from id
$stmt = $conn->prepare("SELECT * FROM teams WHERE id = :team");
$stmt->bindParam(":team", $team);
$stmt->execute();
$result = $stmt->fetch();
if (!empty($result)) {
echo("<h2>" . $result["name"] . "</h2>");
if (!empty($_GET["action"]) && $result["owner"] == $id && ($_GET["action"] == "add" || $_GET["action"] == "remove")) {
if ($_GET["action"] == "add") {
// Find contacts
$contacts = getContacts($id);
// Find team members
$members = [];
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE team = :team");
$stmt->bindParam(":team", $team);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$members[] = $row["member"];
}
// Add user if requested
if (!empty($_GET["user"])) {
$un = htmlspecialchars($_GET["user"]);
$result = userFromUsername($un);
if (!empty($result)) {
$u = $result["id"];
if (in_array($u, $contacts) && !in_array($u, $members)) {
// Add user to team
$s = 2;
$stmt = $conn->prepare("INSERT INTO teammembers (member, team, status) VALUES (:u, :team, :status)");
$stmt->bindParam(":u", $u);
$stmt->bindParam(":team", $team);
$stmt->bindParam(":status", $s);
$stmt->execute();
// Update team members
$members = [];
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE team = :team");
$stmt->bindParam(":team", $team);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$members[] = $row["member"];
}
}
}
}
foreach ($contacts as $user) {
if (!in_array($user, $members)) {
$result = userFromID($user);
$name = $result["firstname"] . " " . $result["lastname"];
echo("<div id=addMember>");
echo("<a id=\"user\" href=\"user.php?id=" . $result["username"] . "\">" . $name . "</a>");
echo("<form method=\"get\" action=teams.php>");
echo("<input type=\"hidden\" name=\"id\" value=\"" . $team . "\" />");
echo("<input type=\"hidden\" name=\"action\" value=\"add\" />");
echo("<input type=\"hidden\" name=\"user\" value=\"" . $result["username"] . "\" />");
echo("<input type=\"submit\" value=\"Add Member\" />");
echo("</form>");
echo("</div>");
}
}
}
else if ($_GET["action"] == "remove") {
// Find contacts
$contacts = getContacts($id);
// Find team members
$members = [];
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE team = :team");
$stmt->bindParam(":team", $team);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$members[] = $row["member"];
}
// Remove user if requested
if (!empty($_GET["user"])) {
$un = htmlspecialchars($_GET["user"]);
$result = userFromUsername($un);
if (!empty($result)) {
$u = $result["id"];
if (in_array($u, $members)) {
// Remove user from team
$stmt = $conn->prepare("DELETE FROM teammembers WHERE team = :team AND member = :u");
$stmt->bindParam(":team", $team);
$stmt->bindParam(":u", $u);
$stmt->execute();
// Update team members
$members = [];
$stmt = $conn->prepare("SELECT * FROM teammembers WHERE team = :team");
$stmt->bindParam(":team", $team);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$members[] = $row["member"];
}
}
}
}
foreach ($members as $user) {
$result = userFromID($user);
$name = $result["firstname"] . " " . $result["lastname"];
echo("<div id=removeMember>");
echo("<a id=\"user\" href=\"user.php?id=" . $result["username"] . "\">" . $name . "</a>");
echo("<form method=\"get\" action=teams.php>");
echo("<input type=\"hidden\" name=\"id\" value=\"" . $team . "\" />");
echo("<input type=\"hidden\" name=\"action\" value=\"remove\" />");
echo("<input type=\"hidden\" name=\"user\" value=\"" . $result["username"] . "\" />");
echo("<input type=\"submit\" value=\"Remove Member\" />");
echo("</form>");
echo("</div>");
}
}
}
else {
if ($result["owner"] == $id) {
// Add contact to team
echo("<form style=\"float:left\" method=\"get\" action=teams.php>");
echo("<input type=\"hidden\" name=\"id\" value=\"" . $team . "\" />");
echo("<input type=\"hidden\" name=\"action\" value=\"add\" />");
echo("<input type=\"submit\" value=\"Add Member\" />");
echo("</form>");
// Remove contact from team
echo("<form style=\"float:right\" method=\"get\" action=teams.php>");
echo("<input type=\"hidden\" name=\"id\" value=\"" . $team . "\" />");
echo("<input type=\"hidden\" name=\"action\" value=\"remove\" />");
echo("<input type=\"submit\" value=\"Remove Member\" />");
echo("</form>");
}
echo("<p id=\"label\">" . $result["description"] . "</p>");
}
}
else {
echo("<p id=\"error\">Team not found.</p>");
}
}
}
else {
// Create new team
echo("<form method=\"get\" action=teams.php>");
echo("<input type=\"hidden\" name=\"id\" value=\"new\" />");
echo("<input type=\"submit\" value=\"Create Team\" />");
echo("</form>");
// Display received invitations
if (!empty($invitations)) {
echo("<h3>Invitations</h3>");
}
foreach ($invitations as $t) {
// Get team information from id
$stmt = $conn->prepare("SELECT * FROM teams WHERE id = :t");
$stmt->bindParam(":t", $t);
$stmt->execute();
$result = $stmt->fetch();
// Display team
echo("<a id=\"team\" href=\"teams.php?id=" . $result["id"] . "\">" . $result["name"] . "</a><br>");
}
// Display teams
if (!empty($teams)) {
echo("<h3>Your Teams</h3>");
}
foreach ($teams as $t) {
// Get team information from id
$stmt = $conn->prepare("SELECT * FROM teams WHERE id = :t");
$stmt->bindParam(":t", $t);
$stmt->execute();
$result = $stmt->fetch();
// Display team
echo("<a id=\"team\" href=\"teams.php?id=" . $result["id"] . "\">" . $result["name"] . "</a><br>");
}
}
} ?>
</div>
</div>
</body>
</html>