Browse Source

#1020 Orgs: user isn't automatically removed from org

Alex Trofimov 8 years ago
parent
commit
069f354f1c

+ 33 - 0
inc/classes/BxDolAccount.php

@@ -113,6 +113,39 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
         return false;
     }
 
+    /**
+     * Switch context automatically to the first available profile
+     * @param $iProfileIdFilterOut profile ID to exclude from the list of possible profiles
+     * @param $iAccountId account ID to use istead of current account
+     * @return true on success or false on error
+     */ 
+    public function updateProfileContextAuto($iProfileIdFilterOut = false, $iAccountId = false)
+    {
+        $oAccount = (!$iAccountId || $iAccountId == $this->_iAccountID ? $this : BxDolAccount::getInstance ($iAccountId));
+        if (!$oAccount)
+            return false;
+        $aAccountInfo = $oAccount->getInfo();
+        $aProfiles = $oAccount->getProfiles();
+        $oProfileAccount = BxDolProfile::getInstanceAccountProfile($oAccount->id());
+
+        // unset deleted profile and account profile
+        if ($iProfileIdFilterOut)
+            unset($aProfiles[$iProfileIdFilterOut]);
+        unset($aProfiles[$oProfileAccount->id()]);
+
+        if ($aProfiles) {
+            // try to use another profile
+            reset($aProfiles);
+            $iProfileId = key($aProfiles);
+        } 
+        else {
+            // if no profiles exist, use account profile
+            $iProfileId = $oProfileAccount->id();
+        }
+
+        return $oAccount->updateProfileContext($iProfileId);
+    }
+    
     public function updateProfileContext($iSwitchToProfileId, $iAccountId = false)
     {
         $iId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;

+ 2 - 21
inc/classes/BxDolProfile.php

@@ -348,27 +348,8 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         // switch profile context if deleted profile is active profile context
         $oAccount = BxDolAccount::getInstance ($aProfileInfo['account_id']);
         $aAccountInfo = $oAccount->getInfo();
-        if (!$bForceDelete && $ID == $aAccountInfo['profile_id']) {
-
-            $aProfiles = $oAccount->getProfiles();            
-            $oProfileAccount = BxDolProfile::getInstanceAccountProfile($aProfileInfo['account_id']);
-
-            // unset deleted profile and account profile
-            unset($aProfiles[$ID]);
-            unset($aProfiles[$oProfileAccount->id()]);
-
-            if ($aProfiles) {
-                // try to use another profile
-                reset($aProfiles);
-                $iProfileId = key($aProfiles);
-            } 
-            else {
-                // if no profiles exist, use account profile
-                $iProfileId = $oProfileAccount->id();
-            }
-
-            $oAccount->updateProfileContext($iProfileId);
-        }
+        if (!$bForceDelete && $ID == $aAccountInfo['profile_id'])
+            $oAccount->updateProfileContextAuto($ID);
 
         // create system event before deletion
         $isStopDeletion = false;

+ 23 - 5
modules/base/groups/classes/BxBaseModGroupsDb.php

@@ -32,24 +32,42 @@ class BxBaseModGroupsDb extends BxBaseModProfileDb
 
     public function toAdmins ($iGroupProfileId, $mixedFansIds)
     {
-        if (is_array($mixedFansIds))
+        if (is_array($mixedFansIds)) {
             foreach ($mixedFansIds as $iFanId)
                 $this->toAdmins ($iFanId);
+            return true;
+        }
 
         $iFanId = (int)$mixedFansIds;
         $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` SET `group_profile_id` = ?, `fan_id` = ?", $iGroupProfileId, $iFanId);
-        return $this->res($sQuery);
+        if (!$this->res($sQuery))
+            return false;
+
+        $oModule = BxDolModule::getInstance($this->_oConfig->getName());
+        if ($oModule && method_exists($oModule, 'onFanAddedToAdmins'))
+            $oModule->onFanAddedToAdmins($iGroupProfileId, $iFanId);
+
+        return true;
     }
 
     public function fromAdmins ($iGroupProfileId, $mixedFansIds)
     {
-        if (is_array($mixedFansIds))
+        if (is_array($mixedFansIds)) {
             foreach ($mixedFansIds as $iFanId)
-                $this->toAdmins ($iFanId);
+                $this->fromAdmins ($iFanId);
+            return true;
+        }
 
         $iFanId = (int)$mixedFansIds;
         $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
-        return $this->res($sQuery);
+        if (!$this->res($sQuery))
+            return false;
+
+        $oModule = BxDolModule::getInstance($this->_oConfig->getName());
+        if ($oModule && method_exists($oModule, 'onFanRemovedFromAdmins'))
+            $oModule->onFanRemovedFromAdmins($iGroupProfileId, $iFanId);
+        
+        return true;
     }
 
     public function deleteAdminsByGroupId ($iGroupProfileId)

+ 3 - 1
modules/base/groups/classes/BxBaseModGroupsModule.php

@@ -138,7 +138,9 @@ class BxBaseModGroupsModule extends BxBaseModProfileModule
         $this->_oDb->fromAdmins($iGroupProfileId, $iProfileId);
 
         if ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions'))
-            $oConn->removeConnection($iProfileId, $iGroupProfileId);
+            return $oConn->removeConnection($iProfileId, $iGroupProfileId);
+
+        return false;
     }
 
     public function serviceAddMutualConnection ($iGroupProfileId, $iInitiatorId, $iIgnoreJoinConfirmation = false)

+ 8 - 0
modules/boonex/organizations/classes/BxOrgsModule.php

@@ -44,6 +44,14 @@ class BxOrgsModule extends BxBaseModGroupsModule
     {
     	return array();
     }
+
+    public function onFanRemovedFromAdmins($iGroupProfileId, $iProfileId)
+    {
+        if (!($oProfile = BxDolProfile::getInstance($iProfileId)))
+            return false;
+        $oAccount = $oProfile->getAccountObject();
+        $oAccount->updateProfileContextAuto();
+    }
 }
 
 /** @} */