Add ability to join and leave groups, get row id in a way supported by both mysql and sqlite

This commit is contained in:
Peder Bergebakken Sundt 2018-08-09 00:49:49 +02:00
parent a63bc3e6a9
commit b965da0a6c
4 changed files with 43 additions and 7 deletions

View File

@ -115,4 +115,4 @@ class ProjectManager{
return $owner; return $owner;
} }
} }

View File

@ -97,4 +97,4 @@
.newuserelement input { .newuserelement input {
border: none; border: none;
background-color: #ddd; background-color: #ddd;
} }

View File

@ -8,7 +8,6 @@ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$motdfetcher = new \pvv\side\MOTD($pdo); $motdfetcher = new \pvv\side\MOTD($pdo);
$motd = $motdfetcher->getMOTD(); $motd = $motdfetcher->getMOTD();
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="no"> <html lang="no">
<head> <head>

View File

@ -18,6 +18,7 @@ $attrs = $as->getAttributes();
$id = $_POST['id']; $id = $_POST['id'];
$do_delete = isset($_POST['delete']); $do_delete = isset($_POST['delete']);
$do_join_or_leave = isset($_POST['join_or_leave']);
$active = $_POST['active']; $active = $_POST['active'];
@ -36,18 +37,54 @@ if($id == 0){
$statement->bindParam(':desc', $desc, PDO::PARAM_STR); $statement->bindParam(':desc', $desc, PDO::PARAM_STR);
$statement->execute(); $statement->execute();
$new_id = $pdo->lastInsertId();
// there's a better way to do this. i just don't know it right now $ownerQuery = "INSERT INTO projectmembers (projectid, name, uname, mail, role, lead, owner) VALUES (:id, :owner, :owneruname, :owneremail, 'Prosjektleder', 1, 1)";
$ownerQuery = 'INSERT INTO projectmembers (projectid, name, uname, mail, role, lead, owner) VALUES (last_insert_rowid(), :owner, :owneruname, :owneremail, \'Prosjektleder\', 1, 1)'; $statement = $pdo->prepare($ownerQuery);
$statement = $pdo->prepare($ownerQuery); $statement->bindParam(':id', $new_id, PDO::PARAM_STR);
$statement->bindParam(':owner', $name, PDO::PARAM_STR); $statement->bindParam(':owner', $name, PDO::PARAM_STR);
$statement->bindParam(':owneruname', $uname, PDO::PARAM_STR); $statement->bindParam(':owneruname', $uname, PDO::PARAM_STR);
$statement->bindParam(':owneremail', $mail, PDO::PARAM_STR); $statement->bindParam(':owneremail', $mail, PDO::PARAM_STR);
$statement->execute(); $statement->execute();
}else{ }
else {
$projectManager = new \pvv\side\ProjectManager($pdo); $projectManager = new \pvv\side\ProjectManager($pdo);
$owner = $projectManager->getProjectOwner($id); $owner = $projectManager->getProjectOwner($id);
$members = $projectManager->getProjectMembers($id);
//if ($do_join_or_leave and $owner['uname'] != $uname) {
if ($do_join_or_leave) {
$is_member = False;
foreach($members as $member){
if ($member['uname'] == $uname and $member['owner']==0){
$is_member = True;
break;
}
}
if ($is_member){//leave
$query = "DELETE FROM projectmembers WHERE projectid=:id AND uname=:uname and lead=0 and owner=0;";
$statement = $pdo->prepare($query);
$statement->bindParam(':id', $id, PDO::PARAM_STR);
$statement->bindParam(':uname', $uname, PDO::PARAM_STR);
$statement->execute();
print("leave");
}
else{//join
$query = "INSERT INTO projectmembers (projectid, name, uname, mail, role, lead, owner) VALUES (:id, :name, :uname, :mail, 'Medlem', 0, 0)";
$statement = $pdo->prepare($query);
$statement->bindParam(':id', $id, PDO::PARAM_STR);
$statement->bindParam(':name', $name, PDO::PARAM_STR);
$statement->bindParam(':uname', $uname, PDO::PARAM_STR);
$statement->bindParam(':mail', $mail, PDO::PARAM_STR);
$statement->execute();
print("join");
}
header('Location: ./info.php?id=' . $id);
exit();
}
if($uname != $owner['uname']){ if($uname != $owner['uname']){
header('Content-Type: text/plain', true, 403); header('Content-Type: text/plain', true, 403);