| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2007 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) VALUES({?},{?},NOW(),{?},{?})', |
| 43 | $uid, $cid, $date, $info); |
| 44 | if($cid == WATCH_FICHE) { |
| 45 | XDB::execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid); |
| 46 | } elseif($cid == WATCH_INSCR) { |
| 47 | XDB::execute('REPLACE INTO contacts (uid,contact) |
| 48 | SELECT uid,ni_id |
| 49 | FROM watch_nonins |
| 50 | WHERE ni_id={?}', $uid); |
| 51 | XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // }}} |
| 56 | // {{{ function _select_notifs_base |
| 57 | |
| 58 | function _select_notifs_base($table, $mail, $where) |
| 59 | { |
| 60 | $cases = Array( |
| 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 | ) |
| 73 | ); |
| 74 | |
| 75 | $our = $cases[$table]; |
| 76 | $sql = " |
| 77 | ( |
| 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"; |
| 84 | if ($mail) { |
| 85 | $sql.=", |
| 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"; |
| 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)) |
| 109 | WHERE $where |
| 110 | )"; |
| 111 | |
| 112 | return $sql; |
| 113 | } |
| 114 | |
| 115 | // }}} |
| 116 | // {{{ function select_notifs |
| 117 | |
| 118 | function select_notifs($mail, $uid=null, $last=null, $iterator=true) |
| 119 | { |
| 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) { |
| 126 | return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid); |
| 127 | } else { |
| 128 | return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // }}} |
| 133 | // {{{ function getNbNotifs |
| 134 | |
| 135 | function getNbNotifs() { |
| 136 | if (!S::has('uid')) { |
| 137 | return 0; |
| 138 | } |
| 139 | $uid = S::v('uid', -1); |
| 140 | $watchlast = S::v('watch_last'); |
| 141 | |
| 142 | // selectionne les notifs de uid, sans detail sur le watcher, depuis |
| 143 | // $watchlast, meme ceux sans surveillance, non ordonnés |
| 144 | $res = select_notifs(false, $uid, $watchlast, false); |
| 145 | $n = $res->numRows(); |
| 146 | $res->free(); |
| 147 | if ($n == 0) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | return "<a href='carnet/panel'>$n événement".($n > 1 ? 's' : '')." !</a>"; |
| 152 | } |
| 153 | |
| 154 | // }}} |
| 155 | // {{{ class AllNotifs |
| 156 | |
| 157 | class AllNotifs { |
| 158 | var $_cats = Array(); |
| 159 | var $_data = Array(); |
| 160 | |
| 161 | function AllNotifs() { |
| 162 | $res = XDB::iterator("SELECT * FROM watch_cat"); |
| 163 | while($tmp = $res->next()) { |
| 164 | $this->_cats[$tmp['id']] = $tmp; |
| 165 | } |
| 166 | |
| 167 | // recupère tous les watchers, avec détails des watchers, a partir du |
| 168 | // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés |
| 169 | $res = select_notifs(true); |
| 170 | |
| 171 | while($tmp = $res->next()) { |
| 172 | $aid = $tmp['aid']; |
| 173 | if (empty($this->_data[$aid])) { |
| 174 | $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'], |
| 175 | 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'], |
| 176 | 'dcd'=>$tmp['dcd']); |
| 177 | } |
| 178 | unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['sexe'], $tmp['mail_fmt'], $tmp['dcd']); |
| 179 | $this->_data[$aid]['data'][$tmp['cid']][] = $tmp; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // }}} |
| 185 | // {{{ class Notifs |
| 186 | |
| 187 | class Notifs { |
| 188 | var $_uid; |
| 189 | var $_cats = Array(); |
| 190 | var $_data = Array(); |
| 191 | |
| 192 | function Notifs($uid, $up=false) { |
| 193 | $this->_uid = $uid; |
| 194 | |
| 195 | $res = XDB::iterator("SELECT * FROM watch_cat"); |
| 196 | while($tmp = $res->next()) { |
| 197 | $this->_cats[$tmp['id']] = $tmp; |
| 198 | } |
| 199 | |
| 200 | $lastweek = date('YmdHis', time() - 7*24*60*60); |
| 201 | |
| 202 | // recupere les notifs du watcher $uid, sans detail sur le watcher, |
| 203 | // depuis la semaine dernière, meme ceux sans surveillance, ordonnés |
| 204 | $res = select_notifs(false, $uid, $lastweek); |
| 205 | while($tmp = $res->next()) { |
| 206 | $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp; |
| 207 | } |
| 208 | |
| 209 | if($up) { |
| 210 | XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // }}} |
| 216 | // {{{ class Watch |
| 217 | |
| 218 | class Watch { |
| 219 | var $_uid; |
| 220 | var $_promos; |
| 221 | var $_nonins; |
| 222 | var $_cats = Array(); |
| 223 | var $_subs; |
| 224 | var $watch_contacts; |
| 225 | var $watch_mail; |
| 226 | |
| 227 | function Watch($uid) { |
| 228 | $this->_uid = $uid; |
| 229 | $this->_promos = new PromoNotifs($uid); |
| 230 | $this->_nonins = new NoninsNotifs($uid); |
| 231 | $this->_subs = new WatchSub($uid); |
| 232 | $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags) |
| 233 | FROM auth_user_quick |
| 234 | WHERE user_id={?}", $uid); |
| 235 | list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow(); |
| 236 | |
| 237 | $res = XDB::iterator("SELECT * FROM watch_cat"); |
| 238 | while($tmp = $res->next()) { |
| 239 | $this->_cats[$tmp['id']] = $tmp; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | function saveFlags() { |
| 244 | $flags = ""; |
| 245 | if ($this->watch_contacts) |
| 246 | $flags = "contacts"; |
| 247 | if ($this->watch_mail) |
| 248 | $flags .= ($flags ? ',' : '')."mail"; |
| 249 | XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}', |
| 250 | $flags, $this->_uid); |
| 251 | } |
| 252 | |
| 253 | function cats() { |
| 254 | return $this->_cats; |
| 255 | } |
| 256 | |
| 257 | function subs($i) { |
| 258 | return $this->_subs->_data[$i]; |
| 259 | } |
| 260 | |
| 261 | function promos() { |
| 262 | return $this->_promos->toRanges(); |
| 263 | } |
| 264 | |
| 265 | function nonins() { |
| 266 | return $this->_nonins->_data; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // }}} |
| 271 | // {{{ class WatchSub |
| 272 | |
| 273 | class WatchSub { |
| 274 | var $_uid; |
| 275 | var $_data = Array(); |
| 276 | |
| 277 | function WatchSub($uid) { |
| 278 | $this->_uid = $uid; |
| 279 | $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid); |
| 280 | while(list($c) = $res->next()) { |
| 281 | $this->_data[$c] = $c; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | function update($ind) { |
| 286 | $this->_data = Array(); |
| 287 | XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid); |
| 288 | foreach (Env::v($ind) as $key=>$val) { |
| 289 | XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key); |
| 290 | if(mysql_affected_rows()) { |
| 291 | $this->_data[$key] = $key; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // }}} |
| 298 | // {{{ class PromoNotifs |
| 299 | |
| 300 | class PromoNotifs { |
| 301 | var $_uid; |
| 302 | var $_data = Array(); |
| 303 | |
| 304 | function PromoNotifs($uid) { |
| 305 | $this->_uid = $uid; |
| 306 | $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid); |
| 307 | while (list($p) = $res->next()) { |
| 308 | $this->_data[intval($p)] = intval($p); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | function add($p) { |
| 313 | $promo = intval($p); |
| 314 | XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo); |
| 315 | $this->_data[$promo] = $promo; |
| 316 | asort($this->_data); |
| 317 | } |
| 318 | |
| 319 | function del($p) { |
| 320 | $promo = intval($p); |
| 321 | XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo); |
| 322 | unset($this->_data[$promo]); |
| 323 | } |
| 324 | |
| 325 | function addRange($_p1,$_p2) { |
| 326 | $p1 = intval($_p1); |
| 327 | $p2 = intval($_p2); |
| 328 | $values = Array(); |
| 329 | for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) { |
| 330 | $values[] = "('{$this->_uid}',$i)"; |
| 331 | $this->_data[$i] = $i; |
| 332 | } |
| 333 | XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values)); |
| 334 | asort($this->_data); |
| 335 | } |
| 336 | |
| 337 | function delRange($_p1,$_p2) { |
| 338 | $p1 = intval($_p1); |
| 339 | $p2 = intval($_p2); |
| 340 | $where = Array(); |
| 341 | for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) { |
| 342 | $where[] = "promo=$i"; |
| 343 | unset($this->_data[$i]); |
| 344 | } |
| 345 | XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid); |
| 346 | } |
| 347 | |
| 348 | function toRanges() { |
| 349 | $ranges = Array(); |
| 350 | $I = Array(); |
| 351 | foreach($this->_data as $promo) { |
| 352 | if(!isset($I[0])) { |
| 353 | $I = Array($promo,$promo); |
| 354 | } |
| 355 | elseif($I[1]+1 == $promo) { |
| 356 | $I[1] ++; |
| 357 | } |
| 358 | else { |
| 359 | $ranges[] = $I; |
| 360 | $I = Array($promo,$promo); |
| 361 | } |
| 362 | } |
| 363 | if(isset($I[0])) $ranges[] = $I; |
| 364 | return $ranges; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // }}} |
| 369 | // {{{ class NoninsNotifs |
| 370 | |
| 371 | class NoninsNotifs { |
| 372 | var $_uid; |
| 373 | var $_data = Array(); |
| 374 | |
| 375 | function NoninsNotifs($uid) { |
| 376 | $this->_uid = $uid; |
| 377 | $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id |
| 378 | FROM watch_nonins AS w |
| 379 | INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id) |
| 380 | WHERE w.uid = {?} |
| 381 | ORDER BY promo,nom", $uid); |
| 382 | while($tmp = $res->next()) { |
| 383 | $this->_data[$tmp['user_id']] = $tmp; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | function del($p) { |
| 388 | unset($this->_data["$p"]); |
| 389 | XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p); |
| 390 | } |
| 391 | |
| 392 | function add($p) { |
| 393 | XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p); |
| 394 | $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id |
| 395 | FROM auth_user_md5 |
| 396 | WHERE user_id={?}', $p); |
| 397 | $this->_data["$p"] = $res->fetchOneAssoc(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // }}} |
| 402 | |
| 403 | ?> |