From c24d1c520ed4abcaa542f797f1f5dd850f521c82 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Sat, 29 Jul 2017 19:34:03 +0200 Subject: [PATCH] Clean up code See #2315 --- .../lib/acp/form/TrophyEditForm.class.php | 5 ++++- .../lib/data/trophy/TrophyAction.class.php | 5 ++++- .../files/lib/data/user/UserProfile.class.php | 20 +++++++++---------- .../lib/data/user/UserProfileAction.class.php | 13 ++++++++---- .../user/trophy/UserTrophyAction.class.php | 5 ++++- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php b/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php index b6bf4e2277..fd8f0bece7 100644 --- a/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TrophyEditForm.class.php @@ -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'); } diff --git a/wcfsetup/install/files/lib/data/trophy/TrophyAction.class.php b/wcfsetup/install/files/lib/data/trophy/TrophyAction.class.php index be7ab5ed4e..104a4aab61 100644 --- a/wcfsetup/install/files/lib/data/trophy/TrophyAction.class.php +++ b/wcfsetup/install/files/lib/data/trophy/TrophyAction.class.php @@ -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]); } } diff --git a/wcfsetup/install/files/lib/data/user/UserProfile.class.php b/wcfsetup/install/files/lib/data/user/UserProfile.class.php index 497ec97487..61db5c41cf 100644 --- a/wcfsetup/install/files/lib/data/user/UserProfile.class.php +++ b/wcfsetup/install/files/lib/data/user/UserProfile.class.php @@ -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)); } diff --git a/wcfsetup/install/files/lib/data/user/UserProfileAction.class.php b/wcfsetup/install/files/lib/data/user/UserProfileAction.class.php index ee680ab977..7cc0915c05 100644 --- a/wcfsetup/install/files/lib/data/user/UserProfileAction.class.php +++ b/wcfsetup/install/files/lib/data/user/UserProfileAction.class.php @@ -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 ]); diff --git a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php index f4dcf49c6c..caf7bb46ab 100644 --- a/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php +++ b/wcfsetup/install/files/lib/data/user/trophy/UserTrophyAction.class.php @@ -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'); } -- 2.20.1