From 9b1ccf5c634c5bdb93f2e9600686ea2d1d2b5117 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Thu, 2 Aug 2018 12:55:17 +0200 Subject: [PATCH] Add reaction copy action See #2508 --- .../files/lib/data/like/LikeAction.class.php | 82 +------------- .../data/reaction/ReactionAction.class.php | 105 ++++++++++++++++++ 2 files changed, 109 insertions(+), 78 deletions(-) diff --git a/wcfsetup/install/files/lib/data/like/LikeAction.class.php b/wcfsetup/install/files/lib/data/like/LikeAction.class.php index 35be5e5a59..40f1d974f0 100644 --- a/wcfsetup/install/files/lib/data/like/LikeAction.class.php +++ b/wcfsetup/install/files/lib/data/like/LikeAction.class.php @@ -1,9 +1,6 @@ * @package WoltLabSuite\Core\Data\Like + * @deprecated since 3.2, use \wcf\data\reaction\ReactionAction instead * * @method Like create() * @method LikeEditor[] getObjects() @@ -335,79 +333,7 @@ class LikeAction extends AbstractDatabaseObjectAction implements IGroupedUserLis * Copies likes from one object id to another. */ public function copy() { - $sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.like.likeableObject', $this->parameters['sourceObjectType']); - $targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.like.likeableObject', $this->parameters['targetObjectType']); - - // - // step 1) get data - // - - // get like object - $sql = "SELECT * - FROM wcf".WCF_N."_like_object - WHERE objectTypeID = ? - AND objectID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([ - $sourceObjectType->objectTypeID, - $this->parameters['sourceObjectID'] - ]); - $row = $statement->fetchArray(); - - // no (dis-)likes at all - if ($row === false) { - return; - } - - unset($row['likeObjectID']); - $row['objectTypeID'] = $targetObjectType->objectTypeID; - $row['objectID'] = $this->parameters['targetObjectID']; - $newLikeObject = LikeObjectEditor::create($row); - - // - // step 2) copy - // - - $sql = "INSERT INTO wcf".WCF_N."_like - (objectID, objectTypeID, objectUserID, userID, time, likeValue) - SELECT ".$this->parameters['targetObjectID'].", ".$targetObjectType->objectTypeID.", objectUserID, userID, time, likeValue - FROM wcf".WCF_N."_like - WHERE objectTypeID = ? - AND objectID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([ - $sourceObjectType->objectTypeID, - $this->parameters['sourceObjectID'] - ]); - - // - // step 3) update owner - // - - if ($newLikeObject->objectUserID) { - $sql = "SELECT COUNT(*) - FROM wcf".WCF_N."_like - WHERE objectTypeID = ? - AND objectID = ? - AND likeValue = ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([ - $targetObjectType->objectTypeID, - $this->parameters['targetObjectID'], - Like::LIKE - ]); - $count = $statement->fetchSingleColumn(); - - if ($count) { - // update received likes - $userEditor = new UserEditor(new User($newLikeObject->objectUserID)); - $userEditor->updateCounters([ - 'likesReceived' => $count - ]); - - // add activity points - UserActivityPointHandler::getInstance()->fireEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', [$newLikeObject->objectUserID => $count]); - } - } + $reactionAction = new ReactionAction([], 'copy', $this->getParameters()); + $reactionAction->executeAction(); } } diff --git a/wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php b/wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php index 57b68115db..ede79405db 100644 --- a/wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php +++ b/wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php @@ -2,14 +2,19 @@ namespace wcf\data\reaction; use wcf\data\like\IRestrictedLikeObjectTypeProvider; use wcf\data\like\LikeEditor; +use wcf\data\like\object\LikeObjectEditor; use wcf\data\like\ViewableLikeList; +use wcf\data\object\type\ObjectTypeCache; use wcf\data\reaction\type\ReactionType; use wcf\data\reaction\type\ReactionTypeCache; use wcf\data\AbstractDatabaseObjectAction; +use wcf\data\user\User; +use wcf\data\user\UserEditor; use wcf\system\exception\IllegalLinkException; use wcf\system\exception\PermissionDeniedException; use wcf\system\exception\UserInputException; use wcf\system\reaction\ReactionHandler; +use wcf\system\user\activity\point\UserActivityPointHandler; use wcf\system\WCF; /** @@ -235,4 +240,104 @@ class ReactionAction extends AbstractDatabaseObjectAction { 'template' => WCF::getTPL()->fetch('userProfileLikeItem') ]; } + + /** + * Copies likes from one object id to another. + */ + public function copy() { + $sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.like.likeableObject', $this->parameters['sourceObjectType']); + $targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.like.likeableObject', $this->parameters['targetObjectType']); + + // + // step 1) get data + // + + // get like object + $sql = "SELECT * + FROM wcf".WCF_N."_like_object + WHERE objectTypeID = ? + AND objectID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + $sourceObjectType->objectTypeID, + $this->parameters['sourceObjectID'] + ]); + $row = $statement->fetchArray(); + + // no (dis-)likes at all + if ($row === false) { + return; + } + + unset($row['likeObjectID']); + $row['objectTypeID'] = $targetObjectType->objectTypeID; + $row['objectID'] = $this->parameters['targetObjectID']; + $newLikeObject = LikeObjectEditor::create($row); + + // + // step 2) copy + // + + $sql = "INSERT INTO wcf".WCF_N."_like + (objectID, objectTypeID, objectUserID, userID, time, likeValue, reactionTypeID) + SELECT ".$this->parameters['targetObjectID'].", ".$targetObjectType->objectTypeID.", objectUserID, userID, time, likeValue, reactionTypeID + FROM wcf".WCF_N."_like + WHERE objectTypeID = ? + AND objectID = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + $sourceObjectType->objectTypeID, + $this->parameters['sourceObjectID'] + ]); + + // + // step 3) update owner + // + + if ($newLikeObject->objectUserID) { + $sql = "SELECT COUNT(*) as count, like_table.reactionTypeID + FROM wcf".WCF_N."_like like_table + WHERE like_table.objectTypeID = ? + AND like_table.objectID = ? + GROUP BY like_table.reactionTypeID"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute([ + $targetObjectType->objectTypeID, + $this->parameters['targetObjectID'] + ]); + + $updateValues = [ + 'positive' => 0, + 'neutral' => 0, + 'negative' => 0 + ]; + while ($row = $statement->fetchArray()) { + $reactionType = ReactionTypeCache::getInstance()->getReactionTypeByID($row['reactionTypeID']); + + if (!$reactionType->isDisabled) { + if ($reactionType->isPositive()) { + $updateValues['positive'] += $row['count']; + } + else if ($reactionType->isNegative()) { + $updateValues['negative'] += $row['count']; + } + else if ($reactionType->isNeutral()) { + $updateValues['neutral'] += $row['count']; + } + } + } + + // update received likes + $userEditor = new UserEditor(new User($newLikeObject->objectUserID)); + $userEditor->updateCounters([ + 'likesReceived' => $updateValues['positive'], + 'positiveReactionsReceived' => $updateValues['positive'], + 'negativeReactionsReceived' => $updateValues['negative'], + 'neutralReactionsReceived' => $updateValues['neutral'] + ]); + + // add activity points + UserActivityPointHandler::getInstance()->fireEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', [$newLikeObject->objectUserID => $updateValues['positive']]); + } + } } -- 2.20.1