054d07a99371ba4d6a239dfd8e932441df5635ab
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 CarnetModule
extends PLModule
27 'carnet' => $this->make_hook('index', AUTH_COOKIE
),
28 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE
),
29 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE
),
31 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE
),
32 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE
),
33 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE
),
34 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_PUBLIC
, 'user', NO_HTTPS
),
35 'carnet/contacts/csv' => $this->make_hook('csv', AUTH_PUBLIC
, 'user', NO_HTTPS
),
36 'carnet/contacts/csv/birthday' => $this->make_hook('csv_birthday', AUTH_PUBLIC
, 'user', NO_HTTPS
),
38 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC
, 'user', NO_HTTPS
),
42 function _add_rss_link(&$page)
44 if (!S
::hasAuthToken()) {
47 $page->setRssLink('Polytechnique.org :: Carnet',
48 '/carnet/rss/'.S
::v('hruid').'/'.S
::v('token').'/rss.xml');
51 function handler_index(&$page)
53 $page->changeTpl('carnet/index.tpl');
54 $page->setTitle('Mon carnet');
55 $this->_add_rss_link($page);
58 function handler_panel(&$page)
60 $page->changeTpl('carnet/panel.tpl');
62 if (Get
::has('read')) {
63 XDB
::execute('UPDATE watch
64 SET last = FROM_UNIXTIME({?})
66 Get
::i('read'), S
::i('uid'));
67 S
::set('watch_last', Get
::i('read'));
68 Platal
::session()->updateNbNotifs();
69 pl_redirect('carnet/panel');
72 require_once 'notifs.inc.php';
73 $page->assign('now', time());
76 $notifs = Watch
::getEvents($user, time() - (7 * 86400));
77 $page->assign('notifs', $notifs);
78 $page->assign('today', date('Y-m-d'));
79 $this->_add_rss_link($page);
82 private function getSinglePromotion(PlPage
&$page, $promo)
84 if (!(is_int($promo) ||
ctype_digit($promo)) ||
$promo < 1920 ||
$promo > date('Y')) {
85 $page->trigError('Promotion invalide : ' . $promo . '.');
91 private function getPromo(PlPage
&$page, $promo)
93 if (strpos($promo, '-') === false
) {
94 $promo = $this->getSinglePromotion($page, $promo);
102 list($promo1, $promo2) = explode('-', $promo);
103 $promo1 = $this->getSinglePromotion($page, $promo1);
107 $promo2 = $this->getSinglePromotion($page, $promo2);
111 if ($promo1 > $promo2) {
112 $page->trigError('Intervalle non valide : ' . $promo . '.');
116 for ($i = $promo1 ; $i <= $promo2 ; ++
$i) {
122 private function addPromo(PlPage
&$page, $promo)
124 $promos = $this->getPromo($page, $promo);
125 if (!$promos ||
count($promos) == 0) {
129 foreach ($promos as $promo) {
130 $to_add[] = XDB
::format('({?}, {?})', S
::i('uid'), $promo);
132 XDB
::execute('INSERT IGNORE INTO watch_promo (uid, promo)
133 VALUES ' . implode(', ', $to_add));
136 private function delPromo(PlPage
&$page, $promo)
138 $promos = $this->getPromo($page, $promo);
139 if (!$promos ||
count($promos) == 0) {
142 $to_delete = array();
143 foreach ($promos as $promo) {
144 $to_delete[] = XDB
::format('{?}', $promo);
146 XDB
::execute('DELETE FROM watch_promo
147 WHERE ' . XDB
::format('uid = {?}', S
::i('uid')) . '
148 AND promo IN (' . implode(', ', $to_delete) . ')');
151 public function addNonRegistered(PlPage
&$page, PlUser
&$user)
153 XDB
::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id)
154 VALUES ({?}, {?})', S
::i('uid'), $user->id());
157 public function delNonRegistered(PlPage
&$page, PlUser
&$user)
159 XDB
::execute('DELETE FROM watch_nonins
160 WHERE uid = {?} AND ni_id = {?}',
161 S
::i('uid'), $user->id());
164 public function handler_notifs(&$page, $action = null
, $arg = null
)
166 $page->changeTpl('carnet/notifs.tpl');
169 S
::assert_xsrf_token();
172 $this->addPromo($page, $arg);
176 $this->delPromo($page, $arg);
180 $user = User
::get($arg);
182 $this->delNonRegistered($page, $user);
187 $user = User
::get($arg);
189 $this->addNonRegistered($page, $user);
195 if (Env
::has('subs')) {
196 S
::assert_xsrf_token();
197 $flags = new PlFlagSet();
198 foreach (Env
::v('sub') as $key=>$value) {
199 $flags->addFlag($key, $value);
201 XDB
::execute('UPDATE watch
203 WHERE uid = {?}', $flags, S
::i('uid'));
206 if (Env
::has('flags_contacts')) {
207 S
::assert_xsrf_token();
208 XDB
::execute('UPDATE watch
209 SET ' . XDB
::changeFlag('flags', 'contacts', Env
::b('contacts')) . '
210 WHERE uid = {?}', S
::i('uid'));
213 if (Env
::has('flags_mail')) {
214 S
::assert_xsrf_token();
215 XDB
::execute('UPDATE watch
216 SET ' . XDB
::changeFlag('flags', 'mail', Env
::b('mail')) . '
217 WHERE uid = {?}', S
::i('uid'));
221 $nonins = new UserFilter(new UFC_WatchRegistration($user));
223 $promo = XDB
::fetchColumn('SELECT promo
226 ORDER BY promo', S
::i('uid'));
227 $page->assign('promo_count', count($promo));
231 foreach ($promo as $p) {
232 if (is_null($range_start)) {
233 $range_start = $range_end = $p;
234 } else if ($p != $range_end +
1) {
235 $ranges[] = array($range_start, $range_end);
236 $range_start = $range_end = $p;
241 $ranges[] = array($range_start, $range_end);
242 $page->assign('promo_ranges', $ranges);
243 $page->assign('nonins', $nonins->getUsers());
245 list($flags, $actions) = XDB
::fetchOneRow('SELECT flags, actions
247 WHERE uid = {?}', S
::i('uid'));
248 $flags = new PlFlagSet($flags);
249 $actions = new PlFlagSet($actions);
250 $page->assign('flags', $flags);
251 $page->assign('actions', $actions);
254 function handler_contacts(&$page, $action = null
, $subaction = null
, $ssaction = null
)
256 $page->setTitle('Mes contacts');
257 $this->_add_rss_link($page);
262 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
263 // It allows direct linking to contact adding in the RSS feed.
264 if (Env
::v('action') && Env
::v('token') !== S
::v('token')) {
265 S
::assert_xsrf_token();
267 switch (Env
::v('action')) {
269 if (($contact = User
::get(Env
::v('user')))) {
270 if (XDB
::execute("DELETE FROM contacts
271 WHERE uid = {?} AND contact = {?}",
272 $uid, $contact->id())) {
273 Platal
::session()->updateNbNotifs();
274 $page->trigSuccess("Contact retiré !");
280 if (($contact = User
::get(Env
::v('user')))) {
281 if (XDB
::execute("REPLACE INTO contacts (uid, contact)
283 $uid, $contact->id())) {
284 Platal
::session()->updateNbNotifs();
285 $page->trigSuccess('Contact ajouté !');
287 $page->trigWarning('Contact déjà dans la liste !');
295 require_once 'userset.inc.php';
297 if ($action == 'search') {
298 $action = $subaction;
299 $subaction = $ssaction;
302 if ($search && trim(Env
::v('quick'))) {
303 $base = 'carnet/contacts/search';
304 $view = new SearchSet(true
, false
, new UFC_Contact($user));
306 $base = 'carnet/contacts';
307 $view = new ProfileSet(new UFC_Contact($user));
310 $view->addMod('minifiche', 'Mini-fiches', true
);
311 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
312 // TODO: Reactivate when the new map is completed.
313 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
314 $view->apply('carnet/contacts', $page, $action, $subaction);
315 //if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
316 $page->changeTpl('carnet/mescontacts.tpl');
320 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
322 $this->load('contacts.pdf.inc.php');
325 Platal
::session()->close();
327 $order = array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
328 if ($arg0 == 'promo') {
329 $order = array_unshift($order, new UFO_Promo());
331 $order[] = new UFO_Promo();
333 $filter = new UserFilter(new UFC_Contact($user), $order);
335 $pdf = new ContactsPDF();
337 $it = $filter->iterProfiles();
338 while ($p = $it->next()) {
339 $pdf = ContactsPDF
::addContact($pdf, $p, $arg0 == 'photos' ||
$arg1 == 'photos');
346 function handler_rss(&$page, $user = null
, $hash = null
)
348 $this->load('feed.inc.php');
349 $feed = new CarnetFeed();
350 return $feed->run($page, $user, $hash);
353 function buildBirthRef(Profile
$profile)
355 $date = strtotime($profile->birthdate
);
356 $tomorrow = $date +
86400;
358 'timestamp' => $date,
359 'date' => date('Ymd', $date),
360 'tomorrow' => date('Ymd', $tomorrow),
361 'hruid' => $profile->hrid(),
362 'summary' => 'Anniversaire de ' . $profile->fullName(true
)
366 function handler_csv_birthday(&$page, $alias = null
, $hash = null
)
368 $user = Platal
::session()->tokenAuth($alias, $hash);
369 if (is_null($user)) {
377 $page->changeTpl('carnet/calendar.outlook.tpl', NO_SKIN
);
378 $filter = new UserFilter(new UFC_Contact($user));
379 $profiles = $filter->iterProfiles();
380 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
381 $years = array(date("Y"));
382 for ($i = 1; $i <= 10; ++
$i) {
383 $years[] = $years[0] +
$i;
385 $page->assign('years', $years);
387 if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
388 $lang = strtolower($matches[1]);
390 $page->assign('lang', $lang);
392 $encoding = 'iso8859-15';
396 pl_content_headers("text/comma-separated-values;charset=".$encoding);
399 function handler_ical(&$page, $alias = null
, $hash = null
)
401 $user = Platal
::session()->tokenAuth($alias, $hash);
402 if (is_null($user)) {
410 require_once 'ical.inc.php';
411 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
412 $page->register_function('display_ical', 'display_ical');
414 $filter = new UserFilter(new UFC_Contact($user));
415 $profiles = $filter->iterProfiles();
416 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
418 pl_content_headers("text/calendar");
421 function handler_vcard(&$page, $photos = null
)
423 $pf = new ProfileFilter(new UFC_Contact(S
::user()));
424 $vcard = new VCard($photos == 'photos');
425 $vcard->addProfiles($pf->getProfiles());
429 function handler_csv(&$page, $alias = null
, $hash = null
)
431 $user = Platal
::session()->tokenAuth($alias, $hash);
432 if (is_null($user)) {
440 $page->changeTpl('carnet/mescontacts.outlook.tpl', NO_SKIN
);
441 $pf = new ProfileFilter(new UFC_Contact($user));
442 require_once 'carnet/outlook.inc.php';
443 Outlook
::output_profiles($pf->getProfiles(), 'fr');
447 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: