From: Tim Düsterhus Date: Wed, 25 Aug 2021 11:39:38 +0000 (+0200) Subject: Merge branch '5.3' into 5.4 X-Git-Tag: 5.4.5_RC_1~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=557f880e6e5a829e7b241fb8550c5e512ce16b6d;p=GitHub%2FWoltLab%2FWCF.git Merge branch '5.3' into 5.4 --- 557f880e6e5a829e7b241fb8550c5e512ce16b6d diff --cc wcfsetup/install/files/lib/system/form/builder/field/user/UserFormField.class.php index ee08fe3acd,a595e8d195..e9fb58f68b --- 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 @@@ -21,208 -19,200 +21,210 @@@ use wcf\util\StringUtil /** * Implementation of a form field to enter existing users. - * - * @author Matthias Schmidt - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @package WoltLabSuite\Core\System\Form\Builder\Field\User - * @since 5.2 + * + * @author Matthias Schmidt + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @package WoltLabSuite\Core\System\Form\Builder\Field\User + * @since 5.2 */ -class UserFormField extends AbstractFormField implements IAutoFocusFormField, IImmutableFormField, IMultipleFormField, INullableFormField { - use TAutoFocusFormField; - use TImmutableFormField; - use TMultipleFormField; - use TNullableFormField; - - /** - * @inheritDoc - */ - protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/User'; - - /** - * @inheritDoc - */ - protected $templateName = '__userFormField'; - - /** - * user profiles of the entered users (and `null` for non-existing users; only relevant for - * invalid inputs) - * @var UserProfile[]|null[] - */ - protected $users = []; - - /** - * Returns the user profiles of the entered users (and `null` for non-existing users; only - * relevant for invalid inputs). - * - * @return UserProfile[]|null[] - */ - public function getUsers() { - return $this->users; - } - - /** - * @inheritDoc - */ - public function getSaveValue() { - if (empty($this->getUsers())) { - if ($this->isNullable()) { - return null; - } - - return 0; - } - - return current($this->getUsers())->userID; - } - - /** - * @inheritDoc - */ - public function populate() { - parent::populate(); - - if ($this->allowsMultiple()) { - $this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor('multipleUsers', function(IFormDocument $document, array $parameters) { - if ($this->checkDependencies()) { - $parameters[$this->getObjectProperty()] = array_values(array_map(function(UserProfile $user) { - return $user->userID; - }, $this->getUsers())); - } - - return $parameters; - })); - } - - return $this; - } - - /** - * @inheritDoc - */ - public function readValue() { - if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { - $this->users = []; - - $value = $this->getDocument()->getRequestData($this->getPrefixedId()); - - if (is_string($value)) { - if ($this->allowsMultiple()) { - $this->value = ArrayUtil::trim(explode(',', $value)); - } - else { - $this->value = StringUtil::trim($value); - } - } - } - - return $this; - } - - /** - * @inheritDoc - */ - public function validate() { - if ( - $this->isRequired() && ( - ($this->getValue() === null || $this->getValue() === '') || - (is_array($this->getValue()) && empty($this->getValue())) - ) - ) { - $this->addValidationError(new FormFieldValidationError('empty')); - } - - if ($this->getValue() !== null) { - if ($this->allowsMultiple()) { - if ($this->getMinimumMultiples() > 0 && count($this->getValue()) < $this->getMinimumMultiples()) { - $this->addValidationError(new FormFieldValidationError( - 'minimumMultiples', - 'wcf.form.field.user.error.minimumMultiples', - [ - 'minimumCount' => $this->getMinimumMultiples(), - 'count' => count($this->getValue()) - ] - )); - } - else if ($this->getMaximumMultiples() !== IMultipleFormField::NO_MAXIMUM_MULTIPLES && count($this->getValue()) > $this->getMaximumMultiples()) { - $this->addValidationError(new FormFieldValidationError( - 'maximumMultiples', - 'wcf.form.field.user.error.maximumMultiples', - [ - 'maximumCount' => $this->getMaximumMultiples(), - 'count' => count($this->getValue()) - ] - )); - } - else { - // validate users - $this->users = UserProfile::getUserProfilesByUsername($this->getValue()); - - $nonExistentUsernames = []; - foreach ($this->getValue() as $username) { - if (!isset($this->users[$username])) { - $nonExistentUsernames[] = $username; - } - } - - if (!empty($nonExistentUsernames)) { - $this->addValidationError(new FormFieldValidationError( - 'nonExistent', - 'wcf.form.field.user.error.nonExistent', - ['nonExistentUsernames' => $nonExistentUsernames] - )); - } - } - } - 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); - } +class UserFormField extends AbstractFormField implements + IAutoFocusFormField, + IImmutableFormField, + IMultipleFormField, + INullableFormField +{ + use TAutoFocusFormField; + use TImmutableFormField; + use TMultipleFormField; + use TNullableFormField; + + /** + * @inheritDoc + */ + protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/User'; + + /** + * @inheritDoc + */ + protected $templateName = '__userFormField'; + + /** + * user profiles of the entered users (and `null` for non-existing users; only relevant for + * invalid inputs) + * @var UserProfile[]|null[] + */ + protected $users = []; + + /** + * Returns the user profiles of the entered users (and `null` for non-existing users; only + * relevant for invalid inputs). + * + * @return UserProfile[]|null[] + */ + public function getUsers() + { + return $this->users; + } + + /** + * @inheritDoc + */ + public function getSaveValue() + { + if (empty($this->getUsers())) { + if ($this->isNullable()) { + return null; + } + + return 0; + } + + return \current($this->getUsers())->userID; + } + + /** + * @inheritDoc + */ + public function populate() + { + parent::populate(); + + if ($this->allowsMultiple()) { + $this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor( + 'multipleUsers', + function (IFormDocument $document, array $parameters) { + if ($this->checkDependencies()) { + $parameters[$this->getObjectProperty()] = \array_column($this->getUsers(), 'userID'); + } + + return $parameters; + } + )); + } + + return $this; + } + + /** + * @inheritDoc + */ + public function readValue() + { + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { + $this->users = []; + + $value = $this->getDocument()->getRequestData($this->getPrefixedId()); + + if (\is_string($value)) { + if ($this->allowsMultiple()) { + $this->value = ArrayUtil::trim(\explode(',', $value)); + } else { + $this->value = StringUtil::trim($value); + } + } + } + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() + { + if ($this->isRequired()) { + if ( + $this->getValue() === null + || $this->getValue() === '' + || (\is_array($this->getValue()) && empty($this->getValue())) + ) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + } + - if ($this->allowsMultiple()) { - if ($this->getMinimumMultiples() > 0 && \count($this->getValue()) < $this->getMinimumMultiples()) { - $this->addValidationError(new FormFieldValidationError( - 'minimumMultiples', - 'wcf.form.field.user.error.minimumMultiples', - [ - 'minimumCount' => $this->getMinimumMultiples(), - 'count' => \count($this->getValue()), - ] - )); - } elseif ( - $this->getMaximumMultiples() !== IMultipleFormField::NO_MAXIMUM_MULTIPLES - && \count($this->getValue()) > $this->getMaximumMultiples() - ) { - $this->addValidationError(new FormFieldValidationError( - 'maximumMultiples', - 'wcf.form.field.user.error.maximumMultiples', - [ - 'maximumCount' => $this->getMaximumMultiples(), - 'count' => \count($this->getValue()), - ] - )); - } else { - // validate users - $this->users = UserProfile::getUserProfilesByUsername($this->getValue()); - - $nonExistentUsernames = []; - foreach ($this->getValue() as $username) { - if (!isset($this->users[$username])) { - $nonExistentUsernames[] = $username; ++ if ($this->getValue() !== null) { ++ if ($this->allowsMultiple()) { ++ if ($this->getMinimumMultiples() > 0 && \count($this->getValue()) < $this->getMinimumMultiples()) { ++ $this->addValidationError(new FormFieldValidationError( ++ 'minimumMultiples', ++ 'wcf.form.field.user.error.minimumMultiples', ++ [ ++ 'minimumCount' => $this->getMinimumMultiples(), ++ 'count' => \count($this->getValue()), ++ ] ++ )); ++ } elseif ( ++ $this->getMaximumMultiples() !== IMultipleFormField::NO_MAXIMUM_MULTIPLES ++ && \count($this->getValue()) > $this->getMaximumMultiples() ++ ) { ++ $this->addValidationError(new FormFieldValidationError( ++ 'maximumMultiples', ++ 'wcf.form.field.user.error.maximumMultiples', ++ [ ++ 'maximumCount' => $this->getMaximumMultiples(), ++ 'count' => \count($this->getValue()), ++ ] ++ )); ++ } else { ++ // validate users ++ $this->users = UserProfile::getUserProfilesByUsername($this->getValue()); ++ ++ $nonExistentUsernames = []; ++ foreach ($this->getValue() as $username) { ++ if (!isset($this->users[$username])) { ++ $nonExistentUsernames[] = $username; ++ } ++ } ++ ++ if (!empty($nonExistentUsernames)) { ++ $this->addValidationError(new FormFieldValidationError( ++ 'nonExistent', ++ 'wcf.form.field.user.error.nonExistent', ++ ['nonExistentUsernames' => $nonExistentUsernames] ++ )); + } + } ++ } elseif ($this->getValue() !== '') { ++ $user = UserProfile::getUserProfileByUsername($this->getValue()); + - if (!empty($nonExistentUsernames)) { ++ if ($user === null) { + $this->addValidationError(new FormFieldValidationError( + 'nonExistent', - 'wcf.form.field.user.error.nonExistent', - ['nonExistentUsernames' => $nonExistentUsernames] ++ 'wcf.form.field.user.error.invalid' + )); ++ } else { ++ $this->users[] = $user; + } + } - } elseif ($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(static function (UserProfile $user) { + return $user->username; + }, $this->users); + } else { + $user = UserProfileRuntimeCache::getInstance()->getObject($value); + $this->users[] = $user; + $value = $user->username; + } + + return parent::value($value); + } }