Просмотр исходного кода

#281 Groups: clean-up user's data when profile is deleted; re-assign author when author profile is deleted

Alex Trofimov 10 лет назад
Родитель
Сommit
bf8c53e950

+ 12 - 0
modules/base/general/classes/BxBaseModGeneralDb.php

@@ -21,6 +21,18 @@ class BxBaseModGeneralDb extends BxDolModuleDb
         parent::__construct($oConfig);
         $this->_oConfig = $oConfig;
     }
+
+    public function getEntriesByAuthor ($iProfileId)
+    {
+        $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
+        return $this->getAll($sQuery);
+    }
+
+    public function getEntriesNumByAuthor ($iProfileId)
+    {
+        $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
+        return $this->getOne($sQuery);
+    }
 }
 
 /** @} */

+ 0 - 12
modules/base/text/classes/BxBaseModTextDb.php

@@ -24,18 +24,6 @@ class BxBaseModTextDb extends BxBaseModGeneralDb
         $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_ID'] . "` = ?", $iContentId);
         return $this->getRow($sQuery);
     }
-
-    public function getEntriesByAuthor ($iProfileId)
-    {
-        $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
-        return $this->getAll($sQuery);
-    }
-
-    public function getEntriesNumByAuthor ($iProfileId)
-    {
-        $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
-        return $this->getOne($sQuery);
-    }
 }
 
 /** @} */

+ 8 - 1
modules/boonex/groups/classes/BxGroupsAlertsResponse.php

@@ -16,8 +16,15 @@ class BxGroupsAlertsResponse extends BxDolAlertsResponse
 {
     public function response($oAlert)
     {
-        if ('bx_groups_fans' == $oAlert->sUnit && 'connection_added' == $oAlert->sAction && !$oAlert->aExtras['mutual'])
+        if ('bx_groups_fans' == $oAlert->sUnit && 'connection_added' == $oAlert->sAction && !$oAlert->aExtras['mutual']) {
             BxDolService::call('bx_groups', 'add_mutual_connection', array($oAlert->aExtras['content'], $oAlert->aExtras['initiator']));
+        }
+
+        if ('profile' == $oAlert->sUnit && 'delete' == $oAlert->sAction) {
+            BxDolService::call('bx_groups', 'delete_profile_from_fans_and_admins', array($oAlert->iObject));
+            BxDolService::call('bx_groups', 'reassign_entities_by_author', array($oAlert->iObject));
+        }
+
     }
 }
 

+ 24 - 1
modules/boonex/groups/classes/BxGroupsDb.php

@@ -19,6 +19,17 @@ class BxGroupsDb extends BxBaseModProfileDb
         parent::__construct($oConfig);
     }
 
+    public function updateAuthorById ($iContentId, $iProfileId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $sQuery = "UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_AUTHOR'] . "` = :" . $CNF['FIELD_AUTHOR'] . " WHERE `id` = :id";
+        return $this->query($sQuery, array(
+    		'id' => $iContentId,
+    		$CNF['FIELD_AUTHOR'] => $iProfileId,
+    	));
+    }
+
     public function toAdmins ($iGroupProfileId, $mixedFansIds)
     {
         if (is_array($mixedFansIds))
@@ -41,17 +52,29 @@ class BxGroupsDb extends BxBaseModProfileDb
         return $this->res($sQuery);
     }
 
-    public function deleteAdmins ($iGroupProfileId)
+    public function deleteAdminsByGroupId ($iGroupProfileId)
     {
         $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
         return $this->res($sQuery);
     }
 
+    public function deleteAdminsByProfileId ($iProfileId)
+    {
+        $sQuery = $this->prepare("DELETE FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `fan_id` = ?", $iProfileId);
+        return $this->res($sQuery);
+    }
+
     public function isAdmin ($iGroupProfileId, $iFanId)
     {
         $sQuery = $this->prepare("SELECT `id` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ? AND `fan_id` = ?", $iGroupProfileId, $iFanId);
         return $this->getOne($sQuery) ? true : false;
     }
+
+    public function getAdmins ($iGroupProfileId)
+    {
+        $sQuery = $this->prepare("SELECT `fan_id` FROM `" . $this->_oConfig->CNF['TABLE_ADMINS'] . "` WHERE `group_profile_id` = ?", $iGroupProfileId);
+        return $this->getColumn($sQuery);
+    }
 }
 
 /** @} */

+ 1 - 1
modules/boonex/groups/classes/BxGroupsFormsEntryHelper.php

@@ -53,7 +53,7 @@ class BxGroupsFormsEntryHelper extends BxBaseModProfileFormsEntryHelper
         $oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_oModule->_oConfig->getName());
 
         if ($oGroupProfile)
-            $this->_oModule->_oDb->deleteAdmins($oGroupProfile->id());
+            $this->_oModule->_oDb->deleteAdminsByGroupId($oGroupProfile->id());
 
         if (isset($CNF['OBJECT_CONNECTIONS']) && $oGroupProfile && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
             $oConnection->onDeleteInitiatorAndContent($oGroupProfile->id());

+ 51 - 0
modules/boonex/groups/classes/BxGroupsModule.php

@@ -37,6 +37,57 @@ class BxGroupsModule extends BxBaseModProfileModule
         return false;
     }
 
+    /**
+     * Delete profile from fans and admins tables
+     * @param $iProfileId profile id 
+     */
+    public function serviceDeleteProfileFromFansAndAdmins ($iProfileId)
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $this->_oDb->deleteAdminsByProfileId($iProfileId);
+
+        if (isset($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])))
+            $oConnection->onDeleteInitiatorAndContent($iProfileId);
+    }
+
+    /**
+     * Reset group's author for particular group
+     * @param $iContentId group id 
+     * @return false of error, or number of updated records on success
+     */
+    public function serviceReassignEntityAuthor ($iContentId)
+    {
+        $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
+        if (!$aContentInfo)
+            return false;
+
+        if (!($oGroupProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->getName())))
+            return false;
+
+        $aAdmins = $this->_oDb->getAdmins($oGroupProfile->id());
+
+        return $this->_oDb->updateAuthorById($iContentId, $aAdmins ? array_pop($aAdmins) : 0);
+    }
+
+    /**
+     * Reset group's author when author profile is deleted
+     * @param $iProfileId profile id 
+     * @return number of changed items
+     */
+    public function serviceReassignEntitiesByAuthor ($iProfileId)
+    {
+        $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
+        if (!$a)
+            return 0;
+
+        $iCount = 0;
+        foreach ($a as $aContentInfo)
+            $iCount += ('' == $this->serviceReassignEntityAuthor($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
+
+        return $iCount;
+    }
+
     public function servicePrepareFields ($aFieldsProfile)
     {
         $aFieldsProfile['group_name'] = $aFieldsProfile['name'];

+ 2 - 1
modules/boonex/groups/install/sql/enable.sql

@@ -363,7 +363,8 @@ INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALU
 SET @iHandler := LAST_INSERT_ID();
 
 INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
-('bx_groups_fans', 'connection_added', @iHandler);
+('bx_groups_fans', 'connection_added', @iHandler),
+('profile', 'delete', @iHandler);
 
 -- PRIVACY