Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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 };
08cce2ff 42 XDB::execute('REPLACE INTO watch_ops (uid,cid,known,date,info) VALUES({?},{?},NOW(),{?},{?})',
612a2d8a 43 $uid, $cid, $date, $info);
0337d704 44 if($cid == WATCH_FICHE) {
a3a049fc 45 XDB::execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid);
0337d704 46 } elseif($cid == WATCH_INSCR) {
a3a049fc 47 XDB::execute('REPLACE INTO contacts (uid,contact)
612a2d8a 48 SELECT uid,ni_id
49 FROM watch_nonins
50 WHERE ni_id={?}', $uid);
a3a049fc 51 XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
0337d704 52 }
53}
54
55// }}}
56// {{{ function _select_notifs_base
57
58function _select_notifs_base($table, $mail, $where)
59{
60 $cases = Array(
612a2d8a 61 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false,
62 'freq_sql' => '',
63 'contact_sql' => '1'
64 ),
65 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true,
66 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
67 'contact_sql' => 'NOT (c.contact IS NULL)'
68 ),
69 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => false,
70 'freq_sql' => '',
71 'contact_sql' => '0'
72 )
0337d704 73 );
74
75 $our = $cases[$table];
76 $sql = "
77 (
612a2d8a 78 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
79 u.deces != 0 AS dcd,
80 a.alias AS bestalias,
81 wo.*,
82 {$our['contact_sql']} AS contact,
83 (u.perms IN('admin','user')) AS inscrit";
0337d704 84 if ($mail) {
85 $sql.=",
612a2d8a 86 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
87 b.alias AS abestalias, (v.flags='femme') AS sexe, q.core_mail_fmt AS mail_fmt";
0337d704 88 }
89
90 $sql .= "
91 FROM $table AS w
92 INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
93 INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
94 INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
95 if ($mail) {
96 $sql .="
97 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
98 }
99 if ($our['need_contact']) {
100 $sql .="
101 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
102 }
103
104 $sql .="
105 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() )').")
106 INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
107 INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
108 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
612a2d8a 109 WHERE $where
110 )";
0337d704 111
112 return $sql;
113}
114
115// }}}
116// {{{ function select_notifs
117
118function select_notifs($mail, $uid=null, $last=null, $iterator=true)
119{
0337d704 120 $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}';
121 $sql = _select_notifs_base('contacts', $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
122 $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
123 $sql .= _select_notifs_base('watch_nonins', $mail, $where);
124
125 if ($iterator) {
08cce2ff 126 return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
0337d704 127 } else {
08cce2ff 128 return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
0337d704 129 }
130}
131
132// }}}
133// {{{ function getNbNotifs
134
787bb3d7 135function getNbNotifs()
612a2d8a 136{
cab08090 137 if (!S::has('uid')) {
0337d704 138 return 0;
139 }
cab08090 140 $uid = S::v('uid', -1);
141 $watchlast = S::v('watch_last');
0337d704 142
a3a049fc 143 // selectionne les notifs de uid, sans detail sur le watcher, depuis
a7de4ef7 144 // $watchlast, meme ceux sans surveillance, non ordonnés
0337d704 145 $res = select_notifs(false, $uid, $watchlast, false);
146 $n = $res->numRows();
147 $res->free();
7da8ef90 148 if ($n == 0) {
149 return;
150 }
151
a7de4ef7 152 return "<a href='carnet/panel'>$n événement".($n > 1 ? 's' : '')." !</a>";
0337d704 153}
154
155// }}}
156// {{{ class AllNotifs
157
787bb3d7 158class AllNotifs
612a2d8a 159{
160 public $_cats = Array();
161 public $_data = Array();
0337d704 162
612a2d8a 163 public function __construct()
164 {
a3a049fc 165 $res = XDB::iterator("SELECT * FROM watch_cat");
166 while($tmp = $res->next()) {
0337d704 167 $this->_cats[$tmp['id']] = $tmp;
168 }
169
a7de4ef7 170 // recupère tous les watchers, avec détails des watchers, a partir du
171 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
a3a049fc 172 $res = select_notifs(true);
0337d704 173
a3a049fc 174 while($tmp = $res->next()) {
175 $aid = $tmp['aid'];
0337d704 176 if (empty($this->_data[$aid])) {
177 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
612a2d8a 178 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'],
179 'dcd'=>$tmp['dcd']);
0337d704 180 }
a3a049fc 181 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['sexe'], $tmp['mail_fmt'], $tmp['dcd']);
182 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
183 }
0337d704 184 }
185}
186
187// }}}
188// {{{ class Notifs
189
787bb3d7 190class Notifs
612a2d8a 191{
192 public $_uid;
193 public $_cats = Array();
194 public $_data = Array();
a3a049fc 195
6720bd53 196 function __construct($uid, $up=false)
612a2d8a 197 {
a3a049fc 198 $this->_uid = $uid;
199
200 $res = XDB::iterator("SELECT * FROM watch_cat");
201 while($tmp = $res->next()) {
0337d704 202 $this->_cats[$tmp['id']] = $tmp;
203 }
204
b576bf60 205 $lastweek = date('YmdHis', time() - 7*24*60*60);
0337d704 206
a3a049fc 207 // recupere les notifs du watcher $uid, sans detail sur le watcher,
a7de4ef7 208 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
0337d704 209 $res = select_notifs(false, $uid, $lastweek);
a3a049fc 210 while($tmp = $res->next()) {
211 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
212 }
0337d704 213
a3a049fc 214 if($up) {
215 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
216 }
0337d704 217 }
218}
219
220// }}}
221// {{{ class Watch
222
787bb3d7 223class Watch
612a2d8a 224{
225 public $_uid;
226 public $_promos;
227 public $_nonins;
228 public $_cats = Array();
229 public $_subs;
230 public $watch_contacts;
231 public $watch_mail;
232
233 public function __construct($uid)
234 {
a3a049fc 235 $this->_uid = $uid;
236 $this->_promos = new PromoNotifs($uid);
237 $this->_nonins = new NoninsNotifs($uid);
238 $this->_subs = new WatchSub($uid);
239 $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
612a2d8a 240 FROM auth_user_quick
241 WHERE user_id={?}", $uid);
a3a049fc 242 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
243
244 $res = XDB::iterator("SELECT * FROM watch_cat");
245 while($tmp = $res->next()) {
0337d704 246 $this->_cats[$tmp['id']] = $tmp;
247 }
248 }
249
612a2d8a 250 public function saveFlags()
251 {
a3a049fc 252 $flags = "";
253 if ($this->watch_contacts)
254 $flags = "contacts";
255 if ($this->watch_mail)
256 $flags .= ($flags ? ',' : '')."mail";
257 XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
612a2d8a 258 $flags, $this->_uid);
0337d704 259 }
260
612a2d8a 261 public function cats()
262 {
a3a049fc 263 return $this->_cats;
0337d704 264 }
265
612a2d8a 266 public function subs($i)
267 {
a3a049fc 268 return $this->_subs->_data[$i];
0337d704 269 }
a3a049fc 270
612a2d8a 271 public function promos()
272 {
a3a049fc 273 return $this->_promos->toRanges();
0337d704 274 }
a3a049fc 275
612a2d8a 276 public function nonins()
277 {
a3a049fc 278 return $this->_nonins->_data;
0337d704 279 }
280}
281
282// }}}
283// {{{ class WatchSub
284
612a2d8a 285class WatchSub
286{
287 public $_uid;
288 public $_data = Array();
0337d704 289
612a2d8a 290 public function __construct($uid)
291 {
a3a049fc 292 $this->_uid = $uid;
293 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
294 while(list($c) = $res->next()) {
0337d704 295 $this->_data[$c] = $c;
296 }
297 }
298
612a2d8a 299 public function update($ind)
300 {
a3a049fc 301 $this->_data = Array();
302 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
5e2307dc 303 foreach (Env::v($ind) as $key=>$val) {
a3a049fc 304 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
0380bf85 305 if(XDB::affectedRows()) {
0337d704 306 $this->_data[$key] = $key;
307 }
a3a049fc 308 }
0337d704 309 }
310}
311
312// }}}
313// {{{ class PromoNotifs
314
612a2d8a 315class PromoNotifs
316{
317 public $_uid;
318 public $_data = Array();
0337d704 319
612a2d8a 320 public function __construct($uid)
321 {
a3a049fc 322 $this->_uid = $uid;
323 $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
324 while (list($p) = $res->next()) {
0337d704 325 $this->_data[intval($p)] = intval($p);
326 }
327 }
328
612a2d8a 329 public function add($p)
330 {
a3a049fc 331 $promo = intval($p);
332 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
333 $this->_data[$promo] = $promo;
334 asort($this->_data);
0337d704 335 }
a3a049fc 336
612a2d8a 337 public function del($p)
338 {
a3a049fc 339 $promo = intval($p);
340 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
341 unset($this->_data[$promo]);
0337d704 342 }
a3a049fc 343
612a2d8a 344 public function addRange($_p1,$_p2)
345 {
a3a049fc 346 $p1 = intval($_p1);
347 $p2 = intval($_p2);
348 $values = Array();
349 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
350 $values[] = "('{$this->_uid}',$i)";
351 $this->_data[$i] = $i;
352 }
353 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
354 asort($this->_data);
0337d704 355 }
356
612a2d8a 357 public function delRange($_p1,$_p2)
358 {
a3a049fc 359 $p1 = intval($_p1);
360 $p2 = intval($_p2);
361 $where = Array();
362 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
363 $where[] = "promo=$i";
364 unset($this->_data[$i]);
365 }
366 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
0337d704 367 }
368
612a2d8a 369 public function toRanges()
370 {
a3a049fc 371 $ranges = Array();
372 $I = Array();
373 foreach($this->_data as $promo) {
374 if(!isset($I[0])) {
375 $I = Array($promo,$promo);
376 }
377 elseif($I[1]+1 == $promo) {
378 $I[1] ++;
379 }
380 else {
381 $ranges[] = $I;
382 $I = Array($promo,$promo);
383 }
384 }
385 if(isset($I[0])) $ranges[] = $I;
386 return $ranges;
0337d704 387 }
388}
389
390// }}}
391// {{{ class NoninsNotifs
392
612a2d8a 393class NoninsNotifs
394{
395 public $_uid;
396 public $_data = Array();
0337d704 397
612a2d8a 398 public function __construct($uid)
399 {
a3a049fc 400 $this->_uid = $uid;
401 $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
612a2d8a 402 FROM watch_nonins AS w
403 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
404 WHERE w.uid = {?}
405 ORDER BY promo,nom", $uid);
a3a049fc 406 while($tmp = $res->next()) {
0337d704 407 $this->_data[$tmp['user_id']] = $tmp;
408 }
409 }
410
612a2d8a 411 public function del($p)
412 {
a3a049fc 413 unset($this->_data["$p"]);
414 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
0337d704 415 }
416
612a2d8a 417 public function add($p)
418 {
a3a049fc 419 XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
420 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
612a2d8a 421 FROM auth_user_md5
422 WHERE user_id={?}', $p);
a3a049fc 423 $this->_data["$p"] = $res->fetchOneAssoc();
0337d704 424 }
425}
a3a049fc 426
0337d704 427// }}}
428
a7de4ef7 429// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 430?>