X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fxnetgrp.php;h=d1b126407a1b2a8de448162e740ecdc516a85cdd;hb=94bf736ee13302194c25e7f204cfb2d80b9d835b;hp=35f589e146c0e6b682953c3e04d4a45d0506338f;hpb=bb88d138d64cbab3b4a1859a531df0c4479d051b;p=platal.git diff --git a/modules/xnetgrp.php b/modules/xnetgrp.php index 35f589e..d1b1264 100644 --- a/modules/xnetgrp.php +++ b/modules/xnetgrp.php @@ -81,24 +81,34 @@ class XnetGrpModule extends PLModule Env::i('unread'), S::i('uid')); pl_redirect("#art" . Env::i('unread')); } - // XXX: Fix promo_min; promo_max - $arts = XDB::iterator("SELECT a.*, FIND_IN_SET('photo', a.flags) AS photo + + /* TODO: refines this filter on promotions by using userfilter. */ + $user = S::user(); + if ($user->hasProfile()) { + $promo = XDB::format('{?}', $user->profile()->entry_year); + $minCondition = ' OR promo_min <= ' . $promo; + $maxCondition = ' OR promo_max >= ' . $promo; + } else { + $minCondition = ''; + $maxCondition = ''; + } + $arts = XDB::iterator('SELECT a.*, FIND_IN_SET(\'photo\', a.flags) AS photo FROM group_announces AS a LEFT JOIN group_announces_read AS r ON (r.uid = {?} AND r.announce_id = a.id) WHERE asso_id = {?} AND expiration >= CURRENT_DATE() - AND (promo_min = 0 OR promo_min <= {?}) - AND (promo_max = 0 OR promo_max >= {?}) + AND (promo_min = 0' . $minCondition . ') + AND (promo_max = 0' . $maxCondition . ') AND r.announce_id IS NULL - ORDER BY a.expiration", - S::i('uid'), $globals->asso('id'), S::i('promo'), S::i('promo')); - $index = XDB::iterator("SELECT a.id, a.titre, r.uid IS NULL AS nonlu + ORDER BY a.expiration', + S::i('uid'), $globals->asso('id')); + $index = XDB::iterator('SELECT a.id, a.titre, r.uid IS NULL AS nonlu FROM group_announces AS a LEFT JOIN group_announces_read AS r ON (a.id = r.announce_id AND r.uid = {?}) WHERE asso_id = {?} AND expiration >= CURRENT_DATE() - AND (promo_min = 0 OR promo_min <= {?}) - AND (promo_max = 0 OR promo_max >= {?}) - ORDER BY a.expiration", - S::i('uid'), $globals->asso('id'), S::i('promo'), S::i('promo')); + AND (promo_min = 0' . $minCondition . ') + AND (promo_max = 0' . $maxCondition . ') + ORDER BY a.expiration', + S::i('uid'), $globals->asso('id')); $page->assign('article_index', $index); } else { $arts = XDB::iterator("SELECT *, FIND_IN_SET('photo', flags) AS photo @@ -120,7 +130,7 @@ class XnetGrpModule extends PLModule $platal->ns . "rss/rss.xml"); } else { $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News", - $platal->ns . 'rss/'.S::v('hruid') .'/'.S::v('token').'/rss.xml'); + $platal->ns . 'rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml'); } $page->assign('articles', $arts); @@ -300,6 +310,11 @@ class XnetGrpModule extends PLModule $view->addMod('trombi', 'Trombinoscope'); $view->apply('annuaire', $page, $action, $subaction); $page->changeTpl('xnetgrp/annuaire.tpl'); + $count = XDB::fetchOneCell('SELECT COUNT(*) + FROM group_members + WHERE asso_id = {?}', + $globals->asso('id')); + $page->assign('nb_tot', $count); return; } @@ -326,6 +341,7 @@ class XnetGrpModule extends PLModule $users = $uf->getUsers(new PlLimit(NB_PER_PAGE, $ofs * NB_PER_PAGE)); $count = $uf->getTotalCount(); + $page->assign('nb_tot', $count); $page->assign('pages', floor(($count + NB_PER_PAGE - 1) / NB_PER_PAGE)); $page->assign('current', $ofs); $page->assign('order', $sort); @@ -358,9 +374,6 @@ class XnetGrpModule extends PLModule $filename = $globals->asso('diminutif') . '.csv'; } $users = $globals->asso()->getMembersFilter(null, new UFO_Name('directory_name'))->getUsers(); - header('Content-Type: text/x-csv; charset=utf-8;'); - header('Pragma: '); - header('Cache-Control: '); pl_content_headers("text/x-csv"); $page->changeTpl('xnetgrp/annuaire-csv.tpl', NO_SKIN); $page->assign('users', $users); @@ -477,16 +490,13 @@ class XnetGrpModule extends PLModule XDB::execute("INSERT INTO group_member_sub_requests (asso_id, uid, ts, reason) VALUES ({?}, {?}, NOW(), {?})", $globals->asso('id'), S::i('uid'), Post::v('message')); - $res = XDB::query('SELECT IF(m.email IS NULL, - CONCAT(al.alias,"@polytechnique.org"), - m.email) - FROM group_members AS m - INNER JOIN aliases AS al ON (al.type = "a_vie" - AND al.uid = m.uid) - WHERE perms="admin" AND m.asso_id = {?}', - $globals->asso('id')); - $emails = $res->fetchColumn(); - $to = implode(',', $emails); + $uf = New UserFilter(New UFC_Group($globals->asso('id'), true)); + $admins = $uf->iterUsers(); + $admin = $admins->next(); + $to = $admin->bestalias; + while ($admin = $admins->next()) { + $to .= ', ' . $admin->bestalias; + } $append = "\n" . "-- \n" @@ -626,55 +636,66 @@ class XnetGrpModule extends PLModule S::assert_xsrf_token(); - // Finds or creates account + // Finds or creates account: first cases are for users with an account. if (!User::isForeignEmailAddress($email)) { - // standard x account + // Standard account $user = User::get($email); } else if (!isvalid_email($email)) { // email might not be a regular email but an alias or a hruid $user = User::get($email); if (!$user) { // need a valid email address - $page->trigError("« $email Â» n'est pas une adresse email valide."); + $page->trigError('« ' . $email . ' Â» n\'est pas une adresse email valide.'); return; } } else if (Env::v('x') && Env::i('userid')) { - // user is an x but might not yet be registered $user = User::getWithUID(Env::i('userid')); if (!$user) { - $page->trigError("Utilisateur invalide"); + $page->trigError('Utilisateur invalide.'); return; } - // add email for marketing if unknown - if ($user->state == 'pending' && Env::v('market')) { - $market = Marketing::get($user->uid, $email); - if (!$market) { - $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'), - Env::v('market_from'), S::v('uid')); - $market->add(); + + // User has an account but is not yet registered. + if ($user->state == 'pending') { + // Add email in account table. + XDB::query('UPDATE accounts + SET email = {?} + WHERE uid = {?} AND email IS NULL', + Post::t('email'), $user->id()); + // Add email for marketing if required. + if (Env::v('market')) { + $market = Marketing::get($user->uid, $email); + if (!$market) { + $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'), + Env::v('market_from'), S::v('uid')); + $market->add(); + } } } } else { - // user is not an x - $hruid = strtolower(str_replace('@','.',$email).'.ext'); - // might already exists (in another group for example) + // User is of type xnet. + list($firstname, $lastname) = explode('@', $email); + $hruid = User::makeHrid($firstname, $lastname, 'ext'); + // User might already have an account (in another group for example). $user = User::get($hruid); + + // If the user has no account yet, creates new account: build names from email address. if (empty($user)) { - // creates new account: build names from email address $display_name = ucwords(strtolower(substr($hruid, strpos('.', $hruid)))); - $full_name = ucwords(strtolower(str_replace('.', ' ',substr($email, strpos('@', $email))))); - XDB::execute("INSERT INTO accounts (hruid, display_name, full_name, email, type) - VALUES({?}, {?}, {?}, {?}, 'xnet')", - $hruid, $display_name, $full_name, $email); + $full_name = ucwords(strtolower(str_replace('.', ' ', substr($email, strpos('@', $email))))); + XDB::execute('INSERT INTO accounts (hruid, display_name, full_name, email, type) + VALUES ({?}, {?}, {?}, {?}, \'xnet\')', + $hruid, $display_name, $full_name, $email); $user = User::get($hruid); } } + if ($user) { - XDB::execute("REPLACE INTO group_members (uid, asso_id) - VALUES ({?}, {?})", + XDB::execute('REPLACE INTO group_members (uid, asso_id) + VALUES ({?}, {?})', $user->id(), $globals->asso('id')); $this->removeSubscriptionRequest($user->id()); - pl_redirect("member/" . $user->login()); + pl_redirect('member/' . $user->login()); } } @@ -690,16 +711,16 @@ class XnetGrpModule extends PLModule } } if (empty($users)) { - list($nom, $prenom) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom'))); + list($lastname, $firstname) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom'))); $cond = new PFC_And(new PFC_Not(new UFC_Registered())); - if (!empty($nom)) { - $cond->addChild(new UFC_Name(Profile::LASTNAME, $nom, UFC_Name::CONTAINS)); + if (!empty($lastname)) { + $cond->addChild(new UFC_Name(Profile::LASTNAME, $lastname, UFC_Name::CONTAINS)); } - if (!empty($prenom)) { - $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $prenom, UFC_Name::CONTAINS)); + if (!empty($firstname)) { + $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $firstname, UFC_Name::CONTAINS)); } - if (Env::i('promo')) { - $cond->addChild(new UFC_Promo('=', UserFilter::GRADE_ING, Env::i('promo'))); + if (Env::t('promo')) { + $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, Env::t('promo'))); } $uf = new UserFilter($cond); $users = $uf->getUsers(new PlLimit(30)); @@ -768,7 +789,7 @@ class XnetGrpModule extends PLModule function handler_unsubscribe(&$page) { $page->changeTpl('xnetgrp/membres-del.tpl'); - $user = S::user()->id(); + $user = S::user(); if (empty($user)) { return PL_NOT_FOUND; } @@ -781,10 +802,18 @@ class XnetGrpModule extends PLModule S::assert_xsrf_token(); } + $hasSingleGroup = $user->hasSingleGroup(); + if ($this->unsubscribe($user)) { - $page->trigSuccess('Vous avez été désinscrit du groupe avec succès.'); + $page->trigSuccess('Tu as été désinscrit du groupe avec succès.'); } else { - $page->trigWarning('Vous avez été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des listes de diffusion.'); + $page->trigWarning('Tu as été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des listes de diffusion.'); + } + if ($user->type == 'xnet' && $hasSingleGroup && Post::has('accountDeletion')) { + XDB::execute('DELETE FROM acounts + WHERE uid = {?}', + $user->id()); + $page->trigSuccess('Ton compte a bien été supprimé.'); } $page->assign('is_member', is_member(true)); } @@ -796,6 +825,14 @@ class XnetGrpModule extends PLModule if (empty($user)) { return PL_NOT_FOUND; } + + global $globals; + + if (!$user->inGroup($globals->asso('id'))) { + pl_redirect('annuaire'); + } + + $page->assign('self', false); $page->assign('user', $user); if (!Post::has('confirm')) { @@ -804,16 +841,38 @@ class XnetGrpModule extends PLModule S::assert_xsrf_token(); } + $hasSingleGroup = $user->hasSingleGroup(); + if ($this->unsubscribe($user)) { $page->trigSuccess("{$user->fullName()} a été désinscrit du groupe !"); } else { $page->trigWarning("{$user->fullName()} a été désinscrit du groupe, mais des erreurs subsistent !"); } + + // Either deletes or notifies site administrators if it was the last group + // of a xnet account. + if ($user->type == 'xnet' && $hasSingleGroup) { + if ($user->state == 'pending') { + // If the user has never logged in the site, we delete her account. + XDB::execute('DELETE FROM acounts + WHERE uid = {?}', + $user->id()); + } else { + // It the user has already logged in the site, we notify site + // administrators that there is a new xnet account without any + // group. + $mailer = new PlMailer('xnetgrp/unsubscription.mail.tpl'); + $mailer->assign('user', $user); + $mailer->assign('groupId', $globals->asso('id')); + $mailer->assign('groupName', $globals->asso('nom')); + $mailer->send(); + } + } } - private function changeLogin(PlPage &$page, PlUser &$user, MMList &$mmlist, $login) + private function changeLogin(PlPage &$page, PlUser &$user, $login) { - // Search the uid of the user... + // Search the user's uid. $xuser = User::getSilent($login); if (!$xuser) { $accounts = User::getPendingAccounts($login); @@ -825,46 +884,33 @@ class XnetGrpModule extends PLModule return false; } $xuser = User::getSilent($accounts[0]['uid']); - $sub = false; } if (!$xuser) { return false; } - // Check if the user is in some groups as an X and as an ext - XDB::execute("DELETE g1 FROM group_members AS g1, group_members AS g2 - WHERE g1.uid = {?} AND g2.uid = {?} AND g1.asso_id = g2.asso_id", - $user->id(), $xuser->id()); - XDB::execute("UPDATE group_members - SET uid = {?} - WHERE uid = {?}", - $xuser->id(), $user->id()); - - // Update subscription to aliases - if ($sub && $user->forlifeEmail() != $xuser->forlifeEmail()) { - XDB::execute("UPDATE IGNORE virtual_redirect AS vr - SET vr.redirect = {?} - WHERE vr.redirect = {?}", - $xuser->forlifeEmail(), $user->forlifeEmail()); - foreach (Env::v('ml1', array()) as $ml => $state) { - $mmlist->replace_email($ml, $user->forlifeEmail(), $xuser->forlifeEmail()); - } + if ($user->mergeIn($xuser)) { + return $xuser->login(); } - return $xuser->login(); + return $user->login(); } function handler_admin_member(&$page, $user) { global $globals; - $page->changeTpl('xnetgrp/membres-edit.tpl'); - $user = User::getSilent($user); if (empty($user)) { return PL_NOT_FOUND; } + if (!$user->inGroup($globals->asso('id'))) { + pl_redirect('annuaire'); + } + + $page->changeTpl('xnetgrp/membres-edit.tpl'); + $mmlist = new MMList($user, $globals->asso('mail_domain')); if (Post::has('change')) { @@ -872,8 +918,7 @@ class XnetGrpModule extends PLModule // Convert user status to X if (!Post::blank('login_X')) { - // TODO: Rewrite changeLogin!!! - $forlife = $this->changeLogin($page, $user, $mmlist, Post::t('login_X')); + $forlife = $this->changeLogin($page, $user, Post::t('login_X')); if ($forlife) { pl_redirect('member/' . $forlife); } @@ -884,13 +929,27 @@ class XnetGrpModule extends PLModule $from_email = $user->forlifeEmail(); if (!$user->profile()) { XDB::query('UPDATE accounts - SET full_name = {?}, display_name = {?}, sex = {?}, email = {?}, type = {?} + SET full_name = {?}, directory_name = {?}, display_name = {?}, + sex = {?}, email = {?}, type = {?} WHERE uid = {?}', - Post::v('full_name'), Post::v('display_name'), (Post::v('sex') == 'male')?'male':'female', Post::v('email'), (Post::v('type') == 'xnet')?'xnet':'virtual', - $user->id()); - if (XDB::affectedRows()) { - $page->trigSuccess('Données de l\'utilisateur mise à jour.'); + Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'), + (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'), + (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id()); + // If user is of type xnet and new password is given. + if (!Post::blank('pwhash') && Post::t('type') == 'xnet') { + XDB::query('UPDATE accounts + SET password = {?} + WHERE uid = {?}', + Post::t('pwhash'), $user->id()); } + } else if (!$user->perms) { + XDB::query('UPDATE accounts + SET email = {?} + WHERE uid = {?}', + Post::t('email'), $user->id()); + } + if (XDB::affectedRows()) { + $page->trigSuccess('Données de l\'utilisateur mises à jour.'); } // Update group params for user @@ -902,11 +961,13 @@ class XnetGrpModule extends PLModule WHERE uid = {?} AND asso_id = {?}', ($perms == 'admin') ? 'admin' : 'membre', $comm, $user->id(), $globals->asso('id')); - if ($perms != $user->group_perms) { - $page->trigSuccess('Permissions modifiées !'); - } - if ($comm != $user->group_comm) { - $page->trigSuccess('Commentaire mis à jour.'); + if (XDB::affectedRows()) { + if ($perms != $user->group_perms) { + $page->trigSuccess('Permissions modifiées !'); + } + if ($comm != $user->group_comm) { + $page->trigSuccess('Commentaire mis à jour.'); + } } } @@ -967,6 +1028,8 @@ class XnetGrpModule extends PLModule } } + $page->addJsLink('password.js'); + $page->assign('onlyGroup', $user->hasSingleGroup()); $page->assign('user', $user); $page->assign('listes', $mmlist->get_lists($user->forlifeEmail())); $page->assign('alias', $user->emailAliases($globals->asso('mail_domain'), 'user', true)); @@ -1049,6 +1112,7 @@ class XnetGrpModule extends PLModule $art['prenom'] = S::v('prenom'); $art['promo'] = S::v('promo'); $art['hruid'] = S::user()->login(); + $art['uid'] = S::user()->id(); $art['expiration'] = Post::v('expiration'); $art['public'] = Post::has('public'); $art['xorg'] = Post::has('xorg');