xref: /dokuwiki/lib/plugins/styling/admin.php (revision 06c157573b322f8d04933f27fe8e2307ca7a2b30)
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 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class admin_plugin_styling extends DokuWiki_Admin_Plugin {
13
14    public $ispopup = false;
15
16    /**
17     * @return int sort number in admin menu
18     */
19    public function getMenuSort() {
20        return 1000;
21    }
22
23    /**
24     * @return bool true if only access for superuser, false is for superusers and moderators
25     */
26    public function forAdminOnly() {
27        return true;
28    }
29
30    /**
31     * handle the different actions (also called from ajax)
32     */
33    public function handle() {
34        global $INPUT;
35        $run = $INPUT->extract('run')->str('run');
36        if(!$run) return;
37        $run = "run_$run";
38        $this->$run();
39    }
40
41    /**
42     * Render HTML output, e.g. helpful text and a form
43     */
44    public function html() {
45        $class = 'nopopup';
46        if($this->ispopup) $class = 'ispopup page';
47
48        echo '<div id="plugin__styling" class="'.$class.'">';
49        ptln('<h1>'.$this->getLang('menu').'</h1>');
50        $this->form();
51        echo '</div>';
52    }
53
54    /**
55     * Create the actual editing form
56     */
57    public function form() {
58        global $conf;
59        global $ID;
60
61        $styleUtil = new \dokuwiki\StyleUtils($conf['template'], true);
62        $styleini     = $styleUtil->cssStyleini();
63        $replacements = $styleini['replacements'];
64
65        if($this->ispopup) {
66            $target = DOKU_BASE.'lib/plugins/styling/popup.php';
67        } else {
68            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
69        }
70
71        if(empty($replacements)) {
72            echo '<p class="error">'.$this->getLang('error').'</p>';
73        } else {
74            echo $this->locale_xhtml('intro');
75
76            echo '<form class="styling" method="post" action="'.$target.'">';
77
78            echo '<table><tbody>';
79            foreach($replacements as $key => $value) {
80                $name = tpl_getLang($key);
81                if(empty($name)) $name = $this->getLang($key);
82                if(empty($name)) $name = $key;
83
84                echo '<tr>';
85                echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>';
86                echo '<td><input type="text" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'" value="'.hsc($value).'" '.$this->colorClass($key).' dir="ltr" /></td>';
87                echo '</tr>';
88            }
89            echo '</tbody></table>';
90
91            echo '<p>';
92            echo '<button type="submit" name="run[preview]" class="btn_preview primary">'.$this->getLang('btn_preview').'</button> ';
93            echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>'; #FIXME only if preview.ini exists
94            echo '</p>';
95
96            echo '<p>';
97            echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>';
98            echo '</p>';
99
100            echo '<p>';
101            echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>'; #FIXME only if local.ini exists
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