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