Toby Chui преди 5 години
родител
ревизия
9b9941e41e
променени са 4 файла, в които са добавени 195 реда и са изтрити 2 реда
  1. 49 0
      src/ajaxGetPost.php
  2. 115 0
      src/comment.php
  3. 20 2
      src/home.php
  4. 11 0
      src/postComment.php

+ 49 - 0
src/ajaxGetPost.php

@@ -0,0 +1,49 @@
+		<?php
+			include_once 'auth.php';
+			//Get the post id for comment and viewing all its previous comments
+			if (isset($_GET['post'])){
+				include_once("requestDB.php");
+				include_once("getUserImage.php");
+				$postID = $_GET['post'];
+				//Get the post content
+				$postData = query("select * from post where postid='" . $postID . "'","teabag");
+				foreach ($postData as $post){
+					echo '
+					<div class="ts card">
+						<div class="content">
+							<div class="header"><img class="ts image postAvator" src="' . getUserIconPathByUsername($post["username"]) .'" style="margin-right:-20px;width:80px;height:80px;">' . $post["username"] . '</div>
+							<div class="meta">
+								<a>@ '.$post["posttime"].'</a>
+							</div>
+							<div class="description">
+								<p>'.$post["content"].'</p>
+							</div>
+						</div>
+					</div>
+					';
+				}
+				
+				//Echo the previous comments
+				$postData = query("select * from comments where postid='" . $postID . "' ORDER BY commenttime","teabag");
+				foreach ($postData as $comment){
+					echo '<div class="ts structured message" style="margin-left:50px;">
+						<div class="avatar">
+							<img src="' . getUserIconPathByUsername($comment["fromuser"]) . '">
+						</div>
+						<div class="content">
+							<div class="description">
+								<p>' . $comment["content"] . '</p>
+								<p> By ' . $comment["fromuser"] . ' @ ' . $comment["commenttime"] .'</p>
+							</div>
+							<div class="actions">
+								<a class="primary" onClick="reply(\'' . $comment["fromuser"]  . '\');">Reply</a>
+							</div>
+						</div>
+					</div>';
+				}
+				
+				
+			}else{
+				die("No post found.");
+			}
+		?>

+ 115 - 0
src/comment.php

@@ -0,0 +1,115 @@
+<?php
+include_once 'auth.php';
+//Get information of local data storage from auth.php
+$userAccountName = $_SESSION['login']; 
+$dataStorage = $sysConfigDir . '/userdata/' .  $_SESSION['login'] . "/"; 
+
+?>
+<html>
+<head>
+
+<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
+<title>TeaBag.tw</title>
+<link rel="stylesheet" href="script/tocas/tocas.css">
+<script src="script/tocas/tocas.js"></script>
+<script src="script/jquery.min.js"></script>
+<style>
+.extrapadding{
+	padding-left:10%;
+	padding-right:10%;
+}
+.removePadding{
+	padding:0px !important;
+}
+.active.item{
+	color:#4287f5 !important;
+	font-weight: bold !important;
+}
+.postAvator{
+	height:50px !important;
+	padding-right:25px;
+}
+.teabag.card{
+	max-width:350px;
+}
+.nopadding{
+	padding:0px !important;
+}
+.selectable{
+	cursor:pointer;
+	padding:5px !important;
+}
+.selectable:hover{
+	background-color:#f2f2f2 !important;
+}
+.online{
+	color: #88db99;
+}
+.linebreak{
+	white-space: pre-wrap;
+}
+</style>
+</head>
+	<body>
+	<div class="ts tabbed menu extrapadding">
+		<a class="item" href="index.php"><img src="img/minilogo.png" style="height:25px;"></a>
+		<a class="item" href="home.php"><i class="home icon"></i> Following</a>
+		<a class="item" href="index.php"><i class="user outline icon"></i>Timeline</a>
+		<a class="item"><i class="mail outline icon"></i>Notification</a>
+		<a class="active item"><i class="comments outline icon"></i>Post Comment</a>
+		<div class="right item removePadding">
+		<div class="ts icon tiny input">
+			<input type="text" placeholder="Search...">
+			<i class="circular search link icon"></i>
+		</div>
+		<a class="item" href="logout.php"><i class="log out icon"></i>Logout</a>
+		</div>
+	</div>
+	<div id="comments" class="ts container">
+		
+	</div>
+	<div class="ts container" style="padding-top:12px;">
+	<?php
+	//Echo comemnt box
+	
+	echo '<div class="ts segment" style="margin-left:50px;">
+		<div class="ts icon fluid input">
+			<input id="commentText" type="text" placeholder="Comment.">
+			<i class="send link icon" onClick="sendComment();"></i>
+		</div>
+		<small>Press Enter or the send button on the right to leave a comment.</small>
+	</div>';
+	?>
+	</div>
+	<script>
+		var postID = "<?php echo $_GET['post']; ?>";
+		initPostAndComments();
+		
+		function initPostAndComments(){
+			$("#comments").html("");
+			$("#comments").load("ajaxGetPost.php?post=" + postID);
+			
+		}
+		function reply(username){
+			if ($("#commentText").val() == ""){
+				$("#commentText").val("#" + username + " ");
+			}
+		}
+		
+		function sendComment(){
+			var comment = $("#commentText").val();
+			$.post("postComment.php",{"comment":comment,"postid":postID}).done(function(data){
+				initPostAndComments();
+				$("#commentText").val("");
+			});
+		}
+		$("#commentText").on('keypress',function(e) {
+			if(e.which == 13 && $("#commentText").val() != "") {
+				//Enter pressed and comment not empty. Send Comment
+				sendComment();
+			}
+		});
+	</script>
+	</body>
+</html>
+

