1<?php
2
3if (file_exists(DOKU_PLUGIN . 'bureaucracy/fields/field.php')) {
4    require_once DOKU_PLUGIN . 'bureaucracy/fields/field.php';
5
6    class syntax_plugin_bureaucracy_field_semanticdataplugin extends syntax_plugin_bureaucracy_field {
7
8        function __construct($syntax_plugin, $args) {
9            $dthlp =& plugin_load('helper', 'semanticdata');
10            if(!$dthlp) msg('Loading the data helper failed. Make sure the data plugin is installed.',-1);
11
12            $this->init($syntax_plugin, $args);
13            $n_args = array();
14            foreach ($args as $arg) {
15                if ($arg[0] !== '_') {
16                    $n_args[] = $arg;
17                    continue;
18                }
19                $datatype = $dthlp->_column($arg);
20                if (is_array($datatype['type'])) {
21                    $datatype['basetype'] = $datatype['type']['type'];
22                    $datatype['enum'] = $datatype['type']['enum'];
23                    $datatype['type'] = $datatype['origtype'];
24                } else {
25                    $datatype['basetype'] = $datatype['type'];
26                }
27            }
28            $this->standardArgs($n_args);
29
30            if (isset($datatype['enum'])) {
31                $values = preg_split('/\s*,\s*/', $datatype['enum']);
32                if (!$datatype['multi'] && $this->opt['optional']) array_unshift($values, '');
33                $this->opt['args'] = $values;
34                $this->additional = ($datatype['multi'] ? array('multiple' => 'multiple'): array());
35            } else {
36                $classes = 'data_type_' . $datatype['type'] . ($datatype['multi'] ? 's' : '') .  ' ' .
37                           'data_type_' . $datatype['basetype'] . ($datatype['multi'] ? 's' : '');
38                $content = form_makeTextField('@@NAME@@', '@@VALUE@@', '@@LABEL@@', '', '@@CLASS@@ ' . $classes);
39
40                $this->tpl = $content;
41            }
42        }
43
44        function render($params, $form) {
45            if (isset($this->tpl)) {
46                parent::render($params, $form);
47            } else {
48                $this->_handlePreload();
49                if(!$form->_infieldset){
50                    $form->startFieldset('');
51                }
52                if ($this->error) {
53                    $params['class'] = 'bureaucracy_error';
54                }
55                $params = array_merge($this->opt, $params);
56                $form->addElement(call_user_func_array('form_makeListboxField',
57                                                       $this->_parse_tpl(array('@@NAME@@',
58                                                        $params['args'], '@@VALUE|' . $params['args'][0] . '@@',
59                                                        '@@LABEL@@', '', '@@CLASS@@', $this->additional),
60                                                        $params)));
61            }
62        }
63    }
64}
65