xref: /dokuwiki/lib/plugins/styling/admin.php (revision d634152e42ef1c75d899f021132ce0c2632257ac)
1123bc813SAndreas Gohr<?php
2123bc813SAndreas Gohr/**
3123bc813SAndreas Gohr * DokuWiki Plugin styling (Admin Component)
4123bc813SAndreas Gohr *
5123bc813SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6123bc813SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
7123bc813SAndreas Gohr */
8123bc813SAndreas Gohr
9123bc813SAndreas Gohr// must be run within Dokuwiki
10123bc813SAndreas Gohrif(!defined('DOKU_INC')) die();
11123bc813SAndreas Gohr
12123bc813SAndreas Gohrclass admin_plugin_styling extends DokuWiki_Admin_Plugin {
13123bc813SAndreas Gohr
146667cd87SAndreas Gohr    public $ispopup = false;
156667cd87SAndreas Gohr
16123bc813SAndreas Gohr    /**
17123bc813SAndreas Gohr     * @return int sort number in admin menu
18123bc813SAndreas Gohr     */
19123bc813SAndreas Gohr    public function getMenuSort() {
20123bc813SAndreas Gohr        return 1000;
21123bc813SAndreas Gohr    }
22123bc813SAndreas Gohr
23123bc813SAndreas Gohr    /**
24123bc813SAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
25123bc813SAndreas Gohr     */
26123bc813SAndreas Gohr    public function forAdminOnly() {
27123bc813SAndreas Gohr        return true;
28123bc813SAndreas Gohr    }
29123bc813SAndreas Gohr
30123bc813SAndreas Gohr    /**
31123bc813SAndreas Gohr     * handle the different actions (also called from ajax)
32123bc813SAndreas Gohr     */
33123bc813SAndreas Gohr    public function handle() {
34123bc813SAndreas Gohr        global $INPUT;
35123bc813SAndreas Gohr        $run = $INPUT->extract('run')->str('run');
36123bc813SAndreas Gohr        if(!$run) return;
37123bc813SAndreas Gohr        $run = "run_$run";
38123bc813SAndreas Gohr        $this->$run();
39123bc813SAndreas Gohr    }
40123bc813SAndreas Gohr
41123bc813SAndreas Gohr    /**
42123bc813SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
43123bc813SAndreas Gohr     */
44123bc813SAndreas Gohr    public function html() {
456667cd87SAndreas Gohr        $class = 'nopopup';
46*d634152eSAnika Henke        if($this->ispopup) $class = 'ispopup page';
476667cd87SAndreas Gohr
486667cd87SAndreas Gohr        echo '<div id="plugin__styling" class="'.$class.'">';
496667cd87SAndreas Gohr        ptln('<h1>'.$this->getLang('menu').'</h1>');
506667cd87SAndreas Gohr        $this->form();
51123bc813SAndreas Gohr        echo '</div>';
52123bc813SAndreas Gohr    }
53123bc813SAndreas Gohr
54123bc813SAndreas Gohr    /**
55123bc813SAndreas Gohr     * Create the actual editing form
56123bc813SAndreas Gohr     */
576667cd87SAndreas Gohr    public function form() {
58123bc813SAndreas Gohr        global $conf;
59123bc813SAndreas Gohr        global $ID;
60123bc813SAndreas Gohr        define('SIMPLE_TEST', 1); // hack, ideally certain functions should be moved out of css.php
61123bc813SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
62123bc813SAndreas Gohr        $styleini     = css_styleini($conf['template'], true);
63123bc813SAndreas Gohr        $replacements = $styleini['replacements'];
64123bc813SAndreas Gohr
656667cd87SAndreas Gohr        if($this->ispopup) {
666667cd87SAndreas Gohr            $target = DOKU_BASE.'lib/plugins/styling/popup.php';
67123bc813SAndreas Gohr        } else {
68123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
69123bc813SAndreas Gohr        }
70123bc813SAndreas Gohr
71123bc813SAndreas Gohr        if(empty($replacements)) {
72123bc813SAndreas Gohr            echo '<p class="error">'.$this->getLang('error').'</p>';
73123bc813SAndreas Gohr        } else {
74123bc813SAndreas Gohr            echo $this->locale_xhtml('intro');
75123bc813SAndreas Gohr
76123bc813SAndreas Gohr            echo '<form class="styling" method="post" action="'.$target.'">';
77123bc813SAndreas Gohr
78*d634152eSAnika Henke            echo '<table><tbody>';
79123bc813SAndreas Gohr            foreach($replacements as $key => $value) {
80123bc813SAndreas Gohr                $name = tpl_getLang($key);
81123bc813SAndreas Gohr                if(empty($name)) $name = $this->getLang($key);
82123bc813SAndreas Gohr                if(empty($name)) $name = $key;
83123bc813SAndreas Gohr
84123bc813SAndreas Gohr                echo '<tr>';
85123bc813SAndreas Gohr                echo '<td>'.$name.'</td>';
86*d634152eSAnika Henke                echo '<td><input type="text" name="tpl['.hsc($key).']" value="'.hsc($value).'" '.$this->colorClass($key).' /></td>';
87123bc813SAndreas Gohr                echo '</tr>';
88123bc813SAndreas Gohr            }
89*d634152eSAnika Henke            echo '</tbody></table>';
90123bc813SAndreas Gohr
91418cb617SAndreas Gohr            echo '<p>';
92*d634152eSAnika Henke            echo '<input type="submit" name="run[preview]" class="btn_preview primary" value="'.$this->getLang('btn_preview').'" /> ';
93*d634152eSAnika Henke            echo '<input type="submit" name="run[reset]" value="'.$this->getLang('btn_reset').'" />'; #FIXME only if preview.ini exists
94123bc813SAndreas Gohr            echo '</p>';
95123bc813SAndreas Gohr
96418cb617SAndreas Gohr            echo '<p>';
97*d634152eSAnika Henke            echo '<input type="submit" name="run[save]" class="primary" value="'.$this->getLang('btn_save').'" />';
98123bc813SAndreas Gohr            echo '</p>';
99123bc813SAndreas Gohr
100418cb617SAndreas Gohr            echo '<p>';
101*d634152eSAnika Henke            echo '<input type="submit" name="run[revert]" value="'.$this->getLang('btn_revert').'" />'; #FIXME only if local.ini exists
102123bc813SAndreas Gohr            echo '</p>';
103123bc813SAndreas Gohr
104123bc813SAndreas Gohr            echo '</form>';
105123bc813SAndreas Gohr
106123bc813SAndreas Gohr            echo tpl_locale_xhtml('style');
107123bc813SAndreas Gohr
108123bc813SAndreas Gohr        }
109123bc813SAndreas Gohr    }
110123bc813SAndreas Gohr
111123bc813SAndreas Gohr    /**
112123bc813SAndreas Gohr     * set the color class attribute
113123bc813SAndreas Gohr     */
114123bc813SAndreas Gohr    protected function colorClass($key) {
115123bc813SAndreas Gohr        static $colors = array(
116123bc813SAndreas Gohr            'text',
117123bc813SAndreas Gohr            'background',
118123bc813SAndreas Gohr            'text_alt',
119123bc813SAndreas Gohr            'background_alt',
120123bc813SAndreas Gohr            'text_neu',
121123bc813SAndreas Gohr            'background_neu',
122123bc813SAndreas Gohr            'border',
123123bc813SAndreas Gohr            'highlight',
124123bc813SAndreas Gohr            'background_site',
125123bc813SAndreas Gohr            'link',
126123bc813SAndreas Gohr            'existing',
127123bc813SAndreas Gohr            'missing',
128123bc813SAndreas Gohr        );
129123bc813SAndreas Gohr
130123bc813SAndreas Gohr        if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) {
131123bc813SAndreas Gohr            return 'class="color"';
132123bc813SAndreas Gohr        } else {
133123bc813SAndreas Gohr            return '';
134123bc813SAndreas Gohr        }
135123bc813SAndreas Gohr    }
136123bc813SAndreas Gohr
137123bc813SAndreas Gohr    /**
138123bc813SAndreas Gohr     * saves the preview.ini (alos called from ajax directly)
139123bc813SAndreas Gohr     */
140123bc813SAndreas Gohr    public function run_preview() {
141123bc813SAndreas Gohr        global $conf;
142123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
143123bc813SAndreas Gohr        io_saveFile($ini, $this->makeini());
144123bc813SAndreas Gohr    }
145123bc813SAndreas Gohr
146123bc813SAndreas Gohr    /**
147123bc813SAndreas Gohr     * deletes the preview.ini
148123bc813SAndreas Gohr     */
149123bc813SAndreas Gohr    protected function run_reset() {
150123bc813SAndreas Gohr        global $conf;
151123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
152123bc813SAndreas Gohr        io_saveFile($ini, '');
153123bc813SAndreas Gohr    }
154123bc813SAndreas Gohr
155123bc813SAndreas Gohr    /**
156123bc813SAndreas Gohr     * deletes the local style.ini replacements
157123bc813SAndreas Gohr     */
158123bc813SAndreas Gohr    protected function run_revert() {
159123bc813SAndreas Gohr        $this->replaceini('');
160123bc813SAndreas Gohr        $this->run_reset();
161123bc813SAndreas Gohr    }
162123bc813SAndreas Gohr
163123bc813SAndreas Gohr    /**
164123bc813SAndreas Gohr     * save the local style.ini replacements
165123bc813SAndreas Gohr     */
166123bc813SAndreas Gohr    protected function run_save() {
167123bc813SAndreas Gohr        $this->replaceini($this->makeini());
168123bc813SAndreas Gohr        $this->run_reset();
169123bc813SAndreas Gohr    }
170123bc813SAndreas Gohr
171123bc813SAndreas Gohr    /**
172123bc813SAndreas Gohr     * create the replacement part of a style.ini from submitted data
173123bc813SAndreas Gohr     *
174123bc813SAndreas Gohr     * @return string
175123bc813SAndreas Gohr     */
176123bc813SAndreas Gohr    protected function makeini() {
177123bc813SAndreas Gohr        global $INPUT;
178123bc813SAndreas Gohr
179123bc813SAndreas Gohr        $ini = "[replacements]\n";
180123bc813SAndreas Gohr        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
181123bc813SAndreas Gohr        $ini .= ";Any values in this section will be overwritten by that tool again\n";
182123bc813SAndreas Gohr        foreach($INPUT->arr('tpl') as $key => $val) {
183123bc813SAndreas Gohr            $ini .= $key.' = "'.addslashes($val).'"'."\n";
184123bc813SAndreas Gohr        }
185123bc813SAndreas Gohr
186123bc813SAndreas Gohr        return $ini;
187123bc813SAndreas Gohr    }
188123bc813SAndreas Gohr
189123bc813SAndreas Gohr    /**
190123bc813SAndreas Gohr     * replaces the replacement parts in the local ini
191123bc813SAndreas Gohr     *
192123bc813SAndreas Gohr     * @param string $new the new ini contents
193123bc813SAndreas Gohr     */
194123bc813SAndreas Gohr    protected function replaceini($new) {
195123bc813SAndreas Gohr        global $conf;
196123bc813SAndreas Gohr        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
197123bc813SAndreas Gohr        if(file_exists($ini)) {
198123bc813SAndreas Gohr            $old = io_readFile($ini);
199123bc813SAndreas Gohr            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
200123bc813SAndreas Gohr            $old = trim($old);
201123bc813SAndreas Gohr        } else {
202123bc813SAndreas Gohr            $old = '';
203123bc813SAndreas Gohr        }
204123bc813SAndreas Gohr
205123bc813SAndreas Gohr        io_makeFileDir($ini);
206123bc813SAndreas Gohr        io_saveFile($ini, "$old\n\n$new");
207123bc813SAndreas Gohr    }
208123bc813SAndreas Gohr
209123bc813SAndreas Gohr}
210123bc813SAndreas Gohr
211123bc813SAndreas Gohr// vim:ts=4:sw=4:et:
212