2 /***************************************************************************
3 * Copyright (C) 2003-2004 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 define('WATCH_FICHE', 1);
23 define('WATCH_INSCR', 2);
24 define('WATCH_DEATH', 3);
25 define('WATCH_BIRTH', 4);
27 // {{{ function inscription_notifs_base
29 function inscription_notifs_base($uid)
32 $globals->xdb
->execute('REPLACE INTO watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
36 // {{{ function register_watch_op
38 function register_watch_op($uid, $cid, $date='', $info='')
42 $date = date('Y-m-d');
44 $globals->xdb
->execute('REPLACE INTO watch_ops (uid,cid,known,date,info) VALUES({?},{?},NOW(),{?},{?})',
45 $uid, $cid, $date, $info);
46 if($cid == WATCH_FICHE
) {
47 $globals->xdb
->execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid);
48 } elseif($cid == WATCH_INSCR
) {
49 $globals->xdb
->execute('REPLACE INTO contacts (uid,contact)
52 WHERE ni_id={?}', $uid);
53 $globals->xdb
->execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
58 // {{{ function _select_notifs_base
60 function _select_notifs_base($table, $mail, $where)
63 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false
,
67 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true
,
68 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
69 'contact_sql' => 'NOT (c.contact IS NULL)'
71 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => false
,
77 $our = $cases[$table];
80 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
84 {$our['contact_sql']} AS contact,
85 (u.perms IN('admin','user')) AS inscrit";
88 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
89 b.alias AS abestalias, (v.flags='femme') AS sexe, q.core_mail_fmt AS mail_fmt";
94 INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
95 INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
96 INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
99 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
101 if ($our['need_contact']) {
103 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
107 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() )').")
108 INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
109 INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
110 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
118 // {{{ function select_notifs
120 function select_notifs($mail, $uid=null
, $last=null
, $iterator=true
)
123 $where = $mail ?
'q.watch_flags=3' : 'w.uid = {?}';
124 $sql = _select_notifs_base('contacts', $mail, $where.($mail?
'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
125 $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
126 $sql .= _select_notifs_base('watch_nonins', $mail, $where);
129 return $globals->xdb
->iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
131 return $globals->xdb
->query($sql, $last, $uid, $last, $uid, $last, $uid);
136 // {{{ function getNbNotifs
138 function getNbNotifs() {
140 if (!Session
::has('uid')) {
143 $uid = Session
::getInt('uid', -1);
144 $watchlast = Session
::get('watch_last');
146 // selectionne les notifs de uid, sans detail sur le watcher, depuis $watchlast, meme ceux sans surveillance, non ordonnés
147 $res = select_notifs(false
, $uid, $watchlast, false
);
148 $n = $res->numRows();
150 $url = $globals->baseurl
."/carnet/panel.php";
151 if($n==0) { return; }
152 if($n==1) { return "<a href='$url'>1 évènement !</a>"; }
153 return "<a href='$url'>$n évènements !</a>";
157 // {{{ class AllNotifs
160 var $_cats = Array();
161 var $_data = Array();
163 function AllNotifs() {
166 $res = $globals->xdb
->iterator("SELECT * FROM watch_cat");
167 while($tmp = $res->next()) {
168 $this->_cats
[$tmp['id']] = $tmp;
171 // recupère tous les watchers, avec détails des watchers, a partir du watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
172 $res = select_notifs(true
);
174 while($tmp = $res->next()) {
176 if (empty($this->_data
[$aid])) {
177 $this->_data
[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
178 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'],
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;
192 var $_cats = Array();
193 var $_data = Array();
195 function Notifs($uid, $up=false
) {
199 $res = $globals->xdb
->iterator("SELECT * FROM watch_cat");
200 while($tmp = $res->next()) {
201 $this->_cats
[$tmp['id']] = $tmp;
204 $lastweek = date('YmdHis',mktime() - 7*24*60*60);
206 // recupere les notifs du watcher $uid, sans detail sur le watcher, depuis la semaine dernière, meme ceux sans surveillance, ordonnés
207 $res = select_notifs(false
, $uid, $lastweek);
208 while($tmp = $res->next()) {
209 $this->_data
[$tmp['cid']][$tmp['promo']][] = $tmp;
213 $globals->xdb
->execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
225 var $_cats = Array();
230 function Watch($uid) {
233 $this->_promos
= new PromoNotifs($uid);
234 $this->_nonins
= new NoninsNotifs($uid);
235 $this->_subs
= new WatchSub($uid);
236 $res = $globals->xdb
->query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
238 WHERE user_id={?}", $uid);
239 list($this->watch_contacts
,$this->watch_mail
) = $res->fetchOneRow();
241 $res = $globals->xdb
->iterator("SELECT * FROM watch_cat");
242 while($tmp = $res->next()) {
243 $this->_cats
[$tmp['id']] = $tmp;
247 function saveFlags() {
250 if($this->watch_contacts
) $flags = "contacts";
251 if($this->watch_mail
) $flags .= ($flags ?
',' : '')."mail";
252 $globals->xdb
->execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}', $flags, $this->_uid
);
261 return $this->_subs
->_data
[$i];
265 return $this->_promos
->toRanges();
269 return $this->_nonins
->_data
;
274 // {{{ class WatchSub
278 var $_data = Array();
280 function WatchSub($uid) {
283 $res = $globals->xdb
->iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
284 while(list($c) = $res->next()) {
285 $this->_data
[$c] = $c;
289 function update($ind) {
291 $this->_data
= Array();
292 $globals->xdb
->execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid
);
293 foreach(Env
::getMixed($ind) as $key=>$val) {
294 $globals->xdb
->query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid
, $key);
295 if(mysql_affected_rows()) {
296 $this->_data
[$key] = $key;
303 // {{{ class PromoNotifs
307 var $_data = Array();
309 function PromoNotifs($uid) {
312 $res = $globals->xdb
->iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
313 while (list($p) = $res->next()) {
314 $this->_data
[intval($p)] = intval($p);
321 $globals->xdb
->execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid
, $promo);
322 $this->_data
[$promo] = $promo;
329 $globals->xdb
->execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid
, $promo);
330 unset($this->_data
[$promo]);
333 function addRange($_p1,$_p2) {
338 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
339 $values[] = "('{$this->_uid}',$i)";
340 $this->_data
[$i] = $i;
342 $globals->xdb
->execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
346 function delRange($_p1,$_p2) {
351 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
352 $where[] = "promo=$i";
353 unset($this->_data
[$i]);
355 $globals->xdb
->execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid
);
358 function toRanges() {
361 foreach($this->_data
as $promo) {
363 $I = Array($promo,$promo);
365 elseif($I[1]+
1 == $promo) {
370 $I = Array($promo,$promo);
373 if(isset($I[0])) $ranges[] = $I;
379 // {{{ class NoninsNotifs
383 var $_data = Array();
385 function NoninsNotifs($uid) {
388 $res = $globals->xdb
->iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
389 FROM watch_nonins AS w
390 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
392 ORDER BY promo,nom", $uid);
393 while($tmp = $res->next()) {
394 $this->_data
[$tmp['user_id']] = $tmp;
400 unset($this->_data
["$p"]);
401 $globals->xdb
->execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid
, $p);
406 $globals->xdb
->execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid
, $p);
407 $res = $globals->xdb
->query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
409 WHERE user_id={?}', $p);
410 $this->_data
["$p"] = $res->fetchOneAssoc();