88use wcf \data \user \group \UserGroup ;
99use wcf \system \cache \builder \UserGroupCacheBuilder ;
1010use wcf \system \cache \runtime \UserProfileRuntimeCache ;
11- use wcf \system \database \util \PreparedStatementConditionBuilder ;
12- use wcf \system \exception \UserInputException ;
11+ use wcf \system \conversation \TConversationForm ;
1312use wcf \system \flood \FloodControl ;
1413use wcf \system \form \builder \container \FormContainer ;
1514use wcf \system \form \builder \container \wysiwyg \WysiwygFormContainer ;
2423use wcf \system \form \builder \field \validation \FormFieldValidator ;
2524use wcf \system \form \builder \IFormDocument ;
2625use wcf \system \page \PageLocationManager ;
27- use wcf \system \user \storage \UserStorageHandler ;
2826use wcf \system \WCF ;
2927use wcf \util \HeaderUtil ;
3028
3937 */
4038class 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}
0 commit comments