Update `codebox` macro
[GitHub/WoltLab/woltlab.github.io.git] / docs / tutorial / series / part_4.md
1 # Part 4: Box and Box Conditions
2
3 In this part of our tutorial series, we add support for creating boxes listing people.
4
5 ## Package Functionality
6
7 In addition to the existing functions from [part 3](part_3.md), the package will provide the following functionality after this part of the tutorial:
8
9 - Creating boxes dynamically listing people
10 - Filtering the people listed in boxes using conditions
11
12 ## Used Components
13
14 In addition to the components used in previous parts, we will use the [`objectTypeDefinition` package installation plugin](../../package/pip/object-type-definition.md) and use the box and condition APIs.
15
16 To pre-install a specific person list box, we refer to the documentation of the [`box` package installation plugin](../../package/pip/box.md).
17
18
19 ## Package Structure
20
21 The complete package will have the following file structure (_excluding_ unchanged files from [part 3](part_3.md)):
22
23 ```
24 ├── files
25 │ └── lib
26 │ └── system
27 │ ├── box
28 │ │ └── PersonListBoxController.class.php
29 │ └── condition
30 │ └── person
31 │ ├── PersonFirstNameTextPropertyCondition.class.php
32 │ └── PersonLastNameTextPropertyCondition.class.php
33 ├── language
34 │ ├── de.xml
35 │ └── en.xml
36 ├── objectType.xml
37 ├── objectTypeDefinition.xml
38 └── templates
39 └── boxPersonList.tpl
40 ```
41
42 For all changes, please refer to the [source code on GitHub]({jinja{ config.repo_url }}tree/{jinja{ config.edit_uri.split("/")[1] }}/snippets/tutorial/tutorial-series/part-4).
43
44
45 ## Box Controller
46
47 In addition to static boxes with fixed contents, administrators are able to create dynamic boxes with contents from the database.
48 In our case here, we want administrators to be able to create boxes listing people.
49 To do so, we first have to register a new object type for this person list box controller for the object type definition `com.woltlab.wcf.boxController`:
50
51 ```xml
52 <type>
53 <name>com.woltlab.wcf.personList</name>
54 <definitionname>com.woltlab.wcf.boxController</definitionname>
55 <classname>wcf\system\box\PersonListBoxController</classname>
56 </type>
57 ```
58
59 The `com.woltlab.wcf.boxController` object type definition requires the provided class to implement `wcf\system\box\IBoxController`:
60
61 {jinja{ codebox(
62 title="files/lib/system/box/PersonListBoxController.class.php",
63 language="php",
64 filepath="tutorial/tutorial-series/part-4/files/lib/system/box/PersonListBoxController.class.php"
65 ) }}
66
67 By extending `AbstractDatabaseObjectListBoxController`, we only have to provide minimal data ourself and rely mostly on the default implementation provided by `AbstractDatabaseObjectListBoxController`:
68
69 1. As we will support [conditions](#conditions) for the listed people, we have to set the relevant condition definition via `$conditionDefinition`.
70 2. `AbstractDatabaseObjectListBoxController` already supports restricting the number of listed objects.
71 To do so, you only have to specify the default number of listed objects via `$defaultLimit`.
72 3. `AbstractDatabaseObjectListBoxController` also supports setting the sort order of the listed objects.
73 You have to provide the supported sort fields via `$validSortFields` and specify the prefix used for the language items of the sort fields via `$sortFieldLanguageItemPrefix` so that for every `$validSortField` in `$validSortFields`, the language item `{$sortFieldLanguageItemPrefix}.{$validSortField}` must exist.
74 4. The box system supports [different positions](../../package/pip/box.md#position).
75 Each box controller specifies the positions it supports via `$supportedPositions`.
76 To keep the implementation simple here as different positions might require different output in the template, we restrict ourselves to sidebars.
77 5. `getObjectList()` returns an instance of `DatabaseObjectList` that is used to read the listed objects.
78 `getObjectList()` itself must not call `readObjects()`, as `AbstractDatabaseObjectListBoxController` takes care of calling the method after adding the conditions and setting the sort order.
79 6. `getTemplate()` returns the contents of the box relying on the `boxPersonList` template here:
80 ```smarty
81 --8<-- "tutorial/tutorial-series/part-4/templates/boxPersonList.tpl"
82 ```
83 The template relies on a `.sidebarItemList` element, which is generally used for sidebar listings.
84 (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 `getTemplate()`.)
85 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 `wcf.person.boxList.description.{$boxSortField}`.
86 We only add one such language item when sorting the people by comments:
87 In such a case, the number of comments will be shown.
88 (When sorting by first and last name, there are no additional useful information that could be shown here, though the plugin from [part 2](part_2.md) adding support for birthdays might also show the birthday when sorting by first or last name.)
89
90 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.
91
92
93 ## Conditions
94
95 The condition system can be used to generally filter a list of objects.
96 In our case, the box system supports conditions to filter the objects shown in a specific box.
97 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.
98 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.
99
100 The first step for condition support is to register a object type definition for the relevant conditions requiring the `IObjectListCondition` interface:
101
102 {jinja{ codebox(
103 title="objectTypeDefinition.xml",
104 language="xml",
105 filepath="tutorial/tutorial-series/part-4/objectTypeDefinition.xml"
106 ) }}
107
108 Next, we register the specific conditions for filtering by the first and last name using this object type condition:
109
110 ```xml
111 <type>
112 <name>com.woltlab.wcf.people.firstName</name>
113 <definitionname>com.woltlab.wcf.box.personList.condition</definitionname>
114 <classname>wcf\system\condition\person\PersonFirstNameTextPropertyCondition</classname>
115 </type>
116 <type>
117 <name>com.woltlab.wcf.people.lastName</name>
118 <definitionname>com.woltlab.wcf.box.personList.condition</definitionname>
119 <classname>wcf\system\condition\person\PersonLastNameTextPropertyCondition</classname>
120 </type>
121 ```
122
123 `PersonFirstNameTextPropertyCondition` and `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:
124
125 {jinja{ codebox(
126 title="files/lib/system/condition/person/PersonFirstNameTextPropertyCondition.class.php",
127 language="php",
128 filepath="tutorial/tutorial-series/part-4/files/lib/system/condition/person/PersonFirstNameTextPropertyCondition.class.php"
129 ) }}
130
131 1. `$className` contains the class name of the relevant database object from which the class name of the database object list is derived and `$propertyName` is the name of the database object's property that contains the value used for filtering.
132 1. By setting `$supportsMultipleValues` to `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.
133 1. `$description` (optional), `$fieldName`, and `$label` are used in the output of the form field.
134
135 (The implementation here is specific for `AbstractObjectTextPropertyCondition`.
136 The `wcf\system\condition` namespace also contains several other default condition implementations.)