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