xref: /plugin/struct/admin/schemas.php (revision 1fc2361fe236472e7585162e283a99db231aaf7e)
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;
10f36cc634SAndreas Gohruse dokuwiki\plugin\struct\meta\CSVExporter;
11*1fc2361fSSzymon Olewniczakuse dokuwiki\plugin\struct\meta\CSVLookupImporter;
12*1fc2361fSSzymon Olewniczakuse dokuwiki\plugin\struct\meta\CSVPageImporter;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder;
15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor;
16ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
17ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1887fdbc6bSMichael Große
19d5a1a6dcSAndreas Gohr// must be run within Dokuwiki
2087fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
2187fdbc6bSMichael Große
2287fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
2387fdbc6bSMichael Große
2487fdbc6bSMichael Große    /**
2587fdbc6bSMichael Große     * @return int sort number in admin menu
2687fdbc6bSMichael Große     */
2787fdbc6bSMichael Große    public function getMenuSort() {
2887fdbc6bSMichael Große        return 500;
2987fdbc6bSMichael Große    }
3087fdbc6bSMichael Große
3187fdbc6bSMichael Große    /**
3287fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
3387fdbc6bSMichael Große     */
3487fdbc6bSMichael Große    public function forAdminOnly() {
354d220607SAndreas Gohr        return false;
3687fdbc6bSMichael Große    }
3787fdbc6bSMichael Große
3887fdbc6bSMichael Große    /**
3987fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
4087fdbc6bSMichael Große     */
4187fdbc6bSMichael Große    public function handle() {
4287fdbc6bSMichael Große        global $INPUT;
43d5a1a6dcSAndreas Gohr        global $ID;
44e33460e2SMichael Grosse        global $config_cascade;
45e33460e2SMichael Grosse        $config_file_path = end($config_cascade['main']['local']);
4687fdbc6bSMichael Große
478ddf87afSAndreas Gohr        // form submit
4887fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
4987fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
50d5a1a6dcSAndreas Gohr            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
5187fdbc6bSMichael Große            if(!$builder->build()) {
5287fdbc6bSMichael Große                msg('something went wrong while saving', -1);
5387fdbc6bSMichael Große            }
54ae5c46faSAndreas Gohr            touch(action_plugin_struct_cache::getSchemaRefreshFile());
5587fdbc6bSMichael Große        }
568ddf87afSAndreas Gohr        // export
57d486d6d7SAndreas Gohr        if($table && $INPUT->bool('export')) {
58d5a1a6dcSAndreas Gohr            $builder = new Schema($table);
59d486d6d7SAndreas Gohr            header('Content-Type: application/json');
60d486d6d7SAndreas Gohr            header("Content-Disposition: attachment; filename=$table.struct.json");
61d486d6d7SAndreas Gohr            echo $builder->toJSON();
62d486d6d7SAndreas Gohr            exit;
63d486d6d7SAndreas Gohr        }
648ddf87afSAndreas Gohr        // import
658ddf87afSAndreas Gohr        if($table && $INPUT->bool('import')) {
668ddf87afSAndreas Gohr            if(isset($_FILES['schemafile']['tmp_name'])) {
678ddf87afSAndreas Gohr                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
688ddf87afSAndreas Gohr                if(!$json) {
698ddf87afSAndreas Gohr                    msg('Something went wrong with the upload', -1);
708ddf87afSAndreas Gohr                } else {
710845722bSAndreas Gohr                    $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
728ddf87afSAndreas Gohr                    if(!$builder->build()) {
738ddf87afSAndreas Gohr                        msg('something went wrong while saving', -1);
748ddf87afSAndreas Gohr                    }
75ae5c46faSAndreas Gohr                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
768ddf87afSAndreas Gohr                }
778ddf87afSAndreas Gohr            }
788ddf87afSAndreas Gohr        }
79a0b3799eSAndreas Gohr
80a0b3799eSAndreas Gohr        // import CSV
81a0b3799eSAndreas Gohr        if($table && $INPUT->bool('importcsv')) {
82a0b3799eSAndreas Gohr            if(isset($_FILES['csvfile']['tmp_name'])) {
83a0b3799eSAndreas Gohr                try {
84*1fc2361fSSzymon Olewniczak                    if ($INPUT->bool('lookup')) {
85*1fc2361fSSzymon Olewniczak                        $csvImporter = new CSVLookupImporter($table, $_FILES['csvfile']['tmp_name']);
86*1fc2361fSSzymon Olewniczak                    } else {
87*1fc2361fSSzymon Olewniczak                        $csvImporter = new CSVPageImporter($table, $_FILES['csvfile']['tmp_name']);
88*1fc2361fSSzymon Olewniczak                    }
89*1fc2361fSSzymon Olewniczak                    $csvImporter->import();
90*1fc2361fSSzymon Olewniczak
916d2df532SAndreas Gohr                    msg($this->getLang('admin_csvdone'), 1);
92a0b3799eSAndreas Gohr                } catch(StructException $e) {
93a0b3799eSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
94a0b3799eSAndreas Gohr                }
95a0b3799eSAndreas Gohr            }
96a0b3799eSAndreas Gohr        }
97a0b3799eSAndreas Gohr
98f36cc634SAndreas Gohr        // export CSV
99f36cc634SAndreas Gohr        if($table && $INPUT->bool('exportcsv')) {
100f36cc634SAndreas Gohr            header('Content-Type: text/csv');
101f36cc634SAndreas Gohr            header('Content-Disposition: attachment; filename="' . $table . '.csv";');
102f36cc634SAndreas Gohr            new CSVExporter($table);
103f36cc634SAndreas Gohr            exit();
104f36cc634SAndreas Gohr        }
105f36cc634SAndreas Gohr
106d5a1a6dcSAndreas Gohr        // delete
107d5a1a6dcSAndreas Gohr        if($table && $INPUT->bool('delete')) {
108d5a1a6dcSAndreas Gohr            if($table != $INPUT->str('confirm')) {
109d5a1a6dcSAndreas Gohr                msg($this->getLang('del_fail'), -1);
110d5a1a6dcSAndreas Gohr            } else {
111d5a1a6dcSAndreas Gohr                try {
112d5a1a6dcSAndreas Gohr                    $schema = new Schema($table);
113d5a1a6dcSAndreas Gohr                    $schema->delete();
114d5a1a6dcSAndreas Gohr                    msg($this->getLang('del_ok'), 1);
115ae5c46faSAndreas Gohr                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
116d5a1a6dcSAndreas Gohr                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
117d5a1a6dcSAndreas Gohr                } catch(StructException $e) {
118d5a1a6dcSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
119d5a1a6dcSAndreas Gohr                }
120d5a1a6dcSAndreas Gohr            }
121d5a1a6dcSAndreas Gohr        }
122d5a1a6dcSAndreas Gohr
12387fdbc6bSMichael Große    }
12487fdbc6bSMichael Große
12587fdbc6bSMichael Große    /**
12687fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
12787fdbc6bSMichael Große     */
12887fdbc6bSMichael Große    public function html() {
12987fdbc6bSMichael Große        global $INPUT;
13087fdbc6bSMichael Große
13187fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
13287fdbc6bSMichael Große        if($table) {
1337c080d69SAndreas Gohr            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
1347c080d69SAndreas Gohr            if($schema->isLookup()) {
1357c080d69SAndreas Gohr                $hl = 'edithl lookup';
1367c080d69SAndreas Gohr            } else {
1377c080d69SAndreas Gohr                $hl = 'edithl page';
1387c080d69SAndreas Gohr            }
1397c080d69SAndreas Gohr
1406af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
1417c080d69SAndreas Gohr            echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>';
1428ddf87afSAndreas Gohr
1438ddf87afSAndreas Gohr            echo '<ul class="tabs" id="plugin__struct_tabs">';
1448ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1458ddf87afSAndreas Gohr            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
1468ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1478ddf87afSAndreas Gohr            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
148d5a1a6dcSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
149d5a1a6dcSAndreas Gohr            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
1508ddf87afSAndreas Gohr            echo '</ul>';
1518ddf87afSAndreas Gohr            echo '<div class="panelHeader"></div>';
1528ddf87afSAndreas Gohr
1537c080d69SAndreas Gohr            $editor = new SchemaEditor($schema);
15487fdbc6bSMichael Große            echo $editor->getEditor();
1550845722bSAndreas Gohr            echo $this->html_json($schema);
1560845722bSAndreas Gohr            echo $this->html_delete($schema);
157d486d6d7SAndreas Gohr
15887fdbc6bSMichael Große        } else {
1596af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
1608ddf87afSAndreas Gohr            echo $this->html_newschema();
16187fdbc6bSMichael Große        }
16287fdbc6bSMichael Große    }
16387fdbc6bSMichael Große
16487fdbc6bSMichael Große    /**
1658ddf87afSAndreas Gohr     * Form for handling import/export from/to JSON
1660845722bSAndreas Gohr     *
1670845722bSAndreas Gohr     * @param Schema $schema
1688ddf87afSAndreas Gohr     * @return string
1698ddf87afSAndreas Gohr     */
1700845722bSAndreas Gohr    protected function html_json(Schema $schema) {
1718ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
1728ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
1738ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
1740845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
1750845722bSAndreas Gohr        $form->setHiddenField('lookup', $schema->isLookup());
1768ddf87afSAndreas Gohr
1778ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('export'));
1788ddf87afSAndreas Gohr        $form->addButton('export', $this->getLang('btn_export'));
1798ddf87afSAndreas Gohr        $form->addFieldsetClose();
1808ddf87afSAndreas Gohr
1818ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('import'));
1828ddf87afSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
1838ddf87afSAndreas Gohr        $form->addButton('import', $this->getLang('btn_import'));
1848ddf87afSAndreas Gohr        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
1858ddf87afSAndreas Gohr        $form->addFieldsetClose();
186a0b3799eSAndreas Gohr
1876d2df532SAndreas Gohr        $form->addFieldsetOpen($this->getLang('admin_csvexport'));
188f36cc634SAndreas Gohr        $form->addButton('exportcsv', $this->getLang('btn_export'));
189f36cc634SAndreas Gohr        $form->addFieldsetClose();
190f36cc634SAndreas Gohr
1916d2df532SAndreas Gohr        $form->addFieldsetOpen($this->getLang('admin_csvimport'));
192a0b3799eSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'));
193a0b3799eSAndreas Gohr        $form->addButton('importcsv', $this->getLang('btn_import'));
1946d2df532SAndreas Gohr        $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>');
195a0b3799eSAndreas Gohr        $form->addFieldsetClose();
196a0b3799eSAndreas Gohr
1978ddf87afSAndreas Gohr        return $form->toHTML();
1988ddf87afSAndreas Gohr    }
1998ddf87afSAndreas Gohr
2008ddf87afSAndreas Gohr    /**
201d5a1a6dcSAndreas Gohr     * Form for deleting schemas
2020845722bSAndreas Gohr     *
2030845722bSAndreas Gohr     * @param Schema $schema
204d5a1a6dcSAndreas Gohr     * @return string
205d5a1a6dcSAndreas Gohr     */
2060845722bSAndreas Gohr    protected function html_delete(Schema $schema) {
207d5a1a6dcSAndreas Gohr        $form = new Form(array('id' => 'plugin__struct_delete'));
208d5a1a6dcSAndreas Gohr        $form->setHiddenField('do', 'admin');
209d5a1a6dcSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
2100845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
211d5a1a6dcSAndreas Gohr
212d5a1a6dcSAndreas Gohr        $form->addHTML($this->locale_xhtml('delete_intro'));
213d5a1a6dcSAndreas Gohr
214d5a1a6dcSAndreas Gohr        $form->addFieldsetOpen($this->getLang('tab_delete'));
215d5a1a6dcSAndreas Gohr        $form->addTextInput('confirm', $this->getLang('del_confirm'));
216d5a1a6dcSAndreas Gohr        $form->addButton('delete', $this->getLang('btn_delete'));
217d5a1a6dcSAndreas Gohr        $form->addFieldsetClose();
218d5a1a6dcSAndreas Gohr        return $form->toHTML();
219d5a1a6dcSAndreas Gohr    }
220d5a1a6dcSAndreas Gohr
221d5a1a6dcSAndreas Gohr    /**
22287fdbc6bSMichael Große     * Form to add a new schema
2238ddf87afSAndreas Gohr     *
2248ddf87afSAndreas Gohr     * @return string
22587fdbc6bSMichael Große     */
22687fdbc6bSMichael Große    protected function html_newschema() {
22787fdbc6bSMichael Große        $form = new Form();
2284e427bd5SAndreas Gohr        $form->addClass('struct_newschema');
22987fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
23087fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
231dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
23287fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
2334e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
2344e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
23587fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
23687fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
23787fdbc6bSMichael Große        $form->addFieldsetClose();
2388ddf87afSAndreas Gohr        return $form->toHTML();
23987fdbc6bSMichael Große    }
24087fdbc6bSMichael Große
24187fdbc6bSMichael Große    /**
24287fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
24387fdbc6bSMichael Große     *
24487fdbc6bSMichael Große     * @return array
24587fdbc6bSMichael Große     */
24687fdbc6bSMichael Große    public function getTOC() {
24787fdbc6bSMichael Große        global $ID;
24887fdbc6bSMichael Große
24987fdbc6bSMichael Große        $toc = array();
2508ddf87afSAndreas Gohr        $link = wl(
2518ddf87afSAndreas Gohr            $ID, array(
25287fdbc6bSMichael Große                   'do' => 'admin',
253dbffe06eSAndreas Gohr                   'page' => 'struct_assignments'
2548ddf87afSAndreas Gohr               )
2558ddf87afSAndreas Gohr        );
256dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
2577c080d69SAndreas Gohr        $slink = wl(
2588ddf87afSAndreas Gohr            $ID, array(
259dbffe06eSAndreas Gohr                   'do' => 'admin',
260dbffe06eSAndreas Gohr                   'page' => 'struct_schemas'
2618ddf87afSAndreas Gohr               )
2628ddf87afSAndreas Gohr        );
2637c080d69SAndreas Gohr        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
26487fdbc6bSMichael Große
2657c080d69SAndreas Gohr        $tables = Schema::getAll('page');
2667c080d69SAndreas Gohr        if($tables) {
2677c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
268097f4a53SAndreas Gohr            foreach($tables as $table) {
2698ddf87afSAndreas Gohr                $link = wl(
2708ddf87afSAndreas Gohr                    $ID, array(
27187fdbc6bSMichael Große                           'do' => 'admin',
272dbffe06eSAndreas Gohr                           'page' => 'struct_schemas',
273097f4a53SAndreas Gohr                           'table' => $table
2748ddf87afSAndreas Gohr                       )
2758ddf87afSAndreas Gohr                );
27687fdbc6bSMichael Große
2777c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
27887fdbc6bSMichael Große            }
2797c080d69SAndreas Gohr        }
2807c080d69SAndreas Gohr
2817c080d69SAndreas Gohr        $tables = Schema::getAll('lookup');
2827c080d69SAndreas Gohr        if($tables) {
2837c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
2847c080d69SAndreas Gohr            foreach($tables as $table) {
2857c080d69SAndreas Gohr                $link = wl(
2867c080d69SAndreas Gohr                    $ID, array(
2877c080d69SAndreas Gohr                           'do' => 'admin',
2887c080d69SAndreas Gohr                           'page' => 'struct_schemas',
2897c080d69SAndreas Gohr                           'table' => $table
2907c080d69SAndreas Gohr                       )
2917c080d69SAndreas Gohr                );
2927c080d69SAndreas Gohr
2937c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
2947c080d69SAndreas Gohr            }
2957c080d69SAndreas Gohr        }
2967c080d69SAndreas Gohr
29787fdbc6bSMichael Große        return $toc;
29887fdbc6bSMichael Große    }
29987fdbc6bSMichael Große
300a0b3799eSAndreas Gohr
301a0b3799eSAndreas Gohr
30287fdbc6bSMichael Große}
30387fdbc6bSMichael Große
30487fdbc6bSMichael Große// vim:ts=4:sw=4:et:
305