2 /***************************************************************************
3 * Copyright (C) 2003-2009 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)
31 XDB
::execute('REPLACE INTO watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
35 // {{{ function register_watch_op
37 function register_watch_op($uid, $cid, $date = '', $info = '')
40 $date = date('Y-m-d');
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
) {
47 register_profile_update($uid, $info);
49 XDB
::execute('UPDATE auth_user_md5
51 WHERE user_id = {?}', $uid);
52 } elseif ($cid == WATCH_INSCR
) {
53 XDB
::execute('REPLACE INTO contacts (uid, contact)
56 WHERE ni_id = {?}', $uid);
57 XDB
::execute('DELETE FROM watch_nonins
58 WHERE ni_id = {?}', $uid);
59 } elseif ($cid == WATCH_DEATH
) {
60 // We delete nonins watches both for the dead (if non registered), and
61 // for people watched by the dead.
62 XDB
::execute('DELETE FROM watch_nonins
63 WHERE ni_id = {?} OR uid = {?}', $uid);
65 Platal
::session()->updateNbNotifs();
69 // {{{ function _select_notifs_base
71 function _select_notifs_base($table, $mail, $where)
74 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false
,
78 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true
,
79 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
80 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
82 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true
,
84 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
88 $our = $cases[$table];
91 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
92 u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
95 {$our['contact_sql']} AS contact,
96 (u.perms IN('admin','user')) AS inscrit";
99 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
100 b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
105 INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
106 INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
107 INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
110 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
112 if ($our['need_contact']) {
114 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
118 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() )').")
119 INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
120 INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
121 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
129 // {{{ function select_notifs
131 function select_notifs($mail, $uid=null
, $last=null
, $iterator=true
)
133 $where = $mail ?
'q.watch_flags=3' : 'w.uid = {?}';
134 $sql = _select_notifs_base('contacts', $mail, $where.($mail?
'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
135 $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
136 $sql .= _select_notifs_base('watch_nonins', $mail, $where);
139 return XDB
::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
141 return XDB
::query($sql, $last, $uid, $last, $uid, $last, $uid);
149 $prf_desc = array('nom' => 'Son patronyme',
150 'freetext' => 'Le texte libre',
151 'mobile' => 'Son numéro de téléphone portable',
152 'nationalite' => 'Sa nationalité',
153 'nick' => 'Son surnom',
154 'web' => 'L\'adresse de son site web',
155 'appli1' => 'Son école d\'application',
156 'appli2' => 'Son école de post-application',
157 'addresses' => 'Ses adresses',
158 'section' => 'Sa section sportive',
159 'binets' => 'La liste de ses binets',
160 'medals' => 'Ses décorations',
161 'cv' => 'Son Curriculum Vitae',
162 'jobs' => 'Ses informations professionnelles',
163 'photo' => 'Sa photographie');
165 function get_profile_change_details($event, $limit) {
167 $res = XDB
::iterRow("SELECT field
169 WHERE uid = {?} AND ts > {?}
171 $event['uid'], $limit);
172 if ($res->total() > 0) {
174 while (list($field) = $res->next()) {
175 $data[] .= $prf_desc[$field];
177 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
183 // {{{ function register_profile_update
185 function register_profile_update($uid, $field) {
186 XDB
::execute("REPLACE INTO watch_profile (uid, ts, field)
187 VALUES ({?}, NOW(), {?})",
191 // {{{ class AllNotifs
195 public $_cats = Array();
196 public $_data = Array();
198 public function __construct()
200 $res = XDB
::iterator("SELECT * FROM watch_cat");
201 while($tmp = $res->next()) {
202 $this->_cats
[$tmp['id']] = $tmp;
205 // recupère tous les watchers, avec détails des watchers, a partir du
206 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
207 $res = select_notifs(true
);
209 while($tmp = $res->next()) {
211 if (empty($this->_data
[$aid])) {
212 $this->_data
[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
213 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
216 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
217 $this->_data
[$aid]['data'][$tmp['cid']][] = $tmp;
228 public $_cats = Array();
229 public $_data = Array();
231 function __construct($uid, $up=false
)
235 $res = XDB
::iterator("SELECT * FROM watch_cat");
236 while($tmp = $res->next()) {
237 $this->_cats
[$tmp['id']] = $tmp;
240 $lastweek = date('YmdHis', time() - 7*24*60*60);
242 // recupere les notifs du watcher $uid, sans detail sur le watcher,
243 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
244 $res = select_notifs(false
, $uid, $lastweek);
245 while($tmp = $res->next()) {
246 if ($tmp['cid'] == WATCH_FICHE
) {
247 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
249 $this->_data
[$tmp['cid']][$tmp['promo']][] = $tmp;
253 XDB
::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
266 public $_cats = Array();
268 public $watch_contacts;
271 public function __construct($uid)
274 $this->_promos
= new PromoNotifs($uid);
275 $this->_nonins
= new NoninsNotifs($uid);
276 $this->_subs
= new WatchSub($uid);
277 $res = XDB
::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
279 WHERE user_id={?}", $uid);
280 list($this->watch_contacts
,$this->watch_mail
) = $res->fetchOneRow();
282 $res = XDB
::iterator("SELECT * FROM watch_cat");
283 while($tmp = $res->next()) {
284 $this->_cats
[$tmp['id']] = $tmp;
288 public function saveFlags()
291 if ($this->watch_contacts
)
293 if ($this->watch_mail
)
294 $flags .= ($flags ?
',' : '')."mail";
295 XDB
::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
296 $flags, $this->_uid
);
299 public function cats()
304 public function subs($i)
306 return $this->_subs
->_data
[$i];
309 public function promos()
311 return $this->_promos
->toRanges();
314 public function nonins()
316 return $this->_nonins
->_data
;
321 // {{{ class WatchSub
326 public $_data = Array();
328 public function __construct($uid)
331 $res = XDB
::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
332 while(list($c) = $res->next()) {
333 $this->_data
[$c] = $c;
337 public function update($ind)
339 $this->_data
= Array();
340 XDB
::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid
);
341 foreach (Env
::v($ind) as $key=>$val) {
342 XDB
::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid
, $key);
343 if(XDB
::affectedRows()) {
344 $this->_data
[$key] = $key;
351 // {{{ class PromoNotifs
356 public $_data = Array();
358 public function __construct($uid)
361 $res = XDB
::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
362 while (list($p) = $res->next()) {
363 $this->_data
[intval($p)] = intval($p);
367 public function add($p)
370 XDB
::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid
, $promo);
371 $this->_data
[$promo] = $promo;
375 public function del($p)
378 XDB
::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid
, $promo);
379 unset($this->_data
[$promo]);
382 public function addRange($_p1,$_p2)
387 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
388 $values[] = "('{$this->_uid}',$i)";
389 $this->_data
[$i] = $i;
391 XDB
::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
395 public function delRange($_p1,$_p2)
400 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
401 $where[] = "promo=$i";
402 unset($this->_data
[$i]);
404 XDB
::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid
);
407 public function toRanges()
411 foreach($this->_data
as $promo) {
413 $I = Array($promo,$promo);
415 elseif($I[1]+
1 == $promo) {
420 $I = Array($promo,$promo);
423 if(isset($I[0])) $ranges[] = $I;
429 // {{{ class NoninsNotifs
434 public $_data = Array();
436 public function __construct($uid)
439 $res = XDB
::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
440 FROM watch_nonins AS w
441 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
443 ORDER BY promo,nom", $uid);
444 while($tmp = $res->next()) {
445 $this->_data
[$tmp['user_id']] = $tmp;
449 public function del($p)
451 unset($this->_data
["$p"]);
452 XDB
::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid
, $p);
455 public function add($p)
457 XDB
::execute('INSERT IGNORE INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid
, $p);
458 $res = XDB
::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
460 WHERE user_id={?}', $p);
461 $this->_data
["$p"] = $res->fetchOneAssoc();
467 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: