Clean up code
authorJoshua Rüsweg <josh@bastelstu.be>
Sat, 29 Jul 2017 17:34:03 +0000 (19:34 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Sat, 29 Jul 2017 17:34:03 +0000 (19:34 +0200)
See #2315

wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php
wcfsetup/install/files/lib/data/trophy/TrophyAction.class.php
wcfsetup/install/files/lib/data/user/UserProfile.class.php
wcfsetup/install/files/lib/data/user/UserProfileAction.class.php
wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php

index b6bf4e2277be16293b7a5c490fede39791d09524..fd8f0bece7b8af340154c700a6bf6f541ed3da9b 100644 (file)
@@ -187,7 +187,10 @@ class TrophyEditForm extends TrophyAddForm {
                
                // reset special trophies, if trophy is disabled 
                if ($this->isDisabled) {
-                       WCF::getDB()->prepareStatement("DELETE FROM wcf". WCF_N ."_user_special_trophy WHERE trophyID = ?")->execute([$this->trophyID]);
+                       $sql = "DELETE FROM wcf". WCF_N ."_user_special_trophy WHERE trophyID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->trophyID]); 
+                       
                        UserStorageHandler::getInstance()->resetAll('specialTrophies');
                }
                
index be7ab5ed4eda120fb1ce3c0827bd403024bf232f..104a4aab614ba29f4661ce5ceaa5043710eb87bc 100644 (file)
@@ -77,11 +77,14 @@ class TrophyAction extends AbstractDatabaseObjectAction implements IToggleAction
         * @inheritDoc
         */
        public function toggle() {
+               $sql = "DELETE FROM wcf". WCF_N ."_user_special_trophy WHERE trophyID = ?";
+               $deleteStatement = WCF::getDB()->prepareStatement($sql);
+               
                foreach ($this->getObjects() as $trophy) {
                        $trophy->update(['isDisabled' => $trophy->isDisabled ? 0 : 1]);
                        
                        if (!$trophy->isDisabled) {
-                               WCF::getDB()->prepareStatement("DELETE FROM wcf". WCF_N ."_user_special_trophy WHERE trophyID = ?")->execute([$trophy->trophyID]);
+                               $deleteStatement->execute([$trophy->trophyID]);
                        }
                }
                
index 497ec97487ad176c7be3582c99cf3c5836dfe478..61db5c41cf1624a345d4f82887a79d6946cbc103 100644 (file)
@@ -328,15 +328,11 @@ class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject {
                $specialTrophies = UserStorageHandler::getInstance()->getField('specialTrophies', $this->userID);
                
                if ($specialTrophies === null) {
-                       // load special trophies for the user 
-                       $specialTrophies = [];
-                       
-                       $statement = WCF::getDB()->prepareStatement("SELECT trophyID FROM wcf".WCF_N."_user_special_trophy WHERE userID = ?");
-                       $statement->execute([$this->userID]); 
-                       
-                       while ($trophyID = $statement->fetchColumn()) {
-                               $specialTrophies[] = $trophyID; 
-                       }
+                       // load special trophies for the user
+                       $sql = "SELECT trophyID FROM wcf".WCF_N."_user_special_trophy WHERE userID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->userID]);
+                       $specialTrophies = $statement->fetchAll(\PDO::FETCH_COLUMN);
                        
                        UserStorageHandler::getInstance()->update($this->userID, 'specialTrophies', serialize($specialTrophies));
                }
@@ -356,8 +352,10 @@ class UserProfile extends DatabaseObjectDecorator implements ITitledLinkObject {
                        $conditionBuilder->add('userID = ?', [$this->userID]);
                        $conditionBuilder->add('trophyID IN (?)', [$trophyDeleteIDs]);
                        
-                       // reset some special trophies 
-                       WCF::getDB()->prepareStatement("DELETE FROM wcf".WCF_N."_user_special_trophy ".$conditionBuilder)->execute($conditionBuilder->getParameters());
+                       // reset the user special trophies 
+                       $sql = "DELETE FROM wcf".WCF_N."_user_special_trophy ".$conditionBuilder; 
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
                        
                        UserStorageHandler::getInstance()->update($this->userID, 'specialTrophies', serialize($specialTrophies));
                }
index ee680ab97745aed1104a33352e3e0521afea0e54..7cc0915c052dcede51acbbcf468ae4208582a01a 100644 (file)
@@ -431,16 +431,21 @@ class UserProfileAction extends UserAction {
                        $this->readObjects();
                }
                
+               $sql = "DELETE FROM wcf".WCF_N."_user_special_trophy WHERE userID = ?";
+               $deleteStatement = WCF::getDB()->prepareStatement($sql);
+               
+               $sql = "INSERT INTO wcf".WCF_N."_user_special_trophy (userID, trophyID) VALUES (?, ?)";
+               $insertStatement = WCF::getDB()->prepareStatement($sql);
+               
                foreach ($this->getObjects() as $user) {
                        WCF::getDB()->beginTransaction();
                        
-                       WCF::getDB()->prepareStatement("DELETE FROM wcf".WCF_N."_user_special_trophy WHERE userID = ?")->execute([$user->userID]);
+                       // delete all user special trophies for the user
+                       $deleteStatement->execute([$user->userID]);
                        
                        if (!empty($this->parameters['trophyIDs'])) {
-                               $statement = WCF::getDB()->prepareStatement("INSERT INTO wcf".WCF_N."_user_special_trophy (userID, trophyID) VALUES (?, ?)");
-                               
                                foreach ($this->parameters['trophyIDs'] as $trophyID) {
-                                       $statement->execute([
+                                       $insertStatement->execute([
                                                $user->userID, 
                                                $trophyID
                                        ]);
index f4dcf49c6cfdacd2b39a5c0444e45742e7e42946..caf7bb46abc560b302ad6dc4f106c66b40e6b20d 100644 (file)
@@ -94,7 +94,10 @@ class UserTrophyAction extends AbstractDatabaseObjectAction {
                        $conditionBuilder = new PreparedStatementConditionBuilder(); 
                        $conditionBuilder->add('trophyID NOT IN (?)', [array_unique($userTrophyIDs)]); 
                        $conditionBuilder->add('userID = ?', [$userID]);
-                       WCF::getDB()->prepareStatement("DELETE FROM wcf". WCF_N ."_user_special_trophy ". $conditionBuilder)->execute($conditionBuilder->getParameters());
+                       
+                       $sql = "DELETE FROM wcf". WCF_N ."_user_special_trophy ". $conditionBuilder;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
                        
                        UserStorageHandler::getInstance()->reset([$userID], 'specialTrophies');
                }