Merge branch 'platal-0.10.0'
[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 SET DATE=NOW() WHERE user_id={?}', $uid);
50 } elseif($cid == WATCH_INSCR) {
51 XDB::execute('REPLACE INTO contacts (uid,contact)
52 SELECT uid,ni_id
53 FROM watch_nonins
54 WHERE ni_id={?}', $uid);
55 XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
56 }
57 Platal::session()->updateNbNotifs();
58 }
59
60 // }}}
61 // {{{ function _select_notifs_base
62
63 function _select_notifs_base($table, $mail, $where)
64 {
65 $cases = Array(
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) )',
72 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
73 ),
74 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true,
75 'freq_sql' => '',
76 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
77 )
78 );
79
80 $our = $cases[$table];
81 $sql = "
82 (
83 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
84 u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
85 a.alias AS bestalias,
86 wo.*,
87 {$our['contact_sql']} AS contact,
88 (u.perms IN('admin','user')) AS inscrit";
89 if ($mail) {
90 $sql.=",
91 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
92 b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
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))
114 WHERE $where
115 )";
116
117 return $sql;
118 }
119
120 // }}}
121 // {{{ function select_notifs
122
123 function select_notifs($mail, $uid=null, $last=null, $iterator=true)
124 {
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) {
131 return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
132 } else {
133 return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
134 }
135 }
136
137 // }}}
138 // {{{
139
140 global $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
157 function 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
177 function register_profile_update($uid, $field) {
178 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
179 VALUES ({?}, NOW(), {?})",
180 $uid, $field);
181 }
182
183 // {{{ class AllNotifs
184
185 class AllNotifs
186 {
187 public $_cats = Array();
188 public $_data = Array();
189
190 public function __construct()
191 {
192 $res = XDB::iterator("SELECT * FROM watch_cat");
193 while($tmp = $res->next()) {
194 $this->_cats[$tmp['id']] = $tmp;
195 }
196
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
199 $res = select_notifs(true);
200
201 while($tmp = $res->next()) {
202 $aid = $tmp['aid'];
203 if (empty($this->_data[$aid])) {
204 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
205 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
206 'dcd'=>$tmp['dcd']);
207 }
208 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
209 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
210 }
211 }
212 }
213
214 // }}}
215 // {{{ class Notifs
216
217 class Notifs
218 {
219 public $_uid;
220 public $_cats = Array();
221 public $_data = Array();
222
223 function __construct($uid, $up=false)
224 {
225 $this->_uid = $uid;
226
227 $res = XDB::iterator("SELECT * FROM watch_cat");
228 while($tmp = $res->next()) {
229 $this->_cats[$tmp['id']] = $tmp;
230 }
231
232 $lastweek = date('YmdHis', time() - 7*24*60*60);
233
234 // recupere les notifs du watcher $uid, sans detail sur le watcher,
235 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
236 $res = select_notifs(false, $uid, $lastweek);
237 while($tmp = $res->next()) {
238 if ($tmp['cid'] == WATCH_FICHE) {
239 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
240 }
241 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
242 }
243
244 if($up) {
245 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
246 }
247 }
248 }
249
250 // }}}
251 // {{{ class Watch
252
253 class Watch
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 {
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)
270 FROM auth_user_quick
271 WHERE user_id={?}", $uid);
272 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
273
274 $res = XDB::iterator("SELECT * FROM watch_cat");
275 while($tmp = $res->next()) {
276 $this->_cats[$tmp['id']] = $tmp;
277 }
278 }
279
280 public function saveFlags()
281 {
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={?}',
288 $flags, $this->_uid);
289 }
290
291 public function cats()
292 {
293 return $this->_cats;
294 }
295
296 public function subs($i)
297 {
298 return $this->_subs->_data[$i];
299 }
300
301 public function promos()
302 {
303 return $this->_promos->toRanges();
304 }
305
306 public function nonins()
307 {
308 return $this->_nonins->_data;
309 }
310 }
311
312 // }}}
313 // {{{ class WatchSub
314
315 class WatchSub
316 {
317 public $_uid;
318 public $_data = Array();
319
320 public function __construct($uid)
321 {
322 $this->_uid = $uid;
323 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
324 while(list($c) = $res->next()) {
325 $this->_data[$c] = $c;
326 }
327 }
328
329 public function update($ind)
330 {
331 $this->_data = Array();
332 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
333 foreach (Env::v($ind) as $key=>$val) {
334 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
335 if(XDB::affectedRows()) {
336 $this->_data[$key] = $key;
337 }
338 }
339 }
340 }
341
342 // }}}
343 // {{{ class PromoNotifs
344
345 class PromoNotifs
346 {
347 public $_uid;
348 public $_data = Array();
349
350 public function __construct($uid)
351 {
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()) {
355 $this->_data[intval($p)] = intval($p);
356 }
357 }
358
359 public function add($p)
360 {
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);
365 }
366
367 public function del($p)
368 {
369 $promo = intval($p);
370 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
371 unset($this->_data[$promo]);
372 }
373
374 public function addRange($_p1,$_p2)
375 {
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);
385 }
386
387 public function delRange($_p1,$_p2)
388 {
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);
397 }
398
399 public function toRanges()
400 {
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;
417 }
418 }
419
420 // }}}
421 // {{{ class NoninsNotifs
422
423 class NoninsNotifs
424 {
425 public $_uid;
426 public $_data = Array();
427
428 public function __construct($uid)
429 {
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
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);
436 while($tmp = $res->next()) {
437 $this->_data[$tmp['user_id']] = $tmp;
438 }
439 }
440
441 public function del($p)
442 {
443 unset($this->_data["$p"]);
444 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
445 }
446
447 public function add($p)
448 {
449 XDB::execute('INSERT IGNORE INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
450 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
451 FROM auth_user_md5
452 WHERE user_id={?}', $p);
453 $this->_data["$p"] = $res->fetchOneAssoc();
454 }
455 }
456
457 // }}}
458
459 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
460 ?>