-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupList.php
238 lines (206 loc) · 8.61 KB
/
groupList.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
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>group 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';
// 如果有logout消息,说明用户点击了logout
if(filter_has_var(INPUT_POST, "logout")) {
// 清除所有session
session_unset();
echo "<script>window.location = 'index.php'</script>";
}
// 如果有user_id,说明用户已经成功login,显示group列表
else if(isset($_SESSION["user_id"])) {
if(filter_has_var(INPUT_POST, "isAccept")) {
$isAccept = filter_input(INPUT_POST, "isAccept");
$user_id = filter_input(INPUT_POST, "user_id");
$group_id = filter_input(INPUT_POST, "group_id");
joinApprove($isAccept, $user_id, $group_id);
}
if(needApproveJoinGroup()) {
showJoinApprove();
}
else {
showGroupList();
}
}
// 如果没有user_id,说明session过期,需要重新登录
else {
echo "<script>window.location = 'index.php'</script>";
}
function joinApprove($isAccept, $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");
// 把用户加入到组成员中
if($isAccept) {
$smt = $pdo->prepare("INSERT INTO groupMember VALUES (NULL, ?, ?, 0)");
$smt->bindParam(1, $user_id);
$smt->bindParam(2, $group_id);
if(!$smt->execute())
print_r($smt->errorInfo());
}
// 不管接受或拒绝,都从等待确认列表中删除
$smt = $pdo->prepare("DELETE FROM waitForJoinGroup WHERE user_id=? AND group_id=?");
$smt->bindParam(1, $user_id);
$smt->bindParam(2, $group_id);
if(!$smt->execute())
print_r($smt->errorInfo());
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
function needApproveJoinGroup() {
$leader_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 waitForJoinGroup, groups WHERE groups.leader_id=$leader_id AND groups.group_id=waitForJoinGroup.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 showJoinApprove() {
$leader_id = $_SESSION["user_id"];
$leader_name = getUserNameById($leader_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, groups WHERE groups.leader_id=$leader_id AND groups.group_id=waitForJoinGroup.group_id");
if(!$smt->execute())
print_r($smt->errorInfo());
// 取第一个记录进行处理
if($row = $smt->fetch()) {
$user_id = $row["user_id"];
$group_id = $row["group_id"];
$user_name = getUserNameById($user_id);
$group_name = $row["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
</div>
</header>
<section class="sectionWithFooter">
<br/>
<h1><mark>$user_name</mark> want to join group <mark>$group_name</mark>, do you accept?</h1>
<form method="post">
<input type="hidden" name="user_id" value=$user_id/>
<input type="hidden" name="group_id" value=$group_id/>
<button class="largeButton" name="isAccept" value=1>accept</button><br/>
<button class="mediumButton" name="isAccept" value=0>reject</button>
</form>
</section>
<footer class="fixedFooter">
<div class="footerTitle">user: $leader_name</div>
</footer>
HERE;
print $output;
}
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
function showGroupList() {
//输出页面上面部分
$output =<<<HERE
<header class="fixedHeader">
<div class="headerLeftButton">
<form method="post">
<button id="logout" name="logout"><img border="0" src="images\logoutButton.jpg" width="30" height="30"></button>
</form>
</div>
<div class="headerTitle">
select a group
</div>
<div class="headerRightButton">
<a href="newGroup.php"><img border="0" src="images\plusButton.jpg" width="30" height="30"></a>
</div>
</header>
<section class="sectionWithFooter">
<form method="post" action="prayList.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 * FROM groups");
if(!$smt->execute())
{
print_r($smt->errorInfo());
}
while ($row = $smt->fetch()) {
$group_id = $row["group_id"];
$group_name = $row["name"];
$output =<<<HERE
<p class="groupButtonWrap"><button class="groupItem" name="group_id" value=$group_id>$group_name
HERE;
if($newPrays = prayUnsaw($group_id)) {
$output .= "<mark class='newRemind'> $newPrays new</mark>";
}
$output .="</button></p>";
print $output;
}
} 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 prayUnsaw($group_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 groupMember WHERE user_id=? AND group_id=?");
$smt->bindValue(1, $user_id);
$smt->bindValue(2, $group_id);
if(!$smt->execute())
print_r($smt->errorInfo());
//用户不是这个组的成员
if($smt->rowCount() == 0)
return 0;
$row = $smt->fetch();
$last_saw_pray = $row["last_saw_pray"];
$smt = $pdo->prepare("SELECT * FROM prayList WHERE group_id = ? AND pray_id > ?");
$smt->bindValue(1, $group_id);
$smt->bindValue(2, $last_saw_pray);
if(!$smt->execute())
print_r($smt->errorInfo());
//有多少个reply是在最后看到的reply之后产生的
return $smt->rowCount();
} catch (Exception $ex) {
echo 'ERROR: '. $ex->getMessage();
}
}
?>
</body>
</html>