Added functionality for add friend buttons.

This commit is contained in:
K. Nobel
2017-01-24 14:36:27 +01:00
parent 449b500636
commit d44ddf2793
6 changed files with 170 additions and 23 deletions

View File

@@ -0,0 +1,30 @@
<?php
session_start();
require("../queries/friendship.php");
require("../queries/user.php");
if(empty($_POST["userID"]) OR empty($_POST["delete"]) AND empty($_POST["accept"]) AND empty($_POST["request"])) {
echo "Not enough arguments.";
return;
}
$friendship_status = getFriendshipStatus($_POST["userID"]);
echo "\nfriendshipstatus: $friendship_status";
echo "You: " . $_SESSION["userID"];
echo "other user: " . $_POST["userID"];
if(!empty($_POST["request"]) AND $friendship_status == 0) {
echo "request";
requestFriendship($_POST["userID"]);
} else if(!empty($_POST["delete"]) AND in_array($friendship_status, array(1, 2, 3))) {
echo "delete";
removeFriendship($_POST["userID"]);
} else if (!empty($_POST["accept"]) AND $friendship_status == 3) {
echo "accept";
acceptFriendship($_POST["userID"]);
}
$username = getUsername($_POST["userID"]);
header("Location: profile.php?username=$username");

View File

@@ -15,11 +15,14 @@ include("../queries/nicetime.php");
if(empty($_GET["username"])) { if(empty($_GET["username"])) {
$userID = $_SESSION["userID"]; $userID = $_SESSION["userID"];
echo "USERNAME NOT GIVEN";
} else { } else {
$userID = getUserID($_GET["username"]); $userID = getUserID($_GET["username"]);
} }
$user = selectUser($userID); echo "User ID: $userID";
$user = selectUser($_SESSION["userID"], $userID);
$profile_friends = selectAllFriends($userID); $profile_friends = selectAllFriends($userID);
$profile_groups = selectAllUserGroups($userID); $profile_groups = selectAllUserGroups($userID);
$posts = selectAllUserPosts($userID); $posts = selectAllUserPosts($userID);

View File

