xref: /dokuwiki/lib/plugins/styling/admin.php (revision 7ef28ec151e9f703bc839869e4336ba860b54356)
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 */
871a75f04SAndreas Gohrclass admin_plugin_styling extends DokuWiki_Admin_Plugin
971a75f04SAndreas Gohr{
10123bc813SAndreas Gohr
116667cd87SAndreas Gohr    public $ispopup = false;
126667cd87SAndreas Gohr
13123bc813SAndreas Gohr    /**
14123bc813SAndreas Gohr     * @return int sort number in admin menu
15123bc813SAndreas Gohr     */
1671a75f04SAndreas Gohr    public function getMenuSort()
1771a75f04SAndreas Gohr    {
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     */
2471a75f04SAndreas Gohr    public function forAdminOnly()
2571a75f04SAndreas Gohr    {
26123bc813SAndreas Gohr        return true;
27123bc813SAndreas Gohr    }
28123bc813SAndreas Gohr
29123bc813SAndreas Gohr    /**
30123bc813SAndreas Gohr     * handle the different actions (also called from ajax)
31123bc813SAndreas Gohr     */
3271a75f04SAndreas Gohr    public function handle()
3371a75f04SAndreas Gohr    {
34123bc813SAndreas Gohr        global $INPUT;
35123bc813SAndreas Gohr        $run = $INPUT->extract('run')->str('run');
36123bc813SAndreas Gohr        if (!$run) return;
3771a75f04SAndreas Gohr        $run = 'run'.ucfirst($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     */
4471a75f04SAndreas Gohr    public function html()
4571a75f04SAndreas Gohr    {
466667cd87SAndreas Gohr        $class = 'nopopup';
47d634152eSAnika Henke        if ($this->ispopup) $class = 'ispopup page';
486667cd87SAndreas Gohr
496667cd87SAndreas Gohr        echo '<div id="plugin__styling" class="'.$class.'">';
506667cd87SAndreas Gohr        ptln('<h1>'.$this->getLang('menu').'</h1>');
516667cd87SAndreas Gohr        $this->form();
52123bc813SAndreas Gohr        echo '</div>';
53123bc813SAndreas Gohr    }
54123bc813SAndreas Gohr
55123bc813SAndreas Gohr    /**
56123bc813SAndreas Gohr     * Create the actual editing form
57123bc813SAndreas Gohr     */
5871a75f04SAndreas Gohr    public function form()
5971a75f04SAndreas Gohr    {
60123bc813SAndreas Gohr        global $conf;
61123bc813SAndreas Gohr        global $ID;
62fb1f9089SMichael Große
634593dbd2SAnna Dabrowska        $styleUtil = new \dokuwiki\StyleUtils($conf['template'], true);
644593dbd2SAnna Dabrowska        $styleini     = $styleUtil->cssStyleini();
65123bc813SAndreas Gohr        $replacements = $styleini['replacements'];
66123bc813SAndreas Gohr
676667cd87SAndreas Gohr        if ($this->ispopup) {
686667cd87SAndreas Gohr            $target = DOKU_BASE.'lib/plugins/styling/popup.php';
69123bc813SAndreas Gohr        } else {
70123bc813SAndreas Gohr            $target = wl($ID, array('do' => 'admin', 'page' => 'styling'));
71123bc813SAndreas Gohr        }
72123bc813SAndreas Gohr
73123bc813SAndreas Gohr        if (empty($replacements)) {
74123bc813SAndreas Gohr            echo '<p class="error">'.$this->getLang('error').'</p>';
75123bc813SAndreas Gohr        } else {
76123bc813SAndreas Gohr            echo $this->locale_xhtml('intro');
77123bc813SAndreas Gohr
78123bc813SAndreas Gohr            echo '<form class="styling" method="post" action="'.$target.'">';
79123bc813SAndreas Gohr
80d634152eSAnika Henke            echo '<table><tbody>';
81123bc813SAndreas Gohr            foreach ($replacements as $key => $value) {
82123bc813SAndreas Gohr                $name = tpl_getLang($key);
83123bc813SAndreas Gohr                if (empty($name)) $name = $this->getLang($key);
84123bc813SAndreas Gohr                if (empty($name)) $name = $key;
85123bc813SAndreas Gohr
86123bc813SAndreas Gohr                echo '<tr>';
87147d8f48SAnika Henke                echo '<td><label for="tpl__'.hsc($key).'">'.$name.'</label></td>';
883465db0cSAndreas Gohr                echo '<td><input type="'.$this->colorType($key).'" name="tpl['.hsc($key).']" id="tpl__'.hsc($key).'"
89*7ef28ec1SAndreas Gohr                    value="'.hsc($this->colorValue($value)).'" dir="ltr" /></td>';
90123bc813SAndreas Gohr                echo '</tr>';
91123bc813SAndreas Gohr            }
92d634152eSAnika Henke            echo '</tbody></table>';
93123bc813SAndreas Gohr
94418cb617SAndreas Gohr            echo '<p>';
9564159a61SAndreas Gohr            echo '<button type="submit" name="run[preview]" class="btn_preview primary">'.
9664159a61SAndreas Gohr                $this->getLang('btn_preview').'</button> ';
9764159a61SAndreas Gohr            #FIXME only if preview.ini exists:
9864159a61SAndreas Gohr            echo '<button type="submit" name="run[reset]">'.$this->getLang('btn_reset').'</button>';
99123bc813SAndreas Gohr            echo '</p>';
100123bc813SAndreas Gohr
101418cb617SAndreas Gohr            echo '<p>';
102cf2c8e75SAnika Henke            echo '<button type="submit" name="run[save]" class="primary">'.$this->getLang('btn_save').'</button>';
103123bc813SAndreas Gohr            echo '</p>';
104123bc813SAndreas Gohr
105418cb617SAndreas Gohr            echo '<p>';
10664159a61SAndreas Gohr            #FIXME only if local.ini exists:
10764159a61SAndreas Gohr            echo '<button type="submit" name="run[revert]">'.$this->getLang('btn_revert').'</button>';
108123bc813SAndreas Gohr            echo '</p>';
109123bc813SAndreas Gohr
110123bc813SAndreas Gohr            echo '</form>';
111123bc813SAndreas Gohr
112123bc813SAndreas Gohr            echo tpl_locale_xhtml('style');
113123bc813SAndreas Gohr        }
114123bc813SAndreas Gohr    }
115123bc813SAndreas Gohr
116123bc813SAndreas Gohr    /**
117*7ef28ec1SAndreas Gohr     * Adjust three char color codes to the 6 char one supported by browser's color input
118*7ef28ec1SAndreas Gohr     *
119*7ef28ec1SAndreas Gohr     * @param string $value
120*7ef28ec1SAndreas Gohr     * @return string
121*7ef28ec1SAndreas Gohr     */
122*7ef28ec1SAndreas Gohr    protected function colorValue($value)
123*7ef28ec1SAndreas Gohr    {
124*7ef28ec1SAndreas Gohr        if (preg_match('/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])$/', $value, $match)) {
125*7ef28ec1SAndreas Gohr            return '#' . $match[1] . $match[1] . $match[2] . $match[2] . $match[3] . $match[3];
126*7ef28ec1SAndreas Gohr        }
127*7ef28ec1SAndreas Gohr        return $value;
128*7ef28ec1SAndreas Gohr    }
129*7ef28ec1SAndreas Gohr
130*7ef28ec1SAndreas Gohr    /**
1313465db0cSAndreas Gohr     * Decide the input type based on the key name
1323465db0cSAndreas Gohr     *
1333465db0cSAndreas Gohr     * @param string $key
1343465db0cSAndreas Gohr     * @return string color|text
135123bc813SAndreas Gohr     */
1363465db0cSAndreas Gohr    protected function colorType($key)
13771a75f04SAndreas Gohr    {
138123bc813SAndreas Gohr        static $colors = array(
139123bc813SAndreas Gohr            'text',
140123bc813SAndreas Gohr            'background',
141123bc813SAndreas Gohr            'text_alt',
142123bc813SAndreas Gohr            'background_alt',
143123bc813SAndreas Gohr            'text_neu',
144123bc813SAndreas Gohr            'background_neu',
145123bc813SAndreas Gohr            'border',
146123bc813SAndreas Gohr            'highlight',
147123bc813SAndreas Gohr            'background_site',
148123bc813SAndreas Gohr            'link',
149123bc813SAndreas Gohr            'existing',
150123bc813SAndreas Gohr            'missing',
151123bc813SAndreas Gohr        );
152123bc813SAndreas Gohr
153123bc813SAndreas Gohr        if (preg_match('/colou?r/', $key) || in_array(trim($key, '_'), $colors)) {
1543465db0cSAndreas Gohr            return 'color';
155123bc813SAndreas Gohr        } else {
1563465db0cSAndreas Gohr            return 'text';
157123bc813SAndreas Gohr        }
158123bc813SAndreas Gohr    }
159123bc813SAndreas Gohr
160123bc813SAndreas Gohr    /**
161123bc813SAndreas Gohr     * saves the preview.ini (alos called from ajax directly)
162123bc813SAndreas Gohr     */
16371a75f04SAndreas Gohr    public function runPreview()
16471a75f04SAndreas Gohr    {
165123bc813SAndreas Gohr        global $conf;
166123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
167123bc813SAndreas Gohr        io_saveFile($ini, $this->makeini());
168123bc813SAndreas Gohr    }
169123bc813SAndreas Gohr
170123bc813SAndreas Gohr    /**
171123bc813SAndreas Gohr     * deletes the preview.ini
172123bc813SAndreas Gohr     */
17371a75f04SAndreas Gohr    protected function runReset()
17471a75f04SAndreas Gohr    {
175123bc813SAndreas Gohr        global $conf;
176123bc813SAndreas Gohr        $ini = $conf['cachedir'].'/preview.ini';
177123bc813SAndreas Gohr        io_saveFile($ini, '');
178123bc813SAndreas Gohr    }
179123bc813SAndreas Gohr
180123bc813SAndreas Gohr    /**
181123bc813SAndreas Gohr     * deletes the local style.ini replacements
182123bc813SAndreas Gohr     */
18371a75f04SAndreas Gohr    protected function runRevert()
18471a75f04SAndreas Gohr    {
18571a75f04SAndreas Gohr        $this->replaceIni('');
18671a75f04SAndreas Gohr        $this->runReset();
187123bc813SAndreas Gohr    }
188123bc813SAndreas Gohr
189123bc813SAndreas Gohr    /**
190123bc813SAndreas Gohr     * save the local style.ini replacements
191123bc813SAndreas Gohr     */
19271a75f04SAndreas Gohr    protected function runSave()
19371a75f04SAndreas Gohr    {
19471a75f04SAndreas Gohr        $this->replaceIni($this->makeini());
19571a75f04SAndreas Gohr        $this->runReset();
196123bc813SAndreas Gohr    }
197123bc813SAndreas Gohr
198123bc813SAndreas Gohr    /**
199123bc813SAndreas Gohr     * create the replacement part of a style.ini from submitted data
200123bc813SAndreas Gohr     *
201123bc813SAndreas Gohr     * @return string
202123bc813SAndreas Gohr     */
20371a75f04SAndreas Gohr    protected function makeini()
20471a75f04SAndreas Gohr    {
205123bc813SAndreas Gohr        global $INPUT;
206123bc813SAndreas Gohr
207123bc813SAndreas Gohr        $ini = "[replacements]\n";
208123bc813SAndreas Gohr        $ini .= ";These overwrites have been generated from the Template styling Admin interface\n";
209123bc813SAndreas Gohr        $ini .= ";Any values in this section will be overwritten by that tool again\n";
210123bc813SAndreas Gohr        foreach ($INPUT->arr('tpl') as $key => $val) {
211123bc813SAndreas Gohr            $ini .= $key.' = "'.addslashes($val).'"'."\n";
212123bc813SAndreas Gohr        }
213123bc813SAndreas Gohr
214123bc813SAndreas Gohr        return $ini;
215123bc813SAndreas Gohr    }
216123bc813SAndreas Gohr
217123bc813SAndreas Gohr    /**
218123bc813SAndreas Gohr     * replaces the replacement parts in the local ini
219123bc813SAndreas Gohr     *
220123bc813SAndreas Gohr     * @param string $new the new ini contents
221123bc813SAndreas Gohr     */
22271a75f04SAndreas Gohr    protected function replaceIni($new)
22371a75f04SAndreas Gohr    {
224123bc813SAndreas Gohr        global $conf;
225123bc813SAndreas Gohr        $ini = DOKU_CONF."tpl/".$conf['template']."/style.ini";
226123bc813SAndreas Gohr        if (file_exists($ini)) {
227123bc813SAndreas Gohr            $old = io_readFile($ini);
228123bc813SAndreas Gohr            $old = preg_replace('/\[replacements\]\n.*?(\n\[.*]|$)/s', '\\1', $old);
229123bc813SAndreas Gohr            $old = trim($old);
230123bc813SAndreas Gohr        } else {
231123bc813SAndreas Gohr            $old = '';
232123bc813SAndreas Gohr        }
233123bc813SAndreas Gohr
234123bc813SAndreas Gohr        io_makeFileDir($ini);
235123bc813SAndreas Gohr        io_saveFile($ini, "$old\n\n$new");
236123bc813SAndreas Gohr    }
237123bc813SAndreas Gohr}
238123bc813SAndreas Gohr
239123bc813SAndreas Gohr// vim:ts=4:sw=4:et:
240