2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 class EventsModule
extends PLModule
27 'events' => $this->make_hook('ev', AUTH_COOKIE
),
28 'rss' => $this->make_hook('rss', AUTH_PUBLIC
),
29 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE
, '', NO_AUTH
),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP
, 'admin'),
37 function get_tips($exclude = null
)
39 $exclude = is_null($exclude) ?
'' : ' AND id != ' . $exclude . ' ';
40 $priority = rand(0, 510);
42 $priority = (int)($priority/2);
43 $res = XDB
::query("SELECT *
45 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
46 AND (promo_min = 0 OR promo_min <= {?})
47 AND (promo_max = 0 OR promo_max >= {?})
49 AND (state = 'active')
53 S
::i('promo'), S
::i('promo'), $priority);
54 } while ($priority && !$res->numRows());
55 if (!$res->numRows()) {
58 return $res->fetchOneAssoc();
61 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
63 $page->changeTpl('events/index.tpl');
64 $page->addJsLink('ajax.js');
65 $page->assign('tips', $this->get_tips());
67 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
68 WHERE user_id={?}', S
::v('uid'));
69 list($date, $naissance) = $res->fetchOneRow();
71 // incitation à mettre à jour la fiche
73 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
75 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
76 // si fiche date de + de 400j;
77 $page->assign('fiche_incitation', $date);
80 // Souhaite bon anniversaire
82 if (substr($naissance, 5) == date('m-d')) {
83 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
86 // incitation à mettre une photo
88 $res = XDB
::query('SELECT COUNT(*) FROM photo
89 WHERE uid={?}', S
::v('uid'));
90 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
92 // Incitation à se géolocaliser
93 require_once 'geoloc.inc.php';
94 $res = localize_addresses(S
::v('uid', -1));
95 $page->assign('geoloc_incitation', count($res));
98 if (S
::has('core_rss_hash')) {
99 $page->setRssLink('Polytechnique.org :: News',
100 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
103 // cache les evenements lus et raffiche les evenements a relire
104 if ($action == 'read' && $eid) {
105 XDB
::execute('DELETE evenements_vus.*
106 FROM evenements_vus AS ev
107 INNER JOIN evenements AS e ON e.id = ev.evt_id
108 WHERE peremption < NOW()');
109 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
111 pl_redirect('events#'.$pound);
114 if ($action == 'unread' && $eid) {
115 XDB
::execute('DELETE FROM evenements_vus
116 WHERE evt_id = {?} AND user_id = {?}',
118 pl_redirect('events#newsid'.$eid);
121 // affichage des evenements
122 // annonces promos triées par présence d'une limite sur les promos
123 // puis par dates croissantes d'expiration
124 $promo = S
::v('promo');
125 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
127 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
128 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
129 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
130 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
131 AND (e.promo_min = 0 || e.promo_min <= {?})
132 AND (e.promo_max = 0 || e.promo_max >= {?})
133 AND ev.user_id IS NULL
134 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
135 $page->assign('evenement',
136 XDB
::iterator($sql, S
::v('uid'),
140 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
142 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
143 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
144 AND (e.promo_min = 0 || e.promo_min <= {?})
145 AND (e.promo_max = 0 || e.promo_max >= {?})
146 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
147 $page->assign('evenement_summary',
148 XDB
::iterator($sql, S
::v('uid'),
153 function handler_rss(&$page, $user = null
, $hash = null
)
155 require_once 'rss.inc.php';
157 $uid = init_rss('events/rss.tpl', $user, $hash);
159 $rss = XDB
::iterator(
160 'SELECT e.id, e.titre, e.texte, e.creation_date,
161 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
162 FROM auth_user_md5 AS u
163 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
164 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
165 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
166 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
167 AND peremption >= NOW()', $uid);
168 $page->assign('rss', $rss);
171 function handler_ev_submit(&$page)
173 $page->changeTpl('events/submit.tpl');
175 $titre = Post
::v('titre');
176 $texte = Post
::v('texte');
177 $promo_min = Post
::i('promo_min');
178 $promo_max = Post
::i('promo_max');
179 $peremption = Post
::i('peremption');
180 $valid_mesg = Post
::v('valid_mesg');
181 $action = Post
::v('action');
183 if (($promo_min > $promo_max && $promo_max != 0)||
184 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
185 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
187 $page->trig("L'intervalle de promotions n'est pas valide");
191 require_once('url_catcher.inc.php');
192 $texte_catch_url = url_catcher($texte);
194 $page->assign('titre', $titre);
195 $page->assign('texte', $texte);
196 $page->assign('texte_html', $texte_catch_url);
197 $page->assign('promo_min', $promo_min);
198 $page->assign('promo_max', $promo_max);
199 $page->assign('peremption', $peremption);
200 $page->assign('valid_mesg', $valid_mesg);
201 $page->assign('action', strtolower($action));
203 if ($action == 'Confirmer') {
204 $texte = $texte_catch_url;
205 require_once 'validations.inc.php';
206 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
207 $peremption, $valid_mesg, S
::v('uid'));
209 $page->assign('ok', true
);
213 for ($i = 1 ; $i < 30 ; $i++
) {
214 $time = time() +
3600 * 24 * $i;
215 $p_stamp = date('Ymd', $time);
216 $year = date('Y', $time);
217 $month = date('m', $time);
218 $day = date('d', $time);
220 $select .= "<option value=\"$p_stamp\"";
221 if ($p_stamp == strtr($peremption, array("-" => ""))) {
222 $select .= " selected='selected'";
224 $select .= "> $day / $month / $year</option>\n";
226 $page->assign('select',$select);
229 function handler_tips(&$page, $tips = null
)
231 header('Content-Type: text/html; charset="ISO-8859-15"');
232 $page->changeTpl('include/tips.tpl', NO_SKIN
);
233 $page->assign('tips', $this->get_tips($tips));
236 function handler_admin_tips(&$page, $action = 'list', $id = null
)
238 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
239 $page->assign('title', 'Gestion des Astuces');
240 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
241 $table_editor->describe('peremption', 'date de péremption', true
);
242 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
243 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
244 $table_editor->describe('titre', 'titre', true
);
245 $table_editor->describe('state', 'actif', true
);
246 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
247 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false
);
248 $table_editor->apply($page, $action, $id);
249 if ($action == 'edit' && !is_null($id)) {
250 $page->changeTpl('events/admin_tips.tpl');
254 function handler_admin_events(&$page, $action = 'list', $eid = null
)
256 $page->changeTpl('events/admin.tpl');
257 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
258 $page->register_modifier('hde', 'html_entity_decode');
260 $arch = $action == 'archives';
261 $page->assign('action', $action);
263 if (Post
::v('action') == "Proposer" && $eid) {
264 $promo_min = Post
::i('promo_min');
265 $promo_max = Post
::i('promo_max');
266 if ($promo_min > $promo_max ||
267 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
268 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
270 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
273 XDB
::execute('UPDATE evenements
274 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
276 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
277 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
281 if ($action == 'edit') {
282 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
284 WHERE id={?}', $eid);
285 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
286 $page->assign('titre',$titre);
287 $page->assign('texte',$texte);
288 $page->assign('promo_min',$promo_min);
289 $page->assign('promo_max',$promo_max);
290 $page->assign('peremption',$peremption);
293 for ($i = 1 ; $i < 30 ; $i++
) {
294 $p_stamp=date("Ymd",time()+
3600*24*$i);
295 $year=substr($p_stamp,0,4);
296 $month=substr($p_stamp,4,2);
297 $day=substr($p_stamp,6,2);
299 $select .= "<option value=\"$p_stamp\""
300 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
301 . "> $day / $month / $year</option>\n";
303 $page->assign('select',$select);
307 XDB
::execute('DELETE from evenements
308 WHERE id = {?}', $eid);
312 XDB
::execute('UPDATE evenements
313 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
314 WHERE id = {?}', $eid);
318 XDB
::execute('UPDATE evenements
319 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
320 WHERE id = {?}', $eid);
321 $action = 'archives';
326 XDB
::execute('UPDATE evenements
327 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
328 WHERE id = {?}', $eid);
332 XDB
::execute('UPDATE evenements
333 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
334 WHERE id = {?}', $eid);
338 $pid = ($eid && $action == 'preview') ?
$eid : -1;
339 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
340 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
341 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
342 e.promo_min, e.promo_max,
343 FIND_IN_SET('valide', e.flags) AS fvalide,
344 FIND_IN_SET('archive', e.flags) AS farch,
345 u.promo, u.nom, u.prenom, a.alias AS forlife
347 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
348 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
349 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
350 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
351 $page->assign('evs', XDB
::iterator($sql));
353 $page->assign('arch', $arch);