xref: /plugin/struct/helper/field.php (revision 88b58a212b1e27877882d9dad769a4d01cffdde4)
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        $field = $this->makeField($value, $params['name']);
90        $form->addElement($field);
91    }
92
93    /**
94     * Create the input field
95     *
96     * @param Value $field
97     * @param String $name field's name
98     * @return string
99     */
100    protected function makeField(Value $field, $name) {
101        $trans = hsc($field->getColumn()->getTranslatedLabel());
102        $hint = hsc($field->getColumn()->getTranslatedHint());
103        $class = $hint ? 'hashint' : '';
104        $lclass = $this->error ? 'bureaucracy_error' : '';
105        $colname = $field->getColumn()->getFullQualifiedLabel();
106        $required = $this->opt['optional'] ? '' : ' <sup>*</sup>';
107
108        $id = uniqid('struct__', false);
109        $input = $field->getValueEditor($name, $id);
110
111        $html = '<div class="field">';
112        $html .= "<label class=\"$lclass\" data-column=\"$colname\" for=\"$id\">";
113        $html .= "<span class=\"label $class\" title=\"$hint\">$trans$required</span>";
114        $html .= '</label>';
115        $html .= "<span class=\"input\">$input</span>";
116        $html .= '</div>';
117
118        return $html;
119    }
120
121    /**
122     * Tries to find the correct column and schema
123     *
124     * @throws StructException
125     * @param string $colname
126     * @return \dokuwiki\plugin\struct\meta\Column
127     */
128    protected function findColumn($colname) {
129        list($table, $label) = explode('.', $colname, 2);
130        if(!$table || !$label) {
131            throw new StructException('Field \'%s\' not given in schema.field form', $colname);
132        }
133        $schema = new Schema($table);
134        return $schema->findColumn($label);
135    }
136
137    /**
138     * This ensures all language strings are still working
139     *
140     * @return string always 'bureaucracy'
141     */
142    public function getPluginName() {
143        return 'bureaucracy';
144    }
145
146}
147