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