+ 20 - 2
src/home.php

@@ -48,6 +48,9 @@ $dataStorage = $sysConfigDir . '/userdata/' .  $_SESSION['login'] . "/";
 .linebreak{
 	white-space: pre-wrap;
 }
+a{
+	cursor:pointer;
+}
 </style>
 </head>
 	<body>
@@ -72,10 +75,14 @@ $dataStorage = $sysConfigDir . '/userdata/' .  $_SESSION['login'] . "/";
 			$postData = query("select * from post where (username IN (SELECT `targetName` FROM `following` WHERE `followerName`='" . $username . "')) OR username='" . $username . "'  ORDER BY postid DESC","teabag");
 			include_once("getUserImage.php");
 			foreach ($postData as $post){
+				//Get the number of comments on this object
+				$pid = $post["postid"];
+				$commentLength = query("SELECT COUNT(*) FROM comments WHERE postid='" . $pid . "'","teabag");
+				$commentNumber = $commentLength[0]["COUNT(*)"];
 				echo '
-				<div class="ts card">
+				<div class="ts card" style="margin-bottom:0px; !important">
 					<div class="content">
-						<div class="header"><img class="ts image postAvator" src="' . getUserIconPathByUsername($post["username"]) .'" style="margin-right:-20px;">' . $post["username"] . '</div>
+						<div class="header"><img class="ts image postAvator" src="' . getUserIconPathByUsername($post["username"]) .'" style="margin-right:-20px;width:80px;height:80px;">' . $post["username"] . '</div>
 						<div class="meta">
 							<a>@ '.$post["posttime"].'</a>
 						</div>
@@ -85,6 +92,17 @@ $dataStorage = $sysConfigDir . '/userdata/' .  $_SESSION['login'] . "/";
 					</div>
 				</div>
 				';
+
+				if ($commentNumber > 0){
+					//There is comment on this post
+					echo '<div class="ts primary attached message" style="margin-top:0px; !important">
+						<p>' . $commentNumber . ' people has commented this post. <a href="comment.php?post=' . $pid . '">CLICK HERE to share yours?</a></p>
+					</div>';
+				}else{
+					echo '<div class="ts attached message" style="margin-top:0px; !important">
+						<a href="comment.php?post=' . $pid . '">Leave a comment</a>
+					</div>';
+				}
 			}
 		?>
 	</div>

+ 11 - 0
src/postComment.php

@@ -0,0 +1,11 @@
+<?php
+include_once(__DIR__ . "/auth.php");
+$postID = $_POST['postid'];
+$comment = $_POST['comment'];
+$username = $_SESSION['login'];
+
+//Write comment into database
+include_once("requestDB.php");
+	queryw("INSERT INTO `comments` (`commentID`, `postid`, `content`, `fromuser`, `commenttime`) VALUES (NULL, '" . $postID . "', '" . $comment . "', '" . $username . "', CURRENT_TIMESTAMP)","teabag");
+echo "DONE";
+?>