Happy New Year!!!
[platal.git] / include / notifs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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 {
31 XDB::execute('REPLACE INTO watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
32 }
33
34 // }}}
35 // {{{ function register_watch_op
36
37 function register_watch_op($uid, $cid, $date = '', $info = '')
38 {
39 if (empty($date)) {
40 $date = date('Y-m-d');
41 }
42 XDB::execute('REPLACE INTO watch_ops (uid, cid, known, date, info)
43 VALUES ({?}, {?}, NOW(), {?}, {?})',
44 $uid, $cid, $date, $info);
45 if ($cid == WATCH_FICHE) {
46 if ($info) {
47 register_profile_update($uid, $info);
48 }
49 XDB::execute('UPDATE auth_user_md5
50 SET DATE = NOW()
51 WHERE user_id = {?}', $uid);
52 } elseif ($cid == WATCH_INSCR) {
53 XDB::execute('REPLACE INTO contacts (uid, contact)
54 SELECT uid, ni_id
55 FROM watch_nonins
56 WHERE ni_id = {?}', $uid);
57 XDB::execute('DELETE FROM watch_nonins
58 WHERE ni_id = {?}', $uid);
59 } elseif ($cid == WATCH_DEATH) {
60 // We delete nonins watches both for the dead (if non registered), and
61 // for people watched by the dead.
62 XDB::execute('DELETE FROM watch_nonins
63 WHERE ni_id = {?} OR uid = {?}', $uid);
64 }
65 Platal::session()->updateNbNotifs();
66 }
67
68 // }}}
69 // {{{ function _select_notifs_base
70
71 function _select_notifs_base($table, $mail, $where)
72 {
73 $cases = Array(
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) )',
80 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
81 ),
82 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true,
83 'freq_sql' => '',
84 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
85 )
86 );
87
88 $our = $cases[$table];
89 $sql = "
90 (
91 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
92 u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
93 a.alias AS bestalias,
94 wo.*,
95 {$our['contact_sql']} AS contact,
96 (u.perms IN('admin','user')) AS inscrit";
97 if ($mail) {
98 $sql.=",
99 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
100 b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
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))
122 WHERE $where
123 )";
124
125 return $sql;
126 }
127
128 // }}}
129 // {{{ function select_notifs
130
131 function select_notifs($mail, $uid=null, $last=null, $iterator=true)
132 {
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) {
139 return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
140 } else {
141 return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
142 }
143 }
144
145 // }}}
146 // {{{
147
148 global $prf_desc;
149 $prf_desc = array('nom' => 'Son patronyme',
150 'freetext' => 'Le texte libre',
151 'mobile' => 'Son numéro de téléphone portable',
152 'nationalite' => 'Sa nationalité',
153 'nick' => 'Son surnom',
154 'web' => "L'adresse de son site web",
155 'appli1' => "Son école d'application",
156 'appli2' => 'Son école de post-application',
157 'addresses' => 'Ses adresses',
158 'section' => 'Sa section sportive',
159 'binets' => 'La liste de ses binets',
160 'medals' => 'Ses décorations',
161 'cv' => 'Son curriculum vitae',
162 'jobs' => 'Ses informations professionnelles',
163 'photo' => 'Sa photographie',
164 'broken' => "Il n'a plus d'adresse de redirection valide");
165
166 function get_profile_change_details($event, $limit) {
167 global $prf_desc;
168 $res = XDB::iterRow("SELECT field
169 FROM watch_profile
170 WHERE uid = {?} AND ts > {?}
171 ORDER BY ts DESC",
172 $event['uid'], $limit);
173 if ($res->total() > 0) {
174 $data = array();
175 while (list($field) = $res->next()) {
176 $data[] .= $prf_desc[$field];
177 }
178 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
179 }
180 return null;
181 }
182
183 // }}}
184 // {{{ function register_profile_update
185
186 function register_profile_update($uid, $field) {
187 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
188 VALUES ({?}, NOW(), {?})",
189 $uid, $field);
190 }
191
192 // {{{ class AllNotifs
193
194 class AllNotifs
195 {
196 public $_cats = Array();
197 public $_data = Array();
198
199 public function __construct()
200 {
201 $res = XDB::iterator("SELECT * FROM watch_cat");
202 while($tmp = $res->next()) {
203 $this->_cats[$tmp['id']] = $tmp;
204 }
205
206 // recupère tous les watchers, avec détails des watchers, a partir du
207 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
208 $res = select_notifs(true);
209
210 while($tmp = $res->next()) {
211 $aid = $tmp['aid'];
212 if (empty($this->_data[$aid])) {
213 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
214 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
215 'dcd'=>$tmp['dcd']);
216 }
217 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
218 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
219 }
220 }
221 }
222
223 // }}}
224 // {{{ class Notifs
225
226 class Notifs
227 {
228 public $_uid;
229 public $_cats = Array();
230 public $_data = Array();
231
232 function __construct($uid, $up=false)
233 {
234 $this->_uid = $uid;
235
236 $res = XDB::iterator("SELECT * FROM watch_cat");
237 while($tmp = $res->next()) {
238 $this->_cats[$tmp['id']] = $tmp;
239 }
240
241 $lastweek = date('YmdHis', time() - 7*24*60*60);
242
243 // recupere les notifs du watcher $uid, sans detail sur le watcher,
244 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
245 $res = select_notifs(false, $uid, $lastweek);
246 while($tmp = $res->next()) {
247 if ($tmp['cid'] == WATCH_FICHE) {
248 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
249 }
250 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
251 }
252
253 if($up) {
254 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
255 }
256 }
257 }
258
259 // }}}
260 // {{{ class Watch
261
262 class Watch
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 {
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)
279 FROM auth_user_quick
280 WHERE user_id={?}", $uid);
281 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
282
283 $res = XDB::iterator("SELECT * FROM watch_cat");
284 while($tmp = $res->next()) {
285 $this->_cats[$tmp['id']] = $tmp;
286 }
287 }
288
289 public function saveFlags()
290 {
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={?}',
297 $flags, $this->_uid);
298 }
299
300 public function cats()
301 {
302 return $this->_cats;
303 }
304
305 public function subs($i)
306 {
307 return $this->_subs->_data[$i];
308 }
309
310 public function promos()
311 {
312 return $this->_promos->toRanges();
313 }
314
315 public function nonins()
316 {
317 return $this->_nonins->_data;
318 }
319 }
320
321 // }}}
322 // {{{ class WatchSub
323
324 class WatchSub
325 {
326 public $_uid;
327 public $_data = Array();
328
329 public function __construct($uid)
330 {
331 $this->_uid = $uid;
332 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
333 while(list($c) = $res->next()) {
334 $this->_data[$c] = $c;
335 }
336 }
337
338 public function update($ind)
339 {
340 $this->_data = Array();
341 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
342 foreach (Env::v($ind) as $key=>$val) {
343 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
344 if(XDB::affectedRows()) {
345 $this->_data[$key] = $key;
346 }
347 }
348 }
349 }
350
351 // }}}
352 // {{{ class PromoNotifs
353
354 class PromoNotifs
355 {
356 public $_uid;
357 public $_data = Array();
358
359 public function __construct($uid)
360 {
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()) {
364 $this->_data[intval($p)] = intval($p);
365 }
366 }
367
368 public function add($p)
369 {
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);
374 }
375
376 public function del($p)
377 {
378 $promo = intval($p);
379 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
380 unset($this->_data[$promo]);
381 }
382
383 public function addRange($_p1,$_p2)
384 {
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);
394 }
395
396 public function delRange($_p1,$_p2)
397 {
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);
406 }
407
408 public function toRanges()
409 {
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;
426 }
427 }
428
429 // }}}
430 // {{{ class NoninsNotifs
431
432 class NoninsNotifs
433 {
434 public $_uid;
435 public $_data = Array();
436
437 public function __construct($uid)
438 {
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
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);
445 while($tmp = $res->next()) {
446 $this->_data[$tmp['user_id']] = $tmp;
447 }
448 }
449
450 public function del($p)
451 {
452 unset($this->_data["$p"]);
453 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
454 }
455
456 public function add($p)
457 {
458 XDB::execute('INSERT IGNORE INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
459 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
460 FROM auth_user_md5
461 WHERE user_id={?}', $p);
462 $this->_data["$p"] = $res->fetchOneAssoc();
463 }
464 }
465
466 // }}}
467
468 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
469 ?>