Add reaction copy action
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 2 Aug 2018 10:55:17 +0000 (12:55 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 2 Aug 2018 10:55:17 +0000 (12:55 +0200)
See #2508

wcfsetup/install/files/lib/data/like/LikeAction.class.php
wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php

index 35be5e5a592606526836a02ffdc343662f3ccc97..40f1d974f071e0105baad64aab61f922f42ec1a4 100644 (file)
@@ -1,9 +1,6 @@
 <?php
 namespace wcf\data\like;
-use wcf\data\like\object\LikeObjectEditor;
-use wcf\data\object\type\ObjectTypeCache;
-use wcf\data\user\User;
-use wcf\data\user\UserEditor;
+use wcf\data\reaction\ReactionAction;
 use wcf\data\AbstractDatabaseObjectAction;
 use wcf\data\IGroupedUserListAction;
 use wcf\system\exception\PermissionDeniedException;
@@ -21,6 +18,7 @@ use wcf\system\WCF;
  * @copyright  2001-2018 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @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();
        }
 }
index 57b68115dbb086a89bc732b93d790ac1b8cf1bb1..ede79405db0162df7093fdd685442c13b8da2382 100644 (file)
@@ -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']]);
+               }
+       }
 }