xref: /dokuwiki/lib/plugins/styling/admin.php (revision b4f2363aa1360136c8a826f09aaebc6505211c73)
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).'" value="'.hsc($value).'" '.$this->colorClass($key).' dir="ltr" /></td>';
83                echo '</tr>';
84            }
85            echo '</tbody></table>';
86
87            echo '<p>';
88            echo '<button type="submit" name="run[preview]" class="btn_preview primary">'.$this->getLang('btn_preview').'</button> ';
89            echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>'; #FIXME only if preview.ini exists
90            echo '</p>';
91
92            echo '<p>';
93            echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>';
94            echo '</p>';
95
96            echo '<p>';
97            echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>'; #FIXME only if local.ini exists
98            echo '</p>';
99
100            echo '</form>';
101
102            echo tpl_locale_xhtml('style');
103
104        }
105    }
106
107    /**
108     * set the color class attribute
109     */
110    protected function colorClass($key) {
111        static $colors = array(
112            'text',
113            'background',
114            'text_alt',
115            'background_alt',
116            'text_neu',
117            'background_neu',
118            'border',
119            'highlight',
120            'background_site',
121            'link',
122            'existing',
123            'missing',
124        );
125
126        if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) {
127            return 'class="color"';
128        } else {
129            return '';
130        }
131    }
132
133    /**
134     * saves the preview.ini (alos called from ajax directly)
135     */
136    public function run_preview() {
137        global $conf;
138        $ini = $conf['cachedir'].'/preview.ini';
139        io_saveFile($ini, $this->makeini());
140    }
141
142    /**
143     * deletes the preview.ini
144     */
145    protected function run_reset() {
146        global $conf;
147        $ini = $conf['cachedir'].'/preview.ini';
148        io_saveFile($ini, '');
149    }
150
151    /**
152     * deletes the local style.ini replacements
153     */
154    protected function run_revert() {
155        $this->replaceini('');
156        $this->run_reset();
157    }
158
159    /**
160     * save the local style.ini replacements
161     */
162    protected function run_save() {
163        $this->replaceini($this->makeini());
164        $this->run_reset();
165    }
166
167    /**
168     * create the replacement part of a style.ini from submitted data
169     *
170     * @return string
171     */
172    protected function makeini() {
173        global $INPUT;
174
175        $ini = "[replacements]\n";
176        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
177        $ini .= ";Any values in this section will be overwritten by that tool again\n";
178        foreach($INPUT->arr('tpl') as $key => $val) {
179            $ini .= $key.' = "'.addslashes($val).'"'."\n";
180        }
181
182        return $ini;
183    }
184
185    /**
186     * replaces the replacement parts in the local ini
187     *
188     * @param string $new the new ini contents
189     */
190    protected function replaceini($new) {
191        global $conf;
192        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
193        if(file_exists($ini)) {
194            $old = io_readFile($ini);
195            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
196            $old = trim($old);
197        } else {
198            $old = '';
199        }
200
201        io_makeFileDir($ini);
202        io_saveFile($ini, "$old\n\n$new");
203    }
204
205}
206
207// vim:ts=4:sw=4:et:
208