Skip to content

Part 4: Box and Box Conditions#

In this part of our tutorial series, we add support for creating boxes listing people.

Package Functionality#

In addition to the existing functions from part 3, the package will provide the following functionality after this part of the tutorial:

  • Creating boxes dynamically listing people
  • Filtering the people listed in boxes using conditions

Used Components#

In addition to the components used in previous parts, we will use the objectTypeDefinition package installation plugin and use the box and condition APIs.

To pre-install a specific person list box, we refer to the documentation of the box package installation plugin.

Package Structure#

The complete package will have the following file structure (excluding unchanged files from part 3):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
├── files
│   └── lib
│       └── system
│           ├── box
│           │   └── PersonListBoxController.class.php
│           └── condition
│               └── person
│                   ├── PersonFirstNameTextPropertyCondition.class.php
│                   └── PersonLastNameTextPropertyCondition.class.php
├── language
│   ├── de.xml
│   └── en.xml
├── objectType.xml
├── objectTypeDefinition.xml
└── templates
    └── boxPersonList.tpl

For all changes, please refer to the source code on GitHub.

Box Controller#

In addition to static boxes with fixed contents, administrators are able to create dynamic boxes with contents from the database. In our case here, we want administrators to be able to create boxes listing people. 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:

1
2
3
4
5
<type>
    <name>com.woltlab.wcf.personList</name>
    <definitionname>com.woltlab.wcf.boxController</definitionname>
    <classname>wcf\system\box\PersonListBoxController</classname>
</type>

The com.woltlab.wcf.boxController object type definition requires the provided class to implement wcf\system\box\IBoxController:

files/lib/system/box/PersonListBoxController.class.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace wcf\system\box;

use wcf\data\person\PersonList;
use wcf\system\WCF;

/**
 * Dynamic box controller implementation for a list of persons.
 *
 * @author  Matthias Schmidt
 * @copyright   2001-2021 WoltLab GmbH
 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package WoltLabSuite\Core\System\Box
 */
class PersonListBoxController extends AbstractDatabaseObjectListBoxController
{
    /**
     * @inheritDoc
     */
    protected $conditionDefinition = 'com.woltlab.wcf.box.personList.condition';

    /**
     * @inheritDoc
     */
    public $defaultLimit = 5;

    /**
     * @inheritDoc
     */
    protected $sortFieldLanguageItemPrefix = 'wcf.person';

    /**
     * @inheritDoc
     */
    protected static $supportedPositions = [
        'sidebarLeft',
        'sidebarRight',
    ];

    /**
     * @inheritDoc
     */
    public $validSortFields = [
        'firstName',
        'lastName',
        'comments',
    ];

    /**
     * @inheritDoc
     */
    protected function getObjectList()
    {
        return new PersonList();
    }

    /**
     * @inheritDoc
     */
    protected function getTemplate()
    {
        return WCF::getTPL()->fetch('boxPersonList', 'wcf', [
            'boxPersonList' => $this->objectList,
            'boxSortField' => $this->sortField,
            'boxPosition' => $this->box->position,
        ], true);
    }
}

By extending AbstractDatabaseObjectListBoxController, we only have to provide minimal data ourself and rely mostly on the default implementation provided by AbstractDatabaseObjectListBoxController:

  1. As we will support conditions for the listed people, we have to set the relevant condition definition via $conditionDefinition.
  2. AbstractDatabaseObjectListBoxController already supports restricting the number of listed objects. To do so, you only have to specify the default number of listed objects via $defaultLimit.
  3. AbstractDatabaseObjectListBoxController also supports setting the sort order of the listed objects. 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.
  4. The box system supports different positions. Each box controller specifies the positions it supports via $supportedPositions. To keep the implementation simple here as different positions might require different output in the template, we restrict ourselves to sidebars.
  5. getObjectList() returns an instance of DatabaseObjectList that is used to read the listed objects. getObjectList() itself must not call readObjects(), as AbstractDatabaseObjectListBoxController takes care of calling the method after adding the conditions and setting the sort order.
  6. getTemplate() returns the contents of the box relying on the boxPersonList template here:
templates/boxPersonList.tpl
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<ul class="sidebarItemList">
    {foreach from=$boxPersonList item=boxPerson}
        <li class="box24">
            <span class="icon icon24 fa-user"></span>

            <div class="sidebarItemTitle">
                <h3>{anchor object=$boxPerson}</h3>
                {capture assign='__boxPersonDescription'}{lang __optional=true}wcf.person.boxList.description.{$boxSortField}{/lang}{/capture}
                {if $__boxPersonDescription}
                    <small>{@$__boxPersonDescription}</small>
                {/if}
            </div>
        </li>
    {/foreach}
</ul>

The template relies on a .sidebarItemList element, which is generally used for sidebar listings. (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().) 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}. We only add one such language item when sorting the people by comments: In such a case, the number of comments will be shown. (When sorting by first and last name, there are no additional useful information that could be shown here, though the plugin from part 2 adding support for birthdays might also show the birthday when sorting by first or last name.)

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.

Conditions#

The condition system can be used to generally filter a list of objects. In our case, the box system supports conditions to filter the objects shown in a specific box. 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. 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.

The first step for condition support is to register a object type definition for the relevant conditions requiring the IObjectListCondition interface:

objectTypeDefinition.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="http://www.woltlab.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.woltlab.com http://www.woltlab.com/XSD/5.4/objectTypeDefinition.xsd">
    <import>
        <definition>
            <name>com.woltlab.wcf.box.personList.condition</name>
            <interfacename>wcf\system\condition\IObjectListCondition</interfacename>
        </definition>
    </import>
</data>

Next, we register the specific conditions for filtering by the first and last name using this object type condition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<type>
   <name>com.woltlab.wcf.people.firstName</name>
   <definitionname>com.woltlab.wcf.box.personList.condition</definitionname>
   <classname>wcf\system\condition\person\PersonFirstNameTextPropertyCondition</classname>
</type>
<type>
   <name>com.woltlab.wcf.people.lastName</name>
   <definitionname>com.woltlab.wcf.box.personList.condition</definitionname>
   <classname>wcf\system\condition\person\PersonLastNameTextPropertyCondition</classname>
</type>

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:

files/lib/system/condition/person/PersonFirstNameTextPropertyCondition.class.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace wcf\system\condition\person;

use wcf\data\person\Person;
use wcf\system\condition\AbstractObjectTextPropertyCondition;

/**
 * Condition implementation for the first name of a person.
 *
 * @author  Matthias Schmidt
 * @copyright   2001-2021 WoltLab GmbH
 * @license WoltLab License <http://www.woltlab.com/license-agreement.html>
 * @package WoltLabSuite\Core\System\Condition
 */
class PersonFirstNameTextPropertyCondition extends AbstractObjectTextPropertyCondition
{
    /**
     * @inheritDoc
     */
    protected $className = Person::class;

    /**
     * @inheritDoc
     */
    protected $description = 'wcf.person.condition.firstName.description';

    /**
     * @inheritDoc
     */
    protected $fieldName = 'personFirstName';

    /**
     * @inheritDoc
     */
    protected $label = 'wcf.person.firstName';

    /**
     * @inheritDoc
     */
    protected $propertyName = 'firstName';

    /**
     * @inheritDoc
     */
    protected $supportsMultipleValues = true;
}
  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.
  2. 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.
  3. $description (optional), $fieldName, and $label are used in the output of the form field.

(The implementation here is specific for AbstractObjectTextPropertyCondition. The wcf\system\condition namespace also contains several other default condition implementations.)


Last update: 2021-05-03