aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/OptionsWidget.php
blob: 5f44c0c7aa0910e6e96bc365738067a0bccb7d84 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 */
class OptionsWidget extends ListWidget
{
    const INDEX = 'options';

    /**
     * @param string|null $title Optional alternative title
     */
    public function __construct(?string $title = null)
    {
        parent::__construct();

        $this->addCSSClass('widget-options');
        $this->title = $title ?: _('Einstellungen');
    }

    /**
     * @param string      $label
     * @param bool        $state
     * @param string      $toggle_url     Url to execute the action
     * @param string|null $toggle_url_off Optional alternative url to explicitely
     *                                    turn off the checkbox ($toggle_url will
     *                                    then act as $toggle_url_on)
     * @param Array       $attributes     Optional additional attributes for the anchor
     *
     * @return ButtonElement
     */
    public function addCheckbox(
        string $label,
        $state,
        string $toggle_url,
        ?string $toggle_url_off = null,
        array $attributes = []
    ): ButtonElement {
        // TODO: Remove this some versions after 5.0
        $toggle_url = html_entity_decode($toggle_url);
        $toggle_url_off = isset($toggle_url_off) ? html_entity_decode($toggle_url_off) : null;

        $attributes['class'] = trim(($attributes['class'] ?? '') . ' options-checkbox options-' . ($state ? 'checked' : 'unchecked'));

        return $this->addElement(
            new ButtonElement($label, null, $attributes + [
                'aria-checked' => $state ? 'true' : 'false',
                'formaction'   => $state && $toggle_url_off !== null ? $toggle_url_off : $toggle_url,
                'role'         => 'checkbox',
            ])
        );
    }

    /**
     * Adds a radio button to the widget.
     *
     * @param string $label
     * @param string $url
     * @param bool   $checked
     * @param array $attributes
     *
     * @return ButtonElement
     */
    public function addRadioButton(
        string $label,
        string $url,
        $checked = false,
        array $attributes = []
    ): ButtonElement {
        // TODO: Remove this some versions after 5.0
        $url = html_entity_decode($url);

        $attributes['class'] = trim(($attributes['class'] ?? '') . ' options-radio options-' . ($checked ? 'checked' : 'unchecked'));

        return $this->addElement(
            new ButtonElement($label, null, $attributes + [
                'aria-checked' => $checked ? 'true' : 'false',
                'formaction'   => $url,
                'role'         => 'radio',
            ])
        );
    }

    /**
     * Adds a select element to the widget.
     *
     * @param string $label
     * @param string $url
     * @param string $name            Attribute name
     * @param array  $options         Array of associative options (value => label)
     * @param mixed  $selected_option Currently selected option
     * @param array  $attributes      Additional attributes
     */
    public function addSelect(
        string $label,
        $url,
        string $name,
        array $options,
        $selected_option = false,
        array $attributes = []
    ): SelectListElement {
        $attributes['data-formaction'] = $url;

        return $this->addElement(
            new SelectListElement($label, $name, $options, $selected_option, $attributes)
        );
    }
}