Add a function to get a set of sorted users from a set of uid.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 14 Jan 2009 21:38:55 +0000 (22:38 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 14 Jan 2009 21:38:55 +0000 (22:38 +0100)
Add a smarty function that formats the user name and the link to the
profile.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/profile.php
classes/user.php
modules/xnetlists.php
plugins/function.profile.php [new file with mode: 0644]
templates/xnetlists/sync.tpl

index eae671a..9c89e25 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  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   *
@@ -28,26 +28,34 @@ class Profile
     private function __construct($login)
     {
         if ($login instanceof PlUser) {
-            $res = XDB::query('SELECT  p.pid, p.hrpid
-                                 FROM  account_profiles AS ap
-                           INNER JOIN  profiles AS p ON (p.pid = ap.pid)
-                                WHERE  ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)',
-                             $login->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];
         }
index 0ff64bd..53c4f3c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  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   *
@@ -135,6 +135,73 @@ class User extends PlUser
         throw new UserNotFoundException($res->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:
index f58955f..7e90d3a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  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   *
@@ -207,13 +207,12 @@ class XnetListsModule extends ListsModule
         $not_in_group_x = array();
         $not_in_group_ext = array();
 
-        $ann = XDB::iterator('SELECT  uid
-                                FROM  groupex.membres
-                               WHERE  asso_id = {?}', $globals->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 (file)
index 0000000..8d34076
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+function smarty_function_profile($params, &$smarty)
+{
+    $with_promo = isset($params['with_promo']) ? $params['with_promo'] : false;
+    $with_sex   = isset($params['with_sex']) ? $params['with_sex'] : true;
+    $with_link  = isset($params['with_link']) ? $params['with_link'] : true;
+    $user = $params['user'];
+
+    $name = pl_entities($user->fullName());
+    if ($with_sex && $user->isFemale()) {
+      $name = '&bull;' . $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 = '<a href="profile/' . $profile->hrid() . '" class="popup2">' . $name . '</a>';
+      }
+    }
+    return $name;
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index 1006260..a92d7aa 100644 (file)
@@ -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 @@
     </tr>
     {foreach from=$not_in_list item=u}
     <tr>
-      <td class='checkboxToggle'>{$u->fullName()}</td>
+      <td class='checkboxToggle'>{profile user=$u with_promo=false}</td>
       <td class='checkboxToggle'>{$u->promo()}</td>
       <td class='checkboxToggle'><input type="checkbox" class="moderate_email" name="add[{$u->forlifeEmail()}]" id="add{$u->forlifeEmail()}"/></td>
     </tr>