-
Notifications
You must be signed in to change notification settings - Fork 0
/
prayList.php
292 lines (255 loc) · 10.4 KB
/
prayList.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>pray list</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="pray.css"/>
</head>
<body>
<?php
include_once 'constant.inc';
include_once 'commonFunc.inc';
// 如果有POST过来的group_id,说明用户是从groupList页面跳转过来,判断是否能进入群组
if(filter_has_var(INPUT_POST, "group_id"))
{
$group_id = filter_input(INPUT_POST, "group_id");
$user_id = $_SESSION["user_id"];
//检查用户是否该组成员
if(isMember($user_id, $group_id)) {
$_SESSION["group_id"] = $group_id;
$_SESSION["group_name"] = getGroupNameById($group_id);
showPrayList($group_id);
}
//没有记录,询问用户是否加入该组
else {
showJoinGroup($group_id);
}
}
// 如果有POST过来的joinGroup,说明用户是从joinGroup页面跳转过来
else if(filter_has_var(INPUT_POST, "joinGroup")) {
$group_id = filter_input(INPUT_POST, "joinGroup");
$user_id = $_SESSION["user_id"];
joinGroup($user_id, $group_id);
}
// 如果没有POST消息,而session中有group id,说明是从其他页面跳转过来(pray detail或new group),直接显示pray list
else if(isset ($_SESSION["group_id"])) {
$group_id = $_SESSION["group_id"];
showPrayList($group_id);
}
// 如果POST和session都没有,可能页面过期,重新登录
else {
echo "<script>window.location = 'index.php'</script>";
}
function isMember($user_id, $group_id) {
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("SELECT * FROM groupMember WHERE user_id=? AND group_id=?");
$smt->bindParam(1, $user_id);
$smt->bindParam(2, $group_id);
if(!$smt->execute())
{
print_r($smt->errorInfo());
}
//有记录,说明是该组成员
if($smt->fetch()) {
return true;
}
else {
return false;
}
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
function showPrayList($group_id) {
//输出页面上面部分
$group_name = $_SESSION["group_name"];
$output =<<<HERE
<header class="fixedHeader">
<div class="headerLeftButton">
<a href="groupList.php"><img border="0" src="images\backButton.jpg" width="30" height="30"></a>
</div>
<div class="headerTitle">$group_name</div>
<div class="headerRightButton">
<a href="newPray.php"><img border="0" src="images\plusButton.jpg" width="30" height="30"></a>
</div>
</header>
<section class="sectionWithFooter">
<form method="post" action="prayDetail.php">
HERE;
print $output;
//输出页面中间列表部分
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("SELECT pray_id, user_id, content FROM prayList WHERE group_id=$group_id ORDER BY pray_id DESC");
if(!$smt->execute())
print_r($smt->errorInfo());
$last_saw_pray = 0;
while ($row = $smt->fetch()) {
$pray_id = $row["pray_id"];
$user_name = getUserNameById($row["user_id"]);
$content = $row["content"];
$output =<<<HERE
<p class="prayItemWrap"><button class="prayItem" name="pray_id" value=$pray_id><mark>$user_name: </mark>$content
HERE;
if($newReplys = replyUnsaw($pray_id)) {
$output .= "<mark class='newRemind'> $newReplys new</mark>";
}
$output .= "</button></p>";
print $output;
if($pray_id > $last_saw_pray)
$last_saw_pray = $pray_id;
}
refreshLastSawPray($last_saw_pray);
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
//输出页面下面部分
$user_name = $_SESSION["user_name"];
$output =<<<HERE
</form>
</section>
<footer class="fixedFooter">
<div class="footerTitle">user: $user_name</div>
</footer>
HERE;
print $output;
}
function replyUnsaw($pray_id) {
$user_id = $_SESSION["user_id"];
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("SELECT * FROM lastSawReply WHERE user_id=? AND pray_id=?");
$smt->bindValue(1, $user_id);
$smt->bindValue(2, $pray_id);
if(!$smt->execute())
print_r($smt->errorInfo());
if($smt->rowCount() > 0) {
$row = $smt->fetch();
$last_saw_reply = $row["last_saw_reply"];
}
else
$last_saw_reply = 0;
$smt = $pdo->prepare("SELECT * FROM replyList WHERE pray_id = ? AND reply_id > ?");
$smt->bindValue(1, $pray_id);
$smt->bindValue(2, $last_saw_reply);
if(!$smt->execute())
print_r($smt->errorInfo());
//有多少个reply是在最后看到的reply之后产生的
return $smt->rowCount();
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
function refreshLastSawPray($last_saw_pray) {
$user_id = $_SESSION["user_id"];
$group_id = $_SESSION["group_id"];
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("UPDATE groupMember SET last_saw_pray=? WHERE user_id=? AND group_id=?");
$smt->bindValue(1, $last_saw_pray);
$smt->bindValue(2, $user_id);
$smt->bindValue(3, $group_id);
if(!$smt->execute())
print_r($smt->errorInfo());
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
function showJoinGroup($group_id) {
$user_name = $_SESSION["user_name"];
$output =<<<HERE
<header class="fixedHeader">
<div class="headerLeftButton">
<a href="groupList.php"><img border="0" src="images\backButton.jpg" width="30" height="30"></a>
</div>
<div class="headerTitle">
join a group
</div>
</header>
<section class="sectionWithFooter">
<br/>
<h1>send join request to group leader?</h1>
<form method="post">
<button class="largeButton" name="joinGroup" value=$group_id>send</button>
</form>
<a class="mediumLink" href="groupList.php">cancel</a>
</section>
<footer class="fixedFooter">
<div class="footerTitle">user: $user_name</div>
</footer>
HERE;
print $output;
}
function joinGroup($user_id, $group_id) {
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("SELECT * FROM waitForJoinGroup WHERE user_id=$user_id AND group_id=$group_id");
if(!$smt->execute())
print_r($smt->errorInfo());
//如果已经有申请过就不用重复申请了
if(!$smt->fetch()) {
$smt = $pdo->prepare("INSERT INTO waitForJoinGroup VALUES (NULL, $user_id, $group_id)");
if(!$smt->execute())
print_r($smt->errorInfo());
}
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
// show feedback
$leader_name = getLeaderName($group_id);
$user_name = $_SESSION["user_name"];
$output =<<<HERE
<header class="fixedHeader">
<div class="headerLeftButton">
<a href="groupList.php"><img border="0" src="images\backButton.jpg" width="30" height="30"></a>
</div>
<div class="headerTitle">
join request sent
</div>
</header>
<section class="sectionWithFooter">
<br/>
<h1>join group request already sent to leader <mark>$leader_name</mark>, please remind him(her) to approve</h1>
<a class="largeLink" href="groupList.php">continue</a>
</section>
<footer class="fixedFooter">
<div class="footerTitle">user: $user_name</div>
</footer>
HERE;
print $output;
}
function getLeaderName($group_id) {
try {
$pdo = new PDO("mysql:host=".DBHOST."; port=".DBPORT."; dbname=".DBNAME, DBUSER, DBPASS);
//$pdo = new PDO("mysql:host=localhost; dbname=pray","pray", "pray");
$smt = $pdo->prepare("SELECT * FROM groups WHERE group_id=$group_id");
if(!$smt->execute())
print_r($smt->errorInfo());
$row = $smt->fetch();
if(!$row)
return null;
$leader_id = $row["leader_id"];
$smt = $pdo->prepare("SELECT * FROM users WHERE user_id=$leader_id");
if(!$smt->execute())
print_r($smt->errorInfo());
$row = $smt->fetch();
if(!$row)
return null;
return $row["name"];
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
?>
</body>
</html>