xref: /dokuwiki/lib/plugins/styling/admin.php (revision 1187c33a8f3274ec6c66260ea7d669d51243450e)
1<?php
2/**
3 * DokuWiki Plugin styling (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8class admin_plugin_styling extends DokuWiki_Admin_Plugin
9{
10
11    public $ispopup = false;
12
13    /**
14     * @return int sort number in admin menu
15     */
16    public function getMenuSort()
17    {
18        return 1000;
19    }
20
21    /**
22     * @return bool true if only access for superuser, false is for superusers and moderators
23     */
24    public function forAdminOnly()
25    {
26        return true;
27    }
28
29    /**
30     * handle the different actions (also called from ajax)
31     */
32    public function handle()
33    {
34        global $INPUT;
35        $run = $INPUT->extract('run')->str('run');
36        if (!$run) return;
37        $run = 'run'.ucfirst($run);
38        $this->$run();
39    }
40
41    /**
42     * Render HTML output, e.g. helpful text and a form
43     */
44    public function html()
45    {
46        $class = 'nopopup';
47        if ($this->ispopup) $class = 'ispopup page';
48
49        echo '<div id="plugin__styling" class="'.$class.'">';
50        ptln('<h1>'.$this->getLang('menu').'</h1>');
51        $this->form();
52        echo '</div>';
53    }
54
55    /**
56     * Create the actual editing form
57     */
58    public function form()
59    {
60        global $conf;
61        global $ID;
62
63        $styleUtil = new \dokuwiki\StyleUtils($conf['template'], true);
64        $styleini     = $styleUtil->cssStyleini();
65        $replacements = $styleini['replacements'];
66
67        if ($this->ispopup) {
68            $target = DOKU_BASE.'lib/plugins/styling/popup.php';
69        } else {
70            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
71        }
72
73        if (empty($replacements)) {
74            echo '<p class="error">'.$this->getLang('error').'</p>';
75        } else {
76            echo $this->locale_xhtml('intro');
77
78            echo '<form class="styling" method="post" action="'.$target.'">';
79
80            echo '<table><tbody>';
81            foreach ($replacements as $key => $value) {
82                $name = tpl_getLang($key);
83                if (empty($name)) $name = $this->getLang($key);
84                if (empty($name)) $name = $key;
85
86                echo '<tr>';
87                echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>';
88                echo '<td><input type="'.$this->colorType($key).'" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'"
89                    value="'.hsc($value).'" dir="ltr" /></td>';
90                echo '</tr>';
91            }
92            echo '</tbody></table>';
93
94            echo '<p>';
95            echo '<button type="submit" name="run[preview]" class="btn_preview primary">'.
96                $this->getLang('btn_preview').'</button> ';
97            #FIXME only if preview.ini exists:
98            echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>';
99            echo '</p>';
100
101            echo '<p>';
102            echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>';
103            echo '</p>';
104
105            echo '<p>';
106            #FIXME only if local.ini exists:
107            echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>';
108            echo '</p>';
109
110            echo '</form>';
111
112            echo tpl_locale_xhtml('style');
113        }
114    }
115
116    /**
117     * Decide the input type based on the key name
118     *
119     * @param string $key
120     * @return string color|text
121     */
122    protected function colorType($key)
123    {
124        static $colors = array(
125            'text',
126            'background',
127            'text_alt',
128            'background_alt',
129            'text_neu',
130            'background_neu',
131            'border',
132            'highlight',
133            'background_site',
134            'link',
135            'existing',
136            'missing',
137        );
138
139        if (preg_match('/colou?r/', $key) || in_array(trim($key, '_'), $colors)) {
140            return 'color';
141        } else {
142            return 'text';
143        }
144    }
145
146    /**
147     * saves the preview.ini (alos called from ajax directly)
148     */
149    public function runPreview()
150    {
151        global $conf;
152        $ini = $conf['cachedir'].'/preview.ini';
153        io_saveFile($ini, $this->makeini());
154    }
155
156    /**
157     * deletes the preview.ini
158     */
159    protected function runReset()
160    {
161        global $conf;
162        $ini = $conf['cachedir'].'/preview.ini';
163        io_saveFile($ini, '');
164    }
165
166    /**
167     * deletes the local style.ini replacements
168     */
169    protected function runRevert()
170    {
171        $this->replaceIni('');
172        $this->runReset();
173    }
174
175    /**
176     * save the local style.ini replacements
177     */
178    protected function runSave()
179    {
180        $this->replaceIni($this->makeini());
181        $this->runReset();
182    }
183
184    /**
185     * create the replacement part of a style.ini from submitted data
186     *
187     * @return string
188     */
189    protected function makeini()
190    {
191        global $INPUT;
192
193        $ini = "[replacements]\n";
194        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
195        $ini .= ";Any values in this section will be overwritten by that tool again\n";
196        foreach ($INPUT->arr('tpl') as $key => $val) {
197            $ini .= $key.' = "'.addslashes($val).'"'."\n";
198        }
199
200        return $ini;
201    }
202
203    /**
204     * replaces the replacement parts in the local ini
205     *
206     * @param string $new the new ini contents
207     */
208    protected function replaceIni($new)
209    {
210        global $conf;
211        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
212        if (file_exists($ini)) {
213            $old = io_readFile($ini);
214            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
215            $old = trim($old);
216        } else {
217            $old = '';
218        }
219
220        io_makeFileDir($ini);
221        io_saveFile($ini, "$old\n\n$new");
222    }
223}
224
225// vim:ts=4:sw=4:et:
226