mirror of
https://github.com/cosmocode/dokuwiki-plugin-struct.git
synced 2025-07-25 16:01:54 +00:00
92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace dokuwiki\plugin\struct\types;
|
|
|
|
class Checkbox extends AbstractBaseType
|
|
{
|
|
protected $config = ['values' => 'one, two, three'];
|
|
|
|
/**
|
|
* Creates the options array
|
|
*
|
|
* @return array
|
|
*/
|
|
protected function getOptions()
|
|
{
|
|
$options = explode(',', $this->config['values']);
|
|
$options = array_map('trim', $options);
|
|
$options = array_filter($options);
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* A single checkbox, additional values are ignored
|
|
*
|
|
* @param string $name
|
|
* @param string $rawvalue
|
|
* @param string $htmlID
|
|
*
|
|
* @return string
|
|
*/
|
|
public function valueEditor($name, $rawvalue, $htmlID)
|
|
{
|
|
$options = $this->getOptions();
|
|
$opt = array_shift($options);
|
|
|
|
if ($rawvalue == $opt) {
|
|
$checked = 'checked';
|
|
} else {
|
|
$checked = '';
|
|
}
|
|
$opt = hsc($opt);
|
|
$params = [
|
|
'name' => $name,
|
|
'value' => $opt,
|
|
'class' => 'struct_' . strtolower($this->getClass()),
|
|
'type' => 'checkbox',
|
|
'id' => $htmlID,
|
|
'checked' => $checked
|
|
];
|
|
$attributes = buildAttributes($params, true);
|
|
return "<label><input $attributes> $opt</label>";
|
|
}
|
|
|
|
/**
|
|
* Multiple checkboxes
|
|
*
|
|
* @param string $name
|
|
* @param \string[] $rawvalues
|
|
* @param string $htmlID
|
|
*
|
|
* @return string
|
|
*/
|
|
public function multiValueEditor($name, $rawvalues, $htmlID)
|
|
{
|
|
$class = 'struct_' . strtolower($this->getClass());
|
|
|
|
$html = '';
|
|
foreach ($this->getOptions() as $opt) {
|
|
if (in_array($opt, $rawvalues)) {
|
|
$checked = 'checked';
|
|
} else {
|
|
$checked = '';
|
|
}
|
|
|
|
$params = [
|
|
'name' => $name . '[]',
|
|
'value' => $opt,
|
|
'class' => $class,
|
|
'type' => 'checkbox',
|
|
'id' => $htmlID,
|
|
'checked' => $checked
|
|
];
|
|
$attributes = buildAttributes($params, true);
|
|
$htmlID = '';
|
|
|
|
$opt = hsc($opt);
|
|
$html .= "<label><input $attributes> $opt</label>";
|
|
}
|
|
return $html;
|
|
}
|
|
}
|