Merge commit 'origin/fusionax' into account
[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)
32 SELECT {?}, id
33 FROM watch_cat', $uid);
34 }
35
36 // }}}
37 // {{{ function register_watch_op
38
39 function register_watch_op($uid, $cid, $date='', $info='')
40 {
41 // XXX
42 // TODO: Find out whether uid is a user or a profile!!!
43 // XXX
44 if (empty($date)) {
45 $date = date('Y-m-d');
46 }
47 XDB::execute('REPLACE INTO watch_ops (uid, cid, known, date, info)
48 VALUES ({?}, {?}, NOW(), {?}, {?})',
49 $uid, $cid, $date, $info);
50 if($cid == WATCH_FICHE) {
51 if ($info) {
52 register_profile_update($uid, $info);
53 }
54 XDB::execute('UPDATE profiles
55 SET last_change = NOW()
56 WHERE pid = {?}', $uid);
57 } elseif($cid == WATCH_INSCR) {
58 XDB::execute('REPLACE INTO contacts (uid,contact)
59 SELECT uid, ni_id
60 FROM watch_nonins
61 WHERE ni_id = {?}', $uid);
62 XDB::execute('DELETE FROM watch_nonins
63 WHERE ni_id = {?}', $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
167 function 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
187 function register_profile_update($uid, $field) {
188 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
189 VALUES ({?}, NOW(), {?})",
190 $uid, $field);
191 }
192
193 // {{{ class AllNotifs
194
195 class AllNotifs
196 {
197 public $_cats = Array();
198 public $_data = Array();
199
200 public function __construct()
201 {
202 $res = XDB::iterator("SELECT * FROM watch_cat");
203 while($tmp = $res->next()) {
204 $this->_cats[$tmp['id']] = $tmp;
205 }
206
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
209 $res = select_notifs(true);
210
211 while($tmp = $res->next()) {
212 $aid = $tmp['aid'];
213 if (empty($this->_data[$aid])) {
214 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
215 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
216 'dcd'=>$tmp['dcd']);
217 }
218 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
219 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
220 }
221 }
222 }
223
224 // }}}
225 // {{{ class Notifs
226
227 class Notifs
228 {
229 public $_uid;
230 public $_cats = Array();
231 public $_data = Array();
232
233 function __construct($uid, $up=false)
234 {
235 $this->_uid = $uid;
236
237 $res = XDB::iterator("SELECT * FROM watch_cat");
238 while($tmp = $res->next()) {
239 $this->_cats[$tmp['id']] = $tmp;
240 }
241
242 $lastweek = date('YmdHis', time() - 7*24*60*60);
243
244 // recupere les notifs du watcher $uid, sans detail sur le watcher,
245 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
246 $res = select_notifs(false, $uid, $lastweek);
247 while($tmp = $res->next()) {
248 if ($tmp['cid'] == WATCH_FICHE) {
249 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
250 }
251 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
252 }
253
254 if($up) {
255 XDB::execute('UPDATE watch
256 SET last = NOW()
257 WHERE uid = {?}', $uid);
258 }
259 }
260 }
261
262 // }}}
263 // {{{ class Watch
264
265 class Watch
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 {
277 $this->_uid = $uid;
278 $this->_promos = new PromoNotifs($uid);
279 $this->_nonins = new NoninsNotifs($uid);
280 $this->_subs = new WatchSub($uid);
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();
286
287 $this->_cats = XDB::fetchAllAssoc('id', 'SELECT * FROM watch_cat');
288 }
289
290 public function saveFlags()
291 {
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);
299 }
300
301 public function cats()
302 {
303 return $this->_cats;
304 }
305
306 public function subs($i)
307 {
308 return $this->_subs->_data[$i];
309 }
310
311 public function promos()
312 {
313 return $this->_promos->toRanges();
314 }
315
316 public function nonins()
317 {
318 return $this->_nonins->_data;
319 }
320 }
321
322 // }}}
323 // {{{ class WatchSub
324
325 class WatchSub
326 {
327 public $_uid;
328 public $_data = Array();
329
330 public function __construct($uid)
331 {
332 $this->_uid = $uid;
333 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
334 while(list($c) = $res->next()) {
335 $this->_data[$c] = $c;
336 }
337 }
338
339 public function update($ind)
340 {
341 $this->_data = Array();
342 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
343 foreach (Env::v($ind) as $key=>$val) {
344 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
345 if(XDB::affectedRows()) {
346 $this->_data[$key] = $key;
347 }
348 }
349 }
350 }
351
352 // }}}
353 // {{{ class PromoNotifs
354
355 class PromoNotifs
356 {
357 public $_uid;
358 public $_data = Array();
359
360 public function __construct($uid)
361 {
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()) {
365 $this->_data[intval($p)] = intval($p);
366 }
367 }
368
369 public function add($p)
370 {
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);
375 }
376
377 public function del($p)
378 {
379 $promo = intval($p);
380 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
381 unset($this->_data[$promo]);
382 }
383
384 public function addRange($_p1,$_p2)
385 {
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);
395 }
396
397 public function delRange($_p1,$_p2)
398 {
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);
407 }
408
409 public function toRanges()
410 {
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;
427 }
428 }
429
430 // }}}
431 // {{{ class NoninsNotifs
432
433 class NoninsNotifs
434 {
435 public $_uid;
436 public $_data = Array();
437
438 public function __construct($uid)
439 {
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
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);
446 while($tmp = $res->next()) {
447 $this->_data[$tmp['user_id']] = $tmp;
448 }
449 }
450
451 public function del($p)
452 {
453 unset($this->_data["$p"]);
454 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
455 }
456
457 public function add($p)
458 {
459 XDB::execute('INSERT IGNORE INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
460 $res = XDB::query('SELECT prenom, IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
461 FROM auth_user_md5
462 WHERE user_id={?}', $p);
463 $this->_data["$p"] = $res->fetchOneAssoc();
464 }
465 }
466
467 // }}}
468
469 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
470 ?>