From eb2a343d5fe5d4dc1ab44fbb3fb792579fbfca46 Mon Sep 17 00:00:00 2001 From: Lars van Hijfte Date: Wed, 18 Jan 2017 12:08:53 +0100 Subject: [PATCH 1/5] added live sending messages from user 2 to user 2 --- website/public/chat.php | 1 + website/public/js/sendMessage.js | 14 ++++++++ website/public/sendMessage.php | 16 +++++++++ website/queries/private_message.php | 51 +++++++++++++++++++++++++++++ website/views/chat-view.php | 12 ++++--- 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 website/public/js/sendMessage.js create mode 100644 website/public/sendMessage.php create mode 100644 website/queries/private_message.php diff --git a/website/public/chat.php b/website/public/chat.php index f077a4d..150638d 100644 --- a/website/public/chat.php +++ b/website/public/chat.php @@ -5,6 +5,7 @@ + prepare(" + SELECT + * + FROM + `private_message` + WHERE + `origin` = :user1 AND + `destination` = :user2 OR + `origin` = :user2 AND + `destination` = :user1 + ORDER BY + `creationdate` DESC + LIMIT + :n, 100 + "); + + $stmt->bindParam(":user1", $user1ID); + $stmt->bindParam(":user2", $user2ID); + $stmt->bindParam(":n", $n); + + return $stmt->execute(); +} + +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" => 2, + "destination" => $destination, + "content" => $content + )); +} \ No newline at end of file diff --git a/website/views/chat-view.php b/website/views/chat-view.php index a23a1c5..6b6aecd 100644 --- a/website/views/chat-view.php +++ b/website/views/chat-view.php @@ -37,16 +37,20 @@
-
+ + + /> + />
From 19f525d168703a217ba6dfc981b3e91c05511388 Mon Sep 17 00:00:00 2001 From: Lars van Hijfte Date: Wed, 18 Jan 2017 14:34:15 +0100 Subject: [PATCH 2/5] Live chatting now shows up in the chat-history --- website/public/chat.php | 2 +- website/public/js/chat.js | 50 +++++++++++++++++++++++ website/public/js/sendMessage.js | 14 ------- website/public/sendMessage.php | 2 +- website/queries/private_message.php | 46 ++++++++++++++++++--- website/views/chat-view.php | 63 +++++++++++++++++------------ website/views/head.php | 2 + 7 files changed, 132 insertions(+), 47 deletions(-) create mode 100644 website/public/js/chat.js delete mode 100644 website/public/js/sendMessage.js diff --git a/website/public/chat.php b/website/public/chat.php index 150638d..09be336 100644 --- a/website/public/chat.php +++ b/website/public/chat.php @@ -5,7 +5,7 @@ - + \ +
\ + ' + messages[i].content + '\ +
\ + \ + '); + } +} \ No newline at end of file diff --git a/website/public/js/sendMessage.js b/website/public/js/sendMessage.js deleted file mode 100644 index 1c300b2..0000000 --- a/website/public/js/sendMessage.js +++ /dev/null @@ -1,14 +0,0 @@ -// $("#sendMessageForm").submit(function(e) { -function sendMessage() { - console.log($("#sendMessageForm").serialize()); - $.post( - "sendMessage.php", - $("#sendMessageForm").serialize() - ).done(function( data ) { - alert( "Data Loaded: " + data ); - }); -} - -function loadMessages() { - -} \ No newline at end of file diff --git a/website/public/sendMessage.php b/website/public/sendMessage.php index c0dde12..e555e2a 100644 --- a/website/public/sendMessage.php +++ b/website/public/sendMessage.php @@ -5,7 +5,7 @@ include_once("../queries/private_message.php"); if (isset($_POST["destination"]) && isset($_POST["content"])) { - if (sendMessage($db, $_POST["destination"], $_POST["content"])) { + if (sendMessage($_POST["destination"], $_POST["content"])) { echo $_POST["content"] . " is naar " . $_POST["destination"] . " gestuurd"; } else { echo "YOU FAILED!!!"; diff --git a/website/queries/private_message.php b/website/queries/private_message.php index 22fb085..56c0c26 100644 --- a/website/queries/private_message.php +++ b/website/queries/private_message.php @@ -1,7 +1,13 @@ prepare(" SELECT * @@ -13,16 +19,15 @@ function get100ChatMessagesFromN($n, $user1ID, $user2ID) { `origin` = :user2 AND `destination` = :user1 ORDER BY - `creationdate` DESC - LIMIT - :n, 100 + `messageID` ASC "); $stmt->bindParam(":user1", $user1ID); $stmt->bindParam(":user2", $user2ID); - $stmt->bindParam(":n", $n); - return $stmt->execute(); + $stmt->execute(); + + return json_encode($stmt->fetchAll()); } function sendMessage($destination, $content) { @@ -48,4 +53,33 @@ function sendMessage($destination, $content) { "destination" => $destination, "content" => $content )); +} + +function getNewChatMessages($lastID, $destination) { + $db = $GLOBALS["db"]; + $origin = 2; + + $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()); } \ No newline at end of file diff --git a/website/views/chat-view.php b/website/views/chat-view.php index 6b6aecd..dabd348 100644 --- a/website/views/chat-view.php +++ b/website/views/chat-view.php @@ -10,32 +10,43 @@
-
-
-
Hi!
-
-
-
Hi!
-
-
-
How it's going?
-
-
-
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.
-
-
-
Hi!
-
-
-
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.
-
-
-
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.
-
-
-
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.
-
+
+ + + + + + + + + + + + + + + + + + + + + + + +
+
+ + +
diff --git a/website/views/head.php b/website/views/head.php index 9306edf..39e29af 100644 --- a/website/views/head.php +++ b/website/views/head.php @@ -17,4 +17,6 @@ include_once("../queries/connect.php"); +session_start(); + ?> \ No newline at end of file From 95fd3c0040abd55e1e0e90b409d8d8283a931976 Mon Sep 17 00:00:00 2001 From: Lars van Hijfte Date: Wed, 18 Jan 2017 15:09:52 +0100 Subject: [PATCH 3/5] chat is done? --- website/public/js/chat.js | 7 ++ website/public/styles/chat.css | 4 ++ website/queries/friendship.php | 1 + website/queries/private_message.php | 6 +- website/views/chat-view.php | 100 +++++++++++++++++++--------- 5 files changed, 84 insertions(+), 34 deletions(-) diff --git a/website/public/js/chat.js b/website/public/js/chat.js index 5b56586..dd0b00f 100644 --- a/website/public/js/chat.js +++ b/website/public/js/chat.js @@ -12,6 +12,7 @@ function loadMessages() { messages = JSON.parse(data); addMessages(messages); $("#lastID").val(messages[messages.length - 1].messageID); + $("#chat-history").scrollTop($("#chat-history")[0].scrollHeight); } }); @@ -47,4 +48,10 @@ function addMessages(messages) {
\ '); } +} + +function switchUser(userID) { + $(".destinationID").val(userID); + $("#chat-history").html(""); + $("#lastID").val(""); } \ No newline at end of file diff --git a/website/public/styles/chat.css b/website/public/styles/chat.css index 8f486bb..132c750 100644 --- a/website/public/styles/chat.css +++ b/website/public/styles/chat.css @@ -16,6 +16,10 @@ overflow-y: auto; } +.chat-left .friend-item { + cursor: pointer; +} + .chat-right { width: calc(100% - 256px - 40px); height: calc(100% - 80px); diff --git a/website/queries/friendship.php b/website/queries/friendship.php index 56ce274..dc7a669 100644 --- a/website/queries/friendship.php +++ b/website/queries/friendship.php @@ -3,6 +3,7 @@ function selectAllFriends($db, $userID) { return $db->query(" SELECT + `user`.`userID`, `user`.`username`, `user`.`profilepicture`, `user`.`onlinestatus`, diff --git a/website/queries/private_message.php b/website/queries/private_message.php index 56c0c26..6cf8b16 100644 --- a/website/queries/private_message.php +++ b/website/queries/private_message.php @@ -6,7 +6,7 @@ session_start(); function getOldChatMessages($user2ID) { $db = $GLOBALS["db"]; - $user1ID = 2; + $user1ID = $_SESSION["userID"]; $stmt = $db->prepare(" SELECT @@ -49,7 +49,7 @@ function sendMessage($destination, $content) { "); return $stmt->execute(array( - "origin" => 2, + "origin" => $_SESSION["userID"], "destination" => $destination, "content" => $content )); @@ -57,7 +57,7 @@ function sendMessage($destination, $content) { function getNewChatMessages($lastID, $destination) { $db = $GLOBALS["db"]; - $origin = 2; + $origin = $_SESSION["userID"]; $stmt = $db->prepare(" SELECT diff --git a/website/views/chat-view.php b/website/views/chat-view.php index dabd348..1f9a0d8 100644 --- a/website/views/chat-view.php +++ b/website/views/chat-view.php @@ -1,40 +1,76 @@
-
- - - - - - - - - - - - - - - - - - - - - - - -
@@ -89,7 +65,7 @@ Date: Wed, 18 Jan 2017 15:54:13 +0100 Subject: [PATCH 5/5] Friends in the menu now redirects to the users profile. --- website/public/styles/menu.css | 13 +++++++++++++ website/views/menu.php | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/website/public/styles/menu.css b/website/public/styles/menu.css index 7778545..45163de 100644 --- a/website/public/styles/menu.css +++ b/website/public/styles/menu.css @@ -25,3 +25,16 @@ font-size: 14px; cursor: pointer; } + +.friend-item { + cursor: pointer; +} + +.menu button { + background: none; + color: #333; + width: 100%; + height: 100%; + padding: 0; + text-align: left; +} \ No newline at end of file diff --git a/website/views/menu.php b/website/views/menu.php index 5486cd6..731b284 100644 --- a/website/views/menu.php +++ b/website/views/menu.php @@ -34,14 +34,18 @@ // Echo the friend. echo " - -
  • -
    - PF - $username -
    -
  • -
    +
  • +
    + +
    +
  • "; } if ($i > 1) {