1<?php
2
3use dokuwiki\plugin\structpublish\meta\Assignments;
4use dokuwiki\plugin\structpublish\meta\Constants;
5
6/**
7 * DokuWiki Plugin structpublish (Admin Component)
8 *
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
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;
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(
50                        $assignment['pattern'],
51                        $assignment['user'],
52                        $assignment['status']
53                    );
54                    if (!$ok) {
55                        msg('failed to remove pattern', -1);
56                    }
57                } elseif ($INPUT->str('action') === 'add') {
58                    if ($assignment['pattern'][0] == '/') {
59                        if (@preg_match($assignment['pattern'], null) === false) {
60                            msg('Invalid regular expression. Pattern not saved', -1);
61                        } else {
62                            $ok = $assignments->addPattern(
63                                $assignment['pattern'],
64                                $assignment['user'],
65                                $assignment['status']
66                            );
67                            if (!$ok) {
68                                msg('failed to add pattern', -1);
69                            }
70                        }
71                    } else {
72                        $ok = $assignments->addPattern(
73                            $assignment['pattern'],
74                            $assignment['user'],
75                            $assignment['status']
76                        );
77                        if (!$ok) {
78                            msg('failed to add pattern', -1);
79                        }
80                    }
81                }
82            }
83
84            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'structpublish'), true, '&'));
85        }
86    }
87
88    /**
89     * Render HTML output
90     */
91    public function html()
92    {
93        ptln('<h1>' . $this->getLang('menu') . '</h1>');
94
95        global $ID;
96
97        try {
98            $assignments = Assignments::getInstance();
99        } catch (Exception $e) {
100            msg($e->getMessage(), -1);
101            return;
102        }
103        $list = $assignments->getAllPatterns();
104
105        echo '<form action="' . wl($ID) . '" action="post">';
106        echo '<input type="hidden" name="do" value="admin" />';
107        echo '<input type="hidden" name="page" value="structpublish" />';
108        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
109        echo '<table class="inline">';
110
111        // header
112        echo '<tr>';
113        echo '<th>' . $this->getLang('assign_pattern') . '</th>';
114        echo '<th>' . $this->getLang('assign_status') . '</th>';
115        echo '<th>' . $this->getLang('assign_user') . '</th>';
116        echo '<th></th>';
117        echo '</tr>';
118
119        // existing assignments
120        foreach ($list as $assignment) {
121            $pattern = $assignment['pattern'];
122            $status = $assignment['status'];
123            $user = $assignment['user'];
124
125            $link = wl(
126                $ID,
127                [
128                    'do' => 'admin',
129                    'page' => 'structpublish',
130                    'action' => 'delete',
131                    'sectok' => getSecurityToken(),
132                    'assignment[status]' => $status,
133                    'assignment[pattern]' => $pattern,
134                    'assignment[user]' => $user,
135                ]
136            );
137
138            echo '<tr>';
139            echo '<td>' . hsc($pattern) . '</td>';
140            echo '<td>' . hsc($status) . '</td>';
141            echo '<td>' . hsc($user) . '</td>';
142            echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>';
143            echo '</tr>';
144        }
145
146        // new assignment form
147        echo '<tr>';
148        echo '<td><input type="text" name="assignment[pattern]" /></td>';
149        echo '<td>';
150        echo '<select name="assignment[status]">';
151        foreach ([Constants::ACTION_APPROVE, Constants::ACTION_PUBLISH] as $status) {
152            echo '<option value="' . $status . '">' . $status . '</option>';
153        }
154        echo '</select>';
155        echo '</td>';
156        echo '<td><input type="text" name="assignment[user]" /></td>';
157        echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>';
158        echo '</tr>';
159
160        echo '</table>';
161        echo '</form>';
162    }
163}
164