xref: /plugin/struct/admin/schemas.php (revision 0845722b74c845661d89eb1e777ef847a442a930)
187fdbc6bSMichael Große<?php
287fdbc6bSMichael Große/**
387fdbc6bSMichael Große * DokuWiki Plugin struct (Admin Component)
487fdbc6bSMichael Große *
587fdbc6bSMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
687fdbc6bSMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
787fdbc6bSMichael Große */
887fdbc6bSMichael Große
987fdbc6bSMichael Großeuse dokuwiki\Form\Form;
10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1587fdbc6bSMichael Große
16d5a1a6dcSAndreas Gohr// must be run within Dokuwiki
1787fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
1887fdbc6bSMichael Große
1987fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
2087fdbc6bSMichael Große
2187fdbc6bSMichael Große    /**
2287fdbc6bSMichael Große     * @return int sort number in admin menu
2387fdbc6bSMichael Große     */
2487fdbc6bSMichael Große    public function getMenuSort() {
2587fdbc6bSMichael Große        return 500;
2687fdbc6bSMichael Große    }
2787fdbc6bSMichael Große
2887fdbc6bSMichael Große    /**
2987fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
3087fdbc6bSMichael Große     */
3187fdbc6bSMichael Große    public function forAdminOnly() {
324d220607SAndreas Gohr        return false;
3387fdbc6bSMichael Große    }
3487fdbc6bSMichael Große
3587fdbc6bSMichael Große    /**
3687fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
3787fdbc6bSMichael Große     */
3887fdbc6bSMichael Große    public function handle() {
3987fdbc6bSMichael Große        global $INPUT;
40d5a1a6dcSAndreas Gohr        global $ID;
41e33460e2SMichael Grosse        global $config_cascade;
42e33460e2SMichael Grosse        $config_file_path =  end($config_cascade['main']['local']);
4387fdbc6bSMichael Große
448ddf87afSAndreas Gohr        // form submit
4587fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
4687fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
47d5a1a6dcSAndreas Gohr            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
4887fdbc6bSMichael Große            if(!$builder->build()) {
4987fdbc6bSMichael Große                msg('something went wrong while saving', -1);
5087fdbc6bSMichael Große            }
51e33460e2SMichael Grosse            touch($config_file_path);
5287fdbc6bSMichael Große        }
538ddf87afSAndreas Gohr        // export
54d486d6d7SAndreas Gohr        if($table && $INPUT->bool('export')) {
55d5a1a6dcSAndreas Gohr            $builder = new Schema($table);
56d486d6d7SAndreas Gohr            header('Content-Type: application/json');
57d486d6d7SAndreas Gohr            header("Content-Disposition: attachment; filename=$table.struct.json");
58d486d6d7SAndreas Gohr            echo $builder->toJSON();
59d486d6d7SAndreas Gohr            exit;
60d486d6d7SAndreas Gohr        }
618ddf87afSAndreas Gohr        // import
628ddf87afSAndreas Gohr        if($table && $INPUT->bool('import')) {
638ddf87afSAndreas Gohr            if(isset($_FILES['schemafile']['tmp_name'])) {
648ddf87afSAndreas Gohr                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
658ddf87afSAndreas Gohr                if(!$json) {
668ddf87afSAndreas Gohr                    msg('Something went wrong with the upload', -1);
678ddf87afSAndreas Gohr                } else {
68*0845722bSAndreas Gohr                    $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
698ddf87afSAndreas Gohr                    if(!$builder->build()) {
708ddf87afSAndreas Gohr                        msg('something went wrong while saving', -1);
718ddf87afSAndreas Gohr                    }
72e33460e2SMichael Grosse                    touch($config_file_path);
738ddf87afSAndreas Gohr                }
748ddf87afSAndreas Gohr            }
758ddf87afSAndreas Gohr        }
76d5a1a6dcSAndreas Gohr        // delete
77d5a1a6dcSAndreas Gohr        if($table && $INPUT->bool('delete')) {
78d5a1a6dcSAndreas Gohr            if($table != $INPUT->str('confirm')) {
79d5a1a6dcSAndreas Gohr                msg($this->getLang('del_fail'), -1);
80d5a1a6dcSAndreas Gohr            } else {
81d5a1a6dcSAndreas Gohr                try {
82d5a1a6dcSAndreas Gohr                    $schema = new Schema($table);
83d5a1a6dcSAndreas Gohr                    $schema->delete();
84d5a1a6dcSAndreas Gohr                    msg($this->getLang('del_ok'), 1);
85e33460e2SMichael Grosse                    touch($config_file_path);
86d5a1a6dcSAndreas Gohr                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
87d5a1a6dcSAndreas Gohr                } catch(StructException $e) {
88d5a1a6dcSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
89d5a1a6dcSAndreas Gohr                }
90d5a1a6dcSAndreas Gohr            }
91d5a1a6dcSAndreas Gohr        }
92d5a1a6dcSAndreas Gohr
9387fdbc6bSMichael Große    }
9487fdbc6bSMichael Große
9587fdbc6bSMichael Große    /**
9687fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
9787fdbc6bSMichael Große     */
9887fdbc6bSMichael Große    public function html() {
9987fdbc6bSMichael Große        global $INPUT;
10087fdbc6bSMichael Große
10187fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
10287fdbc6bSMichael Große        if($table) {
1037c080d69SAndreas Gohr            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
1047c080d69SAndreas Gohr            if($schema->isLookup()) {
1057c080d69SAndreas Gohr                $hl = 'edithl lookup';
1067c080d69SAndreas Gohr            } else {
1077c080d69SAndreas Gohr                $hl = 'edithl page';
1087c080d69SAndreas Gohr            }
1097c080d69SAndreas Gohr
1106af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
1117c080d69SAndreas Gohr            echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>';
1128ddf87afSAndreas Gohr
1138ddf87afSAndreas Gohr            echo '<ul class="tabs" id="plugin__struct_tabs">';
1148ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1158ddf87afSAndreas Gohr            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
1168ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1178ddf87afSAndreas Gohr            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
118d5a1a6dcSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
119d5a1a6dcSAndreas Gohr            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
1208ddf87afSAndreas Gohr            echo '</ul>';
1218ddf87afSAndreas Gohr            echo '<div class="panelHeader"></div>';
1228ddf87afSAndreas Gohr
1237c080d69SAndreas Gohr            $editor = new SchemaEditor($schema);
12487fdbc6bSMichael Große            echo $editor->getEditor();
125*0845722bSAndreas Gohr            echo $this->html_json($schema);
126*0845722bSAndreas Gohr            echo $this->html_delete($schema);
127d486d6d7SAndreas Gohr
12887fdbc6bSMichael Große        } else {
1296af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
1308ddf87afSAndreas Gohr            echo $this->html_newschema();
13187fdbc6bSMichael Große        }
13287fdbc6bSMichael Große    }
13387fdbc6bSMichael Große
13487fdbc6bSMichael Große    /**
1358ddf87afSAndreas Gohr     * Form for handling import/export from/to JSON
136*0845722bSAndreas Gohr     *
137*0845722bSAndreas Gohr     * @param Schema $schema
1388ddf87afSAndreas Gohr     * @return string
1398ddf87afSAndreas Gohr     */
140*0845722bSAndreas Gohr    protected function html_json(Schema $schema) {
1418ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
1428ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
1438ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
144*0845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
145*0845722bSAndreas Gohr        $form->setHiddenField('lookup', $schema->isLookup());
1468ddf87afSAndreas Gohr
1478ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('export'));
1488ddf87afSAndreas Gohr        $form->addButton('export', $this->getLang('btn_export'));
1498ddf87afSAndreas Gohr        $form->addFieldsetClose();
1508ddf87afSAndreas Gohr
1518ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('import'));
1528ddf87afSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
1538ddf87afSAndreas Gohr        $form->addButton('import', $this->getLang('btn_import'));
1548ddf87afSAndreas Gohr        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
1558ddf87afSAndreas Gohr        $form->addFieldsetClose();
1568ddf87afSAndreas Gohr        return $form->toHTML();
1578ddf87afSAndreas Gohr    }
1588ddf87afSAndreas Gohr
1598ddf87afSAndreas Gohr    /**
160d5a1a6dcSAndreas Gohr     * Form for deleting schemas
161*0845722bSAndreas Gohr     *
162*0845722bSAndreas Gohr     * @param Schema $schema
163d5a1a6dcSAndreas Gohr     * @return string
164d5a1a6dcSAndreas Gohr     */
165*0845722bSAndreas Gohr    protected function html_delete(Schema $schema) {
166d5a1a6dcSAndreas Gohr        $form = new Form(array('id' => 'plugin__struct_delete'));
167d5a1a6dcSAndreas Gohr        $form->setHiddenField('do', 'admin');
168d5a1a6dcSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
169*0845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
170d5a1a6dcSAndreas Gohr
171d5a1a6dcSAndreas Gohr        $form->addHTML($this->locale_xhtml('delete_intro'));
172d5a1a6dcSAndreas Gohr
173d5a1a6dcSAndreas Gohr        $form->addFieldsetOpen($this->getLang('tab_delete'));
174d5a1a6dcSAndreas Gohr        $form->addTextInput('confirm', $this->getLang('del_confirm'));
175d5a1a6dcSAndreas Gohr        $form->addButton('delete', $this->getLang('btn_delete'));
176d5a1a6dcSAndreas Gohr        $form->addFieldsetClose();
177d5a1a6dcSAndreas Gohr        return $form->toHTML();
178d5a1a6dcSAndreas Gohr    }
179d5a1a6dcSAndreas Gohr
180d5a1a6dcSAndreas Gohr    /**
18187fdbc6bSMichael Große     * Form to add a new schema
1828ddf87afSAndreas Gohr     *
1838ddf87afSAndreas Gohr     * @return string
18487fdbc6bSMichael Große     */
18587fdbc6bSMichael Große    protected function html_newschema() {
18687fdbc6bSMichael Große        $form = new Form();
1874e427bd5SAndreas Gohr        $form->addClass('struct_newschema');
18887fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
18987fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
190dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
19187fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
1924e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
1934e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
19487fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
19587fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
19687fdbc6bSMichael Große        $form->addFieldsetClose();
1978ddf87afSAndreas Gohr        return $form->toHTML();
19887fdbc6bSMichael Große    }
19987fdbc6bSMichael Große
20087fdbc6bSMichael Große    /**
20187fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
20287fdbc6bSMichael Große     *
20387fdbc6bSMichael Große     * @return array
20487fdbc6bSMichael Große     */
20587fdbc6bSMichael Große    public function getTOC() {
20687fdbc6bSMichael Große        global $ID;
20787fdbc6bSMichael Große
20887fdbc6bSMichael Große        $toc = array();
2098ddf87afSAndreas Gohr        $link = wl(
2108ddf87afSAndreas Gohr            $ID, array(
21187fdbc6bSMichael Große                   'do' => 'admin',
212dbffe06eSAndreas Gohr                   'page' => 'struct_assignments'
2138ddf87afSAndreas Gohr               )
2148ddf87afSAndreas Gohr        );
215dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
2167c080d69SAndreas Gohr        $slink = wl(
2178ddf87afSAndreas Gohr            $ID, array(
218dbffe06eSAndreas Gohr                   'do' => 'admin',
219dbffe06eSAndreas Gohr                   'page' => 'struct_schemas'
2208ddf87afSAndreas Gohr               )
2218ddf87afSAndreas Gohr        );
2227c080d69SAndreas Gohr        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
22387fdbc6bSMichael Große
2247c080d69SAndreas Gohr        $tables = Schema::getAll('page');
2257c080d69SAndreas Gohr        if($tables) {
2267c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
227097f4a53SAndreas Gohr            foreach($tables as $table) {
2288ddf87afSAndreas Gohr                $link = wl(
2298ddf87afSAndreas Gohr                    $ID, array(
23087fdbc6bSMichael Große                           'do' => 'admin',
231dbffe06eSAndreas Gohr                           'page' => 'struct_schemas',
232097f4a53SAndreas Gohr                           'table' => $table
2338ddf87afSAndreas Gohr                       )
2348ddf87afSAndreas Gohr                );
23587fdbc6bSMichael Große
2367c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
23787fdbc6bSMichael Große            }
2387c080d69SAndreas Gohr        }
2397c080d69SAndreas Gohr
2407c080d69SAndreas Gohr        $tables = Schema::getAll('lookup');
2417c080d69SAndreas Gohr        if($tables) {
2427c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
2437c080d69SAndreas Gohr            foreach($tables as $table) {
2447c080d69SAndreas Gohr                $link = wl(
2457c080d69SAndreas Gohr                    $ID, array(
2467c080d69SAndreas Gohr                           'do' => 'admin',
2477c080d69SAndreas Gohr                           'page' => 'struct_schemas',
2487c080d69SAndreas Gohr                           'table' => $table
2497c080d69SAndreas Gohr                       )
2507c080d69SAndreas Gohr                );
2517c080d69SAndreas Gohr
2527c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
2537c080d69SAndreas Gohr            }
2547c080d69SAndreas Gohr        }
2557c080d69SAndreas Gohr
25687fdbc6bSMichael Große        return $toc;
25787fdbc6bSMichael Große    }
25887fdbc6bSMichael Große
25987fdbc6bSMichael Große}
26087fdbc6bSMichael Große
26187fdbc6bSMichael Große// vim:ts=4:sw=4:et:
262