1<?php
2/**
3 * Class helper_plugin_bureaucracyau_fieldselect
4 *
5 * Creates a dropdown list
6 */
7class helper_plugin_bureaucracyau_fieldradio extends helper_plugin_bureaucracyau_field {
8
9    protected $mandatory_args = 3;
10
11    /**
12     * Arguments:
13     *  - cmd
14     *  - label
15     *  - option1|option2|etc
16     *  - ^ (optional)
17     *
18     * @param array $args The tokenized definition, only split at spaces
19     */
20    public function initialize($args) {
21        $this->init($args);
22        $this->opt['args'] = array_filter(array_map('trim', explode('|',array_shift($args))));
23        $this->standardArgs($args);
24    }
25
26    /**
27     * Render the field as XHTML
28     *
29     * Outputs the represented field using the passed Doku_Form object.
30     * Additional parameters (CSS class & HTML name) are passed in $params.
31     *
32     * @params array     $params Additional HTML specific parameters
33     * @params Doku_Form $form   The target Doku_Form object
34     * @params int       $formid unique identifier of the form which contains this field
35     */
36    public function renderfield($params, Doku_Form $form, $formid) {
37        $this->_handlePreload();
38        if(!$form->_infieldset){
39            $form->startFieldset('');
40        }
41        if ($this->error) {
42            $params['class'] = 'bureaucracyau_error';
43        }
44        $params = array_merge($this->opt, $params);
45
46        list($name, $entries, $value, $label, $id, $class) = $this->_parse_tpl(
47            array(
48                '@@NAME@@',
49                $params['args'],
50                '@@VALUE@@',
51                '@@DISPLAY@@',
52                '@@ID@@',
53                '@@CLASS@@'
54            ),
55            $params
56        );
57
58        $value = (in_array($value, $entries) ? $value : null);
59        $valueoffieldwithid = ($value !== null ? $value : current($entries));
60        // label
61        $s = '<label';
62        $s .= ' class="radiolabel '.$class.'"';
63        $s .= '><span>' . $label . '</span>';
64        $s .= '</label>';
65        $form->addElement($s);
66
67        // radio fields
68        foreach($entries as $val) {
69            if($value === $val) {
70                $attrs = array('checked' => 'checked');
71            } else {
72                $attrs = array();
73            }
74            if($valueoffieldwithid === $val) {
75                $_id = $id; //e.g. autofocus with 'focus__this' id
76            } else {
77                $_id = '';
78            }
79            $form->addElement(form_makeRadioField($name, $val, $val, $_id, $class, $attrs));
80        }
81    }
82}
83