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