1<?php
2/**
3 * Class helper_plugin_bureaucracy_fieldyesno
4 *
5 * Creates a checkbox
6 */
7class helper_plugin_bureaucracy_fieldyesno extends helper_plugin_bureaucracy_field {
8
9    /**
10     * Arguments:
11     *  - cmd
12     *  - label
13     *  - =yesvalue
14     *  - !falsevalue
15     *  - ^ (optional)
16     *
17     * @param array $args The tokenized definition, only split at spaces
18     */
19    public function initialize($args) {
20        $this->init($args);
21        $newargs = array();
22        foreach ($args as $arg) {
23            switch ($arg[0]) {
24            case '=':
25                if($arg == '==1') {
26                    $this->opt['value'] = '1';
27                }elseif($arg == '==0') {
28                    $this->opt['value'] = '0';
29                }else{
30                    $this->opt['true_value'] = substr($arg, 1);
31                }
32                break;
33            case '!':
34                $this->opt['false_value'] = substr($arg, 1);
35                break;
36            default:
37                $newargs[] = $arg;
38            }
39        }
40        $this->standardArgs($newargs);
41        $this->opt['optional'] = true;
42    }
43
44    /**
45     * Get an arbitrary parameter
46     *
47     * @param string $key
48     * @return mixed|null
49     */
50    public function getParam($key) {
51        if ($key === 'value') {
52            if ($this->opt['value'] === '1') {
53                return isset($this->opt['true_value']) ?
54                       $this->opt['true_value'] :
55                       null;
56            } elseif ($this->opt['value'] === '0') {
57                return isset($this->opt['false_value']) ?
58                       $this->opt['false_value'] :
59                       null;
60            }
61        }
62        return parent::getParam($key);
63    }
64
65    /**
66     * Whether the field is true (used for depending fieldsets)
67     *
68     * @return bool whether field is set
69     */
70    public function isSet_() {
71        return $this->opt['value'] === '1';
72    }
73
74    /**
75     * Render the field as XHTML
76     *
77     * @params array     $params Additional HTML specific parameters
78     * @params Doku_Form $form   The target Doku_Form object
79     * @params int       $formid unique identifier of the form which contains this field
80     */
81    public function renderfield($params, Doku_Form $form, $formid) {
82        $id = 'bureaucracy__'.md5(rand());
83        if(isset($this->opt['id'])) {
84            $id = $this->opt['id'];
85        }
86        $params = array_merge(
87            array('value' => false),
88            $this->opt,
89            $params
90        );
91        $check = $params['value'] ? 'checked="checked"' : '';
92        $this->tpl = '<label class="@@CLASS@@" for="'.$id.'"><span>@@DISPLAY@@</span>'.
93                     '<input type="hidden" name="@@NAME@@" value="0" />' .
94                     '<input type="checkbox" name="@@NAME@@" value="1" id="'.$id.'" ' . $check . ' />' .
95                     '</label>';
96        parent::renderfield($params, $form, $formid);
97    }
98}
99