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