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