xref: /plugin/structpublish/admin.php (revision 910e7e15ab7eacafbaa096c48916ba3bb1c5258f)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Assignments;
4
5/**
6 * DokuWiki Plugin structpublish (Admin Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
10 */
11
12class admin_plugin_structpublish extends DokuWiki_Admin_Plugin
13{
14    /**
15     * @return int sort number in admin menu
16     */
17    public function getMenuSort()
18    {
19        return 555;
20    }
21
22    /**
23     * @return bool true if only access for superuser, false is for superusers and moderators
24     */
25    public function forAdminOnly()
26    {
27        return false;
28    }
29
30    /**
31     * Based on struct pattern assignments
32     */
33    public function handle()
34    {
35        global $INPUT;
36        global $ID;
37
38        try {
39            $assignments = Assignments::getInstance();
40        } catch (Exception $e) {
41            msg($e->getMessage(), -1);
42            return false;
43        }
44
45        if ($INPUT->str('action') && $INPUT->arr('assignment') && checkSecurityToken()) {
46            $assignment = $INPUT->arr('assignment');
47            if (!blank($assignment['pattern']) && !blank($assignment['status'])) {
48                if ($INPUT->str('action') === 'delete') {
49                    $ok = $assignments->removePattern($assignment['pattern'], $assignment['user'], $assignment['status']);
50                    if (!$ok) msg('failed to remove pattern', -1);
51                } elseif ($INPUT->str('action') === 'add') {
52                    if ($assignment['pattern'][0] == '/') {
53                        if (@preg_match($assignment['pattern'], null) === false) {
54                            msg('Invalid regular expression. Pattern not saved', -1);
55                        } else {
56                            $ok = $assignments->addPattern($assignment['pattern'], $assignment['user'], $assignment['status']);
57                            if (!$ok) msg('failed to add pattern', -1);
58                        }
59                    } else {
60                        $ok = $assignments->addPattern($assignment['pattern'],$assignment['user'], $assignment['status']);
61                        if (!$ok) msg('failed to add pattern', -1);
62                    }
63                }
64            }
65
66            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'structpublish'), true, '&'));
67        }
68    }
69
70    /**
71     * Render HTML output, e.g. helpful text and a form
72     */
73    public function html()
74    {
75        ptln('<h1>' . $this->getLang('menu') . '</h1>');
76
77        global $ID;
78
79        try {
80            $assignments = Assignments::getInstance();
81        } catch (Exception $e) {
82            msg($e->getMessage(), -1);
83            return false;
84        }
85        $list = $assignments->getAllPatterns();
86
87        echo '<form action="' . wl($ID) . '" action="post">';
88        echo '<input type="hidden" name="do" value="admin" />';
89        echo '<input type="hidden" name="page" value="structpublish" />';
90        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
91        echo '<table class="inline">';
92
93        // header
94        echo '<tr>';
95        echo '<th>' . $this->getLang('assign_pattern') . '</th>';
96        echo '<th>' . $this->getLang('assign_status') . '</th>';
97        echo '<th>' . $this->getLang('assign_user') . '</th>';
98        echo '<th></th>';
99        echo '</tr>';
100
101        // existing assignments
102        foreach ($list as $assignment) {
103            $pattern = $assignment['pattern'];
104            $status = $assignment['status'];
105            $user = $assignment['user'];
106
107            $link = wl(
108                $ID,
109                [
110                    'do' => 'admin',
111                    'page' => 'structpublish',
112                    'action' => 'delete',
113                    'sectok' => getSecurityToken(),
114                    'assignment[status]' => $status,
115                    'assignment[pattern]' => $pattern,
116                    'assignment[user]' => $user,
117                ]
118            );
119
120            echo '<tr>';
121            echo '<td>' . hsc($pattern) . '</td>';
122            echo '<td>' . hsc($status) . '</td>';
123            echo '<td>' . hsc($user) . '</td>';
124            echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>';
125            echo '</tr>';
126        }
127
128        // new assignment form
129        echo '<tr>';
130        echo '<td><input type="text" name="assignment[pattern]" /></td>';
131        echo '<td>';
132        echo '<select name="assignment[status]">';
133        foreach (['approve', 'publish'] as $status) {
134            echo '<option value="' . $status . '">' . $status . '</option>';
135        }
136        echo '</select>';
137        echo '</td>';
138        echo '<td><input type="text" name="assignment[user]" /></td>';
139        echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>';
140        echo '</tr>';
141
142        echo '</table>';
143        echo '</form>';
144    }
145}
146
147