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