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