Przeglądaj źródła

Ticket #811 - Done for Views, Votes, Reports, Favorites.

AntonLV 9 lat temu
rodzic
commit
8ec813f340

+ 9 - 0
inc/classes/BxDolFavorite.php

@@ -111,6 +111,15 @@ class BxDolFavorite extends BxDolObject
         return $GLOBALS['bx_dol_favorite_systems'];
     }
 
+    public static function onAuthorDelete ($iAuthorId)
+    {
+        $aSystems = self::getSystems();
+        foreach($aSystems as $sSystem => $aSystem)
+            self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
+
+        return true;
+    }
+
 	/**
      * Actions functions
      */

+ 8 - 0
inc/classes/BxDolObject.php

@@ -182,6 +182,14 @@ class BxDolObject extends BxDolFactory implements iBxDolReplaceable
         return true;
     }
 
+    /**
+     * Database functions
+     */
+    public function getQueryObject ()
+    {
+        return $this->_oQuery;
+    }
+
 	/**
      * Permissions functions
      */

+ 53 - 4
inc/classes/BxDolObjectQuery.php

@@ -15,7 +15,10 @@ class BxDolObjectQuery extends BxDolDb
     protected $_oModule;
 
     protected $_sTable;
+
     protected $_sTableTrack;
+    protected $_sTableTrackFieldAuthor;
+
     protected $_sTriggerTable;
     protected $_sTriggerFieldId;
     protected $_sTriggerFieldAuthor;
@@ -31,7 +34,10 @@ class BxDolObjectQuery extends BxDolDb
 
         $aSystem = $this->_oModule->getSystemInfo();
         $this->_sTable = isset($aSystem['table_main']) ? $aSystem['table_main'] : '';
+
         $this->_sTableTrack = isset($aSystem['table_track']) ? $aSystem['table_track'] : '';
+        $this->_sTableTrackFieldAuthor = 'author_id';
+        
         $this->_sTriggerTable = isset($aSystem['trigger_table']) ? $aSystem['trigger_table'] : '';
         $this->_sTriggerFieldId = isset($aSystem['trigger_field_id']) ? $aSystem['trigger_field_id'] : '';
         $this->_sTriggerFieldAuthor = isset($aSystem['trigger_field_author']) ? $aSystem['trigger_field_author'] : '';
@@ -82,17 +88,44 @@ class BxDolObjectQuery extends BxDolDb
     public function deleteObjectEntries($iObjectId)
     {
     	if(!empty($this->_sTable)) {
-	        $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `object_id` = ?", $iObjectId);
+	        $sQuery = $this->prepare("DELETE FROM `{$this->_sTable}` WHERE `object_id` = ?", $iObjectId);
 	        if($this->query($sQuery))
-	        	$this->query("OPTIMIZE TABLE {$this->_sTable}");
+	        	$this->query("OPTIMIZE TABLE `{$this->_sTable}`");
     	}
 
     	if(!empty($this->_sTableTrack)) {
-	        $sQuery = $this->prepare("DELETE FROM {$this->_sTableTrack} WHERE `object_id` = ?", $iObjectId);
+	        $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `object_id` = ?", $iObjectId);
 	        if($this->query($sQuery))
-	        	$this->query ("OPTIMIZE TABLE {$this->_sTableTrack}");
+	        	$this->query ("OPTIMIZE TABLE `{$this->_sTableTrack}`");
     	}
     }
+    public function deleteAuthorEntries($iAuthorId)
+    {
+        if(empty($this->_sTableTrack))
+            return;
+
+        $bTable = !empty($this->_sTable);
+        $bTableTrigger = !empty($this->_sTriggerTable) && !empty($this->_sTriggerFieldCount);
+
+        if($bTable || $bTableTrigger) {
+            $aTracks = $this->getAll("SELECT * FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=:author_id", array('author_id' => $iAuthorId));
+            foreach($aTracks as $aTrack) {
+                if($bTable)
+                    $this->_deleteAuthorEntriesTableMain($aTrack);
+    
+                /**
+                 * Note. It's essential that Trigger Table is updated at the end, 
+                 * because it may require updated data from main ($this->_sTable) table. 
+                 */
+                if($bTableTrigger)
+                    $this->_deleteAuthorEntriesTableTrigger($aTrack);
+            }
+        }
+
+        $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE `{$this->_sTableTrackFieldAuthor}`=?", $iAuthorId);
+        if($this->query($sQuery))
+        	$this->query("OPTIMIZE TABLE `{$this->_sTableTrack}`");
+    }
 
 	public function getObjectAuthorId($iId)
     {
@@ -109,6 +142,12 @@ class BxDolObjectQuery extends BxDolDb
         return (int)$this->getOne($sQuery);
     }
 
