2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 SET DATE=NOW() WHERE user_id={?}', $uid);
50 } elseif($cid == WATCH_INSCR
) {
51 XDB
::execute('REPLACE INTO contacts (uid,contact)
54 WHERE ni_id={?}', $uid);
55 XDB
::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
57 require_once 'xorg.misc.inc.php';
62 // {{{ function _select_notifs_base
64 function _select_notifs_base($table, $mail, $where)
67 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false
,
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) )',
73 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
75 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true
,
77 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
81 $our = $cases[$table];
84 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
85 u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
88 {$our['contact_sql']} AS contact,
89 (u.perms IN('admin','user')) AS inscrit";
92 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
93 b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
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)";
103 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
105 if ($our['need_contact']) {
107 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
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))
122 // {{{ function select_notifs
124 function select_notifs($mail, $uid=null
, $last=null
, $iterator=true
)
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);
132 return XDB
::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
134 return XDB
::query($sql, $last, $uid, $last, $uid, $last, $uid);
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');
158 function get_profile_change_details($event, $limit) {
160 $res = XDB
::iterRow("SELECT field
162 WHERE uid = {?} AND ts > {?}
164 $event['uid'], $limit);
165 if ($res->total() > 0) {
167 while (list($field) = $res->next()) {
168 $data[] .= $prf_desc[$field];
170 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
176 // {{{ function register_profile_update
178 function register_profile_update($uid, $field) {
179 XDB
::execute("REPLACE INTO watch_profile (uid, ts, field)
180 VALUES ({?}, NOW(), {?})",
184 // {{{ class AllNotifs
188 public $_cats = Array();
189 public $_data = Array();
191 public function __construct()
193 $res = XDB
::iterator("SELECT * FROM watch_cat");
194 while($tmp = $res->next()) {
195 $this->_cats
[$tmp['id']] = $tmp;
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
200 $res = select_notifs(true
);
202 while($tmp = $res->next()) {
204 if (empty($this->_data
[$aid])) {
205 $this->_data
[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
206 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
209 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
210 $this->_data
[$aid]['data'][$tmp['cid']][] = $tmp;
221 public $_cats = Array();
222 public $_data = Array();
224 function __construct($uid, $up=false
)
228 $res = XDB
::iterator("SELECT * FROM watch_cat");
229 while($tmp = $res->next()) {
230 $this->_cats
[$tmp['id']] = $tmp;
233 $lastweek = date('YmdHis', time() - 7*24*60*60);
235 // recupere les notifs du watcher $uid, sans detail sur le watcher,
236 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
237 $res = select_notifs(false
, $uid, $lastweek);
238 while($tmp = $res->next()) {
239 if ($tmp['cid'] == WATCH_FICHE
) {
240 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
242 $this->_data
[$tmp['cid']][$tmp['promo']][] = $tmp;
246 XDB
::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
259 public $_cats = Array();
261 public $watch_contacts;
264 public function __construct($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)
272 WHERE user_id={?}", $uid);
273 list($this->watch_contacts
,$this->watch_mail
) = $res->fetchOneRow();
275 $res = XDB
::iterator("SELECT * FROM watch_cat");
276 while($tmp = $res->next()) {
277 $this->_cats
[$tmp['id']] = $tmp;
281 public function saveFlags()
284 if ($this->watch_contacts
)
286 if ($this->watch_mail
)
287 $flags .= ($flags ?
',' : '')."mail";
288 XDB
::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
289 $flags, $this->_uid
);
292 public function cats()
297 public function subs($i)
299 return $this->_subs
->_data
[$i];
302 public function promos()
304 return $this->_promos
->toRanges();
307 public function nonins()
309 return $this->_nonins
->_data
;
314 // {{{ class WatchSub
319 public $_data = Array();
321 public function __construct($uid)
324 $res = XDB
::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
325 while(list($c) = $res->next()) {
326 $this->_data
[$c] = $c;
330 public function update($ind)
332 $this->_data
= Array();
333 XDB
::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid
);
334 foreach (Env
::v($ind) as $key=>$val) {
335 XDB
::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid
, $key);
336 if(XDB
::affectedRows()) {
337 $this->_data
[$key] = $key;
344 // {{{ class PromoNotifs
349 public $_data = Array();
351 public function __construct($uid)
354 $res = XDB
::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
355 while (list($p) = $res->next()) {
356 $this->_data
[intval($p)] = intval($p);
360 public function add($p)
363 XDB
::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid
, $promo);
364 $this->_data
[$promo] = $promo;
368 public function del($p)
371 XDB
::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid
, $promo);
372 unset($this->_data
[$promo]);
375 public function addRange($_p1,$_p2)
380 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
381 $values[] = "('{$this->_uid}',$i)";
382 $this->_data
[$i] = $i;
384 XDB
::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
388 public function delRange($_p1,$_p2)
393 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++
) {
394 $where[] = "promo=$i";
395 unset($this->_data
[$i]);
397 XDB
::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid
);
400 public function toRanges()
404 foreach($this->_data
as $promo) {
406 $I = Array($promo,$promo);
408 elseif($I[1]+
1 == $promo) {
413 $I = Array($promo,$promo);
416 if(isset($I[0])) $ranges[] = $I;
422 // {{{ class NoninsNotifs
427 public $_data = Array();
429 public function __construct($uid)
432 $res = XDB
::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
433 FROM watch_nonins AS w
434 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
436 ORDER BY promo,nom", $uid);
437 while($tmp = $res->next()) {
438 $this->_data
[$tmp['user_id']] = $tmp;
442 public function del($p)
444 unset($this->_data
["$p"]);
445 XDB
::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid
, $p);
448 public function add($p)
450 XDB
::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid
, $p);
451 $res = XDB
::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
453 WHERE user_id={?}', $p);
454 $this->_data
["$p"] = $res->fetchOneAssoc();
460 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: