aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/OptionsWidget.php
blob: 304a5ac26cabc0b149de4b8d79fd2af2bd9d120f (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
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 */
class OptionsWidget extends ListWidget
{
    const INDEX = 'options';

    /**
     * @param String $title Optional alternative title
     */
    public function __construct($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 $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
     */
    public function addCheckbox($label, $state, $toggle_url, $toggle_url_off = null, array $attributes = [])
    {
        // 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;

        $content = sprintf(
            '<button formaction="%s" role="checkbox" aria-checked="%s" class="options-checkbox options-%s" %s>%s</button>',
            htmlReady($state && $toggle_url_off !== null ? $toggle_url_off : $toggle_url),
            $state ? 'true' : 'false',
            $state ? 'checked' : 'unchecked',
            arrayToHtmlAttributes($attributes),
            htmlReady($label)
        );
        $this->addElement(new WidgetElement($content));
    }

    /**
     * @param String $label
     * @param String $url
     * @param bool   $checked
     */
    public function addRadioButton($label, $url, $checked = false, array $attributes = [])
    {
        // TODO: Remove this some versions after 5.0
        $url = html_entity_decode($url);

        $content = sprintf(
            '<button formaction="%s" role="radio" aria-checked="%s" class="options-radio options-%s" %s>%s</button>',
            htmlReady($url),
            $checked ? 'true' : 'false',
            $checked ? 'checked' : 'unchecked',
            arrayToHtmlAttributes($attributes),
            htmlReady($label)
        );
        $this->addElement(new WidgetElement($content));
    }

    /**
     * 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($label, $url, $name, $options, $selected_option = false, $attributes = [])
    {
        $option_content = '';

        foreach ($options as $value => $option) {
            $selected = $value === $selected_option ? 'selected' : '';
            $option_content .= sprintf(
                '<option value="%s" %s>%s</option>', 
                htmlReady($value),
                $selected, 
                htmlReady($option)
            );
        }

        $content = sprintf(
            '<select data-formaction="%s" class="sidebar-selectlist submit-upon-select" name="%s" aria-label="%s" %s>%s</select>',
            htmlReady($url),
            htmlReady($name),
            htmlReady($label),
            arrayToHtmlAttributes($attributes),
            $option_content
        );

        $this->addElement(new WidgetElement($content));
    }
}