From: Florent Bruneau Date: Wed, 14 Jan 2009 21:38:55 +0000 (+0100) Subject: Add a function to get a set of sorted users from a set of uid. X-Git-Tag: xorg/1.0.0~332^2~418 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=832e6fcb12ba94ac0f678837d19aaaa62384756f;p=platal.git Add a function to get a set of sorted users from a set of uid. Add a smarty function that formats the user name and the link to the profile. Signed-off-by: Florent Bruneau --- diff --git a/classes/profile.php b/classes/profile.php index eae671a..9c89e25 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -1,6 +1,6 @@ id()); + $from = 'account_profiles AS ap + INNER JOIN profiles AS p ON (p.pid = ap.pid)'; + $where = XDB::format('ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id()); } else if (is_numeric($login)) { - $res = XDB::query('SELECT p.pid, p.hrpid - FROM profiles AS p - WHERE p.pid = {?}', - $login); + $from = 'profiles AS p'; + $where = XDB::format('p.pid = {?}', $login); } else { - $res = XDB::query('SELECT p.pid, p.hrpid - FROM profiles AS p - WHERE p.hrpid = {?}', - $login); + $from = 'profiles AS p'; + $where = XDB::format('p.hrpid = {?}', $login); } + // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory + $res = XDB::query('SELECT p.*, u.prenom AS first_name, + IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name, + pd.promo AS promo, + CONCAT(u.prenom, " ", u.nom) AS short_name, + IF(u.nom_usage != "", + CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom), + CONCAT(u.nom, ", ", u.prenom)) AS full_name + FROM ' . $from . ' + INNER JOIN auth_user_md5 AS u ON (u.user_id = p.pid) + INNER JOIN profile_display AS pd ON (pd.pid = p.pid) + WHERE ' . $where); if ($res->numRows() != 1) { throw new UserNotFoundException(); } - list($this->pid, $this->hrpid) = $res->fetchOneRow(); + $this->data = $res->fetchOneAssoc(); + $this->pid = $this->data['pid']; + $this->hrpid = $this->data['hrpid']; } public function id() @@ -125,20 +133,6 @@ class Profile return $this->$name; } - if (empty($this->data)) { - // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory - $this->data = XDB::fetchOneAssoc('SELECT p.*, u.prenom AS first_name, - IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name, - u.promo AS promo, - CONCAT(u.prenom, " ", u.nom) AS short_name, - IF(u.nom_usage != "", - CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom), - CONCAT(u.nom, ", ", u.prenom)) AS full_name - FROM profiles AS p - INNER JOIN auth_user_md5 AS u ON (u.user_id = p.pid) - WHERE p.pid = {?}', - $this->id()); - } if (isset($this->data[$name])) { return $this->data[$name]; } diff --git a/classes/user.php b/classes/user.php index 0ff64bd..53c4f3c 100644 --- a/classes/user.php +++ b/classes/user.php @@ -1,6 +1,6 @@ fetchColumn(1)); } + protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null) + { + foreach ($uids as $key=>$uid) { + $uids[$key] = XDB::format('{?}', $uid); + } + $joins = ''; + $orderby = ''; + if (!is_null($sorted)) { + $order = array(); + $with_ap = false; + $with_pd = false; + foreach (explode(',', $sorted) as $part) { + $desc = ($part[0] == '-'); + if ($desc) { + $part = substr($desc, 1); + } + switch ($part) { + case 'promo': + $with_pd = true; + $with_ap = true; + $part = 'IF (pd.promo IS NULL, \'ext\', pd.promo)'; + break; + case 'full_name': + $part = 'a.full_name'; + break; + case 'display_name': + $part = 'a.display_name'; + break; + default: + $part = null; + } + if (!is_null($part)) { + if ($desc) { + $part .= ' DESC'; + } + $order[] = $part; + } + } + if (count($order) > 0) { + if ($with_ap) { + $joins .= "LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))\n"; + } + if ($with_pd) { + $joins .= "LEFT JOIN profile_display AS pd ON (pd.pid = ap.pid)\n"; + } + $orderby = 'ORDER BY ' . implode(', ', $order); + } + } + global $globals; + return XDB::iterator('SELECT a.uid, a.hruid, a.registration_date, + CONCAT(af.alias, \'@' . $globals->mail->domain . '\') AS forlife, + CONCAT(ab.alias, \'@' . $globals->mail->domain . '\') AS bestalias, + a.full_name, a.display_name, a.sex = \'female\' AS gender, + IF(a.state = \'active\', at.perms, \'\') AS perms, + a.email_format, a.is_admin, a.state, a.type, a.skin, + FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment, + a.weak_password IS NOT NULL AS weak_access, + a.token IS NOT NULL AS token_access + FROM accounts AS a + INNER JOIN account_types AS at ON (at.type = a.type) + LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = \'a_vie\') + LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags)) + ' . $joins . ' + WHERE a.uid IN (' . implode(', ', $uids) . ') + ' . $orderby); + } + // Implementation of the data loader. protected function loadMainFields() { @@ -144,25 +211,7 @@ class User extends PlUser && $this->gender !== null && $this->email_format !== null) { return; } - - global $globals; - /** TODO: promo stuff again */ - /** TODO: fix perms field to fit new perms system */ - $res = XDB::query("SELECT a.hruid, a.registration_date, - CONCAT(af.alias, '@{$globals->mail->domain}') AS forlife, - CONCAT(ab.alias, '@{$globals->mail->domain}') AS bestalias, - a.full_name, a.display_name, a.sex = 'female' AS gender, - IF(a.state = 'active', at.perms, '') AS perms, - a.email_format, a.is_admin, a.state, a.type, a.skin, - FIND_IN_SET('watch', a.flags) AS watch, a.comment, - a.weak_password IS NOT NULL AS weak_access, - a.token IS NOT NULL AS token_access - FROM accounts AS a - INNER JOIN account_types AS at ON (at.type = a.type) - LEFT JOIN aliases AS af ON (af.id = a.uid AND af.type = 'a_vie') - LEFT JOIN aliases AS ab ON (ab.id = a.uid AND FIND_IN_SET('bestalias', ab.flags)) - WHERE a.uid = {?}", $this->user_id); - $this->fillFromArray($res->fetchOneAssoc()); + $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->user_id))->next()); } // Specialization of the fillFromArray method, to implement hacks to enable @@ -341,6 +390,17 @@ class User extends PlUser $dom != $globals->mail->alias_dom && $dom != $globals->mail->alias_dom2; } + + // Fetch a set of users from a list of UIDs + public static function getBuildUsersWithUIDs(array $uids, $sortby = null) + { + $fields = self::loadMainFieldsFromUIDs($uids, $sortby); + $users = array(); + while (($list = $fields->next())) { + $users[] = User::getSilentWithValues(null, $list); + } + return $users; + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: diff --git a/modules/xnetlists.php b/modules/xnetlists.php index f58955f..7e90d3a 100644 --- a/modules/xnetlists.php +++ b/modules/xnetlists.php @@ -1,6 +1,6 @@ asso('id')); - + $ann = XDB::fetchColumn('SELECT uid + FROM groupex.membres + WHERE asso_id = {?}', $globals->asso('id')); + $users = User::getBuildUsersWithUIDs($ann, 'promo,full_name'); $not_in_list = array(); - while ($tmp = $ann->next()) { - $user = User::getWithUID($tmp['uid']); + foreach ($users as $user) { if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) { $not_in_list[] = $user; } diff --git a/plugins/function.profile.php b/plugins/function.profile.php new file mode 100644 index 0000000..8d34076 --- /dev/null +++ b/plugins/function.profile.php @@ -0,0 +1,49 @@ +fullName()); + if ($with_sex && $user->isFemale()) { + $name = '•' . $name; + } + if ($with_promo) { + $promo = $user->promo(); + if ($promo) { + $name .= ' (' . pl_entities($promo) . ')'; + } + } + if ($with_link) { + $profile = ($user instanceof Profile) ? $user : $user->profile(); + if ($profile) { + $name = '' . $name . ''; + } + } + return $name; +} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/templates/xnetlists/sync.tpl b/templates/xnetlists/sync.tpl index 1006260..a92d7aa 100644 --- a/templates/xnetlists/sync.tpl +++ b/templates/xnetlists/sync.tpl @@ -1,6 +1,6 @@ {**************************************************************************} {* *} -{* Copyright (C) 2003-2009 Polytechnique.org *} +{* Copyright (C) 2003-2008 Polytechnique.org *} {* http://opensource.polytechnique.org/ *} {* *} {* This program is free software; you can redistribute it and/or modify *} @@ -34,7 +34,7 @@ {foreach from=$not_in_list item=u} - {$u->fullName()} + {profile user=$u with_promo=false} {$u->promo()}