+    public function updateMainTableValue($iObjectId, $iValue)
+    {
+        $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `count` = `count` + ? WHERE `object_id` = ?", (int)$iValue, $iObjectId);
+        return (int)$this->query($sQuery) > 0;
+    }
+
     public function updateTriggerTable($iObjectId)
     {
     	if(empty($this->_sMethodGetEntry))
@@ -132,6 +171,16 @@ class BxDolObjectQuery extends BxDolDb
     	$sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $iObjectId);
         return (int)$this->query($sQuery) > 0;
     }
+
+    protected function _deleteAuthorEntriesTableMain($aTrack)
+    {
+        return $this->updateMainTableValue($aTrack['object_id'], -1);
+    }
+
+    protected function _deleteAuthorEntriesTableTrigger($aTrack)
+    {
+        return $this->updateTriggerTableValue($aTrack['object_id'], -1);
+    }
 }
 
 /** @} */

+ 11 - 2
inc/classes/BxDolProfile.php

@@ -358,10 +358,19 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         if ($isStopDeletion)
             return false;
 
-        // delete associated comments
-        if($bDeleteWithContent)
+        // delete associated content
+        if($bDeleteWithContent) {
 	        BxDolCmts::onAuthorDelete($ID);
 
+	        BxDolReport::onAuthorDelete($ID);
+
+	        BxDolVote::onAuthorDelete($ID);
+
+	        BxDolFavorite::onAuthorDelete($ID);
+
+	        BxDolView::onAuthorDelete($ID);
+        }
+
         // delete connections
         $oConn = BxDolConnection::getObjectInstance('sys_profiles_friends');
         $oConn->onDeleteInitiatorAndContent($ID);

+ 9 - 0
inc/classes/BxDolReport.php

@@ -149,6 +149,15 @@ class BxDolReport extends BxDolObject
         return $GLOBALS['bx_dol_report_systems'];
     }
 
+    public static function onAuthorDelete ($iAuthorId)
+    {
+        $aSystems = self::getSystems();
+        foreach($aSystems as $sSystem => $aSystem)
+            self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
+
+        return true;
+    }
+
     /**
      * Interface functions for outer usage
      */

+ 9 - 0
inc/classes/BxDolView.php

@@ -107,6 +107,15 @@ class BxDolView extends BxDolObject
         return $GLOBALS['bx_dol_view_systems'];
     }
 
+    public static function onAuthorDelete ($iAuthorId)
+    {
+        $aSystems = self::getSystems();
+        foreach($aSystems as $sSystem => $aSystem)
+            self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
+
+        return true;
+    }
+
     public function actionGetViewedBy()
     {
         if (!$this->isEnabled())

+ 2 - 0
inc/classes/BxDolViewQuery.php

@@ -18,6 +18,8 @@ class BxDolViewQuery extends BxDolObjectQuery
     {
         parent::__construct($oModule);
 
+        $this->_sTableTrackFieldAuthor = 'viewer_id';
+
         $aSystem = $this->_oModule->getSystemInfo();
         $this->_iPeriod = (int)$aSystem['period'];
     }

+ 9 - 0
inc/classes/BxDolVote.php

@@ -146,6 +146,15 @@ class BxDolVote extends BxDolObject
         return $GLOBALS['bx_dol_vote_systems'];
     }
 
+    public static function onAuthorDelete ($iAuthorId)
+    {
+        $aSystems = self::getSystems();
+        foreach($aSystems as $sSystem => $aSystem)
+            self::getObjectInstance($sSystem, 0)->getQueryObject()->deleteAuthorEntries($iAuthorId);
+
+        return true;
+    }
+
     public function isUndo()
     {
         return (int)$this->_aSystem['is_undo'] == 1;

+ 15 - 0
inc/classes/BxDolVoteQuery.php

@@ -106,6 +106,21 @@ class BxDolVoteQuery extends BxDolObjectQuery
     	$sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = ?, `{$this->_sTriggerFieldRate}` = ? WHERE `{$this->_sTriggerFieldId}` = ?", $aEntry['count'], $aEntry['rate'], $iObjectId);
         return (int)$this->query($sQuery) > 0;
     }
+
+    protected function _deleteAuthorEntriesTableMain($aTrack)
+    {
+        return $this->query("UPDATE `{$this->_sTable}` SET `count`=`count`-1, `sum`=`sum`-:value WHERE `object_id`=:object_id", array(
+        	'object_id' => $aTrack['object_id'],
+            'value' => $aTrack['value']
+        ));
+    }
+
+    protected function _deleteAuthorEntriesTableTrigger($aTrack)
+    {
+        $aVote = $this->getVote($aTrack['object_id']);
+
+        return $this->_updateTriggerTable($aTrack['object_id'], $aVote);
+    }
 }
 
 /** @} */