Skip to content

Commit 7cc279e

Browse files
committed
Move static function from ConversationAddForm into the new trait TConversationForm
1 parent 4e9b857 commit 7cc279e

3 files changed

Lines changed: 157 additions & 134 deletions

File tree

files/lib/action/AddParticipantConversationDialogAction.class.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
use Psr\Http\Server\RequestHandlerInterface;
1010
use wcf\data\conversation\Conversation;
1111
use wcf\data\user\group\UserGroup;
12-
use wcf\form\ConversationAddForm;
1312
use wcf\http\Helper;
1413
use wcf\system\cache\builder\UserGroupCacheBuilder;
1514
use wcf\system\conversation\command\AddParticipantConversation;
15+
use wcf\system\conversation\TConversationForm;
1616
use wcf\system\exception\IllegalLinkException;
1717
use wcf\system\exception\PermissionDeniedException;
1818
use wcf\system\form\builder\field\BooleanFormField;
@@ -33,6 +33,8 @@
3333
*/
3434
final class AddParticipantConversationDialogAction implements RequestHandlerInterface
3535
{
36+
use TConversationForm;
37+
3638
#[\Override]
3739
public function handle(ServerRequestInterface $request): ResponseInterface
3840
{
@@ -74,7 +76,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7476
$participants = \array_unique(
7577
\array_merge(
7678
$participants,
77-
ConversationAddForm::getUserByGroups($groupIDs)
79+
$this->getUserByGroups($groupIDs)
7880
)
7981
);
8082
}
@@ -123,8 +125,8 @@ private function getForm(Conversation $conversation): Psr15DialogForm
123125
->maximumMultiples(WCF::getSession()->getPermission('user.conversation.maxParticipants'))
124126
->multiple()
125127
->maximumMultiples(WCF::getSession()->getPermission('user.conversation.maxParticipants') - $conversation->participants)
126-
->addValidator(ConversationAddForm::getParticipantsValidator())
127-
->addValidator(ConversationAddForm::getMaximumParticipantsValidator(invisibleParticipantGroupsFieldId: null)),
128+
->addValidator($this->getParticipantsValidator())
129+
->addValidator($this->getMaximumParticipantsValidator(invisibleParticipantGroupsFieldId: null)),
128130
BooleanFormField::create('addGroupParticipants')
129131
->label('wcf.conversation.addGroupParticipants')
130132
->available(\count($groupParticipants) > 0),

files/lib/form/ConversationAddForm.class.php

Lines changed: 9 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use wcf\data\user\group\UserGroup;
99
use wcf\system\cache\builder\UserGroupCacheBuilder;
1010
use wcf\system\cache\runtime\UserProfileRuntimeCache;
11-
use wcf\system\database\util\PreparedStatementConditionBuilder;
12-
use wcf\system\exception\UserInputException;
11+
use wcf\system\conversation\TConversationForm;
1312
use wcf\system\flood\FloodControl;
1413
use wcf\system\form\builder\container\FormContainer;
1514
use wcf\system\form\builder\container\wysiwyg\WysiwygFormContainer;
@@ -24,7 +23,6 @@
2423
use wcf\system\form\builder\field\validation\FormFieldValidator;
2524
use wcf\system\form\builder\IFormDocument;
2625
use wcf\system\page\PageLocationManager;
27-
use wcf\system\user\storage\UserStorageHandler;
2826
use wcf\system\WCF;
2927
use wcf\util\HeaderUtil;
3028

@@ -39,6 +37,8 @@
3937
*/
4038
class ConversationAddForm extends AbstractFormBuilderForm
4139
{
40+
use TConversationForm;
41+
4242
/**
4343
* @inheritDoc
4444
*/
@@ -101,8 +101,8 @@ static function (UserGroup $group) {
101101
->label('wcf.conversation.participants')
102102
->description('wcf.conversation.participants.description')
103103
->maximumMultiples(WCF::getSession()->getPermission('user.conversation.maxParticipants'))
104-
->addValidator(self::getParticipantsValidator())
105-
->addValidator(self::getMaximumParticipantsValidator()),
104+
->addValidator($this->getParticipantsValidator())
105+
->addValidator($this->getMaximumParticipantsValidator()),
106106
BooleanFormField::create('addGroupParticipants')
107107
->label('wcf.conversation.addGroupParticipants')
108108
->available(\count($groupParticipants) > 0),
@@ -120,7 +120,7 @@ static function (UserGroup $group) {
120120
->description('wcf.conversation.invisibleParticipants.description')
121121
->available(WCF::getSession()->getPermission('user.conversation.canAddInvisibleParticipants'))
122122
->maximumMultiples(WCF::getSession()->getPermission('user.conversation.maxParticipants'))
123-
->addValidator(self::getParticipantsValidator())
123+
->addValidator($this->getParticipantsValidator())
124124
->addValidator(
125125
new FormFieldValidator(
126126
'duplicateParticipantsValidator',
@@ -219,7 +219,7 @@ protected function finalizeForm()
219219
->addProcessor(
220220
new CustomFormDataProcessor(
221221
'participantsProcessor',
222-
static function (IFormDocument $document, array $parameters) {
222+
function (IFormDocument $document, array $parameters) {
223223
$participants = $parameters['participants'] ?? [];
224224
$invisibleParticipants = $parameters['invisibleParticipants'] ?? [];
225225

@@ -228,14 +228,14 @@ static function (IFormDocument $document, array $parameters) {
228228
$participants = \array_unique(
229229
\array_merge(
230230
$participants,
231-
ConversationAddForm::getUserByGroups($groupIDs)
231+
$this->getUserByGroups($groupIDs)
232232
)
233233
);
234234
}
235235

236236
if (isset($parameters['invisibleParticipantGroups'])) {
237237
$groupIDs = $parameters['invisibleParticipantGroups'];
238-
$userIDs = ConversationAddForm::getUserByGroups($groupIDs);
238+
$userIDs = $this->getUserByGroups($groupIDs);
239239

240240
$invisibleParticipants = \array_unique(
241241
\array_merge(
@@ -306,125 +306,4 @@ public function saved()
306306

307307
exit;
308308
}
309-
310-
/**
311-
* Returns a validator that checks if the selected participants are valid.
312-
*
313-
* @since 6.2
314-
*/
315-
public static function getParticipantsValidator(): FormFieldValidator
316-
{
317-
return new FormFieldValidator('participantsValidator', static function (UserFormField $formField) {
318-
$users = $formField->getUsers();
319-
$userIDs = \array_column($users, 'userID');
320-
321-
UserStorageHandler::getInstance()->loadStorage($userIDs);
322-
323-
foreach ($users as $user) {
324-
try {
325-
if ($user->userID === WCF::getUser()->userID) {
326-
throw new UserInputException('isAuthor');
327-
}
328-
329-
Conversation::validateParticipant($user, $formField->getId());
330-
} catch (UserInputException $e) {
331-
$formField->addValidationError(
332-
new FormFieldValidationError(
333-
$e->getType(),
334-
'wcf.conversation.participants.error.' . $e->getType(),
335-
[
336-
'username' => $user->username,
337-
]
338-
)
339-
);
340-
}
341-
}
342-
});
343-
}
344-
345-
/**
346-
* Returns a validator that checks if the maximum number of participants is not exceeded.
347-
*
348-
* @since 6.2
349-
*/
350-
public static function getMaximumParticipantsValidator(
351-
string $invisibleParticipantsFieldId = 'invisibleParticipants',
352-
string $participantGroupsFieldId = 'participantGroups',
353-
?string $invisibleParticipantGroupsFieldId = 'invisibleParticipantGroups'
354-
): FormFieldValidator {
355-
return new FormFieldValidator(
356-
'participantsMaximumValidator',
357-
static function (UserFormField $formField) use (
358-
$invisibleParticipantsFieldId,
359-
$participantGroupsFieldId,
360-
$invisibleParticipantGroupsFieldId
361-
) {
362-
$invisibleParticipantsFormField = $formField->getDocument()
363-
->getNodeById($invisibleParticipantsFieldId);
364-
$participantGroupsFormField = $formField->getDocument()
365-
->getNodeById($participantGroupsFieldId);
366-
$isDraftFormField = $formField->getDocument()->getNodeById('isDraft');
367-
$invisibleParticipantGroupsFormField = $invisibleParticipantGroupsFieldId !== null ? $formField->getDocument()
368-
->getNodeById($invisibleParticipantGroupsFieldId) : null;
369-
370-
\assert($invisibleParticipantsFormField === null || $invisibleParticipantsFormField instanceof UserFormField);
371-
\assert($isDraftFormField === null || $isDraftFormField instanceof BooleanFormField);
372-
\assert($participantGroupsFormField === null || $participantGroupsFormField instanceof MultipleSelectionFormField);
373-
\assert($invisibleParticipantGroupsFormField === null || $invisibleParticipantGroupsFormField instanceof MultipleSelectionFormField);
374-
375-
$groupIDs = \array_merge(
376-
$participantGroupsFormField?->getValue() ?: [],
377-
$invisibleParticipantGroupsFormField?->getValue() ?: [],
378-
);
379-
380-
$userIDs = \array_merge(
381-
\array_column($formField->getUsers(), 'userID'),
382-
\array_column($invisibleParticipantsFormField?->getUsers() ?: [], 'userID'),
383-
ConversationAddForm::getUserByGroups($groupIDs)
384-
);
385-
386-
if (\count($userIDs) > WCF::getSession()->getPermission('user.conversation.maxParticipants')) {
387-
$formField->addValidationError(
388-
new FormFieldValidationError(
389-
'tooManyParticipants',
390-
'wcf.conversation.participants.error.tooManyParticipants'
391-
)
392-
);
393-
}
394-
395-
if (!$isDraftFormField?->getValue() && $userIDs === []) {
396-
$formField->addValidationError(new FormFieldValidationError('empty'));
397-
}
398-
}
399-
);
400-
}
401-
402-
/**
403-
* Returns the user IDs of the users that are in the given groups.
404-
*
405-
* @param int[] $groupIDs
406-
*
407-
* @return int[]
408-
*/
409-
public static function getUserByGroups(array $groupIDs): array
410-
{
411-
if ($groupIDs === []) {
412-
return [];
413-
}
414-
415-
$conditionBuilder = new PreparedStatementConditionBuilder();
416-
$conditionBuilder->add('groupID IN (?)', [$groupIDs]);
417-
$sql = "SELECT DISTINCT userID
418-
FROM wcf1_user_to_group
419-
" . $conditionBuilder;
420-
$statement = WCF::getDB()->prepare($sql);
421-
$statement->execute($conditionBuilder->getParameters());
422-
423-
$userIDs = [];
424-
while ($userID = $statement->fetchColumn()) {
425-
$userIDs[] = $userID;
426-
}
427-
428-
return $userIDs;
429-
}
430309
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace wcf\system\conversation;
4+
5+
use wcf\data\conversation\Conversation;
6+
use wcf\system\database\util\PreparedStatementConditionBuilder;
7+
use wcf\system\exception\UserInputException;
8+
use wcf\system\form\builder\field\BooleanFormField;
9+
use wcf\system\form\builder\field\MultipleSelectionFormField;
10+
use wcf\system\form\builder\field\user\UserFormField;
11+
use wcf\system\form\builder\field\validation\FormFieldValidationError;
12+
use wcf\system\form\builder\field\validation\FormFieldValidator;
13+
use wcf\system\user\storage\UserStorageHandler;
14+
use wcf\system\WCF;
15+
16+
/**
17+
* Provides methods that can be used in conversation form builder forms.
18+
*
19+
* @author Olaf Braun
20+
* @copyright 2001-2025 WoltLab GmbH
21+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22+
* @since 6.2
23+
*/
24+
trait TConversationForm
25+
{
26+
/**
27+
* Returns the user IDs of the users that are in the given groups.
28+
*
29+
* @param int[] $groupIDs
30+
*
31+
* @return int[]
32+
*/
33+
protected function getUserByGroups(array $groupIDs): array
34+
{
35+
if ($groupIDs === []) {
36+
return [];
37+
}
38+
39+
$conditionBuilder = new PreparedStatementConditionBuilder();
40+
$conditionBuilder->add('groupID IN (?)', [$groupIDs]);
41+
$sql = "SELECT DISTINCT userID
42+
FROM wcf1_user_to_group
43+
" . $conditionBuilder;
44+
$statement = WCF::getDB()->prepare($sql);
45+
$statement->execute($conditionBuilder->getParameters());
46+
47+
$userIDs = [];
48+
while ($userID = $statement->fetchColumn()) {
49+
$userIDs[] = $userID;
50+
}
51+
52+
return $userIDs;
53+
}
54+
55+
/**
56+
* Returns a validator that checks if the selected participants are valid.
57+
*/
58+
protected function getParticipantsValidator(): FormFieldValidator
59+
{
60+
return new FormFieldValidator('participantsValidator', static function (UserFormField $formField) {
61+
$users = $formField->getUsers();
62+
$userIDs = \array_column($users, 'userID');
63+
64+
UserStorageHandler::getInstance()->loadStorage($userIDs);
65+
66+
foreach ($users as $user) {
67+
try {
68+
if ($user->userID === WCF::getUser()->userID) {
69+
throw new UserInputException('isAuthor');
70+
}
71+
72+
Conversation::validateParticipant($user, $formField->getId());
73+
} catch (UserInputException $e) {
74+
$formField->addValidationError(
75+
new FormFieldValidationError(
76+
$e->getType(),
77+
'wcf.conversation.participants.error.' . $e->getType(),
78+
[
79+
'username' => $user->username,
80+
]
81+
)
82+
);
83+
}
84+
}
85+
});
86+
}
87+
88+
/**
89+
* Returns a validator that checks if the maximum number of participants is not exceeded.
90+
*/
91+
protected function getMaximumParticipantsValidator(
92+
string $invisibleParticipantsFieldId = 'invisibleParticipants',
93+
string $participantGroupsFieldId = 'participantGroups',
94+
?string $invisibleParticipantGroupsFieldId = 'invisibleParticipantGroups'
95+
): FormFieldValidator {
96+
return new FormFieldValidator(
97+
'participantsMaximumValidator',
98+
function (UserFormField $formField) use (
99+
$invisibleParticipantsFieldId,
100+
$participantGroupsFieldId,
101+
$invisibleParticipantGroupsFieldId
102+
) {
103+
$invisibleParticipantsFormField = $formField->getDocument()
104+
->getNodeById($invisibleParticipantsFieldId);
105+
$participantGroupsFormField = $formField->getDocument()
106+
->getNodeById($participantGroupsFieldId);
107+
$isDraftFormField = $formField->getDocument()->getNodeById('isDraft');
108+
$invisibleParticipantGroupsFormField = $invisibleParticipantGroupsFieldId !== null ? $formField->getDocument()
109+
->getNodeById($invisibleParticipantGroupsFieldId) : null;
110+
111+
\assert($invisibleParticipantsFormField === null || $invisibleParticipantsFormField instanceof UserFormField);
112+
\assert($isDraftFormField === null || $isDraftFormField instanceof BooleanFormField);
113+
\assert($participantGroupsFormField === null || $participantGroupsFormField instanceof MultipleSelectionFormField);
114+
\assert($invisibleParticipantGroupsFormField === null || $invisibleParticipantGroupsFormField instanceof MultipleSelectionFormField);
115+
116+
$groupIDs = \array_merge(
117+
$participantGroupsFormField?->getValue() ?: [],
118+
$invisibleParticipantGroupsFormField?->getValue() ?: [],
119+
);
120+
121+
$userIDs = \array_merge(
122+
\array_column($formField->getUsers(), 'userID'),
123+
\array_column($invisibleParticipantsFormField?->getUsers() ?: [], 'userID'),
124+
$this->getUserByGroups($groupIDs)
125+
);
126+
127+
if (\count($userIDs) > WCF::getSession()->getPermission('user.conversation.maxParticipants')) {
128+
$formField->addValidationError(
129+
new FormFieldValidationError(
130+
'tooManyParticipants',
131+
'wcf.conversation.participants.error.tooManyParticipants'
132+
)
133+
);
134+
}
135+
136+
if (!$isDraftFormField?->getValue() && $userIDs === []) {
137+
$formField->addValidationError(new FormFieldValidationError('empty'));
138+
}
139+
}
140+
);
141+
}
142+
}

0 commit comments

Comments
 (0)