<?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;
* @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()
* 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();
}
}
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;
/**
'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']]);
+ }
+ }
}