Add banana in __autoload to be sure session data are correctly handled.
[platal.git] / include / notifs.inc.php
CommitLineData
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
22define('WATCH_FICHE', 1);
23define('WATCH_INSCR', 2);
24define('WATCH_DEATH', 3);
25define('WATCH_BIRTH', 4);
26
27// {{{ function inscription_notifs_base
28
29function 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
37function register_watch_op($uid, $cid, $date='', $info='')
38{
0337d704 39 if (empty($date)) {
40 $date = date('Y-m-d');
41 };
a2a1c2f2
FB
42 XDB::execute('REPLACE INTO watch_ops (uid,cid,known,date,info)
43 VALUES ({?}, {?}, NOW(), {?}, {?})',
44 $uid, $cid, $date, $info);
0337d704 45 if($cid == WATCH_FICHE) {
a2a1c2f2
FB
46 if ($info) {
47 register_profile_update($uid, $info);
48 }
a3a049fc 49 XDB::execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid);
0337d704 50 } elseif($cid == WATCH_INSCR) {
a3a049fc 51 XDB::execute('REPLACE INTO contacts (uid,contact)
612a2d8a 52 SELECT uid,ni_id
53 FROM watch_nonins
54 WHERE ni_id={?}', $uid);
a3a049fc 55 XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
0337d704 56 }
57}
58
59// }}}
60// {{{ function _select_notifs_base
61
62function _select_notifs_base($table, $mail, $where)
63{
64 $cases = Array(
612a2d8a 65 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false,
66 'freq_sql' => '',
67 'contact_sql' => '1'
68 ),
69 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true,
70 '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 71 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
612a2d8a 72 ),
3ebe4a0a 73 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true,
612a2d8a 74 'freq_sql' => '',
3ebe4a0a 75 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
612a2d8a 76 )
0337d704 77 );
78
79 $our = $cases[$table];
80 $sql = "
81 (
612a2d8a 82 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
83 u.deces != 0 AS dcd,
84 a.alias AS bestalias,
85 wo.*,
86 {$our['contact_sql']} AS contact,
87 (u.perms IN('admin','user')) AS inscrit";
0337d704 88 if ($mail) {
89 $sql.=",
612a2d8a 90 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
91 b.alias AS abestalias, (v.flags='femme') AS sexe, q.core_mail_fmt AS mail_fmt";
0337d704 92 }
93
94 $sql .= "
95 FROM $table AS w
96 INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
97 INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
98 INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
99 if ($mail) {
100 $sql .="
101 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
102 }
103 if ($our['need_contact']) {
104 $sql .="
105 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
106 }
107
108 $sql .="
109 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() )').")
110 INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
111 INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
112 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
612a2d8a 113 WHERE $where
114 )";
0337d704 115
116 return $sql;
117}
118
119// }}}
120// {{{ function select_notifs
121
122function select_notifs($mail, $uid=null, $last=null, $iterator=true)
123{
0337d704 124 $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}';
125 $sql = _select_notifs_base('contacts', $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
126 $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
127 $sql .= _select_notifs_base('watch_nonins', $mail, $where);
128
129 if ($iterator) {
08cce2ff 130 return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
0337d704 131 } else {
08cce2ff 132 return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
0337d704 133 }
134}
135
136// }}}
137// {{{ function getNbNotifs
138
eaf30d86 139function getNbNotifs()
612a2d8a 140{
cab08090 141 if (!S::has('uid')) {
0337d704 142 return 0;
143 }
46d33a91
FB
144 $uid = S::v('uid', -1);
145 $watchlast = S::v('watch_last');
0337d704 146
a3a049fc 147 // selectionne les notifs de uid, sans detail sur le watcher, depuis
a7de4ef7 148 // $watchlast, meme ceux sans surveillance, non ordonnés
46d33a91
FB
149 $res = select_notifs(false, $uid, $watchlast, false);
150 $n = $res->numRows();
151 $res->free();
7da8ef90 152 if ($n == 0) {
153 return;
154 }
155
a7de4ef7 156 return "<a href='carnet/panel'>$n événement".($n > 1 ? 's' : '')." !</a>";
0337d704 157}
158
159// }}}
a2a1c2f2
FB
160// {{{
161
162global $prf_desc;
163$prf_desc = array('nom' => 'Son patronyme',
164 'freetext' => 'Le texte libre',
165 'mobile' => 'Son numéro de téléphone portable',
166 'nationalite' => 'Sa nationalité',
167 'nick' => 'Son surnom',
168 'web' => 'L\'adresse de son site web',
169 'appli1' => 'Son école d\'application',
170 'appli2' => 'Son école de post-application',
171 'addresses' => 'Ses adresses',
172 'section' => 'Sa section sportive',
173 'binets' => 'La liste de ses binets',
174 'medals' => 'Ses décorations',
175 'cv' => 'Son Curriculum Vitae',
176 'jobs' => 'Ses informations professionnelles',
177 'photo' => 'Sa photographie');
178
179function get_profile_change_details($event, $limit) {
180 global $prf_desc;
181 $res = XDB::iterRow("SELECT field
182 FROM watch_profile
183 WHERE uid = {?} AND ts > {?}
184 ORDER BY ts DESC",
185 $event['uid'], $limit);
186 if ($res->total() > 0) {
187 $data = array();
188 while (list($field) = $res->next()) {
189 $data[] .= $prf_desc[$field];
190 }
191 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
192 }
193 return null;
194}
195
196// }}}
197// {{{ function register_profile_update
198
199function register_profile_update($uid, $field) {
200 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
201 VALUES ({?}, NOW(), {?})",
202 $uid, $field);
203}
204
0337d704 205// {{{ class AllNotifs
206
eaf30d86 207class AllNotifs
612a2d8a 208{
209 public $_cats = Array();
210 public $_data = Array();
0337d704 211
612a2d8a 212 public function __construct()
213 {
a3a049fc 214 $res = XDB::iterator("SELECT * FROM watch_cat");
215 while($tmp = $res->next()) {
0337d704 216 $this->_cats[$tmp['id']] = $tmp;
217 }
218
a7de4ef7 219 // recupère tous les watchers, avec détails des watchers, a partir du
220 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
a3a049fc 221 $res = select_notifs(true);
0337d704 222
a3a049fc 223 while($tmp = $res->next()) {
224 $aid = $tmp['aid'];
0337d704 225 if (empty($this->_data[$aid])) {
226 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
612a2d8a 227 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'],
228 'dcd'=>$tmp['dcd']);
0337d704 229 }
a3a049fc 230 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['sexe'], $tmp['mail_fmt'], $tmp['dcd']);
231 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
232 }
0337d704 233 }
234}
235
236// }}}
237// {{{ class Notifs
238
eaf30d86 239class Notifs
612a2d8a 240{
241 public $_uid;
242 public $_cats = Array();
243 public $_data = Array();
a3a049fc 244
6720bd53 245 function __construct($uid, $up=false)
612a2d8a 246 {
a3a049fc 247 $this->_uid = $uid;
248
249 $res = XDB::iterator("SELECT * FROM watch_cat");
250 while($tmp = $res->next()) {
0337d704 251 $this->_cats[$tmp['id']] = $tmp;
252 }
253
b576bf60 254 $lastweek = date('YmdHis', time() - 7*24*60*60);
0337d704 255
a3a049fc 256 // recupere les notifs du watcher $uid, sans detail sur le watcher,
a7de4ef7 257 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
0337d704 258 $res = select_notifs(false, $uid, $lastweek);
a3a049fc 259 while($tmp = $res->next()) {
a2a1c2f2
FB
260 if ($tmp['cid'] == WATCH_FICHE) {
261 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
262 }
a3a049fc 263 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
264 }
0337d704 265
a3a049fc 266 if($up) {
267 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
268 }
0337d704 269 }
270}
271
272// }}}
273// {{{ class Watch
274
eaf30d86 275class Watch
612a2d8a 276{
277 public $_uid;
278 public $_promos;
279 public $_nonins;
280 public $_cats = Array();
281 public $_subs;
282 public $watch_contacts;
283 public $watch_mail;
284
285 public function __construct($uid)
286 {
a3a049fc 287 $this->_uid = $uid;
288 $this->_promos = new PromoNotifs($uid);
289 $this->_nonins = new NoninsNotifs($uid);
290 $this->_subs = new WatchSub($uid);
291 $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
3ebe4a0a
FB
292 FROM auth_user_quick
293 WHERE user_id={?}", $uid);
a3a049fc 294 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
295
296 $res = XDB::iterator("SELECT * FROM watch_cat");
297 while($tmp = $res->next()) {
0337d704 298 $this->_cats[$tmp['id']] = $tmp;
299 }
300 }
301
612a2d8a 302 public function saveFlags()
303 {
a3a049fc 304 $flags = "";
305 if ($this->watch_contacts)
306 $flags = "contacts";
307 if ($this->watch_mail)
308 $flags .= ($flags ? ',' : '')."mail";
309 XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
612a2d8a 310 $flags, $this->_uid);
0337d704 311 }
312
612a2d8a 313 public function cats()
314 {
a3a049fc 315 return $this->_cats;
0337d704 316 }
317
612a2d8a 318 public function subs($i)
319 {
a3a049fc 320 return $this->_subs->_data[$i];
0337d704 321 }
a3a049fc 322
612a2d8a 323 public function promos()
324 {
a3a049fc 325 return $this->_promos->toRanges();
0337d704 326 }
a3a049fc 327
612a2d8a 328 public function nonins()
329 {
a3a049fc 330 return $this->_nonins->_data;
0337d704 331 }
332}
333
334// }}}
335// {{{ class WatchSub
336
612a2d8a 337class WatchSub
338{
339 public $_uid;
340 public $_data = Array();
0337d704 341
612a2d8a 342 public function __construct($uid)
343 {
a3a049fc 344 $this->_uid = $uid;
345 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
346 while(list($c) = $res->next()) {
0337d704 347 $this->_data[$c] = $c;
348 }
349 }
350
612a2d8a 351 public function update($ind)
352 {
a3a049fc 353 $this->_data = Array();
354 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
5e2307dc 355 foreach (Env::v($ind) as $key=>$val) {
a3a049fc 356 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
0380bf85 357 if(XDB::affectedRows()) {
0337d704 358 $this->_data[$key] = $key;
359 }
a3a049fc 360 }
0337d704 361 }
362}
363
364// }}}
365// {{{ class PromoNotifs
366
612a2d8a 367class PromoNotifs
368{
369 public $_uid;
370 public $_data = Array();
0337d704 371
612a2d8a 372 public function __construct($uid)
373 {
a3a049fc 374 $this->_uid = $uid;
375 $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
376 while (list($p) = $res->next()) {
0337d704 377 $this->_data[intval($p)] = intval($p);
378 }
379 }
380
612a2d8a 381 public function add($p)
382 {
a3a049fc 383 $promo = intval($p);
384 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
385 $this->_data[$promo] = $promo;
386 asort($this->_data);
0337d704 387 }
a3a049fc 388
612a2d8a 389 public function del($p)
390 {
a3a049fc 391 $promo = intval($p);
392 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
393 unset($this->_data[$promo]);
0337d704 394 }
a3a049fc 395
612a2d8a 396 public function addRange($_p1,$_p2)
397 {
a3a049fc 398 $p1 = intval($_p1);
399 $p2 = intval($_p2);
400 $values = Array();
401 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
402 $values[] = "('{$this->_uid}',$i)";
403 $this->_data[$i] = $i;
404 }
405 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
406 asort($this->_data);
0337d704 407 }
408
612a2d8a 409 public function delRange($_p1,$_p2)
410 {
a3a049fc 411 $p1 = intval($_p1);
412 $p2 = intval($_p2);
413 $where = Array();
414 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
415 $where[] = "promo=$i";
416 unset($this->_data[$i]);
417 }
418 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
0337d704 419 }
420
612a2d8a 421 public function toRanges()
422 {
a3a049fc 423 $ranges = Array();
424 $I = Array();
425 foreach($this->_data as $promo) {
426 if(!isset($I[0])) {
427 $I = Array($promo,$promo);
428 }
429 elseif($I[1]+1 == $promo) {
430 $I[1] ++;
431 }
432 else {
433 $ranges[] = $I;
434 $I = Array($promo,$promo);
435 }
436 }
437 if(isset($I[0])) $ranges[] = $I;
438 return $ranges;
0337d704 439 }
440}
441
442// }}}
443// {{{ class NoninsNotifs
444
612a2d8a 445class NoninsNotifs
446{
447 public $_uid;
448 public $_data = Array();
0337d704 449
612a2d8a 450 public function __construct($uid)
451 {
a3a049fc 452 $this->_uid = $uid;
453 $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
612a2d8a 454 FROM watch_nonins AS w
455 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
456 WHERE w.uid = {?}
457 ORDER BY promo,nom", $uid);
a3a049fc 458 while($tmp = $res->next()) {
0337d704 459 $this->_data[$tmp['user_id']] = $tmp;
460 }
461 }
462
612a2d8a 463 public function del($p)
464 {
a3a049fc 465 unset($this->_data["$p"]);
3ebe4a0a 466 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
0337d704 467 }
468
612a2d8a 469 public function add($p)
470 {
3ebe4a0a 471 XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
a3a049fc 472 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
612a2d8a 473 FROM auth_user_md5
474 WHERE user_id={?}', $p);
a3a049fc 475 $this->_data["$p"] = $res->fetchOneAssoc();
0337d704 476 }
477}
a3a049fc 478
0337d704 479// }}}
480
a7de4ef7 481// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 482?>