2 title: "Part 2: Event Listeners and Template Listeners"
4 permalink: tutorial_tutorial-series_part-2-event-listeners-and-template-listeners.html
5 folder: tutorial/tutorial-series
6 parent: tutorial_tutorial-series
9 In the [first part](tutorial_tutorial-series_part-1-base-structure.html) 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.
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.
17 ## Package Functionality
19 The package should provide the following possibilities/functions:
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
30 We will use the following package installation plugins:
32 - [acpTemplate package installation plugin](package_pip_acp-template.html),
33 - [eventListener package installation plugin](package_pip_event-listener.html),
34 - [file package installation plugin](package_pip_file.html),
35 - [language package installation plugin](package_pip_language.html),
36 - [sql package installation plugin](package_pip_sql.html),
37 - [template package installation plugin](package_pip_template.html),
38 - [templateListener package installation plugin](package_pip_template-listener.html).
40 For more information about the event system, please refer to the [dedicated page on events](php_api_events.html).
45 The package will have the following file structure:
49 │ └── __personAddBirthday.tpl
56 │ ├── BirthdayPersonAddFormListener.class.php
57 │ └── BirthdaySortFieldPersonListPageListener.class.php
63 ├── templateListener.xml
65 ├── __personListBirthday.tpl
66 └── __personListBirthdaySortField.tpl
70 ## Extending Person Model (`install.sql`)
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.html):
76 {% include tutorial/tutorial-series/part-2/install.sql %}
79 If we have a [Person object](tutorial_tutorial-series_part-1-base-structure.html#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`.
82 ## Setting Birthday in ACP
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:
88 {% include tutorial/tutorial-series/part-2/acptemplates/__personAddBirthday.tpl %}
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:
95 {% include tutorial/tutorial-series/part-2/language/de.xml %}
99 {% include tutorial/tutorial-series/part-2/language/en.xml %}
102 The template listener needs to be registered using the [templateListener package installation plugin](package_pip_template-listener.html).
103 The corresponding complete `templateListener.xml` file is included [below](#templatelistenerxml).
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:
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.
116 The following event listeners achieves these requirements:
119 {% include tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdayPersonAddFormListener.class.php %}
122 Some notes on the code:
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.
134 The event listeners are installed using the `eventListener.xml` file shown [below](#eventlistenerxml).
137 ## Adding Birthday Table Column in ACP
139 To add a birthday column to the person list page in the ACP, we need three parts:
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.
145 The first part is a very simple class:
148 {% include tutorial/tutorial-series/part-2/files/lib/system/event/listener/BirthdaySortFieldPersonListPageListener.class.php %}
151 {% include callout.html content="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." type="info" %}
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:
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>
160 For the table body’s column, we need to make sure that the birthday is only show if it is actually set:
163 <td class="columnDate columnBirthday">{if $person->birthday !== '0000-00-00'}{@$person->birthday|strtotime|date}{/if}</td>
167 ## Adding Birthday in Front End
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”.
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:
174 {% highlight smarty %}
175 {% include tutorial/tutorial-series/part-2/templates/__personListBirthdaySortField.tpl %}
178 {% include callout.html content="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." type="info" %}
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.
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:
184 {% highlight smarty %}
185 {% include tutorial/tutorial-series/part-2/templates/__personListBirthday.tpl %}
189 ## `templateListener.xml`
191 The following code shows the `templateListener.xml` file used to install all mentioned template listeners:
194 {% include tutorial/tutorial-series/part-2/templateListener.xml %}
197 In cases where a template is used, we simply use the `include` syntax to load the template.
200 ## `eventListener.xml`
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.
207 {% include tutorial/tutorial-series/part-2/eventListener.xml %}
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>`):
216 {% include tutorial/tutorial-series/part-2/package.xml %}
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.
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).