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