1<?php
2
3/**
4 * DokuWiki Plugin tplinc (Admin Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
8 */
9class admin_plugin_tplinc extends DokuWiki_Admin_Plugin
10{
11
12    /** @var helper_plugin_tplinc */
13    protected $helper;
14
15    /**
16     * admin_plugin_tplinc constructor.
17     */
18    public function __construct()
19    {
20        $this->helper = plugin_load('helper', 'tplinc');
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 true;
29    }
30
31    /**
32     * Should carry out any processing required by the plugin.
33     */
34    public function handle()
35    {
36        global $INPUT;
37
38        if ($INPUT->str('action') == 'save' && checkSecurityToken()) {
39            if ($this->helper->saveAssignments($INPUT->arr('a'))) {
40                msg($this->getLang('saved'), 1);
41            }
42        }
43    }
44
45    /**
46     * Render HTML output, e.g. helpful text and a form
47     */
48    public function html()
49    {
50        global $ID;
51        echo $this->locale_xhtml('intro');
52
53        echo '<form action="' . wl($ID) . '" id="plugin__tplinc" method="POST">';
54        echo '<input type="hidden" name="do" value="admin" />';
55        echo '<input type="hidden" name="page" value="tplinc" />';
56        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
57
58        echo '<table class="inline">';
59
60        // header
61        echo '<thead>';
62        echo '<tr>';
63        echo '<th>' . $this->getLang('pattern') . '</th>';
64        echo '<th>' . $this->getLang('page') . '</th>';
65        echo '<th>' . $this->getLang('location') . '</th>';
66        echo '<th>' . $this->getLang('skipacl') . '</th>';
67        echo '<th></th>';
68        echo '</tr>';
69        echo '</thead>';
70
71        echo '<tbody>';
72        // existing assignments
73        $assignments = $this->helper->loadAssignments();
74        $row = 0;
75        foreach ($assignments as $assignment) {
76            $this->assignmentRow($assignment, $row);
77            $row++;
78        }
79
80        // three more rows for new ones
81        for ($i = 0; $i < 3; $i++) {
82            $this->assignmentRow(array('', '', '', ''), $row);
83            $row++;
84        }
85        echo '<tbody>';
86
87        // save button
88
89        echo '<tfoot>';
90        echo '<tr>';
91        echo '<td colspan="5"><button type="submit" name="action" value="save">' . $this->getLang('save') . '</button></td>';
92        echo '</tr>';
93        echo '</tfoot>';
94
95        echo '</table>';
96        echo '</form>';
97
98        echo $this->locale_xhtml('help');
99
100    }
101
102    /**
103     * One row in the table
104     *
105     * @param array $assignment
106     * @param int $row the row counter
107     */
108    protected function assignmentRow($assignment, $row)
109    {
110        list($pattern, $page, $location, $skipacl) = $assignment;
111        echo '<tr>';
112        echo '<td><input type="text" name="a[x' . $row . '][0]" value="' . hsc($pattern) . '" /></td>';
113        echo '<td><input type="text" name="a[x' . $row . '][1]" value="' . hsc($page) . '" /></td>';
114        echo '<td>';
115        $this->locationBox('a[x' . $row . '][2]', $location);
116
117        echo '<td>';
118        $checked = $skipacl ? 'checked="checked"' : '';
119        echo '<label>';
120        echo '<input type="checkbox" name="a[x' . $row . '][3]" value="1" ' . $checked . '/> ';
121        echo $this->getLang('skipacl');
122        echo '</label>';
123        echo '</td>';
124
125        echo '<td class="drag">' . inlineSVG(__DIR__ . '/drag.svg') . '</td>';
126        echo '</tr>';
127    }
128
129    /**
130     * Create a dropdown for the locations
131     *
132     * @param string $name the parameter name
133     * @param string $loc the current location value
134     */
135    protected function locationBox($name, $loc)
136    {
137        $locations = null;
138        if ($locations === null) $locations = $this->helper->getLocations();
139
140        if (!isset($locations[$loc])) $loc = '';
141
142        echo '<select name="' . $name . '">';
143        foreach ($locations as $location => $label) {
144            $selected = ($location == $loc) ? 'selected="selected"' : '';
145            echo '<option value="' . hsc($location) . '" ' . $selected . '>';
146            echo hsc($label);
147            echo '</option>';
148        }
149        echo '</select>';
150    }
151}
152