xref: /dokuwiki/lib/plugins/styling/admin.php (revision e1d9dcc8b460b6f029ac9c8d5d3b8d23b6e73402)
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="text" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'"
89                    value="'.hsc($value).'" '.$this->colorClass($key).' 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     * set the color class attribute
118     */
119    protected function colorClass($key)
120    {
121        static $colors = array(
122            'text',
123            'background',
124            'text_alt',
125            'background_alt',
126            'text_neu',
127            'background_neu',
128            'border',
129            'highlight',
130            'background_site',
131            'link',
132            'existing',
133            'missing',
134        );
135
136        if (preg_match('/colou?r/', $key) || in_array(trim($key, '_'), $colors)) {
137            return 'class="color"';
138        } else {
139            return '';
140        }
141    }
142
143    /**
144     * saves the preview.ini (alos called from ajax directly)
145     */
146    public function runPreview()
147    {
148        global $conf;
149        $ini = $conf['cachedir'].'/preview.ini';
150        io_saveFile($ini, $this->makeini());
151    }
152
153    /**
154     * deletes the preview.ini
155     */
156    protected function runReset()
157    {
158        global $conf;
159        $ini = $conf['cachedir'].'/preview.ini';
160        io_saveFile($ini, '');
161    }
162
163    /**
164     * deletes the local style.ini replacements
165     */
166    protected function runRevert()
167    {
168        $this->replaceIni('');
169        $this->runReset();
170    }
171
172    /**
173     * save the local style.ini replacements
174     */
175    protected function runSave()
176    {
177        $this->replaceIni($this->makeini());
178        $this->runReset();
179    }
180
181    /**
182     * create the replacement part of a style.ini from submitted data
183     *
184     * @return string
185     */
186    protected function makeini()
187    {
188        global $INPUT;
189
190        $ini = "[replacements]\n";
191        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
192        $ini .= ";Any values in this section will be overwritten by that tool again\n";
193        foreach ($INPUT->arr('tpl') as $key => $val) {
194            $ini .= $key.' = "'.addslashes($val).'"'."\n";
195        }
196
197        return $ini;
198    }
199
200    /**
201     * replaces the replacement parts in the local ini
202     *
203     * @param string $new the new ini contents
204     */
205    protected function replaceIni($new)
206    {
207        global $conf;
208        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
209        if (file_exists($ini)) {
210            $old = io_readFile($ini);
211            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
212            $old = trim($old);
213        } else {
214            $old = '';
215        }
216
217        io_makeFileDir($ini);
218        io_saveFile($ini, "$old\n\n$new");
219    }
220}
221
222// vim:ts=4:sw=4:et:
223