55e77c6810f21a25e2cf064ceea06e1193da588b
[GitHub/WoltLab/woltlab.github.io.git] / docs / tutorial / series / part_1.md
1 # Tutorial Series Part 1: Base Structure
2
3 In the first part of this tutorial series, we will lay out what the basic version of package should be able to do and how to implement these functions.
4
5
6 ## Package Functionality
7
8 The package should provide the following possibilities/functions:
9
10 - Sortable list of all people in the ACP
11 - Ability to add, edit and delete people in the ACP
12 - Restrict the ability to add, edit and delete people (in short: manage people) in the ACP
13 - Sortable list of all people in the front end
14
15
16 ## Used Components
17
18 We will use the following package installation plugins:
19
20 - [acpTemplate package installation plugin](../../package/pip/acp-template.md),
21 - [acpMenu package installation plugin](../../package/pip/acp-menu.md),
22 - [database package installation plugin](../../package/pip/database.md),
23 - [file package installation plugin](../../package/pip/file.md),
24 - [language package installation plugin](../../package/pip/language.md),
25 - [menuItem package installation plugin](../../package/pip/menu-item.md),
26 - [page package installation plugin](../../package/pip/page.md),
27 - [template package installation plugin](../../package/pip/template.md),
28 - [userGroupOption package installation plugin](../../package/pip/user-group-option.md),
29
30 use [database objects](../../php/database-objects.md), create [pages](../../php/pages.md) and use [templates](../../view/templates.md).
31
32
33 ## Package Structure
34
35 The package will have the following file structure:
36
37 ```
38 ├── acpMenu.xml
39 ├── acptemplates
40 │ ├── personAdd.tpl
41 │ └── personList.tpl
42 ├── files
43 │ ├── acp
44 │ │ └── database
45 │ │ └── install_com.woltlab.wcf.people.php
46 │ └── lib
47 │ ├── acp
48 │ │ ├── form
49 │ │ │ ├── PersonAddForm.class.php
50 │ │ │ └── PersonEditForm.class.php
51 │ │ └── page
52 │ │ └── PersonListPage.class.php
53 │ ├── data
54 │ │ └── person
55 │ │ ├── Person.class.php
56 │ │ ├── PersonAction.class.php
57 │ │ ├── PersonEditor.class.php
58 │ │ └── PersonList.class.php
59 │ └── page
60 │ └── PersonListPage.class.php
61 ├── language
62 │ ├── de.xml
63 │ └── en.xml
64 ├── menuItem.xml
65 ├── package.xml
66 ├── page.xml
67 ├── templates
68 │ └── personList.tpl
69 └── userGroupOption.xml
70 ```
71
72
73 ## Person Modeling
74
75 ### Database Table
76
77 As the first step, we have to model the people we want to manage with this package.
78 As this is only an introductory tutorial, we will keep things simple and only consider the first and last name of a person.
79 Thus, the database table we will store the people in only contains three columns:
80
81 1. `personID` is the unique numeric identifier of each person created,
82 1. `firstName` contains the first name of the person,
83 1. `lastName` contains the last name of the person.
84
85 The first file for our package is the `install_com.woltlab.wcf.people.php` file used to create such a database table during package installation:
86
87 ```sql
88 --8<-- "tutorial/tutorial-series/part-1/files/acp/database/install_com.woltlab.wcf.people.php"
89 ```
90
91 ### Database Object
92
93 #### `Person`
94
95 In our PHP code, each person will be represented by an object of the following class:
96
97 ```php
98 --8<-- "tutorial/tutorial-series/part-1/files/lib/data/person/Person.class.php"
99 ```
100
101 The important thing here is that `Person` extends `DatabaseObject`.
102 Additionally, we implement the `IRouteController` interface, which allows us to use `Person` objects to create links, and we implement PHP's magic [__toString()](https://secure.php.net/manual/en/language.oop5.magic.php#object.tostring) method for convenience.
103
104 For every database object, you need to implement three additional classes:
105 an action class, an editor class and a list class.
106
107 #### `PersonAction`
108
109 ```php
110 --8<-- "tutorial/tutorial-series/part-1/files/lib/data/person/PersonAction.class.php"
111 ```
112
113 This implementation of `AbstractDatabaseObjectAction` is very basic and only sets the `$permissionsDelete` and `$requireACP` properties.
114 This is done so that later on, when implementing the people list for the ACP, we can delete people simply via AJAX.
115 `$permissionsDelete` has to be set to the permission needed in order to delete a person.
116 We will later use the [userGroupOption package installation plugin](../../package/pip/user-group-option.md) to create the `admin.content.canManagePeople` permission.
117 `$requireACP` restricts deletion of people to the ACP.
118
119 #### `PersonEditor`
120
121 ```php
122 --8<-- "tutorial/tutorial-series/part-1/files/lib/data/person/PersonEditor.class.php"
123 ```
124
125 This implementation of `DatabaseObjectEditor` fulfills the minimum requirement for a database object editor:
126 setting the static `$baseClass` property to the database object class name.
127
128 #### `PersonList`
129
130 ```php
131 --8<-- "tutorial/tutorial-series/part-1/files/lib/data/person/PersonList.class.php"
132 ```
133
134 Due to the default implementation of `DatabaseObjectList`, our `PersonList` class just needs to extend it and everything else is either automatically set by the code of `DatabaseObjectList` or, in the case of properties and methods, provided by that class.
135
136
137 ## ACP
138
139 Next, we will take care of the controllers and views for the ACP.
140 In total, we need three each:
141
142 1. page to list people,
143 1. form to add people, and
144 1. form to edit people.
145
146 Before we create the controllers and views, let us first create the menu items for the pages in the ACP menu.
147
148 ### ACP Menu
149
150 We need to create three menu items:
151
152 1. a “parent” menu item on the second level of the ACP menu item tree,
153 1. a third level menu item for the people list page, and
154 1. a fourth level menu item for the form to add new people.
155
156 ```xml
157 --8<-- "tutorial/tutorial-series/part-1/acpMenu.xml"
158 ```
159
160 We choose `wcf.acp.menu.link.content` as the parent menu item for the first menu item `wcf.acp.menu.link.person` because the people we are managing is just one form of content.
161 The fourth level menu item `wcf.acp.menu.link.person.add` will only be shown as an icon and thus needs an additional element `icon` which takes a FontAwesome icon class as value.
162
163 ### People List
164
165 To list the people in the ACP, we need a `PersonListPage` class and a `personList` template.
166
167 #### `PersonListPage`
168
169 ```php
170 --8<-- "tutorial/tutorial-series/part-1/files/lib/acp/page/PersonListPage.class.php"
171 ```
172
173 As WoltLab Suite Core already provides a powerful default implementation of a sortable page, our work here is minimal:
174
175 1. We need to set the active ACP menu item via the `$activeMenuItem`.
176 1. `$neededPermissions` contains a list of permissions of which the user needs to have at least one in order to see the person list.
177 We use the same permission for both the menu item and the page.
178 1. The database object list class whose name is provided via `$objectListClassName` and that handles fetching the people from database is the `PersonList` class, which we have already created.
179 1. To validate the sort field passed with the request, we set `$validSortFields` to the available database table columns.
180
181 #### `personList.tpl`
182
183 ```smarty
184 --8<-- "tutorial/tutorial-series/part-1/acptemplates/personList.tpl"
185 ```
186
187 We will go piece by piece through the template code:
188
189 1. We include the `header` template and set the page title `wcf.acp.person.list`.
190 You have to include this template for every page!
191 1. We set the content header and additional provide a button to create a new person in the content header navigation.
192 1. As not all people are listed on the same page if many people have been created, we need a pagination for which we use the `pages` template plugin.
193 The `{hascontent}{content}{/content}{/hascontent}` construct ensures the `.paginationTop` element is only shown if the `pages` template plugin has a return value, thus if a pagination is necessary.
194 1. Now comes the main part of the page, the list of the people, which will only be displayed if any people exist.
195 Otherwise, an info box is displayed using the generic `wcf.global.noItems` language item.
196 The `$objects` template variable is automatically assigned by `wcf\page\MultipleLinkPage` and contains the `PersonList` object used to read the people from database.
197 The table itself consists of a `thead` and a `tbody` element and is extendable with more columns using the template events `columnHeads` and `columns`.
198 In general, every table should provide these events.
199 The default structure of a table is used here so that the first column of the content rows contains icons to edit and to delete the row (and provides another standard event `rowButtons`) and that the second column contains the ID of the person.
200 The table can be sorted by clicking on the head of each column.
201 The used variables `$sortField` and `$sortOrder` are automatically assigned to the template by `SortablePage`.
202 1. The `.contentFooter` element is only shown if people exist as it basically repeats the `.contentHeaderNavigation` and `.paginationTop` element.
203 1. The delete button for each person shown in the `.columnIcon` element relies on the global [`WoltLabSuite/Core/Ui/Object/Action`](../../migration/wsc53/javascript.md#wcfactiondelete-and-wcfactiontoggle) module which only requires the `jsObjectActionContainer` CSS class in combination with the `data-object-action-class-name` attribute for the `table` element, the `jsObjectActionObject` CSS class for each person's `tr` element in combination with the `data-object-id` attribute, and lastly the delete button itself, which is created with the [`objectAction` template plugin](../../view/template-plugins.md#view/template-plugins/#54-objectaction).
204 1. The [`.jsReloadPageWhenEmpty` CSS class](../../migration/wsc53/javascript.md#wcftableemptytablehandler) on the `tbody` element ensures that once all persons on the page have been deleted, the page is reloaded.
205 1. Lastly, the `footer` template is included that terminates the page.
206 You also have to include this template for every page!
207
208 Now, we have finished the page to manage the people so that we can move on to the forms with which we actually create and edit the people.
209
210 ### Person Add Form
211
212 Like the person list, the form to add new people requires a controller class and a template.
213
214 #### `PersonAddForm`
215
216 ```php
217 --8<-- "tutorial/tutorial-series/part-1/files/lib/acp/form/PersonAddForm.class.php"
218 ```
219
220 The properties here consist of three types:
221 the “housekeeping” properties `$activeMenuItem` and `$neededPermissions`, which fulfill the same roles as for `PersonListPage`, and the [`$objectEditLinkController` property](../../migration/wsc52/php.md#addform), which is used to generate a link to edit the newly created person after submitting the form, and finally `$formAction` and `$objectActionClass` required by the [PHP form builder API](../../php/api/form_builder/overview.md) used to generate the form.
222
223 Because of using form builder, we only have to set up the two form fields for entering the first and last name, respectively:
224
225 1. Each field is a simple single-line text field, thus we use [`TextFormField`](../../php/api/form_builder/form_fields.md#textformfield).
226 1. The parameter of the `create()` method expects the id of the field/name of the database object property, which is `firstName` and `lastName`, respectively, here.
227 1. The language item of the label shown in the ouput above the input field is set via the `label()` method.
228 1. As both fields have to be filled out, `required()` is called, and the maximum length is set via `maximumLength()`.
229 1. Lastly, to make it easier to fill out the form more quickly, the first field is auto-focused by calling `autoFocus()`.
230
231 #### `personAdd.tpl`
232
233 ```smarty
234 --8<-- "tutorial/tutorial-series/part-1/acptemplates/personAdd.tpl"
235 ```
236
237 We will now only concentrate on the new parts compared to `personList.tpl`:
238
239 1. We use the `$action` variable to distinguish between the languages items used for adding a person and for creating a person.
240 1. Because of form builder, we only have to call `{@$form->getHtml()}` to generate all relevant output for the form.
241
242 ### Person Edit Form
243
244 As mentioned before, for the form to edit existing people, we only need a new controller as the template has already been implemented in a way that it handles both, adding and editing.
245
246 #### `PersonEditForm`
247
248 ```php
249 --8<-- "tutorial/tutorial-series/part-1/files/lib/acp/form/PersonEditForm.class.php"
250 ```
251
252 In general, edit forms extend the associated add form so that the code to read and to validate the input data is simply inherited.
253
254 After setting a different active menu item, we have to change the value of `$formAction` because this form, in contrast to `PersonAddForm`, does not create but update existing persons.
255
256 As we rely on form builder, the only thing necessary in this controller is to read and validate the edit object, i.e. the edited person, which is done in `readParameters()`.
257
258
259 ## Frontend
260
261 For the front end, that means the part with which the visitors of a website interact, we want to implement a simple sortable page that lists the people.
262 This page should also be directly linked in the main menu.
263
264 ### `page.xml`
265
266 First, let us register the page with the system because every front end page or form needs to be explicitly registered using the [page package installation plugin](../../package/pip/page.md):
267
268 ```xml
269 --8<-- "tutorial/tutorial-series/part-1/page.xml"
270 ```
271
272 For more information about what each of the elements means, please refer to the [page package installation plugin page](../../package/pip/page.md).
273
274 ### `menuItem.xml`
275
276 Next, we register the menu item using the [menuItem package installation plugin](../../package/pip/menu-item.md):
277
278 ```xml
279 --8<-- "tutorial/tutorial-series/part-1/menuItem.xml"
280 ```
281
282 Here, the import parts are that we register the menu item for the main menu `com.woltlab.wcf.MainMenu` and link the menu item with the page `com.woltlab.wcf.people.PersonList`, which we just registered.
283
284 ### People List
285
286 As in the ACP, we need a controller and a template.
287 You might notice that both the controller’s (unqualified) class name and the template name are the same for the ACP and the front end.
288 This is no problem because the qualified names of the classes differ and the files are stored in different directories and because the templates are installed by different package installation plugins and are also stored in different directories.
289
290 #### `PersonListPage`
291
292 ```php
293 --8<-- "tutorial/tutorial-series/part-1/files/lib/page/PersonListPage.class.php"
294 ```
295
296 This class is almost identical to the ACP version.
297 In the front end, we do not need to set the active menu item manually because the system determines the active menu item automatically based on the requested page.
298 Furthermore, `$neededPermissions` has not been set because in the front end, users do not need any special permission to access the page.
299 In the front end, we explicitly set the `$defaultSortField` so that the people listed on the page are sorted by their last name (in ascending order) by default.
300
301 #### `personList.tpl`
302
303 ```smarty
304 --8<-- "tutorial/tutorial-series/part-1/templates/personList.tpl"
305 ```
306
307 If you compare this template to the one used in the ACP, you will recognize similar elements like the `.paginationTop` element, the `p.info` element if no people exist, and the `.contentFooter` element.
308 Furthermore, we include a template called `header` before actually showing any of the page contents and terminate the template by including the `footer` template.
309
310 Now, let us take a closer look at the differences:
311
312 - We do not explicitly create a `.contentHeader` element but simply assign the title to the `contentTitle` variable.
313 The value of the assignment is simply the title of the page and a badge showing the number of listed people.
314 The `header` template that we include later will handle correctly displaying the content header on its own based on the `$contentTitle` variable.
315 - Next, we create additional element for the HTML document’s `<head>` element.
316 In this case, we define the [canonical link of the page](https://en.wikipedia.org/wiki/Canonical_link_element) and, because we are showing paginated content, add links to the previous and next page (if they exist).
317 - We want the page to be sortable but as we will not be using a table for listing the people like in the ACP, we are not able to place links to sort the people into the table head.
318 Instead, usually a box is created in the sidebar on the right-hand side that contains `select` elements to determine sort field and sort order.
319 - The main part of the page is the listing of the people.
320 We use a structure similar to the one used for displaying registered users.
321 Here, for each person, we simply display a FontAwesome icon representing a person and show the person’s full name relying on `Person::__toString()`.
322 Additionally, like in the user list, we provide the initially empty `ul.inlineList.commaSeparated` and `dl.plain.inlineDataList.small` elements that can be filled by plugins using the templates events.
323
324
325 ## `userGroupOption.xml`
326
327 We have already used the `admin.content.canManagePeople` permissions several times, now we need to install it using the [userGroupOption package installation plugin](../../package/pip/user-group-option.md):
328
329 ```xml
330 --8<-- "tutorial/tutorial-series/part-1/userGroupOption.xml"
331 ```
332
333 We use the existing `admin.content` user group option category for the permission as the people are “content” (similar the the ACP menu item).
334 As the permission is for administrators only, we set `defaultvalue` to `0` and `admindefaultvalue` to `1`.
335 This permission is only relevant for registered users so that it should not be visible when editing the guest user group.
336 This is achieved by setting `usersonly` to `1`.
337
338
339 ## `package.xml`
340
341 Lastly, we need to create the `package.xml` file.
342 For more information about this kind of file, please refer to [the `package.xml` page](../../package/package-xml.md).
343
344 ```xml
345 --8<-- "tutorial/tutorial-series/part-1/package.xml"
346 ```
347
348 As this is a package for WoltLab Suite Core 3, we need to require it using `<requiredpackage>`.
349 We require the latest version (when writing this tutorial) `5.4.0 Alpha 1`.
350 Additionally, we disallow installation of the package in the next major version `6.0` by excluding the `6.0.0 Alpha 1` version.
351
352 The most important part are to installation instructions.
353 First, we install the ACP templates, files and templates, create the database table and import the language item.
354 Afterwards, the ACP menu items and the permission are added.
355 Now comes the part of the instructions where the order of the instructions is crucial:
356 In `menuItem.xml`, we refer to the `com.woltlab.wcf.people.PersonList` page that is delivered by `page.xml`.
357 As the menu item package installation plugin validates the given page and throws an exception if the page does not exist, we need to install the page before the menu item!
358
359 ---
360
361 This concludes the first part of our tutorial series after which you now have a working simple package with which you can manage people in the ACP and show the visitors of your website a simple list of all created people in the front end.
362
363 The complete source code of this part can be found on [GitHub]({jinja{ repo_url }}tree/{jinja{ edit_uri.split("/")[1] }}/snippets/tutorial/tutorial-series/part-1).