xref: /plugin/struct/types/Checkbox.php (revision 7fe2cdf28c472c686961bf42f0123eb33d2f3e60)
173ae930bSAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types;
473ae930bSAndreas Gohr
5d6d97f60SAnna Dabrowskaclass Checkbox extends AbstractBaseType
6d6d97f60SAnna Dabrowska{
77234bfb1Ssplitbrain    protected $config = ['values' => 'one, two, three'];
873ae930bSAndreas Gohr
973ae930bSAndreas Gohr    /**
1073ae930bSAndreas Gohr     * Creates the options array
1173ae930bSAndreas Gohr     *
1273ae930bSAndreas Gohr     * @return array
1373ae930bSAndreas Gohr     */
14d6d97f60SAnna Dabrowska    protected function getOptions()
15d6d97f60SAnna Dabrowska    {
1673ae930bSAndreas Gohr        $options = explode(',', $this->config['values']);
1773ae930bSAndreas Gohr        $options = array_map('trim', $options);
1873ae930bSAndreas Gohr        $options = array_filter($options);
1973ae930bSAndreas Gohr        return $options;
2073ae930bSAndreas Gohr    }
2173ae930bSAndreas Gohr
2273ae930bSAndreas Gohr    /**
2373ae930bSAndreas Gohr     * A single checkbox, additional values are ignored
2473ae930bSAndreas Gohr     *
2573ae930bSAndreas Gohr     * @param string $name
26c0230d2cSAndreas Gohr     * @param string $rawvalue
27ee983135SMichael Große     * @param string $htmlID
28ee983135SMichael Große     *
2973ae930bSAndreas Gohr     * @return string
3073ae930bSAndreas Gohr     */
31d6d97f60SAnna Dabrowska    public function valueEditor($name, $rawvalue, $htmlID)
32d6d97f60SAnna Dabrowska    {
3373ae930bSAndreas Gohr        $options = $this->getOptions();
3473ae930bSAndreas Gohr        $opt = array_shift($options);
3573ae930bSAndreas Gohr
36c0230d2cSAndreas Gohr        if ($rawvalue == $opt) {
373e7a5b3cSMichael Große            $checked = 'checked';
3873ae930bSAndreas Gohr        } else {
3973ae930bSAndreas Gohr            $checked = '';
4073ae930bSAndreas Gohr        }
4173ae930bSAndreas Gohr        $opt = hsc($opt);
42*7fe2cdf2SAndreas Gohr        $params = [
43*7fe2cdf2SAndreas Gohr            'name' => $name,
44*7fe2cdf2SAndreas Gohr            'value' => $opt,
45*7fe2cdf2SAndreas Gohr            'class' => 'struct_' . strtolower($this->getClass()),
46*7fe2cdf2SAndreas Gohr            'type' => 'checkbox',
47*7fe2cdf2SAndreas Gohr            'id' => $htmlID,
48*7fe2cdf2SAndreas Gohr            'checked' => $checked
49*7fe2cdf2SAndreas Gohr        ];
503e7a5b3cSMichael Große        $attributes = buildAttributes($params, true);
513e7a5b3cSMichael Große        return "<label><input $attributes>&nbsp;$opt</label>";
5273ae930bSAndreas Gohr    }
5373ae930bSAndreas Gohr
5473ae930bSAndreas Gohr    /**
5573ae930bSAndreas Gohr     * Multiple checkboxes
5673ae930bSAndreas Gohr     *
5773ae930bSAndreas Gohr     * @param string $name
58c0230d2cSAndreas Gohr     * @param \string[] $rawvalues
59ee983135SMichael Große     * @param string $htmlID
60ee983135SMichael Große     *
6173ae930bSAndreas Gohr     * @return string
6273ae930bSAndreas Gohr     */
63d6d97f60SAnna Dabrowska    public function multiValueEditor($name, $rawvalues, $htmlID)
64d6d97f60SAnna Dabrowska    {
6573ae930bSAndreas Gohr        $class = 'struct_' . strtolower($this->getClass());
6673ae930bSAndreas Gohr
6773ae930bSAndreas Gohr        $html = '';
6873ae930bSAndreas Gohr        foreach ($this->getOptions() as $opt) {
69c0230d2cSAndreas Gohr            if (in_array($opt, $rawvalues)) {
703e7a5b3cSMichael Große                $checked = 'checked';
7173ae930bSAndreas Gohr            } else {
7273ae930bSAndreas Gohr                $checked = '';
7373ae930bSAndreas Gohr            }
7473ae930bSAndreas Gohr
75*7fe2cdf2SAndreas Gohr            $params = [
76*7fe2cdf2SAndreas Gohr                'name' => $name . '[]',
77*7fe2cdf2SAndreas Gohr                'value' => $opt,
78*7fe2cdf2SAndreas Gohr                'class' => $class,
79*7fe2cdf2SAndreas Gohr                'type' => 'checkbox',
80*7fe2cdf2SAndreas Gohr                'id' => $htmlID,
81*7fe2cdf2SAndreas Gohr                'checked' => $checked
82*7fe2cdf2SAndreas Gohr            ];
833e7a5b3cSMichael Große            $attributes = buildAttributes($params, true);
843e7a5b3cSMichael Große            $htmlID = '';
85ee983135SMichael Große
8673ae930bSAndreas Gohr            $opt = hsc($opt);
873e7a5b3cSMichael Große            $html .= "<label><input $attributes>&nbsp;$opt</label>";
8873ae930bSAndreas Gohr        }
8973ae930bSAndreas Gohr        return $html;
9073ae930bSAndreas Gohr    }
9173ae930bSAndreas Gohr}
92