1<?php
2/**
3 * DokuWiki Plugin custombuttons (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author Constantinos Xanthopoulos <conx@xanthopoulos.info>
7 */
8class admin_plugin_custombuttons extends DokuWiki_Admin_Plugin {
9
10    /**
11     * Return true for access only by admins (config:superuser) or false if managers are allowed as well
12     *
13     * @return bool
14     */
15    public function forAdminOnly() {
16        return true;
17    }
18
19    /**
20     * return prompt for admin menu
21     */
22    public function getMenuText($language) {
23        return $this->getLang('name');
24    }
25
26    /**
27     * Read config
28     *
29     * @return bool|mixed
30     */
31    protected function loadCBData() {
32        $file = @file_get_contents(DOKU_PLUGIN.'custombuttons/config.json');
33        if (!$file) return false;
34        return json_decode($file, true);
35    }
36
37    /**
38     * Store config
39     *
40     * @param $conf
41     */
42    protected function saveCBData($conf) {
43        $configfile = DOKU_PLUGIN.'custombuttons/config.json';
44        if (is_writable($configfile) || (!file_exists($configfile) && is_writable(DOKU_PLUGIN.'custombuttons'))) {
45            file_put_contents($configfile, json_encode($conf));
46        } else {
47            msg($this->getLang('txt_error'), -1);
48        }
49    }
50
51    protected function reloadBar() {
52        touch(DOKU_CONF.'local.php');
53    }
54
55    /**
56     * Execute the requested action
57     */
58    public function handle() {
59        global $INPUT;
60
61        if ($INPUT->has('add')) {
62            if (!checkSecurityToken()) return;
63
64            $conf = $this->loadCBData();
65            if (!$conf) {
66                $conf = array();
67            }
68            $type = 0;
69            if ($INPUT->str('pretag') != '' && $INPUT->str('posttag') != '') {
70                $type = 1;
71            }
72            array_push($conf, array(
73                'label' => $INPUT->str('label'),
74                'code'  => $INPUT->str('code'),
75                'type'  => $type,
76                'pretag'  => $INPUT->str('pretag'),
77                'posttag' => $INPUT->str('posttag'),
78                'icon'  => $INPUT->str('icon'),
79            ));
80
81            $this->saveCBData($conf);
82            $this->reloadBar();
83        } elseif ($INPUT->has('delete')) {
84            if (!checkSecurityToken()) return;
85
86            $conf = $this->loadCBData();
87            unset($conf[$INPUT->int('delete')]);
88            $this->saveCBData($conf);
89            $this->reloadBar();
90        }
91    }
92
93    /**
94     * Render HTML output
95     */
96    public function html() {
97        global $ID;
98        $conf = $this->loadCBData();
99
100        echo '<div id="custombuttons">';
101        echo '<h1>'.$this->getLang('name').'</h1>';
102
103        // list of custom buttons
104        echo '<h3>'.$this->getLang('btnslist').'</h3>';
105        echo '<form id="cb_button_list" action="'.wl($ID).'" method="post">'
106            .'<input type="hidden" name="do" value="admin" />'
107            .'<input type="hidden" name="page" value="'.$this->getPluginName().'" />';
108
109        formSecurityToken();
110
111        echo '<table class = "inline">';
112        echo '<tr>'
113            .'<th>'.$this->getLang('btnslist_label').'</th>'
114            .'<th>'.$this->getLang('btnslist_code').'</th>'
115            .'<th>'.$this->getLang('btnslist_delete').'</th>'
116            .'</tr>';
117        if ($conf) {
118            foreach ($conf as $key => $button) {
119                echo '<tr>';
120                if (!$button['type']) {
121                    echo '<td>'.hsc($button['label']).'</td>'
122                        .'<td>'.hsc($button['code']).'</td>';
123                } else {
124                    $icon = '';
125                    if ($button['icon']) {
126                        $icon = '<img src="'. DOKU_BASE.'lib/plugins/custombuttons/ico/'.$button['icon'].'" /> ';
127                    }
128                    echo '<td>'.$icon.hsc($button['label']).'</td>'
129                        .'<td>'.hsc($button['pretag']).hsc($button['code']).hsc($button['posttag']).'</td>';
130                };
131                echo '<td><input type="checkbox" name="delete" value="'.$key.'"/></td>';
132                echo '</tr>';
133            }
134        }
135        echo '</table>';
136        echo '<input type="submit" class="button" value="'.$this->getLang('btn_delete').'"/>';
137        echo '</form>';
138        echo '</br></br>';
139
140        // add custom button form
141        echo '<h3>'.$this->getLang('addbtn').'</h3>';
142        echo '<form id="cb_add_button" action="'.wl($ID).'" method="post">'
143            .'<input type="hidden" name="do" value="admin" />'
144            .'<input type="hidden" name="add" value="1" />'
145            .'<input type="hidden" name="page" value="'.$this->getPluginName().'" />';
146        formSecurityToken();
147        echo '<table>';
148        echo '<tr>';
149        echo '<th>'.$this->getLang('addbtn_icon').'</th>';
150        echo '<td>'
151            .'<select name="icon">'
152            .'<option value="">'.$this->getLang('addbtn_textonly').'</option>';
153        $files = glob(dirname(__FILE__).'/ico/*.png');
154        foreach ($files as $file) {
155            $file = hsc(basename($file));
156            echo '<option value="'.$file.'">'.$file.'</option>';
157        };
158        echo '</select>';
159        echo '</td>';
160        echo '<td></td>';
161        echo '</tr>';
162        echo '<tr>'
163            .'<th>'.$this->getLang('addbtn_label').'</th>'
164            .'<td><input type="text" name="label" /></td>'
165            .'<td></td>'
166            .'</tr>';
167        echo '<tr>'
168            .'<th>'.$this->getLang('addbtn_pretag').'</th>'
169            .'<td><input type="text" name="pretag" /></td>'
170            .'<td>*</td>'
171            .'</tr>';
172        echo '<tr>'
173            .'<th>'.$this->getLang('addbtn_posttag').'</th>'
174            .'<td><input type="text" name="posttag" /></td>'
175            .'<td>*</td>'
176            .'</tr>';
177        echo '<tr>'
178            .'<th>'.$this->getLang('addbtn_code').'</th>'
179            .'<td><input type="text" name="code" /></td>'
180            .'<td></td>'
181            .'</tr>';
182        echo '</table>';
183        echo '<input type="submit" class="button" value="'.$this->getLang('btn_add').'" />';
184        echo '</form>';
185        echo '<div id="cb_comment">'.$this->getLang('txt_comment').'</div>';
186        echo '</div>';
187    }
188}
189