Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5ddeb07c | 3 | * Copyright (C) 2003-2007 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | define('WATCH_FICHE', 1); | |
23 | define('WATCH_INSCR', 2); | |
24 | define('WATCH_DEATH', 3); | |
25 | define('WATCH_BIRTH', 4); | |
26 | ||
27 | // {{{ function inscription_notifs_base | |
28 | ||
29 | function inscription_notifs_base($uid) | |
30 | { | |
08cce2ff | 31 | XDB::execute('REPLACE INTO watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid); |
0337d704 | 32 | } |
33 | ||
34 | // }}} | |
35 | // {{{ function register_watch_op | |
36 | ||
37 | function register_watch_op($uid, $cid, $date='', $info='') | |
38 | { | |
0337d704 | 39 | if (empty($date)) { |
40 | $date = date('Y-m-d'); | |
41 | }; | |
08cce2ff | 42 | XDB::execute('REPLACE INTO watch_ops (uid,cid,known,date,info) VALUES({?},{?},NOW(),{?},{?})', |
612a2d8a | 43 | $uid, $cid, $date, $info); |
0337d704 | 44 | if($cid == WATCH_FICHE) { |
a3a049fc | 45 | XDB::execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid); |
0337d704 | 46 | } elseif($cid == WATCH_INSCR) { |
a3a049fc | 47 | XDB::execute('REPLACE INTO contacts (uid,contact) |
612a2d8a | 48 | SELECT uid,ni_id |
49 | FROM watch_nonins | |
50 | WHERE ni_id={?}', $uid); | |
a3a049fc | 51 | XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid); |
0337d704 | 52 | } |
53 | } | |
54 | ||
55 | // }}} | |
e15dcc03 FB |
56 | // {{{ function _select_notifs_count |
57 | ||
58 | function _select_notifs_count($table) | |
59 | { | |
60 | $cases = Array( | |
61 | 'contacts' => Array('wfield' => 'contact', | |
62 | 'ufield' => 'user_id', | |
63 | 'freq_sql' => '', | |
64 | 'where' => 'AND (q.watch_flags=1 OR q.watch_flags=3)' | |
65 | ), | |
66 | 'watch_promo' => Array('wfield' => 'promo', | |
67 | 'ufield' => 'promo', | |
68 | 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" | |
69 | AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )', | |
70 | 'where' => '', | |
71 | ), | |
72 | 'watch_nonins' => Array('wfield' => 'ni_id', | |
73 | 'ufield' => 'user_id', | |
74 | 'freq_sql' => '', | |
75 | 'where' => '', | |
76 | ) | |
77 | ); | |
78 | $our = $cases[$table]; | |
79 | $sql = " | |
80 | ( | |
81 | SELECT COUNT(1) AS count | |
82 | FROM $table AS w | |
83 | INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']}) | |
84 | INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid) | |
85 | INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id) | |
86 | INNER JOIN watch_ops AS wo ON(wo.uid = u.user_id AND wo.known > q.watch_last) | |
87 | INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid) | |
88 | INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']}) | |
89 | WHERE w.uid = {?} {$our['where']} | |
90 | )"; | |
91 | return $sql; | |
92 | } | |
93 | ||
94 | // }}} | |
0337d704 | 95 | // {{{ function _select_notifs_base |
96 | ||
97 | function _select_notifs_base($table, $mail, $where) | |
98 | { | |
99 | $cases = Array( | |
612a2d8a | 100 | 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false, |
101 | 'freq_sql' => '', | |
102 | 'contact_sql' => '1' | |
103 | ), | |
104 | 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true, | |
105 | 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )', | |
3ebe4a0a | 106 | 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)' |
612a2d8a | 107 | ), |
3ebe4a0a | 108 | 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true, |
612a2d8a | 109 | 'freq_sql' => '', |
3ebe4a0a | 110 | 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)' |
612a2d8a | 111 | ) |
0337d704 | 112 | ); |
113 | ||
114 | $our = $cases[$table]; | |
115 | $sql = " | |
116 | ( | |
612a2d8a | 117 | SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, |
118 | u.deces != 0 AS dcd, | |
119 | a.alias AS bestalias, | |
120 | wo.*, | |
121 | {$our['contact_sql']} AS contact, | |
122 | (u.perms IN('admin','user')) AS inscrit"; | |
0337d704 | 123 | if ($mail) { |
124 | $sql.=", | |
612a2d8a | 125 | w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom, |
126 | b.alias AS abestalias, (v.flags='femme') AS sexe, q.core_mail_fmt AS mail_fmt"; | |
0337d704 | 127 | } |
128 | ||
129 | $sql .= " | |
130 | FROM $table AS w | |
131 | INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']}) | |
132 | INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid) | |
133 | INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)"; | |
134 | if ($mail) { | |
135 | $sql .=" | |
136 | INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))"; | |
137 | } | |
138 | if ($our['need_contact']) { | |
139 | $sql .=" | |
140 | LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)"; | |
141 | } | |
142 | ||
143 | $sql .=" | |
144 | INNER JOIN watch_ops AS wo ON(wo.uid = u.user_id AND ".($mail ? 'wo.known > q.watch_last' : '( wo.known > {?} OR wo.date=NOW() )').") | |
145 | INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid) | |
146 | INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']}) | |
147 | LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags)) | |
612a2d8a | 148 | WHERE $where |
149 | )"; | |
0337d704 | 150 | |
151 | return $sql; | |
152 | } | |
153 | ||
154 | // }}} | |
155 | // {{{ function select_notifs | |
156 | ||
157 | function select_notifs($mail, $uid=null, $last=null, $iterator=true) | |
158 | { | |
0337d704 | 159 | $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}'; |
160 | $sql = _select_notifs_base('contacts', $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT "; | |
161 | $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT "; | |
162 | $sql .= _select_notifs_base('watch_nonins', $mail, $where); | |
163 | ||
164 | if ($iterator) { | |
08cce2ff | 165 | return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid); |
0337d704 | 166 | } else { |
08cce2ff | 167 | return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid); |
0337d704 | 168 | } |
169 | } | |
170 | ||
171 | // }}} | |
172 | // {{{ function getNbNotifs | |
173 | ||
eaf30d86 | 174 | function getNbNotifs() |
612a2d8a | 175 | { |
cab08090 | 176 | if (!S::has('uid')) { |
0337d704 | 177 | return 0; |
178 | } | |
e15dcc03 | 179 | $uid = S::i('uid'); |
0337d704 | 180 | |
a3a049fc | 181 | // selectionne les notifs de uid, sans detail sur le watcher, depuis |
a7de4ef7 | 182 | // $watchlast, meme ceux sans surveillance, non ordonnés |
b4ee37a4 FB |
183 | $res = XDB::query(_select_notifs_count('contacts') . ' UNION ' |
184 | . _select_notifs_count('watch_promo') . ' UNION ' | |
e15dcc03 FB |
185 | . _select_notifs_count('watch_nonins'), $uid, $uid, $uid); |
186 | $n = array_sum($res->fetchColumn()); | |
7da8ef90 | 187 | if ($n == 0) { |
188 | return; | |
189 | } | |
190 | ||
a7de4ef7 | 191 | return "<a href='carnet/panel'>$n événement".($n > 1 ? 's' : '')." !</a>"; |
0337d704 | 192 | } |
193 | ||
194 | // }}} | |
195 | // {{{ class AllNotifs | |
196 | ||
eaf30d86 | 197 | class AllNotifs |
612a2d8a | 198 | { |
199 | public $_cats = Array(); | |
200 | public $_data = Array(); | |
0337d704 | 201 | |
612a2d8a | 202 | public function __construct() |
203 | { | |
a3a049fc | 204 | $res = XDB::iterator("SELECT * FROM watch_cat"); |
205 | while($tmp = $res->next()) { | |
0337d704 | 206 | $this->_cats[$tmp['id']] = $tmp; |
207 | } | |
208 | ||
a7de4ef7 | 209 | // recupère tous les watchers, avec détails des watchers, a partir du |
210 | // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés | |
a3a049fc | 211 | $res = select_notifs(true); |
0337d704 | 212 | |
a3a049fc | 213 | while($tmp = $res->next()) { |
214 | $aid = $tmp['aid']; | |
0337d704 | 215 | if (empty($this->_data[$aid])) { |
216 | $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'], | |
612a2d8a | 217 | 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'], |
218 | 'dcd'=>$tmp['dcd']); | |
0337d704 | 219 | } |
a3a049fc | 220 | unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['sexe'], $tmp['mail_fmt'], $tmp['dcd']); |
221 | $this->_data[$aid]['data'][$tmp['cid']][] = $tmp; | |
222 | } | |
0337d704 | 223 | } |
224 | } | |
225 | ||
226 | // }}} | |
227 | // {{{ class Notifs | |
228 | ||
eaf30d86 | 229 | class Notifs |
612a2d8a | 230 | { |
231 | public $_uid; | |
232 | public $_cats = Array(); | |
233 | public $_data = Array(); | |
a3a049fc | 234 | |
6720bd53 | 235 | function __construct($uid, $up=false) |
612a2d8a | 236 | { |
a3a049fc | 237 | $this->_uid = $uid; |
238 | ||
239 | $res = XDB::iterator("SELECT * FROM watch_cat"); | |
240 | while($tmp = $res->next()) { | |
0337d704 | 241 | $this->_cats[$tmp['id']] = $tmp; |
242 | } | |
243 | ||
b576bf60 | 244 | $lastweek = date('YmdHis', time() - 7*24*60*60); |
0337d704 | 245 | |
a3a049fc | 246 | // recupere les notifs du watcher $uid, sans detail sur le watcher, |
a7de4ef7 | 247 | // depuis la semaine dernière, meme ceux sans surveillance, ordonnés |
0337d704 | 248 | $res = select_notifs(false, $uid, $lastweek); |
a3a049fc | 249 | while($tmp = $res->next()) { |
250 | $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp; | |
251 | } | |
0337d704 | 252 | |
a3a049fc | 253 | if($up) { |
254 | XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid); | |
255 | } | |
0337d704 | 256 | } |
257 | } | |
258 | ||
259 | // }}} | |
260 | // {{{ class Watch | |
261 | ||
eaf30d86 | 262 | class Watch |
612a2d8a | 263 | { |
264 | public $_uid; | |
265 | public $_promos; | |
266 | public $_nonins; | |
267 | public $_cats = Array(); | |
268 | public $_subs; | |
269 | public $watch_contacts; | |
270 | public $watch_mail; | |
271 | ||
272 | public function __construct($uid) | |
273 | { | |
a3a049fc | 274 | $this->_uid = $uid; |
275 | $this->_promos = new PromoNotifs($uid); | |
276 | $this->_nonins = new NoninsNotifs($uid); | |
277 | $this->_subs = new WatchSub($uid); | |
278 | $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags) | |
3ebe4a0a FB |
279 | FROM auth_user_quick |
280 | WHERE user_id={?}", $uid); | |
a3a049fc | 281 | list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow(); |
282 | ||
283 | $res = XDB::iterator("SELECT * FROM watch_cat"); | |
284 | while($tmp = $res->next()) { | |
0337d704 | 285 | $this->_cats[$tmp['id']] = $tmp; |
286 | } | |
287 | } | |
288 | ||
612a2d8a | 289 | public function saveFlags() |
290 | { | |
a3a049fc | 291 | $flags = ""; |
292 | if ($this->watch_contacts) | |
293 | $flags = "contacts"; | |
294 | if ($this->watch_mail) | |
295 | $flags .= ($flags ? ',' : '')."mail"; | |
296 | XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}', | |
612a2d8a | 297 | $flags, $this->_uid); |
0337d704 | 298 | } |
299 | ||
612a2d8a | 300 | public function cats() |
301 | { | |
a3a049fc | 302 | return $this->_cats; |
0337d704 | 303 | } |
304 | ||
612a2d8a | 305 | public function subs($i) |
306 | { | |
a3a049fc | 307 | return $this->_subs->_data[$i]; |
0337d704 | 308 | } |
a3a049fc | 309 | |
612a2d8a | 310 | public function promos() |
311 | { | |
a3a049fc | 312 | return $this->_promos->toRanges(); |
0337d704 | 313 | } |
a3a049fc | 314 | |
612a2d8a | 315 | public function nonins() |
316 | { | |
a3a049fc | 317 | return $this->_nonins->_data; |
0337d704 | 318 | } |
319 | } | |
320 | ||
321 | // }}} | |
322 | // {{{ class WatchSub | |
323 | ||
612a2d8a | 324 | class WatchSub |
325 | { | |
326 | public $_uid; | |
327 | public $_data = Array(); | |
0337d704 | 328 | |
612a2d8a | 329 | public function __construct($uid) |
330 | { | |
a3a049fc | 331 | $this->_uid = $uid; |
332 | $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid); | |
333 | while(list($c) = $res->next()) { | |
0337d704 | 334 | $this->_data[$c] = $c; |
335 | } | |
336 | } | |
337 | ||
612a2d8a | 338 | public function update($ind) |
339 | { | |
a3a049fc | 340 | $this->_data = Array(); |
341 | XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid); | |
5e2307dc | 342 | foreach (Env::v($ind) as $key=>$val) { |
a3a049fc | 343 | XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key); |
0380bf85 | 344 | if(XDB::affectedRows()) { |
0337d704 | 345 | $this->_data[$key] = $key; |
346 | } | |
a3a049fc | 347 | } |
0337d704 | 348 | } |
349 | } | |
350 | ||
351 | // }}} | |
352 | // {{{ class PromoNotifs | |
353 | ||
612a2d8a | 354 | class PromoNotifs |
355 | { | |
356 | public $_uid; | |
357 | public $_data = Array(); | |
0337d704 | 358 | |
612a2d8a | 359 | public function __construct($uid) |
360 | { | |
a3a049fc | 361 | $this->_uid = $uid; |
362 | $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid); | |
363 | while (list($p) = $res->next()) { | |
0337d704 | 364 | $this->_data[intval($p)] = intval($p); |
365 | } | |
366 | } | |
367 | ||
612a2d8a | 368 | public function add($p) |
369 | { | |
a3a049fc | 370 | $promo = intval($p); |
371 | XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo); | |
372 | $this->_data[$promo] = $promo; | |
373 | asort($this->_data); | |
0337d704 | 374 | } |
a3a049fc | 375 | |
612a2d8a | 376 | public function del($p) |
377 | { | |
a3a049fc | 378 | $promo = intval($p); |
379 | XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo); | |
380 | unset($this->_data[$promo]); | |
0337d704 | 381 | } |
a3a049fc | 382 | |
612a2d8a | 383 | public function addRange($_p1,$_p2) |
384 | { | |
a3a049fc | 385 | $p1 = intval($_p1); |
386 | $p2 = intval($_p2); | |
387 | $values = Array(); | |
388 | for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) { | |
389 | $values[] = "('{$this->_uid}',$i)"; | |
390 | $this->_data[$i] = $i; | |
391 | } | |
392 | XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values)); | |
393 | asort($this->_data); | |
0337d704 | 394 | } |
395 | ||
612a2d8a | 396 | public function delRange($_p1,$_p2) |
397 | { | |
a3a049fc | 398 | $p1 = intval($_p1); |
399 | $p2 = intval($_p2); | |
400 | $where = Array(); | |
401 | for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) { | |
402 | $where[] = "promo=$i"; | |
403 | unset($this->_data[$i]); | |
404 | } | |
405 | XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid); | |
0337d704 | 406 | } |
407 | ||
612a2d8a | 408 | public function toRanges() |
409 | { | |
a3a049fc | 410 | $ranges = Array(); |
411 | $I = Array(); | |
412 | foreach($this->_data as $promo) { | |
413 | if(!isset($I[0])) { | |
414 | $I = Array($promo,$promo); | |
415 | } | |
416 | elseif($I[1]+1 == $promo) { | |
417 | $I[1] ++; | |
418 | } | |
419 | else { | |
420 | $ranges[] = $I; | |
421 | $I = Array($promo,$promo); | |
422 | } | |
423 | } | |
424 | if(isset($I[0])) $ranges[] = $I; | |
425 | return $ranges; | |
0337d704 | 426 | } |
427 | } | |
428 | ||
429 | // }}} | |
430 | // {{{ class NoninsNotifs | |
431 | ||
612a2d8a | 432 | class NoninsNotifs |
433 | { | |
434 | public $_uid; | |
435 | public $_data = Array(); | |
0337d704 | 436 | |
612a2d8a | 437 | public function __construct($uid) |
438 | { | |
a3a049fc | 439 | $this->_uid = $uid; |
440 | $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id | |
612a2d8a | 441 | FROM watch_nonins AS w |
442 | INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id) | |
443 | WHERE w.uid = {?} | |
444 | ORDER BY promo,nom", $uid); | |
a3a049fc | 445 | while($tmp = $res->next()) { |
0337d704 | 446 | $this->_data[$tmp['user_id']] = $tmp; |
447 | } | |
448 | } | |
449 | ||
612a2d8a | 450 | public function del($p) |
451 | { | |
a3a049fc | 452 | unset($this->_data["$p"]); |
3ebe4a0a | 453 | XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p); |
0337d704 | 454 | } |
455 | ||
612a2d8a | 456 | public function add($p) |
457 | { | |
3ebe4a0a | 458 | XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p); |
a3a049fc | 459 | $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id |
612a2d8a | 460 | FROM auth_user_md5 |
461 | WHERE user_id={?}', $p); | |
a3a049fc | 462 | $this->_data["$p"] = $res->fetchOneAssoc(); |
0337d704 | 463 | } |
464 | } | |
a3a049fc | 465 | |
0337d704 | 466 | // }}} |
467 | ||
a7de4ef7 | 468 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 469 | ?> |