示例#1
0
 /**
  * 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\t*\n\t\t\tFROM\twcf" . WCF_N . "_like_object\n\t\t\tWHERE\tobjectTypeID = ?\n\t\t\t\tAND objectID = ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array($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\twcf" . WCF_N . "_like\n\t\t\t\t\t(objectID, objectTypeID, objectUserID, userID, time, likeValue)\n\t\t\tSELECT\t\t" . $this->parameters['targetObjectID'] . ", " . $targetObjectType->objectTypeID . ", objectUserID, userID, time, likeValue\n\t\t\tFROM\t\twcf" . WCF_N . "_like\n\t\t\tWHERE\t\tobjectTypeID = ?\n\t\t\t\t\tAND objectID = ?";
     $statement = WCF::getDB()->prepareStatement($sql);
     $statement->execute(array($sourceObjectType->objectTypeID, $this->parameters['sourceObjectID']));
     //
     // step 3) update owner
     //
     if ($newLikeObject->objectUserID) {
         $sql = "SELECT\tCOUNT(*) AS count\n\t\t\t\tFROM\twcf" . WCF_N . "_like\n\t\t\t\tWHERE\tobjectTypeID = ?\n\t\t\t\t\tAND objectID = ?\n\t\t\t\t\tAND likeValue = ?";
         $statement = WCF::getDB()->prepareStatement($sql);
         $statement->execute(array($targetObjectType->objectTypeID, $this->parameters['targetObjectID'], Like::LIKE));
         $row = $statement->fetchArray();
         if ($row['count']) {
             // update received likes
             $userEditor = new UserEditor(new User($newLikeObject->objectUserID));
             $userEditor->updateCounters(array('likesReceived' => $row['count']));
             // add activity points
             UserActivityPointHandler::getInstance()->fireEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', array($newLikeObject->objectUserID => $row['count']));
         }
     }
 }
示例#2
0
 /**
  * Removes all likes for given objects.
  * 
  * @param	string			$objectType
  * @param	array<integer>		$objectIDs
  * @param	array<string>		$notificationObjectTypes
  */
 public function removeLikes($objectType, array $objectIDs, array $notificationObjectTypes = array())
 {
     $objectTypeObj = $this->getObjectType($objectType);
     // get like objects
     $likeObjectList = new LikeObjectList();
     $likeObjectList->getConditionBuilder()->add('like_object.objectTypeID = ?', array($objectTypeObj->objectTypeID));
     $likeObjectList->getConditionBuilder()->add('like_object.objectID IN (?)', array($objectIDs));
     $likeObjectList->readObjects();
     $likeObjects = $likeObjectList->getObjects();
     $likeObjectIDs = $likeObjectList->getObjectIDs();
     // reduce count of received users
     $users = array();
     foreach ($likeObjects as $likeObject) {
         if ($likeObject->likes) {
             if (!isset($users[$likeObject->objectUserID])) {
                 $users[$likeObject->objectUserID] = 0;
             }
             $users[$likeObject->objectUserID] += $likeObject->likes;
         }
     }
     foreach ($users as $userID => $receivedLikes) {
         $userEditor = new UserEditor(new User(null, array('userID' => $userID)));
         $userEditor->updateCounters(array('likesReceived' => $receivedLikes * -1));
     }
     // get like ids
     $likeList = new LikeList();
     $likeList->getConditionBuilder()->add('like_table.objectTypeID = ?', array($objectTypeObj->objectTypeID));
     $likeList->getConditionBuilder()->add('like_table.objectID IN (?)', array($objectIDs));
     $likeList->readObjects();
     if (count($likeList)) {
         $likeData = array();
         foreach ($likeList as $like) {
             $likeData[$like->likeID] = $like->userID;
         }
         // delete like notifications
         if (!empty($notificationObjectTypes)) {
             foreach ($notificationObjectTypes as $notificationObjectType) {
                 UserNotificationHandler::getInstance()->removeNotifications($notificationObjectType, $likeList->getObjectIDs());
             }
         } else {
             if (UserNotificationHandler::getInstance()->getObjectTypeID($objectType . '.notification')) {
                 UserNotificationHandler::getInstance()->removeNotifications($objectType . '.notification', $likeList->getObjectIDs());
             }
         }
         // revoke activity points
         UserActivityPointHandler::getInstance()->removeEvents('com.woltlab.wcf.like.activityPointEvent.receivedLikes', $likeData);
         // delete likes
         LikeEditor::deleteAll(array_keys($likeData));
     }
     // delete like objects
     if (!empty($likeObjectIDs)) {
         LikeObjectEditor::deleteAll($likeObjectIDs);
     }
     // delete activity events
     if (UserActivityEventHandler::getInstance()->getObjectTypeID($objectTypeObj->objectType . '.recentActivityEvent')) {
         UserActivityEventHandler::getInstance()->removeEvents($objectTypeObj->objectType . '.recentActivityEvent', $objectIDs);
     }
 }