xref: /plugin/struct/meta/SchemaEditor.php (revision 6af24d3eff33249280549e60e18474e2cc0bf9d0)
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        </tr>");
55
56
57        foreach($this->schema->getColumns() as $key => $obj) {
58            $form->addHTML($this->adminColumn($key, $obj));
59        }
60
61        // FIXME new one needs to be added dynamically, this is just for testing
62        $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort()+10, new Text()), 'new'));
63
64        $form->addHTML('</table>');
65        $form->addButton('save', 'Save')->attr('type','submit');
66        return $form->toHTML() . $this->initJSONEditor();
67    }
68
69    /**
70     * Gives the code to attach the JSON editor to the config field
71     *
72     * We do not use the "normal" way, because this is rarely used code and there's no need to always load it.
73     * @return string
74     */
75    protected function initJSONEditor() {
76        $html = '';
77        $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">';
78        $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">';
79        $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js"></script>';
80        $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.js"></script>';
81        return $html;
82    }
83
84    /**
85     * Returns the HTML to edit a single column definition of the schema
86     *
87     * @param string $column_id
88     * @param Column $col
89     * @param string $key The key to use in the form
90     * @return string
91     * @todo this should probably be reused for adding new columns via AJAX later?
92     */
93    protected function adminColumn($column_id, Column $col, $key='cols') {
94        $base = 'schema['.$key.'][' . $column_id . ']'; // base name for all fields
95
96        $html = '<tr>';
97
98        $html .= '<td>';
99        $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">';
100        $html .= '</td>';
101
102        $html .= '<td>';
103        $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">';
104        $html .= '</td>';
105
106        $html .= '<td>';
107        $checked = $col->getType()->isMulti() ? 'checked="checked"' : '';
108        $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>';
109        $html .= '</td>';
110
111        $html .= '<td>';
112        $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT);
113        $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . hsc($config) . '</textarea>';
114        $html .= '</td>';
115
116        $types = Column::allTypes();
117        $html .= '<td>';
118        $html .= '<select name="' . $base . '[class]">';
119        foreach($types as $type) {
120            $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : '';
121            $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>';
122        }
123        $html .= '</select>';
124        $html .= '</td>';
125
126        $html .= '</tr>';
127
128        return $html;
129    }
130
131}
132