xref: /plugin/struct/admin/schemas.php (revision 7c080d6944fe1302e21893f424c042914e65877e)
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 {
68d5a1a6dcSAndreas Gohr                    $builder = new SchemaImporter($table, $json);
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) {
103*7c080d69SAndreas Gohr            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
104*7c080d69SAndreas Gohr            if($schema->isLookup()) {
105*7c080d69SAndreas Gohr                $hl = 'edithl lookup';
106*7c080d69SAndreas Gohr            } else {
107*7c080d69SAndreas Gohr                $hl = 'edithl page';
108*7c080d69SAndreas Gohr            }
109*7c080d69SAndreas Gohr
1106af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
111*7c080d69SAndreas 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
123*7c080d69SAndreas Gohr            $editor = new SchemaEditor($schema);
12487fdbc6bSMichael Große            echo $editor->getEditor();
1258ddf87afSAndreas Gohr            echo $this->html_json();
126d5a1a6dcSAndreas Gohr            echo $this->html_delete();
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
1368ddf87afSAndreas Gohr     * @return string
1378ddf87afSAndreas Gohr     */
1388ddf87afSAndreas Gohr    protected function html_json() {
1398ddf87afSAndreas Gohr        global $INPUT;
1408ddf87afSAndreas Gohr        $table = Schema::cleanTableName($INPUT->str('table'));
1418ddf87afSAndreas Gohr
1428ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
1438ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
1448ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
1458ddf87afSAndreas Gohr        $form->setHiddenField('table', $table);
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
161d5a1a6dcSAndreas Gohr     * @return string
162d5a1a6dcSAndreas Gohr     */
163d5a1a6dcSAndreas Gohr    protected function html_delete() {
164d5a1a6dcSAndreas Gohr        global $INPUT;
165d5a1a6dcSAndreas Gohr        $table = Schema::cleanTableName($INPUT->str('table'));
166d5a1a6dcSAndreas Gohr
167d5a1a6dcSAndreas Gohr        $form = new Form(array('id' => 'plugin__struct_delete'));
168d5a1a6dcSAndreas Gohr        $form->setHiddenField('do', 'admin');
169d5a1a6dcSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
170d5a1a6dcSAndreas Gohr        $form->setHiddenField('table', $table);
171d5a1a6dcSAndreas Gohr
172d5a1a6dcSAndreas Gohr        $form->addHTML($this->locale_xhtml('delete_intro'));
173d5a1a6dcSAndreas Gohr
174d5a1a6dcSAndreas Gohr        $form->addFieldsetOpen($this->getLang('tab_delete'));
175d5a1a6dcSAndreas Gohr        $form->addTextInput('confirm', $this->getLang('del_confirm'));
176d5a1a6dcSAndreas Gohr        $form->addButton('delete', $this->getLang('btn_delete'));
177d5a1a6dcSAndreas Gohr        $form->addFieldsetClose();
178d5a1a6dcSAndreas Gohr        return $form->toHTML();
179d5a1a6dcSAndreas Gohr    }
180d5a1a6dcSAndreas Gohr
181d5a1a6dcSAndreas Gohr    /**
18287fdbc6bSMichael Große     * Form to add a new schema
1838ddf87afSAndreas Gohr     *
1848ddf87afSAndreas Gohr     * @return string
18587fdbc6bSMichael Große     */
18687fdbc6bSMichael Große    protected function html_newschema() {
18787fdbc6bSMichael Große        $form = new Form();
1884e427bd5SAndreas Gohr        $form->addClass('struct_newschema');
18987fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
19087fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
191dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
19287fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
1934e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
1944e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
19587fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
19687fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
19787fdbc6bSMichael Große        $form->addFieldsetClose();
1988ddf87afSAndreas Gohr        return $form->toHTML();
19987fdbc6bSMichael Große    }
20087fdbc6bSMichael Große
20187fdbc6bSMichael Große    /**
20287fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
20387fdbc6bSMichael Große     *
20487fdbc6bSMichael Große     * @return array
20587fdbc6bSMichael Große     */
20687fdbc6bSMichael Große    public function getTOC() {
20787fdbc6bSMichael Große        global $ID;
20887fdbc6bSMichael Große
20987fdbc6bSMichael Große        $toc = array();
2108ddf87afSAndreas Gohr        $link = wl(
2118ddf87afSAndreas Gohr            $ID, array(
21287fdbc6bSMichael Große                   'do' => 'admin',
213dbffe06eSAndreas Gohr                   'page' => 'struct_assignments'
2148ddf87afSAndreas Gohr               )
2158ddf87afSAndreas Gohr        );
216dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
217*7c080d69SAndreas Gohr        $slink = wl(
2188ddf87afSAndreas Gohr            $ID, array(
219dbffe06eSAndreas Gohr                   'do' => 'admin',
220dbffe06eSAndreas Gohr                   'page' => 'struct_schemas'
2218ddf87afSAndreas Gohr               )
2228ddf87afSAndreas Gohr        );
223*7c080d69SAndreas Gohr        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
22487fdbc6bSMichael Große
225*7c080d69SAndreas Gohr        $tables = Schema::getAll('page');
226*7c080d69SAndreas Gohr        if($tables) {
227*7c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
228097f4a53SAndreas Gohr            foreach($tables as $table) {
2298ddf87afSAndreas Gohr                $link = wl(
2308ddf87afSAndreas Gohr                    $ID, array(
23187fdbc6bSMichael Große                           'do' => 'admin',
232dbffe06eSAndreas Gohr                           'page' => 'struct_schemas',
233097f4a53SAndreas Gohr                           'table' => $table
2348ddf87afSAndreas Gohr                       )
2358ddf87afSAndreas Gohr                );
23687fdbc6bSMichael Große
237*7c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
23887fdbc6bSMichael Große            }
239*7c080d69SAndreas Gohr        }
240*7c080d69SAndreas Gohr
241*7c080d69SAndreas Gohr        $tables = Schema::getAll('lookup');
242*7c080d69SAndreas Gohr        if($tables) {
243*7c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
244*7c080d69SAndreas Gohr            foreach($tables as $table) {
245*7c080d69SAndreas Gohr                $link = wl(
246*7c080d69SAndreas Gohr                    $ID, array(
247*7c080d69SAndreas Gohr                           'do' => 'admin',
248*7c080d69SAndreas Gohr                           'page' => 'struct_schemas',
249*7c080d69SAndreas Gohr                           'table' => $table
250*7c080d69SAndreas Gohr                       )
251*7c080d69SAndreas Gohr                );
252*7c080d69SAndreas Gohr
253*7c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
254*7c080d69SAndreas Gohr            }
255*7c080d69SAndreas Gohr        }
256*7c080d69SAndreas Gohr
25787fdbc6bSMichael Große        return $toc;
25887fdbc6bSMichael Große    }
25987fdbc6bSMichael Große
26087fdbc6bSMichael Große}
26187fdbc6bSMichael Große
26287fdbc6bSMichael Große// vim:ts=4:sw=4:et:
263