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.
As the first step, we have to model the people we want to manage with this package.
As this is only an introductory tutorial, we will keep things simple and only consider the first and last name of a person.
Thus, the database table we will store the people in only contains three columns:
personID is the unique numeric identifier of each person created,
firstName contains the first name of the person,
lastName contains the last name of the person.
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:
<?phpnamespacewcf\data\person;usewcf\data\DatabaseObject;usewcf\system\request\IRouteController;/** * Represents a person. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Data\Person * * @property-read int $personID unique id of the person * @property-read string $firstName first name of the person * @property-read string $lastName last name of the person */classPersonextendsDatabaseObjectimplementsIRouteController{/** * Returns the first and last name of the person if a person object is treated as a string. * * @return string */publicfunction__toString(){return$this->getTitle();}/** * @inheritDoc */publicfunctiongetTitle(){return$this->firstName.' '.$this->lastName;}}
The important thing here is that Person extends DatabaseObject.
Additionally, we implement the IRouteController interface, which allows us to use Person objects to create links, and we implement PHP's magic __toString() method for convenience.
For every database object, you need to implement three additional classes:
an action class, an editor class and a list class.
This implementation of AbstractDatabaseObjectAction is very basic and only sets the $permissionsDelete and $requireACP properties.
This is done so that later on, when implementing the people list for the ACP, we can delete people simply via AJAX.
$permissionsDelete has to be set to the permission needed in order to delete a person.
We will later use the userGroupOption package installation plugin to create the admin.content.canManagePeople permission.
$requireACP restricts deletion of people to the ACP.
<?phpnamespacewcf\data\person;usewcf\data\DatabaseObjectEditor;/** * Provides functions to edit people. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Data\Person * * @method static Person create(array $parameters = []) * @method Person getDecoratedObject() * @mixin Person */classPersonEditorextendsDatabaseObjectEditor{/** * @inheritDoc */protectedstatic$baseClass=Person::class;}
This implementation of DatabaseObjectEditor fulfills the minimum requirement for a database object editor:
setting the static $baseClass property to the database object class name.
<?phpnamespacewcf\data\person;usewcf\data\DatabaseObjectList;/** * Represents a list of people. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Data\Person * * @method Person current() * @method Person[] getObjects() * @method Person|null search($objectID) * @property Person[] $objects */classPersonListextendsDatabaseObjectList{}
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.
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.
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.
<?phpnamespacewcf\acp\page;usewcf\data\person\PersonList;usewcf\page\SortablePage;/** * Shows the list of people. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Page */classPersonListPageextendsSortablePage{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person.list';/** * @inheritDoc */public$neededPermissions=['admin.content.canManagePeople'];/** * @inheritDoc */public$objectListClassName=PersonList::class;/** * @inheritDoc */public$validSortFields=['personID','firstName','lastName'];}
As WoltLab Suite Core already provides a powerful default implementation of a sortable page, our work here is minimal:
We need to set the active ACP menu item via the $activeMenuItem.
$neededPermissions contains a list of permissions of which the user needs to have at least one in order to see the person list.
We use the same permission for both the menu item and the page.
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.
To validate the sort field passed with the request, we set $validSortFields to the available database table columns.
We will go piece by piece through the template code:
We include the header template and set the page title wcf.acp.person.list.
You have to include this template for every page!
We set the content header and additional provide a button to create a new person in the content header navigation.
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.
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.
Now comes the main part of the page, the list of the people, which will only be displayed if any people exist.
Otherwise, an info box is displayed using the generic wcf.global.noItems language item.
The $objects template variable is automatically assigned by wcf\page\MultipleLinkPage and contains the PersonList object used to read the people from database.
The table itself consists of a thead and a tbody element and is extendable with more columns using the template events columnHeads and columns.
In general, every table should provide these events.
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.
The table can be sorted by clicking on the head of each column.
The used variables $sortField and $sortOrder are automatically assigned to the template by SortablePage.
The .contentFooter element is only shown if people exist as it basically repeats the .contentHeaderNavigation and .paginationTop element.
The delete button for each person shown in the .columnIcon element relies on the global WoltLabSuite/Core/Ui/Object/Action 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.
The .jsReloadPageWhenEmpty CSS class on the tbody element ensures that once all persons on the page have been deleted, the page is reloaded.
Lastly, the footer template is included that terminates the page.
You also have to include this template for every page!
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.
<?phpnamespacewcf\acp\form;usewcf\data\person\PersonAction;usewcf\form\AbstractFormBuilderForm;usewcf\system\form\builder\container\FormContainer;usewcf\system\form\builder\field\TextFormField;/** * Shows the form to create a new person. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Form */classPersonAddFormextendsAbstractFormBuilderForm{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person.add';/** * @inheritDoc */public$formAction='create';/** * @inheritDoc */public$neededPermissions=['admin.content.canManagePeople'];/** * @inheritDoc */public$objectActionClass=PersonAction::class;/** * @inheritDoc */public$objectEditLinkController=PersonEditForm::class;/** * @inheritDoc */protectedfunctioncreateForm(){parent::createForm();$this->form->appendChild(FormContainer::create('data')->label('wcf.global.form.data')->appendChildren([TextFormField::create('firstName')->label('wcf.person.firstName')->required()->autoFocus()->maximumLength(255),TextFormField::create('lastName')->label('wcf.person.lastName')->required()->maximumLength(255),]));}}
The properties here consist of three types:
the “housekeeping” properties $activeMenuItem and $neededPermissions, which fulfill the same roles as for PersonListPage, and the $objectEditLinkController property, 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 used to generate the form.
Because of using form builder, we only have to set up the two form fields for entering the first and last name, respectively:
Each field is a simple single-line text field, thus we use TextFormField.
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.
The language item of the label shown in the ouput above the input field is set via the label() method.
As both fields have to be filled out, required() is called, and the maximum length is set via maximumLength().
Lastly, to make it easier to fill out the form more quickly, the first field is auto-focused by calling autoFocus().
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.
<?phpnamespacewcf\acp\form;usewcf\data\person\Person;usewcf\system\exception\IllegalLinkException;/** * Shows the form to edit an existing person. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Acp\Form */classPersonEditFormextendsPersonAddForm{/** * @inheritDoc */public$activeMenuItem='wcf.acp.menu.link.person';/** * @inheritDoc */public$formAction='update';/** * @inheritDoc */publicfunctionreadParameters(){parent::readParameters();if(isset($_REQUEST['id'])){$this->formObject=newPerson($_REQUEST['id']);if(!$this->formObject->getObjectID()){thrownewIllegalLinkException();}}}}
In general, edit forms extend the associated add form so that the code to read and to validate the input data is simply inherited.
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.
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().
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.
This page should also be directly linked in the main menu.
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:
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.
As in the ACP, we need a controller and a template.
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.
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.
<?phpnamespacewcf\page;usewcf\data\person\PersonList;/** * Shows the list of people. * * @author Matthias Schmidt * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> * @package WoltLabSuite\Core\Page */classPersonListPageextendsSortablePage{/** * @inheritDoc */public$defaultSortField='lastName';/** * @inheritDoc */public$objectListClassName=PersonList::class;/** * @inheritDoc */public$validSortFields=['personID','firstName','lastName'];}
This class is almost identical to the ACP version.
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.
Furthermore, $neededPermissions has not been set because in the front end, users do not need any special permission to access the page.
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.
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.
Furthermore, we include a template called header before actually showing any of the page contents and terminate the template by including the footer template.
Now, let us take a closer look at the differences:
We do not explicitly create a .contentHeader element but simply assign the title to the contentTitle variable.
The value of the assignment is simply the title of the page and a badge showing the number of listed people.
The header template that we include later will handle correctly displaying the content header on its own based on the $contentTitle variable.
Next, we create additional element for the HTML document’s <head> element.
In this case, we define the canonical link of the page and, because we are showing paginated content, add links to the previous and next page (if they exist).
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.
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.
The main part of the page is the listing of the people.
We use a structure similar to the one used for displaying registered users.
Here, for each person, we simply display a FontAwesome icon representing a person and show the person’s full name relying on Person::__toString().
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.
We use the existing admin.content user group option category for the permission as the people are “content” (similar the the ACP menu item).
As the permission is for administrators only, we set defaultvalue to 0 and admindefaultvalue to 1.
This permission is only relevant for registered users so that it should not be visible when editing the guest user group.
This is achieved by setting usersonly to 1.
As this is a package for WoltLab Suite Core 3, we need to require it using <requiredpackage>.
We require the latest version (when writing this tutorial) 5.4.0 Alpha 1.
Additionally, we disallow installation of the package in the next major version 6.0 by excluding the 6.0.0 Alpha 1 version.
The most important part are to installation instructions.
First, we install the ACP templates, files and templates, create the database table and import the language item.
Afterwards, the ACP menu items and the permission are added.
Now comes the part of the instructions where the order of the instructions is crucial:
In menuItem.xml, we refer to the com.woltlab.wcf.people.PersonList page that is delivered by page.xml.
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!
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.
The complete source code of this part can be found on GitHub.