Bye xorg.misc.inc.php
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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 }
c557ed51 57 update_NbNotifs();
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;
141$prf_desc = array('nom' => 'Son patronyme',
142 'freetext' => 'Le texte libre',
143 'mobile' => 'Son numéro de téléphone portable',
144 'nationalite' => 'Sa nationalité',
145 'nick' => 'Son surnom',
146 'web' => 'L\'adresse de son site web',
147 'appli1' => 'Son école d\'application',
148 'appli2' => 'Son école de post-application',
149 'addresses' => 'Ses adresses',
150 'section' => 'Sa section sportive',
151 'binets' => 'La liste de ses binets',
152 'medals' => 'Ses décorations',
153 'cv' => 'Son Curriculum Vitae',
154 'jobs' => 'Ses informations professionnelles',
155 'photo' => 'Sa photographie');
156
157function get_profile_change_details($event, $limit) {
158 global $prf_desc;
159 $res = XDB::iterRow("SELECT field
160 FROM watch_profile
161 WHERE uid = {?} AND ts > {?}
162 ORDER BY ts DESC",
163 $event['uid'], $limit);
164 if ($res->total() > 0) {
165 $data = array();
166 while (list($field) = $res->next()) {
167 $data[] .= $prf_desc[$field];
168 }
169 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
170 }
171 return null;
172}
173
174// }}}
175// {{{ function register_profile_update
176
177function register_profile_update($uid, $field) {
178 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
179 VALUES ({?}, NOW(), {?})",
180 $uid, $field);
181}
182
0337d704 183// {{{ class AllNotifs
184
eaf30d86 185class AllNotifs
612a2d8a 186{
187 public $_cats = Array();
188 public $_data = Array();
0337d704 189
612a2d8a 190 public function __construct()
191 {
a3a049fc 192 $res = XDB::iterator("SELECT * FROM watch_cat");
193 while($tmp = $res->next()) {
0337d704 194 $this->_cats[$tmp['id']] = $tmp;
195 }
196
a7de4ef7 197 // recupère tous les watchers, avec détails des watchers, a partir du
198 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
a3a049fc 199 $res = select_notifs(true);
0337d704 200
a3a049fc 201 while($tmp = $res->next()) {
202 $aid = $tmp['aid'];
0337d704 203 if (empty($this->_data[$aid])) {
204 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
1430b1f1 205 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
612a2d8a 206 'dcd'=>$tmp['dcd']);
0337d704 207 }
1430b1f1 208 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
a3a049fc 209 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
210 }
0337d704 211 }
212}
213
214// }}}
215// {{{ class Notifs
216
eaf30d86 217class Notifs
612a2d8a 218{
219 public $_uid;
220 public $_cats = Array();
221 public $_data = Array();
a3a049fc 222
6720bd53 223 function __construct($uid, $up=false)
612a2d8a 224 {
a3a049fc 225 $this->_uid = $uid;
226
227 $res = XDB::iterator("SELECT * FROM watch_cat");
228 while($tmp = $res->next()) {
0337d704 229 $this->_cats[$tmp['id']] = $tmp;
230 }
231
b576bf60 232 $lastweek = date('YmdHis', time() - 7*24*60*60);
0337d704 233
a3a049fc 234 // recupere les notifs du watcher $uid, sans detail sur le watcher,
a7de4ef7 235 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
0337d704 236 $res = select_notifs(false, $uid, $lastweek);
a3a049fc 237 while($tmp = $res->next()) {
a2a1c2f2
FB
238 if ($tmp['cid'] == WATCH_FICHE) {
239 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
240 }
a3a049fc 241 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
242 }
0337d704 243
a3a049fc 244 if($up) {
245 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
246 }
0337d704 247 }
248}
249
250// }}}
251// {{{ class Watch
252
eaf30d86 253class Watch
612a2d8a 254{
255 public $_uid;
256 public $_promos;
257 public $_nonins;
258 public $_cats = Array();
259 public $_subs;
260 public $watch_contacts;
261 public $watch_mail;
262
263 public function __construct($uid)
264 {
a3a049fc 265 $this->_uid = $uid;
266 $this->_promos = new PromoNotifs($uid);
267 $this->_nonins = new NoninsNotifs($uid);
268 $this->_subs = new WatchSub($uid);
269 $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
3ebe4a0a
FB
270 FROM auth_user_quick
271 WHERE user_id={?}", $uid);
a3a049fc 272 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
273
274 $res = XDB::iterator("SELECT * FROM watch_cat");
275 while($tmp = $res->next()) {
0337d704 276 $this->_cats[$tmp['id']] = $tmp;
277 }
278 }
279
612a2d8a 280 public function saveFlags()
281 {
a3a049fc 282 $flags = "";
283 if ($this->watch_contacts)
284 $flags = "contacts";
285 if ($this->watch_mail)
286 $flags .= ($flags ? ',' : '')."mail";
287 XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
612a2d8a 288 $flags, $this->_uid);
0337d704 289 }
290
612a2d8a 291 public function cats()
292 {
a3a049fc 293 return $this->_cats;
0337d704 294 }
295
612a2d8a 296 public function subs($i)
297 {
a3a049fc 298 return $this->_subs->_data[$i];
0337d704 299 }
a3a049fc 300
612a2d8a 301 public function promos()
302 {
a3a049fc 303 return $this->_promos->toRanges();
0337d704 304 }
a3a049fc 305
612a2d8a 306 public function nonins()
307 {
a3a049fc 308 return $this->_nonins->_data;
0337d704 309 }
310}
311
312// }}}
313// {{{ class WatchSub
314
612a2d8a 315class WatchSub
316{
317 public $_uid;
318 public $_data = Array();
0337d704 319
612a2d8a 320 public function __construct($uid)
321 {
a3a049fc 322 $this->_uid = $uid;
323 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
324 while(list($c) = $res->next()) {
0337d704 325 $this->_data[$c] = $c;
326 }
327 }
328
612a2d8a 329 public function update($ind)
330 {
a3a049fc 331 $this->_data = Array();
332 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
5e2307dc 333 foreach (Env::v($ind) as $key=>$val) {
a3a049fc 334 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
0380bf85 335 if(XDB::affectedRows()) {
0337d704 336 $this->_data[$key] = $key;
337 }
a3a049fc 338 }
0337d704 339 }
340}
341
342// }}}
343// {{{ class PromoNotifs
344
612a2d8a 345class PromoNotifs
346{
347 public $_uid;
348 public $_data = Array();
0337d704 349
612a2d8a 350 public function __construct($uid)
351 {
a3a049fc 352 $this->_uid = $uid;
353 $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
354 while (list($p) = $res->next()) {
0337d704 355 $this->_data[intval($p)] = intval($p);
356 }
357 }
358
612a2d8a 359 public function add($p)
360 {
a3a049fc 361 $promo = intval($p);
362 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
363 $this->_data[$promo] = $promo;
364 asort($this->_data);
0337d704 365 }
a3a049fc 366
612a2d8a 367 public function del($p)
368 {
a3a049fc 369 $promo = intval($p);
370 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
371 unset($this->_data[$promo]);
0337d704 372 }
a3a049fc 373
612a2d8a 374 public function addRange($_p1,$_p2)
375 {
a3a049fc 376 $p1 = intval($_p1);
377 $p2 = intval($_p2);
378 $values = Array();
379 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
380 $values[] = "('{$this->_uid}',$i)";
381 $this->_data[$i] = $i;
382 }
383 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
384 asort($this->_data);
0337d704 385 }
386
612a2d8a 387 public function delRange($_p1,$_p2)
388 {
a3a049fc 389 $p1 = intval($_p1);
390 $p2 = intval($_p2);
391 $where = Array();
392 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
393 $where[] = "promo=$i";
394 unset($this->_data[$i]);
395 }
396 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
0337d704 397 }
398
612a2d8a 399 public function toRanges()
400 {
a3a049fc 401 $ranges = Array();
402 $I = Array();
403 foreach($this->_data as $promo) {
404 if(!isset($I[0])) {
405 $I = Array($promo,$promo);
406 }
407 elseif($I[1]+1 == $promo) {
408 $I[1] ++;
409 }
410 else {
411 $ranges[] = $I;
412 $I = Array($promo,$promo);
413 }
414 }
415 if(isset($I[0])) $ranges[] = $I;
416 return $ranges;
0337d704 417 }
418}
419
420// }}}
421// {{{ class NoninsNotifs
422
612a2d8a 423class NoninsNotifs
424{
425 public $_uid;
426 public $_data = Array();
0337d704 427
612a2d8a 428 public function __construct($uid)
429 {
a3a049fc 430 $this->_uid = $uid;
431 $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
612a2d8a 432 FROM watch_nonins AS w
433 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
434 WHERE w.uid = {?}
435 ORDER BY promo,nom", $uid);
a3a049fc 436 while($tmp = $res->next()) {
0337d704 437 $this->_data[$tmp['user_id']] = $tmp;
438 }
439 }
440
612a2d8a 441 public function del($p)
442 {
a3a049fc 443 unset($this->_data["$p"]);
3ebe4a0a 444 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
0337d704 445 }
446
612a2d8a 447 public function add($p)
448 {
3ebe4a0a 449 XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
a3a049fc 450 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
612a2d8a 451 FROM auth_user_md5
452 WHERE user_id={?}', $p);
a3a049fc 453 $this->_data["$p"] = $res->fetchOneAssoc();
0337d704 454 }
455}
a3a049fc 456
0337d704 457// }}}
458
a7de4ef7 459// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 460?>