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