Migrate callout code
[GitHub/WoltLab/woltlab.github.io.git] / docs / tutorial_tutorial-series_part-2-event-listeners-and-template-listeners.md
1 ---
2 title: "Part 2: Event Listeners and Template Listeners"
3 sidebar: sidebar
4 permalink: tutorial_tutorial-series_part-2-event-listeners-and-template-listeners.html
5 folder: tutorial/tutorial-series
6 parent: tutorial_tutorial-series
7 ---
8
9 In the [first part](tutorial_tutorial-series_part-1-base-structure.md) of this tutorial series, we have created the base structure of our people management package.
10 In further parts, we will use the package of the first part as a basis to directly add new features.
11 In order to explain how event listeners and template works, however, we will not directly adding a new feature to the package by altering it in this part, but we will assume that somebody else created the package and that we want to extend it the “correct” way by creating a plugin.
12
13 The goal of the small plugin that will be created in this part is to add the birthday of the managed people.
14 As in the first part, we will not bother with careful validation of the entered date but just make sure that it is a valid date.
15
16
17 ## Package Functionality
18
19 The package should provide the following possibilities/functions:
20
21 - List person’s birthday (if set) in people list in the ACP
22 - Sort people list by birthday in the ACP
23 - Add or remove birthday when adding or editing person
24 - List person’s birthday (if set) in people list in the front end
25 - Sort people list by birthday in the front end
26
27
28 ## Used Components
29
30 We will use the following package installation plugins:
31
32 - [acpTemplate package installation plugin](package_pip_acp-template.md),
33 - [eventListener package installation plugin](package_pip_event-listener.md),
34 - [file package installation plugin](package_pip_file.md),
35 - [language package installation plugin](package_pip_language.md),
36 - [sql package installation plugin](package_pip_sql.md),
37 - [template package installation plugin](package_pip_template.md),
38 - [templateListener package installation plugin](package_pip_template-listener.md).
39
40 For more information about the event system, please refer to the [dedicated page on events](php_api_events.md).
41
42
43 ## Package Structure
44
45 The package will have the following file structure:
46
47 ```
48 ├── acptemplates
49 │   └── __personAddBirthday.tpl
50 ├── eventListener.xml
51 ├── files
52 │   └── lib
53 │   └── system
54 │   └── event
55 │   └── listener
56 │   ├── BirthdayPersonAddFormListener.class.php
57 │   └── BirthdaySortFieldPersonListPageListener.class.php
58 ├── install.sql
59 ├── language
60 │   ├── de.xml
61 │   └── en.xml
62 ├── package.xml
63 ├── templateListener.xml
64 └── templates
65 ├── __personListBirthday.tpl
66 └── __personListBirthdaySortField.tpl
67 ```
68
69
70 ## Extending Person Model (`install.sql`)
71
72 The existing model of a person only contains the person’s first name and their last name (in additional to the id used to identify created people).
73 To add the birthday to the model, we need to create an additional database table column using the [sql package installation plugin](package_pip_sql.md):
74
75 {% highlight sql %}
76 {% include tutorial/tutorial-series/part-2/install.sql %}
77 {% endhighlight %}
78
79 If we have a [Person object](tutorial_tutorial-series_part-1-base-structure.md#person), this new property can be accessed the same way as the `personID` property, the `firstName` property, or the `lastName` property from the base package: `$person->birthday`.
80
81
82 ## Setting Birthday in ACP
83
84 To set the birthday of a person, we need to extend the `personAdd` template to add an additional birthday field.
85 This can be achieved using the `dataFields` template event at whose position we inject the following template code:
86
87 {% highlight sql %}
88 {% include tutorial/tutorial-series/part-2/acptemplates/__personAddBirthday.tpl %}
89 {% endhighlight %}
90
91 which we store in a `__personAddBirthday.tpl` template file.
92 The used language item `wcf.person.birthday` is actually the only new one for this package:
93
94 {% highlight sql %}
95 {% include tutorial/tutorial-series/part-2/language/de.xml %}
96 {% endhighlight %}
97
98 {% highlight sql %}
99 {% include tutorial/tutorial-series/part-2/language/en.xml %}
100 {% endhighlight %}
101
102 The template listener needs to be registered using the [templateListener package installation plugin](package_pip_template-listener.md).
103 The corresponding complete `templateListener.xml` file is included [below](#templatelistenerxml).
104
105 The template code alone is not sufficient because the `birthday` field is, at the moment, neither read, nor processed, nor saved by any PHP code.
106 This can be be achieved, however, by adding event listeners to `PersonAddForm` and `PersonEditForm` which allow us to execute further code at specific location of the program.
107 Before we take a look at the event listener code, we need to identify exactly which additional steps we need to undertake:
108
109 1. If a person is edited and the form has not been submitted, the existing birthday of that person needs to be read.
110 1. If a person is added or edited and the form has been submitted, the new birthday value needs to be read.
111 1. If a person is added or edited and the form has been submitted, the new birthday value needs to be validated.
112 1. If a person is added or edited and the new birthday value has been successfully validated, the new birthday value needs to be saved.
113 1. If a person is added and the new birthday value has been successfully saved, the internally stored birthday needs to be reset so that the birthday field is empty when the form is shown again.
114 1. The internally stored birthday value needs to be assigned to the template.
115
116 The following event listeners achieves these requirements:
117
118 {% highlight php %}
119 {% include tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdayPersonAddFormListener.class.php %}
120 {% endhighlight %}
121
122 Some notes on the code:
123
124 - We are inheriting from `AbstractEventListener`, instead of just implementing the `IParameterizedEventListener` interface.
125 The `execute()` method of `AbstractEventListener` contains a dispatcher that automatically calls methods called `on` followed by the event name with the first character uppercased, passing the event object and the `$parameters` array.
126 This simple pattern results in the event `foo` being forwarded to the method `onFoo($eventObj, $parameters)`.
127 - The `birthday` column has a default value of `0000-00-00`, which we interpret as “birthday not set”.
128 To show an empty input field in this case, we empty the `birthday` property after reading such a value in `readData()`.
129 - The validation of the date is, as mentioned before, very basic and just checks the form of the string and uses PHP’s [checkdate](https://secure.php.net/manual/en/function.checkdate.php) function to validate the components.
130 - The `save` needs to make sure that the passed date is actually a valid date and set it to `0000-00-00` if no birthday is given.
131 To actually save the birthday in the database, we do not directly manipulate the database but can add an additional field to the data array passed to `PersonAction::create()` via `AbstractForm::$additionalFields`.
132 As the `save` event is the last event fired before the actual save process happens, this is the perfect event to set this array element.
133
134 The event listeners are installed using the `eventListener.xml` file shown [below](#eventlistenerxml).
135
136
137 ## Adding Birthday Table Column in ACP
138
139 To add a birthday column to the person list page in the ACP, we need three parts:
140
141 1. an event listener that makes the `birthday` database table column a valid sort field,
142 1. a template listener that adds the birthday column to the table’s head, and
143 1. a template listener that adds the birthday column to the table’s rows.
144
145 The first part is a very simple class:
146
147 {% highlight php %}
148 {% include tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdaySortFieldPersonListPageListener.class.php %}
149 {% endhighlight %}
150
151 !!! info "We use `SortablePage` as a type hint instead of `wcf\acp\page\PersonListPage` because we will be using the same event listener class in the front end to also allow sorting that list by birthday."
152
153 As the relevant template codes are only one line each, we will simply put them directly in the `templateListener.xml` file that will be shown [later on](#templatelistenerxml).
154 The code for the table head is similar to the other `th` elements:
155
156 ```smarty
157 <th class="columnDate columnBirthday{if $sortField == 'birthday'} active {@$sortOrder}{/if}"><a href="{link controller='PersonList'}pageNo={@$pageNo}&sortField=birthday&sortOrder={if $sortField == 'birthday' && $sortOrder == 'ASC'}DESC{else}ASC{/if}{/link}">{lang}wcf.person.birthday{/lang}</a></th>
158 ```
159
160 For the table body’s column, we need to make sure that the birthday is only show if it is actually set:
161
162 ```smarty
163 <td class="columnDate columnBirthday">{if $person->birthday !== '0000-00-00'}{@$person->birthday|strtotime|date}{/if}</td>
164 ```
165
166
167 ## Adding Birthday in Front End
168
169 In the front end, we also want to make the list sortable by birthday and show the birthday as part of each person’s “statistics”.
170
171 To add the birthday as a valid sort field, we use `BirthdaySortFieldPersonListPageListener` just as in the ACP.
172 In the front end, we will now use a template (`__personListBirthdaySortField.tpl`) instead of a directly putting the template code in the `templateListener.xml` file:
173
174 {% highlight smarty %}
175 {% include tutorial/tutorial-series/part-2/templates/__personListBirthdaySortField.tpl %}
176 {% endhighlight %}
177
178 !!! info "You might have noticed the two underscores at the beginning of the template file. For templates that are included via template listeners, this is the naming convention we use."
179
180 Putting the template code into a file has the advantage that in the administrator is able to edit the code directly via a custom template group, even though in this case this might not be very probable.
181
182 To show the birthday, we use the following template code for the `personStatistics` template event, which again makes sure that the birthday is only shown if it is actually set:
183
184 {% highlight smarty %}
185 {% include tutorial/tutorial-series/part-2/templates/__personListBirthday.tpl %}
186 {% endhighlight %}
187
188
189 ## `templateListener.xml`
190
191 The following code shows the `templateListener.xml` file used to install all mentioned template listeners:
192
193 {% highlight xml %}
194 {% include tutorial/tutorial-series/part-2/templateListener.xml %}
195 {% endhighlight %}
196
197 In cases where a template is used, we simply use the `include` syntax to load the template.
198
199
200 ## `eventListener.xml`
201
202 There are two event listeners, `birthdaySortFieldAdminPersonList` and `birthdaySortFieldPersonList`, that make `birthday` a valid sort field in the ACP and the front end, respectively, and the rest takes care of setting the birthday.
203 The event listener `birthdayPersonAddFormInherited` takes care of the events that are relevant for both adding and editing people, thus it listens to the `PersonAddForm` class but has `inherit` set to `1` so that it also listens to the events of the `PersonEditForm` class.
204 In contrast, reading the existing birthday from a person is only relevant for editing so that the event listener `birthdayPersonEditForm` only listens to that class.
205
206 {% highlight xml %}
207 {% include tutorial/tutorial-series/part-2/eventListener.xml %}
208 {% endhighlight %}
209
210
211 ## `package.xml`
212
213 The only relevant difference between the `package.xml` file of the base page from part 1 and the `package.xml` file of this package is that this package requires the base package `com.woltlab.wcf.people` (see `<requiredpackages>`):
214
215 {% highlight xml %}
216 {% include tutorial/tutorial-series/part-2/package.xml %}
217 {% endhighlight %}
218
219 ---
220
221 This concludes the second part of our tutorial series after which you now have extended the base package using event listeners and template listeners that allow you to enter the birthday of the people.
222
223 The complete source code of this part can be found on [GitHub](https://github.com/WoltLab/woltlab.github.io/tree/master/_includes/tutorial/tutorial-series/part-2).