Return objects with correct counters in UserNotificationAction::createStackable()
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 31 Aug 2017 13:59:20 +0000 (15:59 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 31 Aug 2017 13:59:20 +0000 (15:59 +0200)
wcfsetup/install/files/lib/data/user/notification/UserNotificationAction.class.php

index 373a0eba0151b0f25fe70e0f222be9e7200cbce9..b94d43489311058a9441781497125d5ad5557b8e 100644 (file)
@@ -131,28 +131,26 @@ class UserNotificationAction extends AbstractDatabaseObjectAction {
                $sql = "INSERT IGNORE INTO      wcf".WCF_N."_user_notification_author
                                                (notificationID, authorID, time)
                        VALUES                  (?, ?, ?)";
-               $statement = WCF::getDB()->prepareStatement($sql);
-               
-               WCF::getDB()->beginTransaction();
-               foreach ($notifications as $notificationData) {
-                       $statement->execute([
-                               $notificationData['object']->notificationID,
-                               $this->parameters['authorID'] ?: null,
-                               TIME_NOW
-                       ]);
-               }
-               WCF::getDB()->commitTransaction();
+               $authorStatement = WCF::getDB()->prepareStatement($sql);
                
                // update trigger count
                $sql = "UPDATE  wcf".WCF_N."_user_notification
                        SET     timesTriggered = timesTriggered + ?,
                                guestTimesTriggered = guestTimesTriggered + ?
                        WHERE   notificationID = ?";
-               $statement = WCF::getDB()->prepareStatement($sql);
-               
+               $triggerStatement = WCF::getDB()->prepareStatement($sql);
+       
                WCF::getDB()->beginTransaction();
+               $notificationIDs = [];
                foreach ($notifications as $notificationData) {
-                       $statement->execute([
+                       $notificationIDs[] = $notificationData['object']->notificationID;
+                       
+                       $authorStatement->execute([
+                               $notificationData['object']->notificationID,
+                               $this->parameters['authorID'] ?: null,
+                               TIME_NOW
+                       ]);
+                       $triggerStatement->execute([
                                1,
                                $this->parameters['authorID'] ? 0 : 1,
                                $notificationData['object']->notificationID
@@ -160,6 +158,16 @@ class UserNotificationAction extends AbstractDatabaseObjectAction {
                }
                WCF::getDB()->commitTransaction();
                
+               $notificationList = new UserNotificationList();
+               $notificationList->setObjectIDs($notificationIDs);
+               $notificationList->readObjects();
+               $updatedNotifications = $notificationList->getObjects();
+               
+               $notifications = array_map(function ($notificationData) use ($updatedNotifications) {
+                       $notificationData['object'] = $updatedNotifications[$notificationData['object']->notificationID];
+                       return $notificationData;
+               }, $notifications);
+               
                return $notifications;
        }