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
),
36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC
, 'user', NO_HTTPS
),
40 function _add_rss_link(&$page)
42 if (!S
::hasAuthToken()) {
45 $page->setRssLink('Polytechnique.org :: Carnet',
46 '/carnet/rss/'.S
::v('hruid').'/'.S
::v('token').'/rss.xml');
49 function handler_index(&$page)
51 $page->changeTpl('carnet/index.tpl');
52 $page->setTitle('Mon carnet');
53 $this->_add_rss_link($page);
56 function handler_panel(&$page)
58 $page->changeTpl('carnet/panel.tpl');
60 if (Get
::has('read')) {
61 XDB
::execute('UPDATE watch
62 SET last = FROM_UNIXTIME({?})
64 Get
::i('read'), S
::i('uid'));
65 S
::set('watch_last', Get
::i('read'));
66 Platal
::session()->updateNbNotifs();
67 pl_redirect('carnet/panel');
70 require_once 'notifs.inc.php';
71 $page->assign('now', time());
74 $notifs = Watch
::getEvents($user, time() - (7 * 86400));
75 $page->assign('notifs', $notifs);
76 $page->assign('today', date('Y-m-d'));
77 $this->_add_rss_link($page);
80 private function getSinglePromotion(PlPage
&$page, $promo)
82 if (!ctype_digit($promo) ||
$promo < 1920 ||
$promo > date('Y')) {
83 $page->trigError('Promotion invalide : ' . $promo . '.');
89 private function getPromo(PlPage
&$page, $promo)
91 if (strpos($promo, '-') === false
) {
92 $promo = $this->getSinglePromotion($page, $promo);
100 list($promo1, $promo2) = explode('-', $promo);
101 $promo1 = $this->getSinglePromotion($page, $promo1);
105 $promo2 = $this->getSinglePromotion($page, $promo2);
109 if ($promo1 > $promo2) {
110 $page->trigError('Intervalle non valide : ' . $promo . '.');
114 for ($i = $promo1 ; $i <= $promo2 ; ++
$i) {
120 private function addPromo(PlPage
&$page, $promo)
122 $promos = $this->getPromo($page, $promo);
123 if (!$promos ||
count($promos) == 0) {
127 foreach ($promos as $promo) {
128 $to_add[] = XDB
::format('({?}, {?})', S
::i('uid'), $promo);
130 XDB
::execute('INSERT IGNORE INTO watch_promo (uid, promo)
131 VALUES ' . implode(', ', $to_add));
134 private function delPromo(PlPage
&$page, $promo)
136 $promos = $this->getPromo($page, $promo);
137 if (!$promos ||
count($promos) == 0) {
140 $to_delete = array();
141 foreach ($promos as $promo) {
142 $to_delete[] = XDB
::format('{?}', $promo);
144 XDB
::execute('DELETE FROM watch_promo
145 WHERE ' . XDB
::format('uid = {?}', S
::i('uid')) . '
146 AND promo IN (' . implode(', ', $to_delete) . ')');
149 public function addNonRegistered(PlPage
&$page, PlUser
&$user)
151 XDB
::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id)
152 VALUES ({?}, {?})', S
::i('uid'), $user->id());
155 public function delNonRegistered(PlPage
&$page, PlUser
&$user)
157 XDB
::execute('DELETE FROM watch_nonins
158 WHERE uid = {?} AND ni_id = {?}',
159 S
::i('uid'), $user->id());
162 public function handler_notifs(&$page, $action = null
, $arg = null
)
164 $page->changeTpl('carnet/notifs.tpl');
167 S
::assert_xsrf_token();
170 $this->addPromo($page, $arg);
174 $this->delPromo($page, $arg);
178 $user = User
::get($arg);
180 $this->delNonRegistered($page, $user);
185 $user = User
::get($arg);
187 $this->addNonRegistered($page, $user);
193 if (Env
::has('subs')) {
194 S
::assert_xsrf_token();
195 $flags = new PlFlagSet();
196 foreach (Env
::v('sub') as $key=>$value) {
197 $flags->addFlag($key, $value);
199 XDB
::execute('UPDATE watch
201 WHERE uid = {?}', $flags, S
::i('uid'));
204 if (Env
::has('flags_contacts')) {
205 S
::assert_xsrf_token();
206 XDB
::execute('UPDATE watch
207 SET ' . XDB
::changeFlag('flags', 'contacts', Env
::b('contacts')) . '
208 WHERE uid = {?}', S
::i('uid'));
211 if (Env
::has('flags_mail')) {
212 S
::assert_xsrf_token();
213 XDB
::execute('UPDATE watch
214 SET ' . XDB
::changeFlag('flags', 'mail', Env
::b('mail')) . '
215 WHERE uid = {?}', S
::i('uid'));
219 $nonins = new UserFilter(new UFC_WatchRegistration($user));
221 $promo = XDB
::fetchColumn('SELECT promo
224 ORDER BY promo', S
::i('uid'));
225 $page->assign('promo_count', count($promo));
229 foreach ($promo as $p) {
230 if (is_null($range_start)) {
231 $range_start = $range_end = $p;
232 } else if ($p != $range_end +
1) {
233 $ranges[] = array($range_start, $range_end);
234 $range_start = $range_end = $p;
239 $ranges[] = array($range_start, $range_end);
240 $page->assign('promo_ranges', $ranges);
241 $page->assign('nonins', $nonins->getUsers());
243 list($flags, $actions) = XDB
::fetchOneRow('SELECT flags, actions
245 WHERE uid = {?}', S
::i('uid'));
246 $flags = new PlFlagSet($flags);
247 $actions = new PlFlagSet($actions);
248 $page->assign('flags', $flags);
249 $page->assign('actions', $actions);
252 function handler_contacts(&$page, $action = null
, $subaction = null
, $ssaction = null
)
254 $page->setTitle('Mes contacts');
255 $this->_add_rss_link($page);
258 $user = Env
::v('user');
260 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
261 // It allows direct linking to contact adding in the RSS feed.
262 if (Env
::v('action') && Env
::v('token') !== S
::v('token')) {
263 S
::assert_xsrf_token();
265 switch (Env
::v('action')) {
267 if (($user = User
::get(Env
::v('user')))) {
268 if (XDB
::execute("DELETE FROM contacts
269 WHERE uid = {?} AND contact = {?}", $uid, $user->id())) {
270 $page->trigSuccess("Contact retiré !");
276 if (($user = User
::get(Env
::v('user')))) {
277 if (XDB
::execute("REPLACE INTO contacts (uid, contact)
278 VALUES ({?}, {?})", $uid, $user->id())) {
279 $page->trigSuccess('Contact ajouté !');
281 $page->trigWarning('Contact déjà dans la liste !');
290 require_once 'userset.inc.php';
292 if ($action == 'search') {
293 $action = $subaction;
294 $subaction = $ssaction;
297 if ($search && trim(Env
::v('quick'))) {
298 $base = 'carnet/contacts/search';
299 $view = new SearchSet(true
, false
, new UFC_Contact($user));
301 $base = 'carnet/contacts';
302 $view = new ProfileSet(new UFC_Contact($user));
305 $view->addMod('minifiche', 'Mini-fiches', true
);
306 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
307 // TODO: Reactivate when the new map is completed.
308 // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
309 $view->apply('carnet/contacts', $page, $action, $subaction);
310 //if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
311 $page->changeTpl('carnet/mescontacts.tpl');
315 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
317 $this->load('contacts.pdf.inc.php');
320 Platal
::session()->close();
322 $order = array(new UFO_Name(Profile
::LASTNAME
), new UFO_Name(Profile
::FIRSTNAME
));
323 if ($arg0 == 'promo') {
324 $order = array_unshift($order, new UFO_Promo());
326 $order[] = new UFO_Promo();
328 $filter = new UserFilter(new UFC_Contact($user), $order);
330 $pdf = new ContactsPDF();
332 $it = $filter->iterProfiles();
333 while ($p = $it->next()) {
334 $pdf = ContactsPDF
::addContact($pdf, $p, $arg0 == 'photos' ||
$arg1 == 'photos');
341 function handler_rss(&$page, $user = null
, $hash = null
)
343 $this->load('feed.inc.php');
344 $feed = new CarnetFeed();
345 return $feed->run($page, $user, $hash);
348 function buildBirthRef(Profile
$profile)
350 $date = strtotime($profile->birthdate
);
351 $tomorrow = $date +
86400;
353 'timestamp' => $date,
354 'date' => date('Ymd', $date),
355 'tomorrow' => date('Ymd', $tomorrow),
356 'hruid' => $profile->hrid(),
357 'summary' => 'Anniversaire de ' . $profile->fullName(true
)
362 function handler_ical(&$page, $alias = null
, $hash = null
)
364 $user = Platal
::session()->tokenAuth($alias, $hash);
365 if (is_null($user)) {
373 require_once 'ical.inc.php';
374 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
375 $page->register_function('display_ical', 'display_ical');
377 $filter = new UserFilter(new UFC_Contact($user));
378 $profiles = $filter->iterProfiles();
379 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
381 pl_content_headers("text/calendar");
384 function handler_vcard(&$page, $photos = null
)
386 $pf = new ProfileFilter(new UFC_Contact(S
::user()));
387 $vcard = new VCard($photos == 'photos');
388 $vcard->addProfiles($pf->getProfiles());
393 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: