From 8068e4595b0e38fa8521176d3a7c373f28d8b749 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 2 Nov 2019 15:39:05 +0100 Subject: [PATCH] UserFormField now uses user ids as value --- .../field/user/UserFormField.class.php | 106 ++++++++++++++++-- 1 file changed, 99 insertions(+), 7 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php index d52214a1ad..803eb7a5ad 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php @@ -1,6 +1,8 @@ users; + } + + /** + * @inheritDoc + */ + public function getSaveValue() { + if (empty($this->getUsers())) { + return 0; + } + + return current($this->getUsers())->userID; + } + + /** + * @inheritDoc + */ + public function hasSaveValue() { + return !$this->allowsMultiple() && !empty($this->getUsers()); + } + + /** + * @inheritDoc + */ + public function populate() { + parent::populate(); + + if ($this->allowsMultiple()) { + $this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor('multipleUsers', function(IFormDocument $document, array $parameters) { + if ($this->checkDependencies() && !empty($this->getUsers())) { + $parameters[$this->getObjectProperty()] = array_values(array_map(function(UserProfile $user) { + return $user->userID; + }, $this->getUsers())); + } + + return $parameters; + })); + } + + return $this; + } + /** * @inheritDoc */ @@ -95,11 +154,11 @@ class UserFormField extends AbstractFormField implements IAutoFocusFormField, II } else { // validate users - $users = UserProfile::getUserProfilesByUsername($this->getValue()); + $this->users = UserProfile::getUserProfilesByUsername($this->getValue()); $nonExistentUsernames = []; foreach ($this->getValue() as $username) { - if (!isset($users[$username])) { + if (!isset($this->users[$username])) { $nonExistentUsernames[] = $username; } } @@ -113,14 +172,47 @@ class UserFormField extends AbstractFormField implements IAutoFocusFormField, II } } } - else if ($this->getValue() !== '' && UserProfile::getUserProfileByUsername($this->getValue()) === null) { - $this->addValidationError(new FormFieldValidationError( - 'nonExistent', - 'wcf.form.field.user.error.invalid' - )); + else if ($this->getValue() !== '') { + $user = UserProfile::getUserProfileByUsername($this->getValue()); + + if ($user === null) { + $this->addValidationError(new FormFieldValidationError( + 'nonExistent', + 'wcf.form.field.user.error.invalid' + )); + } + else { + $this->users[] = $user; + } } } parent::validate(); } + + /** + * @inheritDoc + */ + public function value($value) { + // ensure array value for form fields that actually support multiple values; + // allows enabling support for multiple values for existing fields + if ($this->allowsMultiple() && !is_array($value)) { + $value = [$value]; + } + + if ($this->allowsMultiple()) { + $this->users = UserProfileRuntimeCache::getInstance()->getObjects($value); + + $value = array_map(function(UserProfile $user) { + return $user->username; + }, $this->users); + } + else { + $user = UserProfileRuntimeCache::getInstance()->getObject($value); + $this->users[] = $user; + $value = $user->username; + } + + return parent::value($value); + } } -- 2.20.1