xref: /plugin/struct/admin/assignments.php (revision 28e054687da43fd43ab16ca430b183b20dc5ebfe)
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;
111a8d1235SAndreas Gohruse plugin\struct\meta\Assignments;
1287fdbc6bSMichael Großeuse plugin\struct\meta\Schema;
1387fdbc6bSMichael Großeuse plugin\struct\meta\SchemaEditor;
1487fdbc6bSMichael Große
1587fdbc6bSMichael Großeif(!defined('DOKU_INC')) die();
1687fdbc6bSMichael Große
1787fdbc6bSMichael Großeclass admin_plugin_struct_assignments extends DokuWiki_Admin_Plugin {
1887fdbc6bSMichael Große
1987fdbc6bSMichael Große    /**
2087fdbc6bSMichael Große     * @return int sort number in admin menu
2187fdbc6bSMichael Große     */
2287fdbc6bSMichael Große    public function getMenuSort() {
2387fdbc6bSMichael Große        return 501;
2487fdbc6bSMichael Große    }
2587fdbc6bSMichael Große
2640b81cabSAndreas Gohr    /**
2740b81cabSAndreas Gohr     * Return the text that is displayed at the main admin menu
2840b81cabSAndreas Gohr     *
2940b81cabSAndreas Gohr     * @param string $language language code
3040b81cabSAndreas Gohr     * @return string menu string
3140b81cabSAndreas Gohr     */
3240b81cabSAndreas Gohr    public function getMenuText($language) {
3340b81cabSAndreas Gohr        return $this->getLang('menu_assignments');
3487fdbc6bSMichael Große    }
3587fdbc6bSMichael Große
3687fdbc6bSMichael Große    /**
3787fdbc6bSMichael Große     * @return bool true if only access for superuser, false is for superusers and moderators
3887fdbc6bSMichael Große     */
3987fdbc6bSMichael Große    public function forAdminOnly() {
4087fdbc6bSMichael Große        return true;
4187fdbc6bSMichael Große    }
4287fdbc6bSMichael Große
4387fdbc6bSMichael Große    /**
4487fdbc6bSMichael Große     * Should carry out any processing required by the plugin.
4587fdbc6bSMichael Große     */
4687fdbc6bSMichael Große    public function handle() {
4787fdbc6bSMichael Große        global $INPUT;
48*28e05468SAndreas Gohr        global $ID;
4987fdbc6bSMichael Große
501a8d1235SAndreas Gohr        $assignments = new Assignments();
5187fdbc6bSMichael Große        if($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
5287fdbc6bSMichael Große            $assignment = $INPUT->arr('assignment');
53*28e05468SAndreas Gohr            $ok = true;
54*28e05468SAndreas Gohr            if(!blank($assignment['assign']) && !blank($assignment['tbl'])) {
5587fdbc6bSMichael Große                if($INPUT->str('action') === 'delete') {
561a8d1235SAndreas Gohr                    $ok = $assignments->remove($assignment['assign'], $assignment['tbl']);
571a8d1235SAndreas Gohr                } else if($INPUT->str('action') === 'add') {
581a8d1235SAndreas Gohr                    $ok = $assignments->add($assignment['assign'], $assignment['tbl']);
5987fdbc6bSMichael Große                }
60*28e05468SAndreas Gohr            }
61*28e05468SAndreas Gohr
62*28e05468SAndreas Gohr            if(!$ok) {
6387fdbc6bSMichael Große                msg('something went wrong while saving', -1);
6487fdbc6bSMichael Große            }
65*28e05468SAndreas Gohr
66*28e05468SAndreas Gohr            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_assignments'), true, '&'));
6787fdbc6bSMichael Große        }
6887fdbc6bSMichael Große    }
6987fdbc6bSMichael Große
7087fdbc6bSMichael Große    /**
7187fdbc6bSMichael Große     * Render HTML output, e.g. helpful text and a form
7287fdbc6bSMichael Große     */
7387fdbc6bSMichael Große    public function html() {
74a3d1e459SAndreas Gohr        global $ID;
75a3d1e459SAndreas Gohr
7687fdbc6bSMichael Große        echo $this->locale_xhtml('assignments_intro');
7787fdbc6bSMichael Große
785f803f49SAndreas Gohr        //fixme listing schema tables should be moved to one of the meta classes
795f803f49SAndreas Gohr        /** @var helper_plugin_struct_db $helper */
805f803f49SAndreas Gohr        $helper = plugin_load('helper', 'struct_db');
815f803f49SAndreas Gohr        $sqlite = $helper->getDB();
825f803f49SAndreas Gohr        $res = $sqlite->query('SELECT tbl FROM schemas GROUP BY tbl');
835f803f49SAndreas Gohr        $schemas = $sqlite->res2arr($res);
845f803f49SAndreas Gohr        $sqlite->res_close($res);
8587fdbc6bSMichael Große
861a8d1235SAndreas Gohr        $ass = new Assignments();
871a8d1235SAndreas Gohr        $assignments = $ass->getAll();
8887fdbc6bSMichael Große
89*28e05468SAndreas Gohr        echo '<form action="' . wl($ID) . '" action="post">';
90a3d1e459SAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
91a3d1e459SAndreas Gohr        echo '<input type="hidden" name="page" value="struct_assignments" />';
92a3d1e459SAndreas Gohr        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
93a3d1e459SAndreas Gohr        echo '<table class="inline">';
94a3d1e459SAndreas Gohr
95a3d1e459SAndreas Gohr        // header
96a3d1e459SAndreas Gohr        echo '<tr>';
97a3d1e459SAndreas Gohr        echo '<th>Page/Namespace</th>'; // FIXME localize
98a3d1e459SAndreas Gohr        echo '<th>Schema</th>'; // FIXME localize
99a3d1e459SAndreas Gohr        echo '<th></th>';
100a3d1e459SAndreas Gohr        echo '</tr>';
101a3d1e459SAndreas Gohr
102a3d1e459SAndreas Gohr        // existing assignments
10387fdbc6bSMichael Große        foreach($assignments as $assignment) {
10487fdbc6bSMichael Große            $schema = $assignment['tbl'];
10587fdbc6bSMichael Große            $assignee = $assignment['assign'];
106a3d1e459SAndreas Gohr
107*28e05468SAndreas Gohr            $link = wl(
108*28e05468SAndreas Gohr                $ID, array(
109a3d1e459SAndreas Gohr                'do' => 'admin',
110a3d1e459SAndreas Gohr                'page' => 'struct_assignments',
111a3d1e459SAndreas Gohr                'action' => 'delete',
112a3d1e459SAndreas Gohr                'sectok' => getSecurityToken(),
113a3d1e459SAndreas Gohr                'assignment[tbl]' => $schema,
114a3d1e459SAndreas Gohr                'assignment[assign]' => $assignee,
115*28e05468SAndreas Gohr            )
116*28e05468SAndreas Gohr            );
117a3d1e459SAndreas Gohr
118a3d1e459SAndreas Gohr            echo '<tr>';
119a3d1e459SAndreas Gohr            echo '<td>' . hsc($assignee) . '</td>';
120a3d1e459SAndreas Gohr            echo '<td>' . hsc($schema) . '</td>';
121a3d1e459SAndreas Gohr            echo '<td><a href="' . $link . '">Delete</a></td>'; //FIXME localize
122a3d1e459SAndreas Gohr            echo '</tr>';
12387fdbc6bSMichael Große        }
124a3d1e459SAndreas Gohr
125a3d1e459SAndreas Gohr        // new assignment form
126a3d1e459SAndreas Gohr        echo '<tr>';
127a3d1e459SAndreas Gohr        echo '<td><input type="text" name="assignment[assign]" /></td>';
128a3d1e459SAndreas Gohr        echo '<td>';
129a3d1e459SAndreas Gohr        echo '<select name="assignment[tbl]">';
13087fdbc6bSMichael Große        foreach($schemas as $schema) {
131a3d1e459SAndreas Gohr            echo '<option value="' . hsc($schema['tbl']) . '">' . hsc($schema['tbl']) . '</option>';
13287fdbc6bSMichael Große        }
133a3d1e459SAndreas Gohr        echo '</select>';
134a3d1e459SAndreas Gohr        echo '</td>';
135a3d1e459SAndreas Gohr        echo '<td><button type="submit" name="action" value="add">Add</button></td>'; // FIXME localize
136a3d1e459SAndreas Gohr        echo '</tr>';
137a3d1e459SAndreas Gohr
138a3d1e459SAndreas Gohr        echo '</table>';
13987fdbc6bSMichael Große    }
140dbffe06eSAndreas Gohr
141dbffe06eSAndreas Gohr    /**
142dbffe06eSAndreas Gohr     * Copies the TOC from the Schema Editor
143dbffe06eSAndreas Gohr     *
144dbffe06eSAndreas Gohr     * @return array
145dbffe06eSAndreas Gohr     */
146dbffe06eSAndreas Gohr    public function getTOC() {
147dbffe06eSAndreas Gohr        /** @var admin_plugin_struct_schemas $plugin */
148dbffe06eSAndreas Gohr        $plugin = plugin_load('admin', 'struct_schemas');
149dbffe06eSAndreas Gohr        return $plugin->getTOC();
15087fdbc6bSMichael Große    }
15187fdbc6bSMichael Große
15287fdbc6bSMichael Große}
15387fdbc6bSMichael Große
15487fdbc6bSMichael Große// vim:ts=4:sw=4:et:
155