| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 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 | class EventsModule extends PLModule |
| 23 | { |
| 24 | function handlers() |
| 25 | { |
| 26 | return array( |
| 27 | 'events' => $this->make_hook('ev', AUTH_COOKIE), |
| 28 | 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP), |
| 29 | 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'), |
| 30 | |
| 31 | 'nl' => $this->make_hook('nl', AUTH_COOKIE), |
| 32 | 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE), |
| 33 | 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE), |
| 34 | 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'), |
| 35 | 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'), |
| 36 | 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function handler_ev(&$page) |
| 41 | { |
| 42 | $page->changeTpl('login.tpl'); |
| 43 | |
| 44 | $res = XDB::query('SELECT date, naissance FROM auth_user_md5 |
| 45 | WHERE user_id={?}', S::v('uid')); |
| 46 | list($date, $naissance) = $res->fetchOneRow(); |
| 47 | |
| 48 | // incitation à mettre à jour la fiche |
| 49 | |
| 50 | $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), |
| 51 | substr($date, 0, 4)); |
| 52 | if( (time() - $d2) > 60 * 60 * 24 * 400 ) { |
| 53 | // si fiche date de + de 400j; |
| 54 | $page->assign('fiche_incitation', $date); |
| 55 | } |
| 56 | |
| 57 | // Souhaite bon anniversaire |
| 58 | |
| 59 | if (substr($naissance, 5) == date('m-d')) { |
| 60 | $page->assign('birthday', date('Y') - substr($naissance, 0, 4)); |
| 61 | } |
| 62 | |
| 63 | // incitation à mettre une photo |
| 64 | |
| 65 | $res = XDB::query('SELECT COUNT(*) FROM photo |
| 66 | WHERE uid={?}', S::v('uid')); |
| 67 | $page->assign('photo_incitation', $res->fetchOneCell() == 0); |
| 68 | |
| 69 | // Incitation à se géolocaliser |
| 70 | require_once 'geoloc.inc.php'; |
| 71 | $res = localize_addresses(S::v('uid', -1)); |
| 72 | $page->assign('geoloc_incitation', count($res)); |
| 73 | |
| 74 | // affichage de la boîte avec quelques liens |
| 75 | require_once 'login.conf.php'; |
| 76 | $pub_nbElem = $pub_nbLig * $pub_nbCol ; |
| 77 | if (count($pub_tjs) <= $pub_nbElem) { |
| 78 | $publicite = array_slice($pub_tjs, 0, $pub_nbElem); |
| 79 | } else { |
| 80 | $publicite = $pub_tjs ; |
| 81 | } |
| 82 | |
| 83 | $nbAlea = $pub_nbElem - count($publicite) ; |
| 84 | if ($nbAlea > 0) { |
| 85 | $choix = array_rand($pub_rnd,$nbAlea) ; |
| 86 | foreach ($choix as $url) { |
| 87 | $publicite[$url] = $pub_rnd[$url] ; |
| 88 | } |
| 89 | } |
| 90 | $publicite = array_chunk( $publicite , $pub_nbLig , true ) ; |
| 91 | $page->assign_by_ref('publicite', $publicite); |
| 92 | |
| 93 | // ajout du lien RSS |
| 94 | |
| 95 | if (S::has('core_rss_hash')) { |
| 96 | $page->assign('xorg_rss', |
| 97 | array('title' => 'Polytechnique.org :: News', |
| 98 | 'href' => '/rss/'.S::v('forlife') |
| 99 | .'/'.S::v('core_rss_hash').'/rss.xml') |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // cache les evenements lus et raffiche les evenements a relire |
| 104 | if (Env::has('lu')){ |
| 105 | XDB::execute('DELETE FROM evenements_vus AS ev |
| 106 | INNER JOIN evenements AS e ON e.id = ev.evt_id |
| 107 | WHERE peremption < NOW)'); |
| 108 | XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})', |
| 109 | Env::v('lu'), S::v('uid')); |
| 110 | } |
| 111 | |
| 112 | if (Env::has('nonlu')){ |
| 113 | XDB::execute('DELETE FROM evenements_vus |
| 114 | WHERE evt_id = {?} AND user_id = {?}', |
| 115 | Env::v('nonlu'), S::v('uid')); |
| 116 | } |
| 117 | |
| 118 | // affichage des evenements |
| 119 | // annonces promos triées par présence d'une limite sur les promos |
| 120 | // puis par dates croissantes d'expiration |
| 121 | $promo = S::v('promo'); |
| 122 | $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife |
| 123 | FROM evenements AS e |
| 124 | INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id |
| 125 | INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' ) |
| 126 | LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?}) |
| 127 | WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW() |
| 128 | AND (e.promo_min = 0 || e.promo_min <= {?}) |
| 129 | AND (e.promo_max = 0 || e.promo_max >= {?}) |
| 130 | AND ev.user_id IS NULL |
| 131 | ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption"; |
| 132 | $page->assign('evenement', |
| 133 | XDB::iterator($sql, S::v('uid'), |
| 134 | $promo, $promo) |
| 135 | ); |
| 136 | |
| 137 | $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu |
| 138 | FROM evenements AS e |
| 139 | LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?}) |
| 140 | WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW() |
| 141 | AND (e.promo_min = 0 || e.promo_min <= {?}) |
| 142 | AND (e.promo_max = 0 || e.promo_max >= {?}) |
| 143 | ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption"; |
| 144 | $page->assign('evenement_summary', |
| 145 | XDB::iterator($sql, S::v('uid'), |
| 146 | $promo, $promo) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | function handler_ev_submit(&$page) |
| 151 | { |
| 152 | $page->changeTpl('evenements.tpl'); |
| 153 | |
| 154 | $titre = Post::v('titre'); |
| 155 | $texte = Post::v('texte'); |
| 156 | $promo_min = Post::i('promo_min'); |
| 157 | $promo_max = Post::i('promo_max'); |
| 158 | $peremption = Post::i('peremption'); |
| 159 | $valid_mesg = Post::v('valid_mesg'); |
| 160 | $action = Post::v('action'); |
| 161 | |
| 162 | $page->assign('titre', $titre); |
| 163 | $page->assign('texte', $texte); |
| 164 | $page->assign('promo_min', $promo_min); |
| 165 | $page->assign('promo_max', $promo_max); |
| 166 | $page->assign('peremption', $peremption); |
| 167 | $page->assign('valid_mesg', $valid_mesg); |
| 168 | $page->assign('action', strtolower($action)); |
| 169 | |
| 170 | if ($action == 'Confirmer') { |
| 171 | $texte = preg_replace('/((http|ftp)+(s)?:\/\/[^<>\s]+)/i', |
| 172 | '<a href=\"\\0\">\\0</a>', $texte); |
| 173 | $texte = preg_replace('/([^,\s]+@[^,\s]+)/i', |
| 174 | '<a href=\"mailto:\\0\">\\0</a>', $texte); |
| 175 | require_once 'validations.inc.php'; |
| 176 | $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max, |
| 177 | $peremption, $valid_mesg, S::v('uid')); |
| 178 | $evtreq->submit(); |
| 179 | $page->assign('ok', true); |
| 180 | } |
| 181 | |
| 182 | $select = ''; |
| 183 | for ($i = 1 ; $i < 30 ; $i++) { |
| 184 | $time = time() + 3600 * 24 * $i; |
| 185 | $p_stamp = date('Ymd', $time); |
| 186 | $year = date('Y', $time); |
| 187 | $month = date('m', $time); |
| 188 | $day = date('d', $time); |
| 189 | |
| 190 | $select .= "<option value=\"$p_stamp\""; |
| 191 | if ($p_stamp == strtr($peremption, array("-" => ""))) { |
| 192 | $select .= " selected='selected'"; |
| 193 | } |
| 194 | $select .= "> $day / $month / $year</option>\n"; |
| 195 | } |
| 196 | $page->assign('select',$select); |
| 197 | } |
| 198 | |
| 199 | function handler_nl(&$page, $action = null) |
| 200 | { |
| 201 | require_once 'newsletter.inc.php'; |
| 202 | |
| 203 | $page->changeTpl('newsletter/index.tpl'); |
| 204 | $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles'); |
| 205 | |
| 206 | switch ($action) { |
| 207 | case 'out': unsubscribe_nl(); break; |
| 208 | case 'in': subscribe_nl(); break; |
| 209 | default: ; |
| 210 | } |
| 211 | |
| 212 | $page->assign('nls', get_nl_state()); |
| 213 | $page->assign_by_ref('nl_list', get_nl_list()); |
| 214 | } |
| 215 | |
| 216 | function handler_nl_show(&$page, $nid = 'last') |
| 217 | { |
| 218 | $page->changeTpl('newsletter/show.tpl'); |
| 219 | |
| 220 | require_once 'newsletter.inc.php'; |
| 221 | |
| 222 | $nl = new NewsLetter($nid); |
| 223 | $page->assign_by_ref('nl', $nl); |
| 224 | |
| 225 | if (Post::has('send')) { |
| 226 | $nl->sendTo(S::v('prenom'), S::v('nom'), |
| 227 | S::v('bestalias'), S::v('femme'), |
| 228 | S::v('mail_fmt') != 'text'); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | function handler_nl_submit(&$page) |
| 233 | { |
| 234 | $page->changeTpl('newsletter/submit.tpl'); |
| 235 | |
| 236 | require_once 'newsletter.inc.php'; |
| 237 | |
| 238 | if (Post::has('see')) { |
| 239 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append')); |
| 240 | $page->assign('art', $art); |
| 241 | } elseif (Post::has('valid')) { |
| 242 | require_once('validations.inc.php'); |
| 243 | $art = new NLReq(S::v('uid'), Post::v('title'), |
| 244 | Post::v('body'), Post::v('append')); |
| 245 | $art->submit(); |
| 246 | $page->assign('submited', true); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | function handler_admin_events(&$page, $arch) { |
| 251 | $page->changeTpl('admin/evenements.tpl'); |
| 252 | $page->assign('xorg_title','Polytechnique.org - Administration - Evenements'); |
| 253 | |
| 254 | $arch = $arch == 'archives'; |
| 255 | $evid = Post::i('evt_id'); |
| 256 | $page->assign('arch', $arch); |
| 257 | |
| 258 | switch(Post::v('action')) { |
| 259 | case "Proposer": |
| 260 | XDB::execute('UPDATE evenements SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?} WHERE id = {?}', |
| 261 | Post::v('titre'), Post::v('texte'), Post::v('peremption'), Post::v('promo_min'), Post::v('promo_max'), $evid); |
| 262 | break; |
| 263 | |
| 264 | case "Valider": |
| 265 | XDB::execute('UPDATE evenements SET creation_date = creation_date, flags = CONCAT(flags,",valide") WHERE id = {?}', $evid); |
| 266 | break; |
| 267 | |
| 268 | case "Invalider": |
| 269 | XDB::execute('UPDATE evenements SET creation_date = creation_date, flags = REPLACE(flags,"valide", "") WHERE id = {?}', $evid); |
| 270 | break; |
| 271 | |
| 272 | case "Supprimer": |
| 273 | XDB::execute('DELETE from evenements WHERE id = {?}', $evid); |
| 274 | break; |
| 275 | |
| 276 | case "Archiver": |
| 277 | XDB::execute('UPDATE evenements SET creation_date = creation_date, flags = CONCAT(flags,",archive") WHERE id = {?}', $evid); |
| 278 | break; |
| 279 | |
| 280 | case "Desarchiver": |
| 281 | XDB::execute('UPDATE evenements SET creation_date = creation_date, flags = REPLACE(flags,"archive","") WHERE id = {?}', $evid); |
| 282 | break; |
| 283 | |
| 284 | case "Editer": |
| 285 | $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max FROM evenements WHERE id={?}', $evid); |
| 286 | list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow(); |
| 287 | $page->assign('mode', 'edit'); |
| 288 | $page->assign('titre',$titre); |
| 289 | $page->assign('texte',$texte); |
| 290 | $page->assign('promo_min',$promo_min); |
| 291 | $page->assign('promo_max',$promo_max); |
| 292 | $page->assign('peremption',$peremption); |
| 293 | |
| 294 | $select = ""; |
| 295 | for ($i = 1 ; $i < 30 ; $i++) { |
| 296 | $p_stamp=date("Ymd",time()+3600*24*$i); |
| 297 | $year=substr($p_stamp,0,4); |
| 298 | $month=substr($p_stamp,4,2); |
| 299 | $day=substr($p_stamp,6,2); |
| 300 | |
| 301 | $select .= "<option value=\"$p_stamp\"" . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")."> $day / $month / $year</option>\n"; |
| 302 | } |
| 303 | $page->assign('select',$select); |
| 304 | |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | if ($action != "Editer") { |
| 309 | |
| 310 | $sql = "SELECT e.id, e.titre, e.texte, |
| 311 | DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date, |
| 312 | DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption, |
| 313 | e.promo_min, e.promo_max, |
| 314 | FIND_IN_SET('valide', e.flags) AS fvalide, |
| 315 | FIND_IN_SET('archive', e.flags) AS farch, |
| 316 | u.promo, u.nom, u.prenom, a.alias AS forlife |
| 317 | FROM evenements AS e |
| 318 | INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id) |
| 319 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie') |
| 320 | WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags) |
| 321 | ORDER BY FIND_IN_SET('valide',e.flags), peremption"; |
| 322 | $page->assign('evs', XDB::iterator($sql)); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | function handler_admin_nl(&$page, $new = false) { |
| 327 | $page->changeTpl('newsletter/admin.tpl'); |
| 328 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste'); |
| 329 | require_once("newsletter.inc.php"); |
| 330 | |
| 331 | if($new) { |
| 332 | insert_new_nl(); |
| 333 | pl_redirect("admin/newsletter"); |
| 334 | } |
| 335 | |
| 336 | $page->assign_by_ref('nl_list', get_nl_slist()); |
| 337 | } |
| 338 | |
| 339 | function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') { |
| 340 | $page->changeTpl('newsletter/edit.tpl'); |
| 341 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition'); |
| 342 | require_once("newsletter.inc.php"); |
| 343 | |
| 344 | $nl = new NewsLetter($nid); |
| 345 | |
| 346 | if($action == 'delete') { |
| 347 | $nl->delArticle($aid); |
| 348 | pl_redirect("admin/newsletter/edit/$nid"); |
| 349 | } |
| 350 | |
| 351 | if($aid == 'update') { |
| 352 | $nl->_title = Post::v('title'); |
| 353 | $nl->_date = Post::v('date'); |
| 354 | $nl->_head = Post::v('head'); |
| 355 | $nl->save(); |
| 356 | } |
| 357 | |
| 358 | if(Post::v('save')) { |
| 359 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'), |
| 360 | $aid, Post::v('cid'), Post::v('pos')); |
| 361 | $nl->saveArticle($art); |
| 362 | pl_redirect("admin/newsletter/edit/$nid"); |
| 363 | } |
| 364 | |
| 365 | if($action == 'edit') { |
| 366 | $eaid = $aid; |
| 367 | if(Post::has('title')) { |
| 368 | $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'), |
| 369 | $eaid, Post::v('cid'), Post::v('pos')); |
| 370 | } else { |
| 371 | $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid); |
| 372 | } |
| 373 | $page->assign('art', $art); |
| 374 | } |
| 375 | |
| 376 | $page->assign_by_ref('nl',$nl); |
| 377 | } |
| 378 | function handler_admin_nl_cat(&$page, $action = 'list', $id = null) { |
| 379 | require_once('../classes/PLTableEditor.php'); |
| 380 | $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories'); |
| 381 | $page->assign('title', 'Gestion des catégories de la newsletter'); |
| 382 | $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid'); |
| 383 | $table_editor->describe('titre','intitulé',true); |
| 384 | $table_editor->describe('pos','position',true); |
| 385 | $table_editor->apply($page, $action, $id); |
| 386 | } |
| 387 | |
| 388 | } |
| 389 | |
| 390 | ?> |