Add a class Group to handler xnet groups.
[platal.git] / classes / user.php
index 5562e47..cea85c6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -29,6 +29,10 @@ class User extends PlUser
     {
         global $globals;
 
+        if ($login instanceof User) {
+            $machin->id();
+        }
+
         if ($login instanceof Profile) {
             $this->_profile = $login;
             $this->_profile_fetched = true;
@@ -135,7 +139,7 @@ class User extends PlUser
         throw new UserNotFoundException($res->fetchColumn(1));
     }
 
-    protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null)
+    protected static function loadMainFieldsFromUIDs(array $uids, $sorted = null, $count = null, $offset = null)
     {
         global $globals;
         $joins = '';
@@ -148,7 +152,7 @@ class User extends PlUser
             foreach (explode(',', $sorted) as $part) {
                 $desc = ($part[0] == '-');
                 if ($desc) {
-                    $part = substr($desc, 1);
+                    $part = substr($part, 1);
                 }
                 switch ($part) {
                   case 'promo':
@@ -162,6 +166,11 @@ class User extends PlUser
                   case 'display_name':
                     $part = 'a.display_name';
                     break;
+                  case 'directory_name':
+                    $part = 'pd.directory_name';
+                    $with_pd = true;
+                    $with_ap = true;
+                    break;
                   default:
                     $part = null;
                 }
@@ -185,9 +194,20 @@ class User extends PlUser
         if ($globals->asso('id')) {
             $joins .= XDB::format("LEFT JOIN groupex.membres AS gpm ON (gpm.uid = a.uid AND gpm.asso_id = {?})\n", $globals->asso('id'));
             $fields[] = 'gpm.perms AS group_perms';
+            $fields[] = 'gpm.comm AS group_comm';
         }
         if (count($fields) > 0) {
             $fields = ', ' . implode(', ', $fields);
+        } else {
+            $fields = '';
+        }
+        $limit = '';
+        if (!is_null($count)) {
+            if (!is_null($offset)) {
+                $limit = ' LIMIT ' . $offset . ', ' . $count;
+            } else {
+                $limit = ' LIMIT ' . $count;
+            }
         }
         $uids = array_map(array('XDB', 'escape'), $uids);
         return XDB::iterator('SELECT  a.uid, a.hruid, a.registration_date,
@@ -205,7 +225,7 @@ class User extends PlUser
                            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);
+                               ' . $orderby . $limit);
     }
 
     // Implementation of the data loader.
@@ -290,6 +310,22 @@ class User extends PlUser
         return $this->profile()->promo();
     }
 
+    public function firstName()
+    {
+        if (!$this->hasProfile()) {
+            return $this->displayName();
+        }
+        return $this->profile()->firstName();
+    }
+
+    public function lastName()
+    {
+        if (!$this->hasProfile()) {
+            return '';
+        }
+        return $this->profile()->lastName();
+    }
+
     /** Return the main profile attached with this account if any.
      */
     public function profile()
@@ -334,18 +370,32 @@ class User extends PlUser
 
     /** Get all the aliases the user belongs to.
      */
-    public function emailAliases($domain = null)
+    public function emailAliases($domain = null, $type = 'user',  $sub_state = false)
     {
+        $join = XDB::format('(vr.redirect = {?} OR vr.redirect = {?}) ',
+                             $this->forlifeEmail(), $this->m4xForlifeEmail());
         $where = '';
         if (!is_null($domain)) {
-            $where = XDB::format(' AND alias LIKE CONCAT("%@", {?})', $domain);
-        }
-        return XDB::fetchColumn('SELECT  v.alias
-                                   FROM  virtual AS v
-                             INNER JOIN  virtual_redirect AS vr ON (v.vid = vr.vid)
-                                  WHERE  (vr.redirect = {?} OR vr.redirect = {?})
-                                         ' . $where,
-                               $this->forlifeEmail(), $this->m4xForlifeEmail());
+            $where = XDB::format('WHERE v.alias LIKE CONCAT("%@", {?})', $domain);
+        }
+        if (!is_null($type)) {
+            if (empty($where)) {
+                $where = XDB::format('WHERE v.type = {?}', $type);
+            } else {
+                $where .= XDB::format(' AND v.type = {?}', $type);
+            }
+        }
+        if ($sub_state) {
+            return XDB::fetchAllAssoc('alias', 'SELECT  v.alias, vr.redirect IS NOT NULL AS sub
+                                                  FROM  virtual AS v
+                                             LEFT JOIN  virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
+                                                 ' . $where);
+        } else {
+            return XDB::fetchColumn('SELECT  v.alias
+                                       FROM  virtual AS v
+                                 INNER JOIN  virtual_redirect AS vr ON (v.vid = vr.vid AND ' . $join . ')
+                                     ' . $where);
+        }
     }
 
     /** Get the alternative forlife email
@@ -398,9 +448,9 @@ class User extends PlUser
     }
 
     // Fetch a set of users from a list of UIDs
-    public static function getBuildUsersWithUIDs(array $uids, $sortby = null)
+    public static function getBuildUsersWithUIDs(array $uids, $sortby = null, $count = null, $offset = null)
     {
-        $fields = self::loadMainFieldsFromUIDs($uids, $sortby);
+        $fields = self::loadMainFieldsFromUIDs($uids, $sortby, $count, $offset);
         $users = array();
         while (($list = $fields->next())) {
             $users[] = User::getSilentWithValues(null, $list);