@@ -81,8 +81,9 @@ div.posts .post form textarea.newpost {
font-size: 0.8em; font-size: 0.8em;
} }
.profile-button { input.profile-button {
float: right; float: right;
height: auto;
padding: 10px; padding: 10px;
border-radius: 5px; border-radius: 5px;
background-color: #4CAF50; background-color: #4CAF50;

View File

@@ -1,5 +1,7 @@
<?php <?php
require("connect.php");
function selectAllFriends($userID) { function selectAllFriends($userID) {
$stmt = $GLOBALS["db"]->prepare(" $stmt = $GLOBALS["db"]->prepare("
SELECT SELECT
@@ -21,8 +23,8 @@ function selectAllFriends($userID) {
`friendship`.`user2ID` = `user`.`userID` OR `friendship`.`user2ID` = `user`.`userID` OR
`friendship`.`user2ID` = :userID AND `friendship`.`user2ID` = :userID AND
`friendship`.`user1ID` = `user`.`userID`) AND `friendship`.`user1ID` = `user`.`userID`) AND
`role` != 5 AND `role` != 'banned' AND
`status` = 1 `status` = 'confirmed'
"); ");
$stmt->bindParam(':userID', $userID, PDO::PARAM_INT); $stmt->bindParam(':userID', $userID, PDO::PARAM_INT);
@@ -61,3 +63,75 @@ function selectAllFriendRequests() {
return json_encode($stmt->fetchAll()); return json_encode($stmt->fetchAll());
} }
function getFriendshipStatus($userID) {
$stmt = $GLOBALS["db"]->prepare("
SELECT
CASE `status` IS NULL
WHEN TRUE THEN 0
WHEN FALSE THEN
CASE `status` = 'confirmed'
WHEN TRUE THEN
1
WHEN FALSE THEN
CASE `user1ID` = :me AND `user2ID` = :other
WHEN TRUE THEN
2
WHEN FALSE THEN
3
END
END
END AS `friend_state`
FROM
`friendship`
WHERE
`user1ID` = :other AND `user2ID` = :me OR
`user1ID` = :me AND `user2ID` = :other
");
$stmt->bindParam(':me', $_SESSION["userID"], PDO::PARAM_INT);
$stmt->bindParam(':other', $userID, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch()["friend_state"];
}
function requestFriendship($userID) {
$stmt = $GLOBALS["db"]->prepare("
INSERT INTO `friendship` (user1ID, user2ID)
VALUES (:user1, :user2)
");
$stmt->bindParam(':user1', $_SESSION["userID"], PDO::PARAM_INT);
$stmt->bindParam(':user2', $userID, PDO::PARAM_INT);
$stmt->execute();
}
function removeFriendship($userID) {
$stmt = $GLOBALS["db"]->prepare("
DELETE FROM `friendship`
WHERE
`user1ID` = :user1 AND
`user2ID` = :user2 OR
`user1ID` = :user2 AND
`user2ID` = :user1
");
$stmt->bindParam(':user1', $_SESSION["userID"], PDO::PARAM_INT);
$stmt->bindParam(':user2', $userID, PDO::PARAM_INT);
$stmt->execute();
}
function acceptFriendship($userID) {
$stmt = $GLOBALS["db"]->prepare("
UPDATE `friendship`
SET `status`='confirmed'
WHERE
`user1ID` = :user1 AND
`user2ID` = :user2
LIMIT 1
");
$stmt->bindParam(':user1', $userID, PDO::PARAM_INT);
$stmt->bindParam(':user2', $_SESSION["userID"], PDO::PARAM_INT);
$stmt->execute();
}

View File

@@ -17,27 +17,53 @@ function getUserID($username) {
return $stmt->fetch()["userID"]; return $stmt->fetch()["userID"];
} }
function selectUser($userID) { function getUsername($userID) {
$stmt = $GLOBALS["db"]->prepare(" $stmt = $GLOBALS["db"]->prepare("
SELECT SELECT
`username`, `username`
IFNULL(
`profilepicture`,
'../img/notbad.jpg'
) AS profilepicture,
`bio`,
`role`,
`onlinestatus`,
`loggedin`,
`fname`,
`lname`
FROM FROM
`user` `user`
WHERE WHERE
`userID` = :userID `userID` = :userID
"); ");
$stmt->bindParam(':userID', $userID, PDO::PARAM_INT); $stmt->bindParam(':userID', $userID, PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetch()["username"];
}
function selectUser($me, $other) {
$stmt = $GLOBALS["db"]->prepare("
SELECT
`username`, `birthdate`, `location`, `profilepicture`, `bio`, `user`.`creationdate`, `onlinestatus`, `fname`, `lname`,
CASE `status` IS NULL
WHEN TRUE THEN 0
WHEN FALSE THEN
CASE `status` = 'confirmed'
WHEN TRUE THEN
1
WHEN FALSE THEN
CASE `user1ID` = `userID` AND `user2ID` = :me
WHEN TRUE THEN
2
WHEN FALSE THEN
3
END
END
END AS `friend_status`
FROM
`user`
LEFT JOIN
`friendship`
ON
`user1ID` = `userID` AND `user2ID` = :me OR
`user1ID` = :me AND `user2ID` = `userID`
WHERE
`user`.`userID` = :other
");
$stmt->bindParam(':me', $me, PDO::PARAM_INT);
$stmt->bindParam(':other', $other, PDO::PARAM_INT);
$stmt->execute(); $stmt->execute();
return $stmt->fetch(); return $stmt->fetch();
} }

View File

@@ -1,11 +1,24 @@
<div class="content"> <div class="content">
<div class="profile-box platform"> <div class="profile-box platform">
<img class="left profile-picture" src="<?php echo $user["profilepicture"] ?>"> <img class="left profile-picture" src="<?php echo $user["profilepicture"] ?>">
<div class="profile-button">
<p><img src="/img/add-friend.png"> Als vriend toevoegen</p> <form action="edit_friendship.php" method="post">
</div> <input type="hidden" name="userID" value="<?= $userID ?>">
<h1 class="profile-username"><?=$user["username"]?></h1> <?php
<h5 class="profile-username"><?= $user["fname"]?> <?=$user["lname"]?></h5> if($userID != $_SESSION["userID"] AND $user["friend_status"] == 0) {
echo "<input class='profile-button' type='submit' name='request' value='Stuur vriendschapsverzoek!'>";
} else if($user["friend_status"] == 1) {
echo "<input class='profile-button' type='submit' name='delete' value='Verwijder vriend!'>";
} else if($user["friend_status"] == 2) {
echo "<input class='profile-button' type='submit' name='accept' value='Accepteer vriendschapsverzoek!'>";
echo "<input class='profile-button' type='submit' name='delete' value='Weiger vriendschapsverzoek!'>";
} else if($user["friend_status"] == 3) {
echo "<input class='profile-button' type='submit' name='delete' value='Trek vriendschapsverzoek in!'>";
}
?>
</form>
<h1 class="profile-username"><?= $user["fname"]?> <?=$user["lname"]?></h1>
<h5 class="profile-username"><?=$user["username"]?></h5>
<p><?=$user["bio"]?></p> <p><?=$user["bio"]?></p>
</div> </div>
@@ -14,7 +27,7 @@
<p> <p>
<?php <?php
while($friend = $profile_friends->fetch()) { while($friend = $profile_friends->fetch()) {
echo "<a href='/profile/${friend["username"]}/' data-title='${friend["username"]}'><img class='profile-picture' src='${friend["profilepicture"]}' alt='${friend["username"]}'s profielfoto></a>"; echo "<a href='profile.php?username=${friend["username"]}/' data-title='${friend["username"]}'><img class='profile-picture' src='${friend["profilepicture"]}' alt='${friend["username"]}'s profielfoto></a>";
} }