Change sorting order on ML display to use sort_name.
[platal.git] / classes / user.php
index b7ac5f2..eb91985 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,6 +24,7 @@ class User extends PlUser
     const PERM_API_USER_READONLY = 'api_user_readonly';
     const PERM_DIRECTORY_AX      = 'directory_ax';
     const PERM_DIRECTORY_PRIVATE = 'directory_private';
+    const PERM_DIRECTORY_HIDDEN  = 'directory_hidden';
     const PERM_EDIT_DIRECTORY    = 'edit_directory';
     const PERM_FORUMS            = 'forums';
     const PERM_GROUPS            = 'groups';
@@ -85,7 +86,7 @@ class User extends PlUser
         // Checks whether $login is a valid hruid or not.
         $res = XDB::query('SELECT  uid
                              FROM  accounts
-                            WHERE  hruid = {?}', $login);
+                             WHERE  hruid = {?}', $login);
         if ($res->numRows()) {
             return $res->fetchOneCell();
         }
@@ -164,13 +165,13 @@ class User extends PlUser
                                       IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate,
                                       IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias,
                                       (er.redirect IS NULL AND a.state = \'active\' AND FIND_IN_SET(\'mail\', at.perms)) AS lost,
-                                      a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender,
+                                      a.email, a.full_name, a.directory_name, a.display_name, a.sort_name, a.sex = \'female\' AS gender,
                                       IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms,
                                       a.user_perms, a.email_format, a.is_admin, a.state, a.type, at.description AS type_description, a.skin,
                                       FIND_IN_SET(\'watch\', a.flags) AS watch, a.comment,
                                       a.weak_password IS NOT NULL AS weak_access, g.g_account_name IS NOT NULL AS googleapps,
                                       a.token IS NOT NULL AS token_access, a.token, a.last_version,
-                                      UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, UNIX_TIMESTAMP(fp.last_seen) AS banana_last
+                                      s.start AS lastlogin, s.host, fp.last_seen AS banana_last
                                       ' . $fields . '
                                 FROM  accounts               AS a
                           INNER JOIN  account_types          AS at ON (at.type = a.type)
@@ -230,23 +231,23 @@ class User extends PlUser
      *
      * Rules are:
      *  - Everyone can view 'public'
-     *  - directory_ax gives access to 'AX' level
+     *  - directory_ax gives access to 'AX' level, ie. the printed directory
      *  - directory_private gives access to 'private' level
-     *  - admin gives access to 'hidden' level
+     *  - admin and directory_hidden gives access to 'hidden' level
      */
     public function readVisibility()
     {
-        $level = ProfileVisibility::VIS_NONE;
-        if ($this->is_admin) {
-            $level = ProfileVisibility::VIS_HIDDEN;
+        $level = Visibility::VIEW_NONE;
+        if ($this->is_admin || $this->checkPerms('directory_hidden')) {
+            $level = Visibility::VIEW_ADMIN;
         } elseif ($this->checkPerms('directory_private')) {
-            $level = ProfileVisibility::VIS_PRIVATE;
+            $level = Visibility::VIEW_PRIVATE;
         } elseif ($this->checkPerms('directory_ax')) {
-            $level = ProfileVisibility::VIS_AX;
+            $level = Visibility::VIEW_AX;
         } else {
-            $level = ProfileVisibility::VIS_PUBLIC;
+            $level = Visibility::VIEW_PUBLIC;
         }
-        return new ProfileVisibility($level);
+        return Visibility::get($level);
     }
 
     /** Retrieve the 'general' edit visibility.
@@ -254,23 +255,18 @@ class User extends PlUser
      *
      * Rules are:
      *  - Only admins can edit the 'hidden' fields
-     *  - If someone has 'directory_edit' and 'directory_ax': AX level
-     *  - If someone has 'directory_edit' and 'directory_private': Private level
+     *  - If someone has 'directory_edit' (which is actually directory_ax_edit): AX level
      *  - Otherwise, nothing.
      */
     public function editVisibility()
     {
-        $level = ProfileVisibility::VIS_NONE;
+        $level = Visibility::VIEW_NONE;
         if ($this->is_admin) {
-            $level = ProfileVisibility::VIS_HIDDEN;
+            $level = Visibility::VIEW_ADMIN;
         } elseif ($this->checkPerms('directory_edit')) {
-            if ($this->checkPerms('directory_ax')) {
-                $level = ProfileVisibility::VIS_AX;
-            } elseif ($this->checkPerms('directory_private')) {
-                $level = ProfileVisibility::VIS_PRIVATE;
-            }
+            $level = Visibility::VIEW_AX;
         }
-        return new ProfileVisibility($level);
+        return Visibility::get($level);
     }
 
     // We do not want to store the password in the object.
@@ -311,7 +307,7 @@ class User extends PlUser
     public function firstName()
     {
         if (!$this->hasProfile()) {
-            return $this->displayName();
+            return $this->firstname;
         }
         return $this->profile()->firstName();
     }
@@ -319,7 +315,7 @@ class User extends PlUser
     public function lastName()
     {
         if (!$this->hasProfile()) {
-            return '';
+            return $this->lastname;
         }
         return $this->profile()->lastName();
     }
@@ -356,6 +352,14 @@ class User extends PlUser
         return $this->profile()->directory_name;
     }
 
+    public function sortName()
+    {
+        if (!$this->hasProfile()) {
+            return $this->sort_name;
+        }
+        return $this->profile()->sort_name;
+    }
+
     static public function compareDirectoryName($a, $b)
     {
         return strcasecmp(replace_accent($a->directoryName()), replace_accent($b->directoryName()));
@@ -368,10 +372,18 @@ class User extends PlUser
         if (!$this->_profile_fetched || $forceFetch) {
             $this->_profile_fetched = true;
             $this->_profile = Profile::get($this, $fields, $visibility);
+        } else if ($this->_profile !== null && $visibility !== null && !$this->_profile->visibility->equals($visibility)) {
+            return Profile::get($this, $fields, $visibility);
         }
         return $this->_profile;
     }
 
+    public function setPrefetchedProfile(Profile $profile)
+    {
+        $this->_profile_fetched = true;
+        $this->_profile = $profile;
+    }
+
     /** Return true if the user has an associated profile.
      */
     public function hasProfile()
@@ -571,6 +583,10 @@ class User extends PlUser
         $watch['watch_promos'] = XDB::fetchColumn('SELECT  promo
                                                      FROM  watch_promo
                                                     WHERE  uid = {?}', $this->id());
+        $watch['watch_groups'] = XDB::fetchColumn("SELECT  w.groupid
+                                                     FROM  watch_group AS w
+                                               INNER JOIN  groups      AS g ON (w.groupid = g.id AND NOT FIND_IN_SET('private', pub))
+                                                    WHERE  w.uid = {?}", $this->id());
         $watch['watch_users'] = XDB::fetchColumn('SELECT  ni_id
                                                     FROM  watch_nonins
                                                    WHERE  uid = {?}', $this->id());
@@ -601,6 +617,12 @@ class User extends PlUser
         return $this->watch_promos;
     }
 
+    public function watchGroups()
+    {
+        $this->fetchWatchData();
+        return $this->watch_groups;
+    }
+
     public function watchUsers()
     {
         $this->fetchWatchData();
@@ -619,6 +641,7 @@ class User extends PlUser
         unset($this->watch_users);
         unset($this->watch_last);
         unset($this->watch_promos);
+        unset($this->watch_groups);
     }
 
 
@@ -711,7 +734,7 @@ class User extends PlUser
     /**
      * Clears a user.
      *  *always deletes in: account_lost_passwords, register_marketing,
-     *      register_pending, register_subs, watch_nonins, watch, watch_promo
+     *      register_pending, register_subs, watch_nonins, watch, watch_promo, watch_group,
      *  *always keeps in: account_types, accounts, email_virtual, carvas,
      *      group_members, homonyms_list, newsletter_ins, register_mstats, email_source_account
      *  *deletes if $clearAll: account_auth_openid, announce_read, contacts,
@@ -731,7 +754,7 @@ class User extends PlUser
     {
         $tables = array('account_lost_passwords', 'register_marketing',
                         'register_pending', 'register_subs', 'watch_nonins',
-                        'watch', 'watch_promo');
+                        'watch', 'watch_promo', 'watch_group');
 
         foreach ($tables as $t) {
             XDB::execute('DELETE FROM  ' . $t . '
@@ -889,7 +912,6 @@ class User extends PlUser
     public static function makePerms($perms, $is_admin)
     {
         $flags = new PlFlagSet($perms);
-        $flags->addFlag(PERMS_USER);
         if ($is_admin) {
             $flags->addFlag(PERMS_ADMIN);
         }
@@ -1094,5 +1116,5 @@ class UserIterator implements PlIterator
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>