Merge branch 'account'
[platal.git] / classes / user.php
index b99bc3a..1ce06bc 100644 (file)
@@ -170,7 +170,7 @@ class User extends PlUser
 
         $uids = array_map(array('XDB', 'escape'), $uids);
 
-        return XDB::iterator('SELECT  a.uid, a.hruid, a.registration_date,
+        return XDB::iterator('SELECT  a.uid, a.hruid, a.registration_date, ah.alias AS homonym,
                                       CONCAT(af.alias, \'@' . $globals->mail->domain . '\') AS forlife,
                                       CONCAT(af.alias, \'@' . $globals->mail->domain2 . '\') AS forlife_alternate,
                                       CONCAT(ab.alias, \'@' . $globals->mail->domain . '\') AS bestalias,
@@ -187,6 +187,7 @@ class User extends PlUser
                           INNER JOIN  account_types AS at ON (at.type = a.type)
                            LEFT JOIN  aliases AS af ON (af.uid = a.uid AND af.type = \'a_vie\')
                            LEFT JOIN  aliases AS ab ON (ab.uid = a.uid AND FIND_IN_SET(\'bestalias\', ab.flags))
+                           LEFT JOIN  aliases AS ah ON (ah.uid = a.uid AND ah.type = \'homonyme\')
                            LEFT JOIN  emails AS e ON (e.uid = a.uid AND e.flags = \'active\')
                            LEFT JOIN  email_options AS eo ON (eo.uid = a.uid)
                                    ' . $joins . '
@@ -204,7 +205,7 @@ class User extends PlUser
             && $this->gender !== null && $this->email_format !== null) {
             return;
         }
-        $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->user_id))->next());
+        $this->fillFromArray(self::loadMainFieldsFromUIDs(array($this->uid))->next());
     }
 
     // Specialization of the fillFromArray method, to implement hacks to enable
@@ -213,13 +214,6 @@ class User extends PlUser
     // stop being used actively.
     protected function fillFromArray(array $values)
     {
-        // It might happen that the 'user_id' field is called uid in some places
-        // (eg. in sessions), so we hard link uid to user_id to prevent useless
-        // SQL requests.
-        if (!isset($values['user_id']) && isset($values['uid'])) {
-            $values['user_id'] = $values['uid'];
-        }
-
         // Also, if display_name and full_name are not known, but the user's
         // surname and last name are, we can construct the former two.
         if (isset($values['prenom']) && isset($values['nom'])) {
@@ -432,7 +426,7 @@ class User extends PlUser
         $this->fillFromArray($watch);
     }
 
-    public function watch($type)
+    public function watchType($type)
     {
         $this->fetchWatchData();
         return $this->watch_actions->hasFlag($type);
@@ -471,7 +465,7 @@ class User extends PlUser
 
     // Contacts
     private $contacts = null;
-    public function isContact(PlUser &$user)
+    private function fetchContacts()
     {
         if (is_null($this->contacts)) {
             $this->contacts = XDB::fetchAllAssoc('contact', 'SELECT  *
@@ -479,7 +473,24 @@ class User extends PlUser
                                                               WHERE  uid = {?}',
                                                  $this->id());
         }
-        return isset($this->contacts[$user->id()]);
+    }
+
+    public function iterContacts()
+    {
+        $this->fetchContacts();
+        return Profile::iterOverPIDs(array_keys($this->contacts));
+    }
+
+    public function getContacts()
+    {
+        $this->fetchContacts();
+        return Profile::getBulkProfilesWithPIDs(array_keys($this->contacts));
+    }
+
+    public function isContact(Profile &$profile)
+    {
+        $this->fetchContacts();
+        return isset($this->contacts[$profile->id()]);
     }
 
     // Groupes X
@@ -495,6 +506,104 @@ class User extends PlUser
         return $this->groups;
     }
 
+    public function groupNames($institutions = false)
+    {
+        if ($institutions) {
+            $where = ' AND (g.cat = \'GroupesX\' OR g.cat = \'Institutions\')';
+        } else {
+            $where = '';
+        }
+        return XDB::fetchAllAssoc('SELECT  g.diminutif, g.nom, g.site
+                                     FROM  group_members AS gm
+                                LEFT JOIN  groups AS g ON (g.id = gm.asso_id)
+                                    WHERE  gm.uid = {?}' . $where,
+                                  $this->id());
+    }
+
+    /**
+     * Clears a user.
+     *  *always deletes in: account_lost_passwords, register_marketing,
+     *      register_pending, register_subs, watch_nonins, watch, watch_promo
+     *  *always keeps in: account_types, accounts, aliases, axletter_ins, carvas,
+     *      group_members, homonyms, newsletter_ins, register_mstats,
+     *  *deletes if $clearAll: account_auth_openid, announce_read, contacts,
+     *      email_options, email_send_save, emails, forum_innd, forum_profiles,
+     *      forum_subs, gapps_accounts, gapps_nicknames, group_announces_read,
+     *      group_member_sub_requests, reminder, requests, requests_hidden,
+     *      virtual, virtual_redirect, ML
+     *  *modifies if $clearAll: accounts
+     *
+     * Use cases:
+     *  *$clearAll == false: when a user dies, her family still needs to keep in
+     *      touch with the community.
+     *  *$clearAll == true: in every other case we want the account to be fully
+     *      deleted so that it can not be used anymore.
+     */
+    public function clear($clearAll = true)
+    {
+        XDB::execute('DELETE FROM  account_lost_passwords, register_marketing,
+                                   register_pending, register_subs, watch_nonins,
+                                   watch, watch_promo
+                            WHERE  uid = {?}',
+                     $this->id());
+
+        if ($clearAll) {
+            $groupIds = XDB::iterator('SELECT  asso_id
+                                         FROM  group_members
+                                        WHERE  uid = {?}',
+                                      $this->id());
+            while ($groupId = $groupIds->next()) {
+                $group = Group::get($groupId);
+                if ($group->notif_unsub) {
+                    $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl');
+                    $admins = $group->iterAdmins();
+                    while ($admin = $admins->next()) {
+                        $mailer->addTo($admin);
+                    }
+                    $mailer->assign('group', $group->shortname);
+                    $mailer->assign('user', $this);
+                    $mailer->assign('selfdone', false);
+                    $mailer->send();
+                }
+            }
+
+            XDB::execute('DELETE FROM  account_auth_openid, announce_read, contacts,
+                                       email_options, email_send_save, emails,
+                                       forum_innd, forum_profiles, forum_subs,
+                                       gapps_accounts, gapps_nicknames, group_announces_read,
+                                       group_members, group_member_sub_requests, reminder, requests,
+                                       requests_hidden
+                                WHERE  uid = {?}',
+                         $this->id());
+            XDB::execute("UPDATE  accounts
+                             SET  registration_date = 0, state = 'pending', password = NULL,
+                                  weak_password = NULL, token = NULL, is_admin = 0
+                           WHERE  uid = {?}",
+                         $this->id());
+
+            XDB::execute('DELETE  v.*
+                            FROM  virtual          AS v
+                      INNER JOIN  virtual_redirect AS r ON (v.vid = r.vid)
+                           WHERE  redirect = {?} OR redirect = {?}',
+                         $this->forlifeEmail(), $this->m4xForlifeEmail());
+            XDB::execute('DELETE FROM  virtual_redirect
+                                WHERE  redirect = {?} OR redirect = {?}',
+                         $this->forlifeEmail(), $this->m4xForlifeEmail());
+
+            if ($globals->mailstorage->googleapps_domain) {
+                require_once 'googleapps.inc.php';
+
+                if (GoogleAppsAccount::account_status($uid)) {
+                    $account = new GoogleAppsAccount($user);
+                    $account->suspend();
+                }
+            }
+        }
+
+        $mmlist = new MMList($this);
+        $mmlist->kill($alias, $clearAll);
+    }
+
     // Return permission flags for a given permission level.
     public static function makePerms($perms, $is_admin)
     {