Option to filter notifications by content language settings
authorMarcel Werk <burntime@woltlab.com>
Tue, 29 Mar 2022 14:38:40 +0000 (16:38 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 29 Mar 2022 14:38:40 +0000 (16:38 +0200)
wcfsetup/install/files/lib/system/user/notification/UserNotificationHandler.class.php

index 9213f606bbd823b808f4c249fde65b652b859f62..dd25c418d665c814b0452f1aef45eb05b2893221 100644 (file)
@@ -90,6 +90,7 @@ class UserNotificationHandler extends SingletonFactory
      * @param int[] $recipientIDs
      * @param mixed[] $additionalData
      * @param int $baseObjectID
+     * @param int $contentLanguageID
      * @throws  SystemException
      */
     public function fireEvent(
@@ -98,7 +99,8 @@ class UserNotificationHandler extends SingletonFactory
         IUserNotificationObject $notificationObject,
         array $recipientIDs,
         array $additionalData = [],
-        $baseObjectID = 0
+        $baseObjectID = 0,
+        $contentLanguageID = 0
     ) {
         // check given object type and event name
         if (!isset($this->availableEvents[$objectType][$eventName])) {
@@ -238,6 +240,14 @@ class UserNotificationHandler extends SingletonFactory
             }
         }
 
+        // Remove recipients who do not have the content language enabled.
+        if ($contentLanguageID) {
+            $recipientIDs = $this->filterUsersByContentLanguage($recipientIDs, $contentLanguageID);
+            if (empty($recipientIDs)) {
+                return;
+            }
+        }
+
         // get recipients
         $recipientList = new UserNotificationEventRecipientList();
         $recipientList->getConditionBuilder()->add('event_to_user.eventID = ?', [$event->eventID]);
@@ -1066,4 +1076,23 @@ class UserNotificationHandler extends SingletonFactory
 
         return [];
     }
+
+    public function filterUsersByContentLanguage(array $userIDs, int $contentLanguageID): array
+    {
+        $conditions = new PreparedStatementConditionBuilder();
+        $conditions->add("userID IN (?)", [$userIDs]);
+        $conditions->add("languageID = ?", [$contentLanguageID]);
+
+        $sql = "SELECT  userID
+                FROM    wcf" . WCF_N . "_user_to_language
+                " . $conditions;
+        $statement = WCF::getDB()->prepareStatement($sql);
+        $statement->execute($conditions->getParameters());
+        $filterUserIDs = [];
+        while ($userID = $statement->fetchColumn()) {
+            $filterUserIDs[] = $userID;
+        }
+
+        return \array_intersect($userIDs, $filterUserIDs);
+    }
 }