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