Fixes ProfileAddresses
[platal.git] / classes / user.php
index 7db7df4..83d7b0a 100644 (file)
@@ -205,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
@@ -214,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'])) {
@@ -485,19 +478,19 @@ class User extends PlUser
     public function iterContacts()
     {
         $this->fetchContacts();
-        return self::iterOverUIDs(array_keys($this->contacts));
+        return Profile::iterOverPIDs(array_keys($this->contacts));
     }
 
     public function getContacts()
     {
         $this->fetchContacts();
-        return self::getBulkUsersWithUIDs(array_keys($this->contacts));
+        return Profile::getBulkProfilesWithPIDs(array_keys($this->contacts));
     }
 
-    public function isContact(PlUser &$user)
+    public function isContact(Profile &$profile)
     {
         $this->fetchContacts();
-        return isset($this->contacts[$user->id()]);
+        return isset($this->contacts[$profile->id()]);
     }
 
     // Groupes X
@@ -513,6 +506,90 @@ class User extends PlUser
         return $this->groups;
     }
 
+    /**
+     * 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)
     {