xref: /plugin/struct/admin/schemas.php (revision f36cc6349bec628714533650d22fbd77300c65ab)
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;
10*f36cc634SAndreas Gohruse dokuwiki\plugin\struct\meta\CSVExporter;
11a0b3799eSAndreas Gohruse dokuwiki\plugin\struct\meta\CSVImporter;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor;
15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
16ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1787fdbc6bSMichael Große
18d5a1a6dcSAndreas Gohr// must be run within Dokuwiki
1987fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
2087fdbc6bSMichael Große
2187fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin {
2287fdbc6bSMichael Große
2387fdbc6bSMichael Große    /**
2487fdbc6bSMichael Große     * @return int sort number in admin menu
2587fdbc6bSMichael Große     */
2687fdbc6bSMichael Große    public function getMenuSort() {
2787fdbc6bSMichael Große        return 500;
2887fdbc6bSMichael Große    }
2987fdbc6bSMichael Große
3087fdbc6bSMichael Große    /**
3187fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
3287fdbc6bSMichael Große     */
3387fdbc6bSMichael Große    public function forAdminOnly() {
344d220607SAndreas Gohr        return false;
3587fdbc6bSMichael Große    }
3687fdbc6bSMichael Große
3787fdbc6bSMichael Große    /**
3887fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
3987fdbc6bSMichael Große     */
4087fdbc6bSMichael Große    public function handle() {
4187fdbc6bSMichael Große        global $INPUT;
42d5a1a6dcSAndreas Gohr        global $ID;
43e33460e2SMichael Grosse        global $config_cascade;
44e33460e2SMichael Grosse        $config_file_path = end($config_cascade['main']['local']);
4587fdbc6bSMichael Große
468ddf87afSAndreas Gohr        // form submit
4787fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
4887fdbc6bSMichael Große        if($table && $INPUT->bool('save') && checkSecurityToken()) {
49d5a1a6dcSAndreas Gohr            $builder = new SchemaBuilder($table, $INPUT->arr('schema'));
5087fdbc6bSMichael Große            if(!$builder->build()) {
5187fdbc6bSMichael Große                msg('something went wrong while saving', -1);
5287fdbc6bSMichael Große            }
53ae5c46faSAndreas Gohr            touch(action_plugin_struct_cache::getSchemaRefreshFile());
5487fdbc6bSMichael Große        }
558ddf87afSAndreas Gohr        // export
56d486d6d7SAndreas Gohr        if($table && $INPUT->bool('export')) {
57d5a1a6dcSAndreas Gohr            $builder = new Schema($table);
58d486d6d7SAndreas Gohr            header('Content-Type: application/json');
59d486d6d7SAndreas Gohr            header("Content-Disposition: attachment; filename=$table.struct.json");
60d486d6d7SAndreas Gohr            echo $builder->toJSON();
61d486d6d7SAndreas Gohr            exit;
62d486d6d7SAndreas Gohr        }
638ddf87afSAndreas Gohr        // import
648ddf87afSAndreas Gohr        if($table && $INPUT->bool('import')) {
658ddf87afSAndreas Gohr            if(isset($_FILES['schemafile']['tmp_name'])) {
668ddf87afSAndreas Gohr                $json = io_readFile($_FILES['schemafile']['tmp_name'], false);
678ddf87afSAndreas Gohr                if(!$json) {
688ddf87afSAndreas Gohr                    msg('Something went wrong with the upload', -1);
698ddf87afSAndreas Gohr                } else {
700845722bSAndreas Gohr                    $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup'));
718ddf87afSAndreas Gohr                    if(!$builder->build()) {
728ddf87afSAndreas Gohr                        msg('something went wrong while saving', -1);
738ddf87afSAndreas Gohr                    }
74ae5c46faSAndreas Gohr                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
758ddf87afSAndreas Gohr                }
768ddf87afSAndreas Gohr            }
778ddf87afSAndreas Gohr        }
78a0b3799eSAndreas Gohr
79a0b3799eSAndreas Gohr        // import CSV
80a0b3799eSAndreas Gohr        if($table && $INPUT->bool('importcsv')) {
81a0b3799eSAndreas Gohr            if(isset($_FILES['csvfile']['tmp_name'])) {
82a0b3799eSAndreas Gohr                try {
83a0b3799eSAndreas Gohr                    new CSVImporter($table, $_FILES['csvfile']['tmp_name']);
84a0b3799eSAndreas Gohr                    msg('CSV imported', 1);
85a0b3799eSAndreas Gohr                } catch(StructException $e) {
86a0b3799eSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
87a0b3799eSAndreas Gohr                }
88a0b3799eSAndreas Gohr            }
89a0b3799eSAndreas Gohr        }
90a0b3799eSAndreas Gohr
91*f36cc634SAndreas Gohr        // export CSV
92*f36cc634SAndreas Gohr        if($table && $INPUT->bool('exportcsv')) {
93*f36cc634SAndreas Gohr            header('Content-Type: text/csv');
94*f36cc634SAndreas Gohr            header('Content-Disposition: attachment; filename="' . $table . '.csv";');
95*f36cc634SAndreas Gohr            new CSVExporter($table);
96*f36cc634SAndreas Gohr            exit();
97*f36cc634SAndreas Gohr        }
98*f36cc634SAndreas Gohr
99d5a1a6dcSAndreas Gohr        // delete
100d5a1a6dcSAndreas Gohr        if($table && $INPUT->bool('delete')) {
101d5a1a6dcSAndreas Gohr            if($table != $INPUT->str('confirm')) {
102d5a1a6dcSAndreas Gohr                msg($this->getLang('del_fail'), -1);
103d5a1a6dcSAndreas Gohr            } else {
104d5a1a6dcSAndreas Gohr                try {
105d5a1a6dcSAndreas Gohr                    $schema = new Schema($table);
106d5a1a6dcSAndreas Gohr                    $schema->delete();
107d5a1a6dcSAndreas Gohr                    msg($this->getLang('del_ok'), 1);
108ae5c46faSAndreas Gohr                    touch(action_plugin_struct_cache::getSchemaRefreshFile());
109d5a1a6dcSAndreas Gohr                    send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&'));
110d5a1a6dcSAndreas Gohr                } catch(StructException $e) {
111d5a1a6dcSAndreas Gohr                    msg(hsc($e->getMessage()), -1);
112d5a1a6dcSAndreas Gohr                }
113d5a1a6dcSAndreas Gohr            }
114d5a1a6dcSAndreas Gohr        }
115d5a1a6dcSAndreas Gohr
11687fdbc6bSMichael Große    }
11787fdbc6bSMichael Große
11887fdbc6bSMichael Große    /**
11987fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
12087fdbc6bSMichael Große     */
12187fdbc6bSMichael Große    public function html() {
12287fdbc6bSMichael Große        global $INPUT;
12387fdbc6bSMichael Große
12487fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
12587fdbc6bSMichael Große        if($table) {
1267c080d69SAndreas Gohr            $schema = new Schema($table, 0, $INPUT->bool('lookup'));
1277c080d69SAndreas Gohr            if($schema->isLookup()) {
1287c080d69SAndreas Gohr                $hl = 'edithl lookup';
1297c080d69SAndreas Gohr            } else {
1307c080d69SAndreas Gohr                $hl = 'edithl page';
1317c080d69SAndreas Gohr            }
1327c080d69SAndreas Gohr
1336af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_edit');
1347c080d69SAndreas Gohr            echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>';
1358ddf87afSAndreas Gohr
1368ddf87afSAndreas Gohr            echo '<ul class="tabs" id="plugin__struct_tabs">';
1378ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1388ddf87afSAndreas Gohr            echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>';
1398ddf87afSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
1408ddf87afSAndreas Gohr            echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>';
141d5a1a6dcSAndreas Gohr            /** @noinspection HtmlUnknownAnchorTarget */
142d5a1a6dcSAndreas Gohr            echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>';
1438ddf87afSAndreas Gohr            echo '</ul>';
1448ddf87afSAndreas Gohr            echo '<div class="panelHeader"></div>';
1458ddf87afSAndreas Gohr
1467c080d69SAndreas Gohr            $editor = new SchemaEditor($schema);
14787fdbc6bSMichael Große            echo $editor->getEditor();
1480845722bSAndreas Gohr            echo $this->html_json($schema);
1490845722bSAndreas Gohr            echo $this->html_delete($schema);
150d486d6d7SAndreas Gohr
15187fdbc6bSMichael Große        } else {
1526af24d3eSAndreas Gohr            echo $this->locale_xhtml('editor_intro');
1538ddf87afSAndreas Gohr            echo $this->html_newschema();
15487fdbc6bSMichael Große        }
15587fdbc6bSMichael Große    }
15687fdbc6bSMichael Große
15787fdbc6bSMichael Große    /**
1588ddf87afSAndreas Gohr     * Form for handling import/export from/to JSON
1590845722bSAndreas Gohr     *
1600845722bSAndreas Gohr     * @param Schema $schema
1618ddf87afSAndreas Gohr     * @return string
1628ddf87afSAndreas Gohr     */
1630845722bSAndreas Gohr    protected function html_json(Schema $schema) {
1648ddf87afSAndreas Gohr        $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json'));
1658ddf87afSAndreas Gohr        $form->setHiddenField('do', 'admin');
1668ddf87afSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
1670845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
1680845722bSAndreas Gohr        $form->setHiddenField('lookup', $schema->isLookup());
1698ddf87afSAndreas Gohr
1708ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('export'));
1718ddf87afSAndreas Gohr        $form->addButton('export', $this->getLang('btn_export'));
1728ddf87afSAndreas Gohr        $form->addFieldsetClose();
1738ddf87afSAndreas Gohr
1748ddf87afSAndreas Gohr        $form->addFieldsetOpen($this->getLang('import'));
1758ddf87afSAndreas Gohr        $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'));
1768ddf87afSAndreas Gohr        $form->addButton('import', $this->getLang('btn_import'));
1778ddf87afSAndreas Gohr        $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>');
1788ddf87afSAndreas Gohr        $form->addFieldsetClose();
179a0b3799eSAndreas Gohr
180*f36cc634SAndreas Gohr        $form->addFieldsetOpen($this->getLang('csvexport'));
181*f36cc634SAndreas Gohr        $form->addButton('exportcsv', $this->getLang('btn_export'));
182*f36cc634SAndreas Gohr        $form->addFieldsetClose();
183*f36cc634SAndreas Gohr
184a0b3799eSAndreas Gohr        if($schema->isLookup()) {
185a0b3799eSAndreas Gohr            $form->addFieldsetOpen($this->getLang('csvimport'));
186a0b3799eSAndreas Gohr            $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'));
187a0b3799eSAndreas Gohr            $form->addButton('importcsv', $this->getLang('btn_import'));
188a0b3799eSAndreas Gohr            $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('csv_help_link') . '</a></p>');
189a0b3799eSAndreas Gohr            $form->addFieldsetClose();
190a0b3799eSAndreas Gohr        }
191a0b3799eSAndreas Gohr
1928ddf87afSAndreas Gohr        return $form->toHTML();
1938ddf87afSAndreas Gohr    }
1948ddf87afSAndreas Gohr
1958ddf87afSAndreas Gohr    /**
196d5a1a6dcSAndreas Gohr     * Form for deleting schemas
1970845722bSAndreas Gohr     *
1980845722bSAndreas Gohr     * @param Schema $schema
199d5a1a6dcSAndreas Gohr     * @return string
200d5a1a6dcSAndreas Gohr     */
2010845722bSAndreas Gohr    protected function html_delete(Schema $schema) {
202d5a1a6dcSAndreas Gohr        $form = new Form(array('id' => 'plugin__struct_delete'));
203d5a1a6dcSAndreas Gohr        $form->setHiddenField('do', 'admin');
204d5a1a6dcSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
2050845722bSAndreas Gohr        $form->setHiddenField('table', $schema->getTable());
206d5a1a6dcSAndreas Gohr
207d5a1a6dcSAndreas Gohr        $form->addHTML($this->locale_xhtml('delete_intro'));
208d5a1a6dcSAndreas Gohr
209d5a1a6dcSAndreas Gohr        $form->addFieldsetOpen($this->getLang('tab_delete'));
210d5a1a6dcSAndreas Gohr        $form->addTextInput('confirm', $this->getLang('del_confirm'));
211d5a1a6dcSAndreas Gohr        $form->addButton('delete', $this->getLang('btn_delete'));
212d5a1a6dcSAndreas Gohr        $form->addFieldsetClose();
213d5a1a6dcSAndreas Gohr        return $form->toHTML();
214d5a1a6dcSAndreas Gohr    }
215d5a1a6dcSAndreas Gohr
216d5a1a6dcSAndreas Gohr    /**
21787fdbc6bSMichael Große     * Form to add a new schema
2188ddf87afSAndreas Gohr     *
2198ddf87afSAndreas Gohr     * @return string
22087fdbc6bSMichael Große     */
22187fdbc6bSMichael Große    protected function html_newschema() {
22287fdbc6bSMichael Große        $form = new Form();
2234e427bd5SAndreas Gohr        $form->addClass('struct_newschema');
22487fdbc6bSMichael Große        $form->addFieldsetOpen($this->getLang('create'));
22587fdbc6bSMichael Große        $form->setHiddenField('do', 'admin');
226dbffe06eSAndreas Gohr        $form->setHiddenField('page', 'struct_schemas');
22787fdbc6bSMichael Große        $form->addTextInput('table', $this->getLang('schemaname'));
2284e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked');
2294e427bd5SAndreas Gohr        $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1');
23087fdbc6bSMichael Große        $form->addButton('', $this->getLang('save'));
23187fdbc6bSMichael Große        $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could
23287fdbc6bSMichael Große        $form->addFieldsetClose();
2338ddf87afSAndreas Gohr        return $form->toHTML();
23487fdbc6bSMichael Große    }
23587fdbc6bSMichael Große
23687fdbc6bSMichael Große    /**
23787fdbc6bSMichael Große     * Adds all available schemas to the Table of Contents
23887fdbc6bSMichael Große     *
23987fdbc6bSMichael Große     * @return array
24087fdbc6bSMichael Große     */
24187fdbc6bSMichael Große    public function getTOC() {
24287fdbc6bSMichael Große        global $ID;
24387fdbc6bSMichael Große
24487fdbc6bSMichael Große        $toc = array();
2458ddf87afSAndreas Gohr        $link = wl(
2468ddf87afSAndreas Gohr            $ID, array(
24787fdbc6bSMichael Große                   'do' => 'admin',
248dbffe06eSAndreas Gohr                   'page' => 'struct_assignments'
2498ddf87afSAndreas Gohr               )
2508ddf87afSAndreas Gohr        );
251dbffe06eSAndreas Gohr        $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, '');
2527c080d69SAndreas Gohr        $slink = wl(
2538ddf87afSAndreas Gohr            $ID, array(
254dbffe06eSAndreas Gohr                   'do' => 'admin',
255dbffe06eSAndreas Gohr                   'page' => 'struct_schemas'
2568ddf87afSAndreas Gohr               )
2578ddf87afSAndreas Gohr        );
2587c080d69SAndreas Gohr        $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, '');
25987fdbc6bSMichael Große
2607c080d69SAndreas Gohr        $tables = Schema::getAll('page');
2617c080d69SAndreas Gohr        if($tables) {
2627c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, '');
263097f4a53SAndreas Gohr            foreach($tables as $table) {
2648ddf87afSAndreas Gohr                $link = wl(
2658ddf87afSAndreas Gohr                    $ID, array(
26687fdbc6bSMichael Große                           'do' => 'admin',
267dbffe06eSAndreas Gohr                           'page' => 'struct_schemas',
268097f4a53SAndreas Gohr                           'table' => $table
2698ddf87afSAndreas Gohr                       )
2708ddf87afSAndreas Gohr                );
27187fdbc6bSMichael Große
2727c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
27387fdbc6bSMichael Große            }
2747c080d69SAndreas Gohr        }
2757c080d69SAndreas Gohr
2767c080d69SAndreas Gohr        $tables = Schema::getAll('lookup');
2777c080d69SAndreas Gohr        if($tables) {
2787c080d69SAndreas Gohr            $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, '');
2797c080d69SAndreas Gohr            foreach($tables as $table) {
2807c080d69SAndreas Gohr                $link = wl(
2817c080d69SAndreas Gohr                    $ID, array(
2827c080d69SAndreas Gohr                           'do' => 'admin',
2837c080d69SAndreas Gohr                           'page' => 'struct_schemas',
2847c080d69SAndreas Gohr                           'table' => $table
2857c080d69SAndreas Gohr                       )
2867c080d69SAndreas Gohr                );
2877c080d69SAndreas Gohr
2887c080d69SAndreas Gohr                $toc[] = html_mktocitem($link, hsc($table), 2, '');
2897c080d69SAndreas Gohr            }
2907c080d69SAndreas Gohr        }
2917c080d69SAndreas Gohr
29287fdbc6bSMichael Große        return $toc;
29387fdbc6bSMichael Große    }
29487fdbc6bSMichael Große
295a0b3799eSAndreas Gohr
296a0b3799eSAndreas Gohr
29787fdbc6bSMichael Große}
29887fdbc6bSMichael Große
29987fdbc6bSMichael Große// vim:ts=4:sw=4:et:
300