xref: /dokuwiki/lib/plugins/styling/admin.php (revision 418cb6171fc2d6d04a505e28c123b22c384db8c5)
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
14123bc813SAndreas Gohr    /**
15123bc813SAndreas Gohr     * @return int sort number in admin menu
16123bc813SAndreas Gohr     */
17123bc813SAndreas Gohr    public function getMenuSort() {
18123bc813SAndreas Gohr        return 1000;
19123bc813SAndreas Gohr    }
20123bc813SAndreas Gohr
21123bc813SAndreas Gohr    /**
22123bc813SAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
23123bc813SAndreas Gohr     */
24123bc813SAndreas Gohr    public function forAdminOnly() {
25123bc813SAndreas Gohr        return true;
26123bc813SAndreas Gohr    }
27123bc813SAndreas Gohr
28123bc813SAndreas Gohr    /**
29123bc813SAndreas Gohr     * @param string $language
30123bc813SAndreas Gohr     * @return string
31123bc813SAndreas Gohr     */
32123bc813SAndreas Gohr    public function getMenuText($language) {
33123bc813SAndreas Gohr        $js = $this->getLang('js');
34123bc813SAndreas Gohr        return $js['menu'];
35123bc813SAndreas Gohr    }
36123bc813SAndreas Gohr
37123bc813SAndreas Gohr    /**
38123bc813SAndreas Gohr     * handle the different actions (also called from ajax)
39123bc813SAndreas Gohr     */
40123bc813SAndreas Gohr    public function handle() {
41123bc813SAndreas Gohr        global $INPUT;
42123bc813SAndreas Gohr        $run = $INPUT->extract('run')->str('run');
43123bc813SAndreas Gohr        if(!$run) return;
44123bc813SAndreas Gohr        $run = "run_$run";
45123bc813SAndreas Gohr        $this->$run();
46123bc813SAndreas Gohr    }
47123bc813SAndreas Gohr
48123bc813SAndreas Gohr    /**
49123bc813SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
50123bc813SAndreas Gohr     */
51123bc813SAndreas Gohr    public function html() {
52123bc813SAndreas Gohr        echo '<div id="plugin__styling">';
53123bc813SAndreas Gohr        ptln('<h1>'.$this->getMenuText('').'</h1>');
54123bc813SAndreas Gohr        $this->form(false);
55123bc813SAndreas Gohr        echo '</div>';
56123bc813SAndreas Gohr    }
57123bc813SAndreas Gohr
58123bc813SAndreas Gohr    /**
59123bc813SAndreas Gohr     * Create the actual editing form
60e4632ba4SScrutinizer Auto-Fixer     * @param boolean $isajax
61123bc813SAndreas Gohr     */
62123bc813SAndreas Gohr    public function form($isajax) {
63123bc813SAndreas Gohr        global $conf;
64123bc813SAndreas Gohr        global $ID;
65123bc813SAndreas Gohr        define('SIMPLE_TEST', 1); // hack, ideally certain functions should be moved out of css.php
66123bc813SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
67123bc813SAndreas Gohr        $styleini     = css_styleini($conf['template'], true);
68123bc813SAndreas Gohr        $replacements = $styleini['replacements'];
69123bc813SAndreas Gohr
70123bc813SAndreas Gohr        if($isajax) {
71123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'styling_plugin'));
72123bc813SAndreas Gohr        } else {
73123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
74123bc813SAndreas Gohr        }
75123bc813SAndreas Gohr
76123bc813SAndreas Gohr        if(empty($replacements)) {
77123bc813SAndreas Gohr            echo '<p class="error">'.$this->getLang('error').'</p>';
78123bc813SAndreas Gohr        } else {
79123bc813SAndreas Gohr            echo $this->locale_xhtml('intro');
80123bc813SAndreas Gohr
81123bc813SAndreas Gohr            echo '<form class="styling" method="post" action="'.$target.'">';
82123bc813SAndreas Gohr
83123bc813SAndreas Gohr            echo '<table>';
84123bc813SAndreas Gohr            foreach($replacements as $key => $value) {
85123bc813SAndreas Gohr                $name = tpl_getLang($key);
86123bc813SAndreas Gohr                if(empty($name)) $name = $this->getLang($key);
87123bc813SAndreas Gohr                if(empty($name)) $name = $key;
88123bc813SAndreas Gohr
89123bc813SAndreas Gohr                echo '<tr>';
90123bc813SAndreas Gohr                echo '<td>'.$name.'</td>';
91123bc813SAndreas Gohr                echo '<td><input type="text" name="tpl['.hsc($key).']" value="'.hsc($value).'" '.$this->colorClass($key).' />';
92123bc813SAndreas Gohr                echo '</tr>';
93123bc813SAndreas Gohr            }
94123bc813SAndreas Gohr            echo '</table>';
95123bc813SAndreas Gohr
96*418cb617SAndreas Gohr            echo '<p>';
97123bc813SAndreas Gohr            echo '<input type="submit" name="run[preview]" class="btn_preview" value="'.$this->getLang('btn_preview').'">';
98123bc813SAndreas Gohr            echo '<input type="submit" name="run[reset]" value="'.$this->getLang('btn_reset').'">'; #FIXME only if preview.ini exists
99123bc813SAndreas Gohr            echo '</p>';
100123bc813SAndreas Gohr
101*418cb617SAndreas Gohr            echo '<p>';
102123bc813SAndreas Gohr            echo '<input type="submit" name="run[save]" value="'.$this->getLang('btn_save').'">';
103123bc813SAndreas Gohr            echo '</p>';
104123bc813SAndreas Gohr
105*418cb617SAndreas Gohr            echo '<p>';
106123bc813SAndreas Gohr            echo '<input type="submit" name="run[revert]" value="'.$this->getLang('btn_revert').'">'; #FIXME only if local.ini exists
107123bc813SAndreas Gohr            echo '</p>';
108123bc813SAndreas Gohr
109123bc813SAndreas Gohr            echo '</form>';
110123bc813SAndreas Gohr
111123bc813SAndreas Gohr            echo tpl_locale_xhtml('style');
112123bc813SAndreas Gohr
113123bc813SAndreas Gohr        }
114123bc813SAndreas Gohr    }
115123bc813SAndreas Gohr
116123bc813SAndreas Gohr    /**
117123bc813SAndreas Gohr     * set the color class attribute
118123bc813SAndreas Gohr     */
119123bc813SAndreas Gohr    protected function colorClass($key) {
120123bc813SAndreas Gohr        static $colors = array(
121123bc813SAndreas Gohr            'text',
122123bc813SAndreas Gohr            'background',
123123bc813SAndreas Gohr            'text_alt',
124123bc813SAndreas Gohr            'background_alt',
125123bc813SAndreas Gohr            'text_neu',
126123bc813SAndreas Gohr            'background_neu',
127123bc813SAndreas Gohr            'border',
128123bc813SAndreas Gohr            'highlight',
129123bc813SAndreas Gohr            'background_site',
130123bc813SAndreas Gohr            'link',
131123bc813SAndreas Gohr            'existing',
132123bc813SAndreas Gohr            'missing',
133123bc813SAndreas Gohr        );
134123bc813SAndreas Gohr
135123bc813SAndreas Gohr        if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) {
136123bc813SAndreas Gohr            return 'class="color"';
137123bc813SAndreas Gohr        } else {
138123bc813SAndreas Gohr            return '';
139123bc813SAndreas Gohr        }
140123bc813SAndreas Gohr    }
141123bc813SAndreas Gohr
142123bc813SAndreas Gohr    /**
143123bc813SAndreas Gohr     * saves the preview.ini (alos called from ajax directly)
144123bc813SAndreas Gohr     */
145123bc813SAndreas Gohr    public function run_preview() {
146123bc813SAndreas Gohr        global $conf;
147123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
148123bc813SAndreas Gohr        io_saveFile($ini, $this->makeini());
149123bc813SAndreas Gohr    }
150123bc813SAndreas Gohr
151123bc813SAndreas Gohr    /**
152123bc813SAndreas Gohr     * deletes the preview.ini
153123bc813SAndreas Gohr     */
154123bc813SAndreas Gohr    protected function run_reset() {
155123bc813SAndreas Gohr        global $conf;
156123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
157123bc813SAndreas Gohr        io_saveFile($ini, '');
158123bc813SAndreas Gohr    }
159123bc813SAndreas Gohr
160123bc813SAndreas Gohr    /**
161123bc813SAndreas Gohr     * deletes the local style.ini replacements
162123bc813SAndreas Gohr     */
163123bc813SAndreas Gohr    protected function run_revert() {
164123bc813SAndreas Gohr        $this->replaceini('');
165123bc813SAndreas Gohr        $this->run_reset();
166123bc813SAndreas Gohr    }
167123bc813SAndreas Gohr
168123bc813SAndreas Gohr    /**
169123bc813SAndreas Gohr     * save the local style.ini replacements
170123bc813SAndreas Gohr     */
171123bc813SAndreas Gohr    protected function run_save() {
172123bc813SAndreas Gohr        $this->replaceini($this->makeini());
173123bc813SAndreas Gohr        $this->run_reset();
174123bc813SAndreas Gohr    }
175123bc813SAndreas Gohr
176123bc813SAndreas Gohr    /**
177123bc813SAndreas Gohr     * create the replacement part of a style.ini from submitted data
178123bc813SAndreas Gohr     *
179123bc813SAndreas Gohr     * @return string
180123bc813SAndreas Gohr     */
181123bc813SAndreas Gohr    protected function makeini() {
182123bc813SAndreas Gohr        global $INPUT;
183123bc813SAndreas Gohr
184123bc813SAndreas Gohr        $ini = "[replacements]\n";
185123bc813SAndreas Gohr        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
186123bc813SAndreas Gohr        $ini .= ";Any values in this section will be overwritten by that tool again\n";
187123bc813SAndreas Gohr        foreach($INPUT->arr('tpl') as $key => $val) {
188123bc813SAndreas Gohr            $ini .= $key.' = "'.addslashes($val).'"'."\n";
189123bc813SAndreas Gohr        }
190123bc813SAndreas Gohr
191123bc813SAndreas Gohr        return $ini;
192123bc813SAndreas Gohr    }
193123bc813SAndreas Gohr
194123bc813SAndreas Gohr    /**
195123bc813SAndreas Gohr     * replaces the replacement parts in the local ini
196123bc813SAndreas Gohr     *
197123bc813SAndreas Gohr     * @param string $new the new ini contents
198123bc813SAndreas Gohr     */
199123bc813SAndreas Gohr    protected function replaceini($new) {
200123bc813SAndreas Gohr        global $conf;
201123bc813SAndreas Gohr        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
202123bc813SAndreas Gohr        if(file_exists($ini)) {
203123bc813SAndreas Gohr            $old = io_readFile($ini);
204123bc813SAndreas Gohr            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
205123bc813SAndreas Gohr            $old = trim($old);
206123bc813SAndreas Gohr        } else {
207123bc813SAndreas Gohr            $old = '';
208123bc813SAndreas Gohr        }
209123bc813SAndreas Gohr
210123bc813SAndreas Gohr        io_makeFileDir($ini);
211123bc813SAndreas Gohr        io_saveFile($ini, "$old\n\n$new");
212123bc813SAndreas Gohr    }
213123bc813SAndreas Gohr
214123bc813SAndreas Gohr}
215123bc813SAndreas Gohr
216123bc813SAndreas Gohr// vim:ts=4:sw=4:et:
217