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