xref: /plugin/struct/meta/SchemaEditor.php (revision 28e054687da43fd43ab16ca430b183b20dc5ebfe)
1<?php
2
3namespace plugin\struct\meta;
4
5use dokuwiki\Form\Form;
6use plugin\struct\types\Text;
7
8/**
9 * Class SchemaEditor
10 *
11 * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the
12 * schema happens in the SchemaBuilder class.
13 *
14 * @package plugin\struct\meta
15 */
16class SchemaEditor {
17    /** @var Schema the schema that is edited */
18    protected $schema;
19
20    /** @var \DokuWiki_Plugin  */
21    protected $hlp;
22
23    /**
24     * SchemaEditor constructor.
25     * @param Schema $schema
26     */
27    public function __construct(Schema $schema) {
28        $this->schema = $schema;
29        $this->hlp = plugin_load('helper', 'struct_config');
30    }
31
32    /**
33     * Returns the Admin Form to edit the schema
34     *
35     * This data is processed by the SchemaBuilder class
36     *
37     * @return string the HTML for the editor form
38     * @see SchemaBuilder
39     */
40    public function getEditor() {
41        $form = new Form(array('method' => 'POST', 'id'=>'plugin__struct'));
42        $form->setHiddenField('do', 'admin');
43        $form->setHiddenField('page', 'struct_schemas');
44        $form->setHiddenField('table', $this->schema->getTable());
45        $form->setHiddenField('schema[id]', $this->schema->getId());
46
47        $form->addHTML('<table class="inline">');
48        $form->addHTML("<tr>
49            <th>{$this->hlp->getLang('editor_sort')}</th>
50            <th>{$this->hlp->getLang('editor_label')}</th>
51            <th>{$this->hlp->getLang('editor_multi')}</th>
52            <th>{$this->hlp->getLang('editor_conf')}</th>
53            <th>{$this->hlp->getLang('editor_type')}</th>
54            <th>{$this->hlp->getLang('editor_enabled')}</th>
55        </tr>");
56
57
58        foreach($this->schema->getColumns() as $key => $obj) {
59            $form->addHTML($this->adminColumn($key, $obj));
60        }
61
62        // FIXME new one needs to be added dynamically, this is just for testing
63        $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort()+10, new Text()), 'new'));
64
65        $form->addHTML('</table>');
66        $form->addButton('save', 'Save')->attr('type','submit');
67        return $form->toHTML() . $this->initJSONEditor();
68    }
69
70    /**
71     * Gives the code to attach the JSON editor to the config field
72     *
73     * We do not use the "normal" way, because this is rarely used code and there's no need to always load it.
74     * @return string
75     */
76    protected function initJSONEditor() {
77        $html = '';
78        $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">';
79        $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">';
80        $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js"></script>';
81        $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.js"></script>';
82        return $html;
83    }
84
85    /**
86     * Returns the HTML to edit a single column definition of the schema
87     *
88     * @param string $column_id
89     * @param Column $col
90     * @param string $key The key to use in the form
91     * @return string
92     * @todo this should probably be reused for adding new columns via AJAX later?
93     */
94    protected function adminColumn($column_id, Column $col, $key='cols') {
95        $base = 'schema['.$key.'][' . $column_id . ']'; // base name for all fields
96
97        $class = $col->isEnabled() ? '' : 'disabled';
98
99        $html = "<tr class=\"$class\">";
100
101        $html .= '<td class="sort">';
102        $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">';
103        $html .= '</td>';
104
105        $html .= '<td class="label">';
106        $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">';
107        $html .= '</td>';
108
109        $html .= '<td class="ismulti">';
110        $checked = $col->getType()->isMulti() ? 'checked="checked"' : '';
111        $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>';
112        $html .= '</td>';
113
114        $html .= '<td class="config">';
115        $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT);
116        $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . hsc($config) . '</textarea>';
117        $html .= '</td>';
118
119        $types = Column::allTypes();
120        $html .= '<td class="class">';
121        $html .= '<select name="' . $base . '[class]">';
122        foreach($types as $type) {
123            $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : '';
124            $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>';
125        }
126        $html .= '</select>';
127        $html .= '</td>';
128
129
130        $html .= '<td class="isenabled">';
131        $checked = $col->isEnabled() ? 'checked="checked"' : '';
132        $html .= '<input type="checkbox" name="' . $base . '[isenabled]" value="1" ' . $checked . '>';
133        $html .= '</td>';
134
135        $html .= '</tr>';
136
137        return $html;
138    }
139
140}
141