xref: /plugin/struct/helper/field.php (revision 8824339dfb9d3e3672bb1214601296a5f483eeb4)
1<?php
2use dokuwiki\plugin\struct\meta\Column;
3use dokuwiki\plugin\struct\meta\Schema;
4use dokuwiki\plugin\struct\meta\StructException;
5use dokuwiki\plugin\struct\meta\Validator;
6use dokuwiki\plugin\struct\meta\Value;
7
8/**
9 * Allows adding a single struct field as a bureaucracy field
10 *
11 * This class is used when a field of the type struct_field is encountered in the
12 * bureaucracy syntax.
13 */
14class helper_plugin_struct_field extends helper_plugin_bureaucracy_field {
15
16    /** @var  Column */
17    public $column;
18
19    /**
20     * Initialize the appropriate column
21     *
22     * @param array $args
23     */
24    public function initialize($args) {
25        parent::initialize($args);
26
27        // find the column
28        try {
29            $this->column = $this->findColumn($this->opt['label']);
30        } catch(StructException $e) {
31            msg(hsc($e->getMessage()), -1);
32        }
33    }
34
35    /**
36     * Sets the value and validates it
37     *
38     * @param mixed $value
39     * @return bool value was set successfully validated
40     */
41    protected function setVal($value) {
42        if(!$this->column) {
43            $value = '';
44        } else {
45            $validator = new Validator();
46            $this->error = !$validator->validateValue($this->column, $value);
47            if($this->error) {
48                foreach($validator->getErrors() as $error) {
49                    msg(hsc($error), -1);
50                }
51            }
52
53        }
54
55        if($value === array() || $value === '') {
56            if(!isset($this->opt['optional'])) {
57                $this->error = true;
58                msg(sprintf($this->getLang('e_required'), hsc($this->opt['label'])), -1);
59            }
60        }
61
62        $this->opt['value'] = $value;
63        return !$this->error;
64    }
65
66    /**
67     * Creates the HTML for the field
68     *
69     * @param array $params
70     * @param Doku_Form $form
71     * @param int $formid
72     */
73    public function renderfield($params, Doku_Form $form, $formid) {
74        if(!$this->column) return;
75
76        // this is what parent does
77        $this->_handlePreload();
78        if(!$form->_infieldset) {
79            $form->startFieldset('');
80        }
81        if($this->error) {
82            $params['class'] = 'bureaucracy_error';
83        }
84
85        // output the field
86        $value = new Value($this->column, $this->opt['value']);
87        $field = $this->makeField($value, $params['name']);
88        $form->addElement($field);
89    }
90
91    /**
92     * Create the input field
93     *
94     * @param Value $field
95     * @param String $name field's name
96     * @return string
97     */
98    protected function makeField(Value $field, $name) {
99        $trans = hsc($field->getColumn()->getTranslatedLabel());
100        $hint = hsc($field->getColumn()->getTranslatedHint());
101        $class = $hint ? 'hashint' : '';
102        $lclass = $this->error ? 'bureaucracy_error' : '';
103        $colname = $field->getColumn()->getFullQualifiedLabel();
104        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
105
106        $input = $field->getValueEditor($name);
107
108        $html = '';
109        $html .= "<label class=\"$lclass\" data-column=\"$colname\">";
110        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
111        $html .= "<span class=\"input\">$input</span>";
112        $html .= '</label>';
113
114        return $html;
115    }
116
117    /**
118     * Tries to find the correct column and schema
119     *
120     * @throws StructException
121     * @param string $colname
122     * @return \dokuwiki\plugin\struct\meta\Column
123     */
124    protected function findColumn($colname) {
125        list($table, $label) = explode('.', $colname, 2);
126        if(!$table || !$label) {
127            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
128        }
129        $schema = new Schema($table);
130        return $schema->findColumn($label);
131    }
132
133    /**
134     * This ensures all language strings are still working
135     *
136     * @return string always 'bureaucracy'
137     */
138    public function getPluginName() {
139        return 'bureaucracy';
140    }
141
142}
143