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