xref: /dokuwiki/lib/plugins/styling/admin.php (revision 64159a61e94d0ce680071c8890e144982c3a8cbe)
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 Gohrclass admin_plugin_styling extends DokuWiki_Admin_Plugin {
9123bc813SAndreas Gohr
106667cd87SAndreas Gohr    public $ispopup = false;
116667cd87SAndreas Gohr
12123bc813SAndreas Gohr    /**
13123bc813SAndreas Gohr     * @return int sort number in admin menu
14123bc813SAndreas Gohr     */
15123bc813SAndreas Gohr    public function getMenuSort() {
16123bc813SAndreas Gohr        return 1000;
17123bc813SAndreas Gohr    }
18123bc813SAndreas Gohr
19123bc813SAndreas Gohr    /**
20123bc813SAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
21123bc813SAndreas Gohr     */
22123bc813SAndreas Gohr    public function forAdminOnly() {
23123bc813SAndreas Gohr        return true;
24123bc813SAndreas Gohr    }
25123bc813SAndreas Gohr
26123bc813SAndreas Gohr    /**
27123bc813SAndreas Gohr     * handle the different actions (also called from ajax)
28123bc813SAndreas Gohr     */
29123bc813SAndreas Gohr    public function handle() {
30123bc813SAndreas Gohr        global $INPUT;
31123bc813SAndreas Gohr        $run = $INPUT->extract('run')->str('run');
32123bc813SAndreas Gohr        if(!$run) return;
33123bc813SAndreas Gohr        $run = "run_$run";
34123bc813SAndreas Gohr        $this->$run();
35123bc813SAndreas Gohr    }
36123bc813SAndreas Gohr
37123bc813SAndreas Gohr    /**
38123bc813SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
39123bc813SAndreas Gohr     */
40123bc813SAndreas Gohr    public function html() {
416667cd87SAndreas Gohr        $class = 'nopopup';
42d634152eSAnika Henke        if($this->ispopup) $class = 'ispopup page';
436667cd87SAndreas Gohr
446667cd87SAndreas Gohr        echo '<div id="plugin__styling" class="'.$class.'">';
456667cd87SAndreas Gohr        ptln('<h1>'.$this->getLang('menu').'</h1>');
466667cd87SAndreas Gohr        $this->form();
47123bc813SAndreas Gohr        echo '</div>';
48123bc813SAndreas Gohr    }
49123bc813SAndreas Gohr
50123bc813SAndreas Gohr    /**
51123bc813SAndreas Gohr     * Create the actual editing form
52123bc813SAndreas Gohr     */
536667cd87SAndreas Gohr    public function form() {
54123bc813SAndreas Gohr        global $conf;
55123bc813SAndreas Gohr        global $ID;
56fb1f9089SMichael Große
57fb1f9089SMichael Große        $styleUtil = new \dokuwiki\StyleUtils();
58fb1f9089SMichael Große        $styleini     = $styleUtil->cssStyleini($conf['template'], true);
59123bc813SAndreas Gohr        $replacements = $styleini['replacements'];
60123bc813SAndreas Gohr
616667cd87SAndreas Gohr        if($this->ispopup) {
626667cd87SAndreas Gohr            $target = DOKU_BASE.'lib/plugins/styling/popup.php';
63123bc813SAndreas Gohr        } else {
64123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
65123bc813SAndreas Gohr        }
66123bc813SAndreas Gohr
67123bc813SAndreas Gohr        if(empty($replacements)) {
68123bc813SAndreas Gohr            echo '<p class="error">'.$this->getLang('error').'</p>';
69123bc813SAndreas Gohr        } else {
70123bc813SAndreas Gohr            echo $this->locale_xhtml('intro');
71123bc813SAndreas Gohr
72123bc813SAndreas Gohr            echo '<form class="styling" method="post" action="'.$target.'">';
73123bc813SAndreas Gohr
74d634152eSAnika Henke            echo '<table><tbody>';
75123bc813SAndreas Gohr            foreach($replacements as $key => $value) {
76123bc813SAndreas Gohr                $name = tpl_getLang($key);
77123bc813SAndreas Gohr                if(empty($name)) $name = $this->getLang($key);
78123bc813SAndreas Gohr                if(empty($name)) $name = $key;
79123bc813SAndreas Gohr
80123bc813SAndreas Gohr                echo '<tr>';
81147d8f48SAnika Henke                echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>';
82*64159a61SAndreas Gohr                echo '<td><input type="text" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'"
83*64159a61SAndreas Gohr                    value="'.hsc($value).'" '.$this->colorClass($key).' dir="ltr" /></td>';
84123bc813SAndreas Gohr                echo '</tr>';
85123bc813SAndreas Gohr            }
86d634152eSAnika Henke            echo '</tbody></table>';
87123bc813SAndreas Gohr
88418cb617SAndreas Gohr            echo '<p>';
89*64159a61SAndreas Gohr            echo '<button type="submit" name="run[preview]" class="btn_preview primary">'.
90*64159a61SAndreas Gohr                $this->getLang('btn_preview').'</button> ';
91*64159a61SAndreas Gohr            #FIXME only if preview.ini exists:
92*64159a61SAndreas Gohr            echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>';
93123bc813SAndreas Gohr            echo '</p>';
94123bc813SAndreas Gohr
95418cb617SAndreas Gohr            echo '<p>';
96cf2c8e75SAnika Henke            echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>';
97123bc813SAndreas Gohr            echo '</p>';
98123bc813SAndreas Gohr
99418cb617SAndreas Gohr            echo '<p>';
100*64159a61SAndreas Gohr            #FIXME only if local.ini exists:
101*64159a61SAndreas Gohr            echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>';
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