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