Part 4: Box and Box Conditions#
In this part of our tutorial series, we add support for creating boxes listing people.
In addition to the existing functions from part 3, the package will provide the following functionality after this part of the tutorial:
- Creating boxes dynamically listing people
- Filtering the people listed in boxes using conditions
In addition to the components used in previous parts, we will use the
objectTypeDefinition package installation plugin and use the box and condition APIs.
To pre-install a specific person list box, we refer to the documentation of the
box package installation plugin.
The complete package will have the following file structure (excluding unchanged files from part 3):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
For all changes, please refer to the source code on GitHub.
In addition to static boxes with fixed contents, administrators are able to create dynamic boxes with contents from the database.
In our case here, we want administrators to be able to create boxes listing people.
To do so, we first have to register a new object type for this person list box controller for the object type definition
1 2 3 4 5
com.woltlab.wcf.boxController object type definition requires the provided class to implement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
AbstractDatabaseObjectListBoxController, we only have to provide minimal data ourself and rely mostly on the default implementation provided by
- As we will support conditions for the listed people, we have to set the relevant condition definition via
AbstractDatabaseObjectListBoxControlleralready supports restricting the number of listed objects. To do so, you only have to specify the default number of listed objects via
AbstractDatabaseObjectListBoxControlleralso supports setting the sort order of the listed objects. You have to provide the supported sort fields via
$validSortFieldsand specify the prefix used for the language items of the sort fields via
$sortFieldLanguageItemPrefixso that for every
$validSortFields, the language item
- The box system supports different positions.
Each box controller specifies the positions it supports via
$supportedPositions. To keep the implementation simple here as different positions might require different output in the template, we restrict ourselves to sidebars.
getObjectList()returns an instance of
DatabaseObjectListthat is used to read the listed objects.
getObjectList()itself must not call
AbstractDatabaseObjectListBoxControllertakes care of calling the method after adding the conditions and setting the sort order.
getTemplate()returns the contents of the box relying on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The template relies on a
.sidebarItemList element, which is generally used for sidebar listings.
(If different box positions were supported, we either have to generate different output by considering the value of
$boxPosition in the template or by using different templates in
One specific piece of code is the
$__boxPersonDescription variable, which supports an optional description below the person's name relying on the optional language item
We only add one such language item when sorting the people by comments:
In such a case, the number of comments will be shown.
(When sorting by first and last name, there are no additional useful information that could be shown here, though the plugin from part 2 adding support for birthdays might also show the birthday when sorting by first or last name.)
Lastly, we also provide the language item
wcf.acp.box.boxController.com.woltlab.wcf.personList, which is used in the list of available box controllers.
The condition system can be used to generally filter a list of objects. In our case, the box system supports conditions to filter the objects shown in a specific box. Admittedly, our current person implementation only contains minimal data so that filtering might not make the most sense here but it will still show how to use the condition system for boxes. We will support filtering the people by their first and last name so that, for example, a box can be created listing all people with a specific first name.
The first step for condition support is to register a object type definition for the relevant conditions requiring the
1 2 3 4 5 6 7 8 9
Next, we register the specific conditions for filtering by the first and last name using this object type condition:
1 2 3 4 5 6 7 8 9 10
PersonLastNameTextPropertyCondition only differ minimally so that we only focus on
PersonFirstNameTextPropertyCondition here, which relies on the default implementation
AbstractObjectTextPropertyCondition and only requires specifying different object properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
$classNamecontains the class name of the relevant database object from which the class name of the database object list is derived and
$propertyNameis the name of the database object's property that contains the value used for filtering.
- By setting
true, multiple comma-separated values can be specified so that, for example, a box can also only list people with either of two specific first names.
$labelare used in the output of the form field.
(The implementation here is specific for
wcf\system\condition namespace also contains several other default condition implementations.)