Merge branch 'lars-chat' into 'master'
Lars chat See merge request !59
This commit was merged in pull request #63.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
<style>
|
<style>
|
||||||
@import url("styles/chat.css");
|
@import url("styles/chat.css");
|
||||||
</style>
|
</style>
|
||||||
|
<script src="js/chat.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?php
|
<?php
|
||||||
|
|||||||
57
website/public/js/chat.js
Normal file
57
website/public/js/chat.js
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
$(document).ready(function() {
|
||||||
|
loadMessages();
|
||||||
|
});
|
||||||
|
|
||||||
|
function loadMessages() {
|
||||||
|
$.post(
|
||||||
|
"loadMessages.php",
|
||||||
|
$("#lastIDForm").serialize()
|
||||||
|
).done(function(data) {
|
||||||
|
if (data && data != "[]") {
|
||||||
|
console.log(data);
|
||||||
|
messages = JSON.parse(data);
|
||||||
|
addMessages(messages);
|
||||||
|
$("#lastID").val(messages[messages.length - 1].messageID);
|
||||||
|
$("#chat-history").scrollTop($("#chat-history")[0].scrollHeight);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(loadMessages, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function sendMessage() {
|
||||||
|
console.log($("#sendMessageForm").serialize());
|
||||||
|
$.post(
|
||||||
|
"sendMessage.php",
|
||||||
|
$("#sendMessageForm").serialize()
|
||||||
|
).done(function( data ) {
|
||||||
|
console.log(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
$("#newContent").val("");
|
||||||
|
}
|
||||||
|
|
||||||
|
function addMessages(messages) {
|
||||||
|
for(i in messages) {
|
||||||
|
if (messages[i].destination == $(".destinationID").val()) {
|
||||||
|
type = "chat-message-self";
|
||||||
|
} else {
|
||||||
|
type = "chat-message-other";
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#chat-history").append('\
|
||||||
|
<div class="chat-message"> \
|
||||||
|
<div class="' + type + '">\
|
||||||
|
' + messages[i].content + '\
|
||||||
|
</div> \
|
||||||
|
</div>\
|
||||||
|
');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchUser(userID) {
|
||||||
|
$(".destinationID").val(userID);
|
||||||
|
$("#chat-history").html("");
|
||||||
|
$("#lastID").val("");
|
||||||
|
}
|
||||||
16
website/public/sendMessage.php
Normal file
16
website/public/sendMessage.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
include_once("../queries/private_message.php");
|
||||||
|
|
||||||
|
if (isset($_POST["destination"]) &&
|
||||||
|
isset($_POST["content"])) {
|
||||||
|
|
||||||
|
if (sendMessage($_POST["destination"], $_POST["content"])) {
|
||||||
|
echo $_POST["content"] . " is naar " . $_POST["destination"] . " gestuurd";
|
||||||
|
} else {
|
||||||
|
echo "YOU FAILED!!!";
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
echo "maybe dont try to hax the system?";
|
||||||
|
}
|
||||||
@@ -25,3 +25,16 @@
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.friend-item {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu button {
|
||||||
|
background: none;
|
||||||
|
color: #333;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
padding: 0;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
function selectAllFriends($db, $userID) {
|
function selectAllFriends($db, $userID) {
|
||||||
return $db->query("
|
return $db->query("
|
||||||
SELECT
|
SELECT
|
||||||
|
`user`.`userID`,
|
||||||
`user`.`username`,
|
`user`.`username`,
|
||||||
`user`.`profilepicture`,
|
`user`.`profilepicture`,
|
||||||
`user`.`onlinestatus`,
|
`user`.`onlinestatus`,
|
||||||
|
|||||||
85
website/queries/private_message.php
Normal file
85
website/queries/private_message.php
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
include_once("connect.php");
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
function getOldChatMessages($user2ID) {
|
||||||
|
$db = $GLOBALS["db"];
|
||||||
|
$user1ID = $_SESSION["userID"];
|
||||||
|
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
`private_message`
|
||||||
|
WHERE
|
||||||
|
`origin` = :user1 AND
|
||||||
|
`destination` = :user2 OR
|
||||||
|
`origin` = :user2 AND
|
||||||
|
`destination` = :user1
|
||||||
|
ORDER BY
|
||||||
|
`messageID` ASC
|
||||||
|
");
|
||||||
|
|
||||||
|
$stmt->bindParam(":user1", $user1ID);
|
||||||
|
$stmt->bindParam(":user2", $user2ID);
|
||||||
|
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
return json_encode($stmt->fetchAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendMessage($destination, $content) {
|
||||||
|
$db = $GLOBALS["db"];
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
INSERT INTO
|
||||||
|
`private_message`
|
||||||
|
(
|
||||||
|
`origin`,
|
||||||
|
`destination`,
|
||||||
|
`content`
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(
|
||||||
|
:origin,
|
||||||
|
:destination,
|
||||||
|
:content
|
||||||
|
)
|
||||||
|
");
|
||||||
|
|
||||||
|
return $stmt->execute(array(
|
||||||
|
"origin" => $_SESSION["userID"],
|
||||||
|
"destination" => $destination,
|
||||||
|
"content" => $content
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNewChatMessages($lastID, $destination) {
|
||||||
|
$db = $GLOBALS["db"];
|
||||||
|
$origin = $_SESSION["userID"];
|
||||||
|
|
||||||
|
$stmt = $db->prepare("
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
`private_message`
|
||||||
|
WHERE
|
||||||
|
(
|
||||||
|
`origin` = :user1 AND
|
||||||
|
`destination` = :user2 OR
|
||||||
|
`origin` = :user2 AND
|
||||||
|
`destination` = :user1) AND
|
||||||
|
`messageID` > :lastID
|
||||||
|
ORDER BY
|
||||||
|
`messageID` ASC
|
||||||
|
");
|
||||||
|
|
||||||
|
$stmt->bindParam(':user1', $origin);
|
||||||
|
$stmt->bindParam(':user2', $destination);
|
||||||
|
$stmt->bindParam(':lastID', $lastID);
|
||||||
|
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
|
return json_encode($stmt->fetchAll());
|
||||||
|
}
|
||||||
@@ -1,52 +1,83 @@
|
|||||||
<div class="content">
|
<div class="content">
|
||||||
<div class="chat">
|
<div class="chat">
|
||||||
<nav class="chat-left left platform chat-recent">
|
<nav class="nav-list chat-left left platform chat-recent">
|
||||||
<h5>Chats</h5>
|
<h5>Chats</h5>
|
||||||
<a href="#">
|
<ul>
|
||||||
<div class="chat-conversation">
|
<?php
|
||||||
<img class="profile-picture" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTDnuRSeeyPve7KwDvJJ6OBzj3gyghwLcE2z9kZeYBOyZavh3mw">
|
include_once("../queries/friendship.php");
|
||||||
Rudolf Leslo
|
|
||||||
|
if (empty($_SESSION["userID"]))
|
||||||
|
$_SESSION["userID"] = 2;
|
||||||
|
|
||||||
|
// Get all the friends of a user.
|
||||||
|
$friends = selectAllFriends($db, $_SESSION["userID"]);
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
|
// Print all the users.
|
||||||
|
while($friend = $friends->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
$i ++;
|
||||||
|
|
||||||
|
// Set default values of a friend.
|
||||||
|
$username = $friend["username"];
|
||||||
|
$userID = $friend["userID"];
|
||||||
|
$pf = "img/notbad.jpg";
|
||||||
|
|
||||||
|
// Change values if needed.
|
||||||
|
if (!empty($friend["profilepicture"]))
|
||||||
|
$pf = $friend["profilepicture"];
|
||||||
|
|
||||||
|
// Echo the friend.
|
||||||
|
echo "
|
||||||
|
<li class='friend-item' onclick='switchUser(\"$userID\")'>
|
||||||
|
<div class='friend'>
|
||||||
|
<img alt='PF' class='profile-picture' src='$pf'/>
|
||||||
|
$username
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</li>
|
||||||
|
";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</ul>
|
||||||
|
<!-- <a href="#">-->
|
||||||
|
<!-- <div class="chat-conversation">-->
|
||||||
|
<!-- <img class="profile-picture" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTDnuRSeeyPve7KwDvJJ6OBzj3gyghwLcE2z9kZeYBOyZavh3mw">-->
|
||||||
|
<!-- Rudolf Leslo-->
|
||||||
|
<!-- </div>-->
|
||||||
|
<!-- </a>-->
|
||||||
</nav>
|
</nav>
|
||||||
<div class="chat-right right">
|
<div class="chat-right right">
|
||||||
<div class="chat-history platform">
|
<div id="chat-history" class="chat-history platform">
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-self">Hi!</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-other">Hi!</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-self">How it's going?</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-self">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-other">Hi!</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-other">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-other">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
|
|
||||||
</div>
|
|
||||||
<div class="chat-message">
|
|
||||||
<div class="chat-message-self">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
<form id="lastIDForm">
|
||||||
|
<input type="hidden"
|
||||||
|
id="lastID"
|
||||||
|
name="lastID"
|
||||||
|
value=""
|
||||||
|
/>
|
||||||
|
<input type="hidden"
|
||||||
|
name="destination"
|
||||||
|
class="destinationID"
|
||||||
|
value=""
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
<div class="chat-field">
|
<div class="chat-field">
|
||||||
<form method="post">
|
<form id="sendMessageForm" action="javascript:sendMessage();">
|
||||||
|
<input type="hidden"
|
||||||
|
name="destination"
|
||||||
|
class="destinationID"
|
||||||
|
value=""
|
||||||
|
/>
|
||||||
<input type="submit"
|
<input type="submit"
|
||||||
value="Verstuur"
|
value="Verstuur"
|
||||||
>
|
/>
|
||||||
<span>
|
<span>
|
||||||
<input type="text"
|
<input type="text"
|
||||||
name="message"
|
name="content"
|
||||||
|
id="newContent"
|
||||||
placeholder="Reageer..."
|
placeholder="Reageer..."
|
||||||
|
autofocus
|
||||||
required
|
required
|
||||||
>
|
/>
|
||||||
</span>
|
</span>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -17,4 +17,6 @@
|
|||||||
|
|
||||||
include_once("../queries/connect.php");
|
include_once("../queries/connect.php");
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -34,14 +34,18 @@
|
|||||||
|
|
||||||
// Echo the friend.
|
// Echo the friend.
|
||||||
echo "
|
echo "
|
||||||
<a href='#' class='$extraItem'>
|
<li class='friend-item $extraItem'>
|
||||||
<li class='friend-item'>
|
<form action='profile.php' method='get'>
|
||||||
|
<button type='submit'
|
||||||
|
name='username'
|
||||||
|
value='$username'>
|
||||||
<div class='friend'>
|
<div class='friend'>
|
||||||
<img alt='PF' class='profile-picture' src='$pf'/>
|
<img alt='PF' class='profile-picture' src='$pf'/>
|
||||||
$username
|
$username
|
||||||
</div>
|
</div>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
</li>
|
</li>
|
||||||
</a>
|
|
||||||
";
|
";
|
||||||
}
|
}
|
||||||
if ($i > 1) {
|
if ($i > 1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user