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
|
<?php
require_once('common.inc.php');
require_once('user.inc.php');
const NOTICE_DEF_LIM = 5;
const NOTICE_MAX_LIM = 30;
const NOTICE_ACT_CNT = 1;
const NOTICE_ACT_NEW = 2;
const NOTICE_ACT_OLD = 4;
const NOTICE_TYP_USR = 1;
const NOTICE_TYP_PRO = 2;
const NOTICE_TYP_SQR = 3;
const NOTICE_TYP_ALL = 255;
class notice
{
private static function ins_uid($sqlc, $uid)
{
$sqlstr = 'INSERT INTO "notice_cache" ("uid", "tim", "cnt", "tmp", "rsv") VALUES ($1, $2, $3, $4, $5) RETURNING *;';
$sqlarr = array($uid, date("Y-m-d H:i:s"), 0, "0", "0");
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$ret = pg_fetch_object($sqlr);
pg_free_result($sqlr);
return $ret;
}
public static function is_match($sqlc, $uid, &$not)
{
if($not->typ == NOTICE_TYP_ALL)
{
return true;
}
if($not->typ == NOTICE_TYP_USR)
{
return $uid == intval($not->val);
}
require_once('event.inc.php');
if($not->typ == NOTICE_TYP_PRO)
{
if(!event::exec_func('problem.inc.php', 'problem::is_available', array($sqlc, $not->val, $uid)))
{
return false;
}
return true;
}
else if($not->typ == NOTICE_TYP_SQR)
{
$ret = event::exec_func('square.inc.php', 'square::get_user_relationship', array($sqlc, $uid, $not->val));
if($ret >= SQUARE_USER_ACTIVE)
{
return true;
}
return false;
}
return false;
}
private static function update($sqlc, $uid, &$tar, &$arr)
{
if(count($arr) <= 0)
{
return false;
}
$sqlstr = 'UPDATE "notice_cache" SET "tmp"=$1, "cnt"=$2, "tim"=$4 WHERE "uid"=$3 RETURNING *;';
$sqlarr = array($arr[0], intval($tar->cnt)+count($arr), $uid, date('Y-m-d H:i:s'));
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$ret = pg_fetch_object($sqlr);
pg_free_result($sqlr);
foreach($arr as $it)
{
$sqlstr = 'INSERT INTO "user_notice" ("uid", "nid") VALUES ($1, $2);';
$sqlarr = array(intval($uid), intval($it));
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
pg_free_result($sqlr);
}
return $ret;
}
private static function check_new($sqlc, $uid)
{
$sqlstr = 'SELECT * FROM "notice_cache" WHERE "uid"=$1;';
$sqlarr = array($uid);
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$ret = pg_fetch_object($sqlr);
if($ret === false)
{
$ret = notice::ins_uid($sqlc, $uid);
}
pg_free_result($sqlr);
$sqlstr = 'SELECT * FROM "notice" WHERE "nid" > $1 ORDER BY "tim";';
$sqlarr = array(intval($ret->tmp));
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$buf = array();
while($row = pg_fetch_object($sqlr))
{
if(self::is_match($sqlc, $uid, $row))
{
array_unshift($buf, $row->nid);
}
}
pg_free_result($sqlr);
if(count($buf) > 0)
{
return self::update($sqlc, $uid, $ret, $buf);
}
return $ret;
}
// **warning**: this function will clear notice count of uid!!
public static function get($sqlc, $uid, $typ, $arg1, $arg2)
{
$uid = intval($uid);
if(!$uid || !$sqlc)
{
return false;
}
$ret = self::check_new($sqlc, $uid);
if($typ == NOTICE_ACT_CNT)
{
return intval($ret->cnt);
}
else if($typ == NOTICE_ACT_NEW)
{
if($ret->cnt == 0)
{
return array();
}
$sqlstr = 'SELECT "nid" FROM "user_notice" WHERE "uid"=$1 AND "nid">$2 ORDER BY "nid" DESC';
$sqlarr = array($uid, intval($ret->rsv));
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$nids = array();
while($it=pg_fetch_array($sqlr))
{
$nids[] = $it[0];
}
$sqlstr = 'SELECT * FROM "notice" WHERE "nid" IN ('.join($nids, ",").') ORDER BY "tim";';
$sqlr = pg_query($sqlc, $sqlstr);
$buf = array();
while($it = pg_fetch_object($sqlr))
{
$it->nid = intval($it->nid);
$it->typ = intval($it->typ);
$buf[] = $it;
}
pg_free_result($sqlr);
$sqlstr = 'UPDATE "notice_cache" SET "cnt"=0, "rsv"=$2 WHERE "uid"=$1;';
$sqlarr = array($uid, $nids[0]);
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
return $buf;
}
else if($typ == NOTICE_ACT_OLD)
{
if($arg2 <= 0)
{
$arg2 = 1;
}
if($arg2 > NOTICE_MAX_LIM)
{
$arg2 = NOTICE_MAX_LIM;
}
$sqlstr = 'SELECT "nid" FROM "user_notice" WHERE "uid"=$1 AND "nid"<$2 ORDER BY "nid" DESC LIMIT $3';
$sqlarr = array($uid, $arg1, $arg2);
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$nids = array();
while($it=pg_fetch_array($sqlr))
{
$nids[] = $it[0];
}
$sqlstr = 'SELECT * FROM "notice" WHERE "nid" IN ('.join($nids, ",").') ORDER BY "tim";';
$sqlr = pg_query($sqlc, $sqlstr);
$buf = array();
while($it = pg_fetch_object($sqlr))
{
$it->nid = intval($it->nid);
$it->typ = intval($it->typ);
$buf[] = $it;
}
pg_free_result($sqlr);
return $buf;
}
return false;
}
public static function clr($sqlc, $uid, $lim=NOTICE_DEF_LIM)
{
return false;
if($lim < 0)
{
$lim = 0;
}
$ret = self::check_new($sqlc, $uid);
$buf = preg_split("/[,]/", $ret->tmp);
$buf = array_slice($buf, 0, $ret->cnt+$lim);
$sqlstr = 'UPDATE "notice_cache" SET "tmp"=$1 WHERE "uid"=$2;';
$sqlarr = array(join($buf, ","), $uid);
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
if($sqlr)
{
return count($buf);
}
return false;
}
// you can add notice by calling it.
public static function add($sqlc, $typ, $val, $txt)
{
if(!is_string($val))
{
$val = json_encode($val);
}
$sqlstr = 'INSERT INTO "notice" ("typ", "val", "txt", "tim") VALUES ($1, $2, $3, $4) RETURNING *;';
$sqlarr = array($typ, $val, $txt, date('Y-m-d H:i:s'));
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
$ret = pg_fetch_object($sqlr);
if($ret === false)
{
return false;
}
return json_encode($ret);
}
public static function del($sqlc, $cond)
{
$sqlstr = 'DELETE FROM "notice" WHERE 0';
if(isset($cond['nid']))
{
$sqlstr .= ' OR "nid"=$1';
}
if(isset($cond['tim']))
{
$sqlstr .= ' OR "tim" >= $2';
}
$sqlarr = array($cond['nid'], $cond['tim']);
$sqlr = pg_query_params($sqlc, $sqlstr, $sqlarr);
if(!$sqlr)
{
return false;
}
return true;
}
}
?>
|