xref: /dokuwiki/lib/plugins/styling/admin.php (revision 123bc813fd93ab5d8dab3cc4a66a09e613a10aa2)
1*123bc813SAndreas Gohr<?php
2*123bc813SAndreas Gohr/**
3*123bc813SAndreas Gohr * DokuWiki Plugin styling (Admin Component)
4*123bc813SAndreas Gohr *
5*123bc813SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*123bc813SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
7*123bc813SAndreas Gohr */
8*123bc813SAndreas Gohr
9*123bc813SAndreas Gohr// must be run within Dokuwiki
10*123bc813SAndreas Gohrif(!defined('DOKU_INC')) die();
11*123bc813SAndreas Gohr
12*123bc813SAndreas Gohrclass admin_plugin_styling extends DokuWiki_Admin_Plugin {
13*123bc813SAndreas Gohr
14*123bc813SAndreas Gohr    /**
15*123bc813SAndreas Gohr     * @return int sort number in admin menu
16*123bc813SAndreas Gohr     */
17*123bc813SAndreas Gohr    public function getMenuSort() {
18*123bc813SAndreas Gohr        return 1000;
19*123bc813SAndreas Gohr    }
20*123bc813SAndreas Gohr
21*123bc813SAndreas Gohr    /**
22*123bc813SAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
23*123bc813SAndreas Gohr     */
24*123bc813SAndreas Gohr    public function forAdminOnly() {
25*123bc813SAndreas Gohr        return true;
26*123bc813SAndreas Gohr    }
27*123bc813SAndreas Gohr
28*123bc813SAndreas Gohr    /**
29*123bc813SAndreas Gohr     * @param string $language
30*123bc813SAndreas Gohr     * @return string
31*123bc813SAndreas Gohr     */
32*123bc813SAndreas Gohr    public function getMenuText($language) {
33*123bc813SAndreas Gohr        $js = $this->getLang('js');
34*123bc813SAndreas Gohr        return $js['menu'];
35*123bc813SAndreas Gohr    }
36*123bc813SAndreas Gohr
37*123bc813SAndreas Gohr    /**
38*123bc813SAndreas Gohr     * handle the different actions (also called from ajax)
39*123bc813SAndreas Gohr     */
40*123bc813SAndreas Gohr    public function handle() {
41*123bc813SAndreas Gohr        global $INPUT;
42*123bc813SAndreas Gohr        $run = $INPUT->extract('run')->str('run');
43*123bc813SAndreas Gohr        if(!$run) return;
44*123bc813SAndreas Gohr        $run = "run_$run";
45*123bc813SAndreas Gohr        $this->$run();
46*123bc813SAndreas Gohr    }
47*123bc813SAndreas Gohr
48*123bc813SAndreas Gohr    /**
49*123bc813SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
50*123bc813SAndreas Gohr     */
51*123bc813SAndreas Gohr    public function html() {
52*123bc813SAndreas Gohr        echo '<div id="plugin__styling">';
53*123bc813SAndreas Gohr        ptln('<h1>'.$this->getMenuText('').'</h1>');
54*123bc813SAndreas Gohr        $this->form(false);
55*123bc813SAndreas Gohr        echo '</div>';
56*123bc813SAndreas Gohr    }
57*123bc813SAndreas Gohr
58*123bc813SAndreas Gohr    /**
59*123bc813SAndreas Gohr     * Create the actual editing form
60*123bc813SAndreas Gohr     */
61*123bc813SAndreas Gohr    public function form($isajax) {
62*123bc813SAndreas Gohr        global $conf;
63*123bc813SAndreas Gohr        global $ID;
64*123bc813SAndreas Gohr        define('SIMPLE_TEST', 1); // hack, ideally certain functions should be moved out of css.php
65*123bc813SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
66*123bc813SAndreas Gohr        $styleini     = css_styleini($conf['template'], true);
67*123bc813SAndreas Gohr        $replacements = $styleini['replacements'];
68*123bc813SAndreas Gohr
69*123bc813SAndreas Gohr        if($isajax) {
70*123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'styling_plugin'));
71*123bc813SAndreas Gohr        } else {
72*123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
73*123bc813SAndreas Gohr        }
74*123bc813SAndreas Gohr
75*123bc813SAndreas Gohr        if(empty($replacements)) {
76*123bc813SAndreas Gohr            echo '<p class="error">'.$this->getLang('error').'</p>';
77*123bc813SAndreas Gohr        } else {
78*123bc813SAndreas Gohr            echo $this->locale_xhtml('intro');
79*123bc813SAndreas Gohr
80*123bc813SAndreas Gohr            echo '<form class="styling" method="post" action="'.$target.'">';
81*123bc813SAndreas Gohr
82*123bc813SAndreas Gohr            echo '<table>';
83*123bc813SAndreas Gohr            foreach($replacements as $key => $value) {
84*123bc813SAndreas Gohr                $name = tpl_getLang($key);
85*123bc813SAndreas Gohr                if(empty($name)) $name = $this->getLang($key);
86*123bc813SAndreas Gohr                if(empty($name)) $name = $key;
87*123bc813SAndreas Gohr
88*123bc813SAndreas Gohr                echo '<tr>';
89*123bc813SAndreas Gohr                echo '<td>'.$name.'</td>';
90*123bc813SAndreas Gohr                echo '<td><input type="text" name="tpl['.hsc($key).']" value="'.hsc($value).'" '.$this->colorClass($key).' />';
91*123bc813SAndreas Gohr                echo '</tr>';
92*123bc813SAndreas Gohr            }
93*123bc813SAndreas Gohr            echo '</table>';
94*123bc813SAndreas Gohr
95*123bc813SAndreas Gohr            echo '<p class="center">';
96*123bc813SAndreas Gohr            echo '<input type="submit" name="run[preview]" class="btn_preview" value="'.$this->getLang('btn_preview').'">';
97*123bc813SAndreas Gohr            echo '<input type="submit" name="run[reset]" value="'.$this->getLang('btn_reset').'">'; #FIXME only if preview.ini exists
98*123bc813SAndreas Gohr            echo '</p>';
99*123bc813SAndreas Gohr
100*123bc813SAndreas Gohr            echo '<p class="center">';
101*123bc813SAndreas Gohr            echo '<input type="submit" name="run[save]" value="'.$this->getLang('btn_save').'">';
102*123bc813SAndreas Gohr            echo '</p>';
103*123bc813SAndreas Gohr
104*123bc813SAndreas Gohr            echo '<p class="center">';
105*123bc813SAndreas Gohr            echo '<input type="submit" name="run[revert]" value="'.$this->getLang('btn_revert').'">'; #FIXME only if local.ini exists
106*123bc813SAndreas Gohr            echo '</p>';
107*123bc813SAndreas Gohr
108*123bc813SAndreas Gohr            echo '</form>';
109*123bc813SAndreas Gohr
110*123bc813SAndreas Gohr            echo tpl_locale_xhtml('style');
111*123bc813SAndreas Gohr
112*123bc813SAndreas Gohr        }
113*123bc813SAndreas Gohr    }
114*123bc813SAndreas Gohr
115*123bc813SAndreas Gohr    /**
116*123bc813SAndreas Gohr     * set the color class attribute
117*123bc813SAndreas Gohr     */
118*123bc813SAndreas Gohr    protected function colorClass($key) {
119*123bc813SAndreas Gohr        static $colors = array(
120*123bc813SAndreas Gohr            'text',
121*123bc813SAndreas Gohr            'background',
122*123bc813SAndreas Gohr            'text_alt',
123*123bc813SAndreas Gohr            'background_alt',
124*123bc813SAndreas Gohr            'text_neu',
125*123bc813SAndreas Gohr            'background_neu',
126*123bc813SAndreas Gohr            'border',
127*123bc813SAndreas Gohr            'highlight',
128*123bc813SAndreas Gohr            'background_site',
129*123bc813SAndreas Gohr            'link',
130*123bc813SAndreas Gohr            'existing',
131*123bc813SAndreas Gohr            'missing',
132*123bc813SAndreas Gohr        );
133*123bc813SAndreas Gohr
134*123bc813SAndreas Gohr        if(preg_match('/colou?r/', $key) || in_array(trim($key,'_'), $colors)) {
135*123bc813SAndreas Gohr            return 'class="color"';
136*123bc813SAndreas Gohr        } else {
137*123bc813SAndreas Gohr            return '';
138*123bc813SAndreas Gohr        }
139*123bc813SAndreas Gohr    }
140*123bc813SAndreas Gohr
141*123bc813SAndreas Gohr    /**
142*123bc813SAndreas Gohr     * saves the preview.ini (alos called from ajax directly)
143*123bc813SAndreas Gohr     */
144*123bc813SAndreas Gohr    public function run_preview() {
145*123bc813SAndreas Gohr        global $conf;
146*123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
147*123bc813SAndreas Gohr        io_saveFile($ini, $this->makeini());
148*123bc813SAndreas Gohr    }
149*123bc813SAndreas Gohr
150*123bc813SAndreas Gohr    /**
151*123bc813SAndreas Gohr     * deletes the preview.ini
152*123bc813SAndreas Gohr     */
153*123bc813SAndreas Gohr    protected function run_reset() {
154*123bc813SAndreas Gohr        global $conf;
155*123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
156*123bc813SAndreas Gohr        io_saveFile($ini, '');
157*123bc813SAndreas Gohr    }
158*123bc813SAndreas Gohr
159*123bc813SAndreas Gohr    /**
160*123bc813SAndreas Gohr     * deletes the local style.ini replacements
161*123bc813SAndreas Gohr     */
162*123bc813SAndreas Gohr    protected function run_revert() {
163*123bc813SAndreas Gohr        $this->replaceini('');
164*123bc813SAndreas Gohr        $this->run_reset();
165*123bc813SAndreas Gohr    }
166*123bc813SAndreas Gohr
167*123bc813SAndreas Gohr    /**
168*123bc813SAndreas Gohr     * save the local style.ini replacements
169*123bc813SAndreas Gohr     */
170*123bc813SAndreas Gohr    protected function run_save() {
171*123bc813SAndreas Gohr        $this->replaceini($this->makeini());
172*123bc813SAndreas Gohr        $this->run_reset();
173*123bc813SAndreas Gohr    }
174*123bc813SAndreas Gohr
175*123bc813SAndreas Gohr    /**
176*123bc813SAndreas Gohr     * create the replacement part of a style.ini from submitted data
177*123bc813SAndreas Gohr     *
178*123bc813SAndreas Gohr     * @return string
179*123bc813SAndreas Gohr     */
180*123bc813SAndreas Gohr    protected function makeini() {
181*123bc813SAndreas Gohr        global $INPUT;
182*123bc813SAndreas Gohr
183*123bc813SAndreas Gohr        $ini = "[replacements]\n";
184*123bc813SAndreas Gohr        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
185*123bc813SAndreas Gohr        $ini .= ";Any values in this section will be overwritten by that tool again\n";
186*123bc813SAndreas Gohr        foreach($INPUT->arr('tpl') as $key => $val) {
187*123bc813SAndreas Gohr            $ini .= $key.' = "'.addslashes($val).'"'."\n";
188*123bc813SAndreas Gohr        }
189*123bc813SAndreas Gohr
190*123bc813SAndreas Gohr        return $ini;
191*123bc813SAndreas Gohr    }
192*123bc813SAndreas Gohr
193*123bc813SAndreas Gohr    /**
194*123bc813SAndreas Gohr     * replaces the replacement parts in the local ini
195*123bc813SAndreas Gohr     *
196*123bc813SAndreas Gohr     * @param string $new the new ini contents
197*123bc813SAndreas Gohr     */
198*123bc813SAndreas Gohr    protected function replaceini($new) {
199*123bc813SAndreas Gohr        global $conf;
200*123bc813SAndreas Gohr        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
201*123bc813SAndreas Gohr        if(file_exists($ini)) {
202*123bc813SAndreas Gohr            $old = io_readFile($ini);
203*123bc813SAndreas Gohr            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
204*123bc813SAndreas Gohr            $old = trim($old);
205*123bc813SAndreas Gohr        } else {
206*123bc813SAndreas Gohr            $old = '';
207*123bc813SAndreas Gohr        }
208*123bc813SAndreas Gohr
209*123bc813SAndreas Gohr        io_makeFileDir($ini);
210*123bc813SAndreas Gohr        io_saveFile($ini, "$old\n\n$new");
211*123bc813SAndreas Gohr    }
212*123bc813SAndreas Gohr
213*123bc813SAndreas Gohr}
214*123bc813SAndreas Gohr
215*123bc813SAndreas Gohr// vim:ts=4:sw=4:et:
216