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