xref: /plugin/struct/admin/assignments.php (revision 40b81cab7fcfbaf421e6ee2e80edafd63f0ae2dd)
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ße// must be run within Dokuwiki
1087fdbc6bSMichael Großeuse dokuwiki\Form\Form;
1187fdbc6bSMichael Großeuse plugin\struct\meta\Schema;
1287fdbc6bSMichael Großeuse plugin\struct\meta\SchemaEditor;
1387fdbc6bSMichael Große
1487fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
1587fdbc6bSMichael Große
1687fdbc6bSMichael Großeclass admin_plugin_struct_assignments extends DokuWiki_Admin_Plugin {
1787fdbc6bSMichael Große
1887fdbc6bSMichael Große    /** @var helper_plugin_sqlite */
1987fdbc6bSMichael Große    protected $sqlite;
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 501;
2687fdbc6bSMichael Große    }
2787fdbc6bSMichael Große
28*40b81cabSAndreas Gohr    /**
29*40b81cabSAndreas Gohr     * Return the text that is displayed at the main admin menu
30*40b81cabSAndreas Gohr     *
31*40b81cabSAndreas Gohr     * @param string $language language code
32*40b81cabSAndreas Gohr     * @return string menu string
33*40b81cabSAndreas Gohr     */
34*40b81cabSAndreas Gohr    public function getMenuText($language) {
35*40b81cabSAndreas Gohr        return $this->getLang('menu_assignments');
3687fdbc6bSMichael Große    }
3787fdbc6bSMichael Große
3887fdbc6bSMichael Große    /**
3987fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
4087fdbc6bSMichael Große     */
4187fdbc6bSMichael Große    public function forAdminOnly() {
4287fdbc6bSMichael Große        return true;
4387fdbc6bSMichael Große    }
4487fdbc6bSMichael Große
4587fdbc6bSMichael Große    /**
4687fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
4787fdbc6bSMichael Große     */
4887fdbc6bSMichael Große    public function handle() {
4987fdbc6bSMichael Große        global $INPUT;
5087fdbc6bSMichael Große
5187fdbc6bSMichael Große        /** @var \helper_plugin_struct_db $helper */
5287fdbc6bSMichael Große        $helper = plugin_load('helper', 'struct_db');
5387fdbc6bSMichael Große        $this->sqlite = $helper->getDB();
5487fdbc6bSMichael Große
5587fdbc6bSMichael Große        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
5687fdbc6bSMichael Große            $assignment = $INPUT->arr('assignment');
5787fdbc6bSMichael Große            if ($INPUT->str('action') === 'delete') {
5887fdbc6bSMichael Große                $sql = 'DELETE FROM schema_assignments WHERE assign = ? AND tbl = ?';
5987fdbc6bSMichael Große            }
6087fdbc6bSMichael Große            if ($INPUT->str('action') === 'add') {
6187fdbc6bSMichael Große                $sql = 'REPLACE INTO schema_assignments (assign, tbl) VALUES (?,?)';
6287fdbc6bSMichael Große            }
6387fdbc6bSMichael Große            $opts = array($assignment['assign'], $assignment['tbl']);
6487fdbc6bSMichael Große            if(empty($sql) || empty($assignment['assign']) || empty($assignment['tbl']) || !$this->sqlite->query($sql, $opts)) {
6587fdbc6bSMichael Große                msg('something went wrong while saving', -1);
6687fdbc6bSMichael Große            }
6787fdbc6bSMichael Große        }
6887fdbc6bSMichael Große
6987fdbc6bSMichael Große    }
7087fdbc6bSMichael Große
7187fdbc6bSMichael Große    /**
7287fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
7387fdbc6bSMichael Große     */
7487fdbc6bSMichael Große    public function html() {
7587fdbc6bSMichael Große        global $INPUT;
7687fdbc6bSMichael Große
7787fdbc6bSMichael Große        echo $this->locale_xhtml('assignments_intro');
7887fdbc6bSMichael Große
7987fdbc6bSMichael Große        $res = $this->sqlite->query('SELECT tbl FROM schemas GROUP BY tbl');
8087fdbc6bSMichael Große        $schemas = $this->sqlite->res2arr($res);
8187fdbc6bSMichael Große        $this->sqlite->res_close($res);
8287fdbc6bSMichael Große
8387fdbc6bSMichael Große        $res = $this->sqlite->query('SELECT * FROM schema_assignments ORDER BY assign');
8487fdbc6bSMichael Große        $assignments = $this->sqlite->res2arr($res);
8587fdbc6bSMichael Große        $this->sqlite->res_close($res);
8687fdbc6bSMichael Große
8787fdbc6bSMichael Große        echo '<ul>';
8887fdbc6bSMichael Große        foreach ($assignments as $assignment) {
8987fdbc6bSMichael Große            $schema = $assignment['tbl'];
9087fdbc6bSMichael Große            $assignee = $assignment['assign'];
9187fdbc6bSMichael Große            $form = new Form();
9287fdbc6bSMichael Große            $form->setHiddenField("assignment[assign]", $assignee);
9387fdbc6bSMichael Große            $form->setHiddenField("assignment[tbl]", $schema);
9487fdbc6bSMichael Große            $form->addHTML("<button type=\"submit\" name=\"action\" value=\"delete\">Delete</button>");
9587fdbc6bSMichael Große            $html = "<li class=\"level1\"><div class=\"li\">$assignee - $schema ";
9687fdbc6bSMichael Große            $html .= $form->toHTML();
9787fdbc6bSMichael Große            $html .= "</div></li>";
9887fdbc6bSMichael Große            echo $html;
9987fdbc6bSMichael Große        }
10087fdbc6bSMichael Große        $form = new Form();
10187fdbc6bSMichael Große        $form->addTextInput("assignment[assign]",'Page or Namespace: ');
10287fdbc6bSMichael Große        $form->addLabel('Schema','schemaSelect');
10387fdbc6bSMichael Große        $form->addHTML('<select id="schemaSelect" name="assignment[tbl]">');
10487fdbc6bSMichael Große        foreach ($schemas as $schema){
10587fdbc6bSMichael Große            $form->addHTML('<option value="'. $schema['tbl'] .'">'. $schema['tbl'] . '</option>');
10687fdbc6bSMichael Große        }
10787fdbc6bSMichael Große        $form->addHTML('</select>');
10887fdbc6bSMichael Große        $form->addHTML("<button type=\"submit\" name=\"action\" value=\"add\">Add</button>");
10987fdbc6bSMichael Große        $html = "<li class=\"level1\"><div class=\"li\">";
11087fdbc6bSMichael Große        $html .= $form->toHTML();
11187fdbc6bSMichael Große        $html .= "</div></li>";
11287fdbc6bSMichael Große        echo $html;
11387fdbc6bSMichael Große        echo '</ul>';
11487fdbc6bSMichael Große
11587fdbc6bSMichael Große
11687fdbc6bSMichael Große        $table = Schema::cleanTableName($INPUT->str('table'));
11787fdbc6bSMichael Große        if($table) {
11887fdbc6bSMichael Große            echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>';
11987fdbc6bSMichael Große
12087fdbc6bSMichael Große            $editor = new SchemaEditor(new Schema($table));
12187fdbc6bSMichael Große            echo $editor->getEditor();
12287fdbc6bSMichael Große        } else {
12387fdbc6bSMichael Große            $this->html_newschema();
12487fdbc6bSMichael Große        }
12587fdbc6bSMichael Große    }
12687fdbc6bSMichael Große
12787fdbc6bSMichael Große}
12887fdbc6bSMichael Große
12987fdbc6bSMichael Große// vim:ts=4:sw=4:et:
130