forked from codingWithElias/php-chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.php
162 lines (147 loc) · 4.65 KB
/
home.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
session_start();
if (isset($_SESSION['username'])) {
# database connection file
include 'app/db.conn.php';
include 'app/helpers/user.php';
include 'app/helpers/conversations.php';
include 'app/helpers/timeAgo.php';
include 'app/helpers/last_chat.php';
# Getting User data data
$user = getUser($_SESSION['username'], $conn);
# Getting User conversations
$conversations = getConversation($user['user_id'], $conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat App - Home</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link rel="stylesheet"
href="css/style.css">
<link rel="icon" href="img/logo.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body class="d-flex
justify-content-center
align-items-center
vh-100">
<div class="p-2 w-400
rounded shadow">
<div>
<div class="d-flex
mb-3 p-3 bg-light
justify-content-between
align-items-center">
<div class="d-flex
align-items-center">
<img src="uploads/<?=$user['p_p']?>"
class="w-25 rounded-circle">
<h3 class="fs-xs m-2"><?=$user['name']?></h3>
</div>
<a href="logout.php"
class="btn btn-dark">Logout</a>
</div>
<div class="input-group mb-3">
<input type="text"
placeholder="Search..."
id="searchText"
class="form-control">
<button class="btn btn-primary"
id="serachBtn">
<i class="fa fa-search"></i>
</button>
</div>
<ul id="chatList"
class="list-group mvh-50 overflow-auto">
<?php if (!empty($conversations)) { ?>
<?php
foreach ($conversations as $conversation){ ?>
<li class="list-group-item">
<a href="chat.php?user=<?=$conversation['username']?>"
class="d-flex
justify-content-between
align-items-center p-2">
<div class="d-flex
align-items-center">
<img src="uploads/<?=$conversation['p_p']?>"
class="w-10 rounded-circle">
<h3 class="fs-xs m-2">
<?=$conversation['name']?><br>
<small>
<?php
echo lastChat($_SESSION['user_id'], $conversation['user_id'], $conn);
?>
</small>
</h3>
</div>
<?php if (last_seen($conversation['last_seen']) == "Active") { ?>
<div title="online">
<div class="online"></div>
</div>
<?php } ?>
</a>
</li>
<?php } ?>
<?php }else{ ?>
<div class="alert alert-info
text-center">
<i class="fa fa-comments d-block fs-big"></i>
No messages yet, Start the conversation
</div>
<?php } ?>
</ul>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Search
$("#searchText").on("input", function(){
var searchText = $(this).val();
if(searchText == "") return;
$.post('app/ajax/search.php',
{
key: searchText
},
function(data, status){
$("#chatList").html(data);
});
});
// Search using the button
$("#serachBtn").on("click", function(){
var searchText = $("#searchText").val();
if(searchText == "") return;
$.post('app/ajax/search.php',
{
key: searchText
},
function(data, status){
$("#chatList").html(data);
});
});
/**
auto update last seen
for logged in user
**/
let lastSeenUpdate = function(){
$.get("app/ajax/update_last_seen.php");
}
lastSeenUpdate();
/**
auto update last seen
every 10 sec
**/
setInterval(lastSeenUpdate, 10000);
});
</script>
</body>
</html>
<?php
}else{
header("Location: index.php");
exit;
}
?>