From 4de472c7504418e217a2b8792b452e3e4ee49be5 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Tue, 29 May 2018 07:12:17 +0200 Subject: [PATCH] Add support for database object lists as selection form fields' options See #2509 --- .../DevtoolsFormBuilderTestForm.class.php | 7 ++-- .../field/ISelectionFormField.class.php | 10 +++++- .../field/TSelectionFormField.class.php | 31 +++++++++++++++-- .../files/lib/util/ClassUtil.class.php | 34 +++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/DevtoolsFormBuilderTestForm.class.php b/wcfsetup/install/files/lib/acp/form/DevtoolsFormBuilderTestForm.class.php index 973146853b..926cf60166 100644 --- a/wcfsetup/install/files/lib/acp/form/DevtoolsFormBuilderTestForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/DevtoolsFormBuilderTestForm.class.php @@ -1,6 +1,7 @@ description('wcf.global.description') ->addClass('someSection') ->appendChildren([ + SingleSelectionFormField::create('dboTest') + ->label('Users') + ->options(new UserList()), ShowOrderFormField::create() ->options([ 2 => 'Object with id 2', 5 => 'Object with id 5', 4 => 'Object with id 4' - ]) - ->value(2), + ]), TextFormField::create('name') ->label('wcf.global.name'), TitleFormField::create() diff --git a/wcfsetup/install/files/lib/system/form/builder/field/ISelectionFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/ISelectionFormField.class.php index c29bda24a3..6f2ea4692d 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/ISelectionFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/ISelectionFormField.class.php @@ -1,6 +1,7 @@ objectIDs === null`, + * `$options->readObjects()` is called so that the `readObjects()` does not have + * to be called by the API user. + * + * @param array|callable|DatabaseObjectList $options selectable options or callable returning the options * @return static this field * * @throws \InvalidArgumentException if given options are no array or callable or otherwise invalid diff --git a/wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php index 46320758f2..c566f4f7bf 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/TSelectionFormField.class.php @@ -1,8 +1,11 @@ options($options); + } + else if ($options instanceof DatabaseObjectList) { + // automatically read objects + if ($options->objectIDs === null) { + $options->readObjects(); + } + + $dboOptions = []; + foreach ($options as $object) { + if (!ClassUtil::isDecoratedInstanceOf($object, ITitledObject::class)) { + throw new \InvalidArgumentException("The database objects in the passed list must implement '" . ITitledObject::class . "'."); + } + if (!$object::getDatabaseTableIndexIsIdentity()) { + throw new \InvalidArgumentException("The database objects in the passed list must must have an index that identifies the objects."); + } + + $dboOptions[$object->getObjectID()] = $object->getTitle(); + } + + $options = $dboOptions; } // validate options and read possible values diff --git a/wcfsetup/install/files/lib/util/ClassUtil.class.php b/wcfsetup/install/files/lib/util/ClassUtil.class.php index c0f21f76da..5440212b40 100644 --- a/wcfsetup/install/files/lib/util/ClassUtil.class.php +++ b/wcfsetup/install/files/lib/util/ClassUtil.class.php @@ -1,6 +1,7 @@ name; + + if (!is_subclass_of($className, DatabaseObjectDecorator::class)) { + return false; + } + + if (is_subclass_of($className::getBaseClass(), $targetClass)) { + return true; + } + } + while (($parentClass = $parentClass->getParentClass())); + + return false; + } + /** * Forbid creation of ClassUtil objects. */ -- 2.20.1