xref: /plugin/struct/helper/field.php (revision 89be912b2828f73aa728ab2d1347de9d26aada17)
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\Value;
6use dokuwiki\plugin\struct\meta\ValueValidator;
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        $this->init($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        $this->standardArgs($args);
35    }
36
37    /**
38     * Sets the value and validates it
39     *
40     * @param mixed $value
41     * @return bool value was set successfully validated
42     */
43    protected function setVal($value) {
44        if(!$this->column) {
45            $value = '';
46        } else {
47            $validator = new ValueValidator();
48            $this->error = !$validator->validateValue($this->column, $value);
49            if($this->error) {
50                foreach($validator->getErrors() as $error) {
51                    msg(hsc($error), -1);
52                }
53            }
54        }
55
56        if($value === array() || $value === '') {
57            if(!isset($this->opt['optional'])) {
58                $this->error = true;
59                msg(sprintf($this->getLang('e_required'), hsc($this->opt['label'])), -1);
60            }
61        }
62
63        $this->opt['value'] = $value;
64        return !$this->error;
65    }
66
67    /**
68     * Creates the HTML for the field
69     *
70     * @param array $params
71     * @param Doku_Form $form
72     * @param int $formid
73     */
74    public function renderfield($params, Doku_Form $form, $formid) {
75        if(!$this->column) return;
76
77        // this is what parent does
78        $this->_handlePreload();
79        if(!$form->_infieldset) {
80            $form->startFieldset('');
81        }
82        if($this->error) {
83            $params['class'] = 'bureaucracy_error';
84        }
85
86        // output the field
87        $value = new Value($this->column, $this->opt['value']);
88        $field = $this->makeField($value, $params['name']);
89        $form->addElement($field);
90    }
91
92    /**
93     * Create the input field
94     *
95     * @param Value $field
96     * @param String $name field's name
97     * @return string
98     */
99    protected function makeField(Value $field, $name) {
100        $trans = hsc($field->getColumn()->getTranslatedLabel());
101        $hint = hsc($field->getColumn()->getTranslatedHint());
102        $class = $hint ? 'hashint' : '';
103        $lclass = $this->error ? 'bureaucracy_error' : '';
104        $colname = $field->getColumn()->getFullQualifiedLabel();
105        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
106
107        $id = uniqid('struct__', false);
108        $input = $field->getValueEditor($name, $id);
109
110        $html = '<div class="field">';
111        $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">";
112        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
113        $html .= '</label>';
114        $html .= "<span class=\"input\">$input</span>";
115        $html .= '</div>';
116
117        return $html;
118    }
119
120    /**
121     * Tries to find the correct column and schema
122     *
123     * @throws StructException
124     * @param string $colname
125     * @return \dokuwiki\plugin\struct\meta\Column
126     */
127    protected function findColumn($colname) {
128        list($table, $label) = explode('.', $colname, 2);
129        if(!$table || !$label) {
130            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
131        }
132        $schema = new Schema($table);
133        return $schema->findColumn($label);
134    }
135
136    /**
137     * This ensures all language strings are still working
138     *
139     * @return string always 'bureaucracy'
140     */
141    public function getPluginName() {
142        return 'bureaucracy';
143    }
144
145}
146