xref: /dokuwiki/lib/plugins/config/admin.php (revision f8dcd5b0e309717c3de78e412e2914b7b9505c33)
110449332Schris<?php
210449332Schris/**
310449332Schris * Configuration Manager admin plugin
410449332Schris *
510449332Schris * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
610449332Schris * @author     Christopher Smith <chris@jalakai.co.uk>
7685bdd2eSBen Coburn * @author     Ben Coburn <btcoburn@silicodon.net>
810449332Schris */
910449332Schris
10c6639e6aSAndreas Gohruse dokuwiki\plugin\config\core\Configuration;
110a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\Setting;
120a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingFieldset;
130a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingHidden;
1410449332Schris
1510449332Schris/**
1610449332Schris * All DokuWiki plugins to extend the admin function
1710449332Schris * need to inherit from this class
1810449332Schris */
1910449332Schrisclass admin_plugin_config extends DokuWiki_Admin_Plugin {
2010449332Schris
21c6639e6aSAndreas Gohr    const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/';
22c6639e6aSAndreas Gohr
23077c27b2SAndreas Gohr    /** @var Configuration */
24077c27b2SAndreas Gohr    protected $configuration;
25077c27b2SAndreas Gohr
26a017dff4SAndreas Gohr    /** @var bool were there any errors in the submitted data? */
27a017dff4SAndreas Gohr    protected $hasErrors = false;
28a017dff4SAndreas Gohr
29a017dff4SAndreas Gohr    /** @var bool have the settings translations been loaded? */
30a017dff4SAndreas Gohr    protected $promptsLocalized = false;
31a017dff4SAndreas Gohr
3210449332Schris
3310449332Schris    /**
3410449332Schris     * handle user request
3510449332Schris     */
3661e35c35SAndreas Gohr    public function handle() {
37392c9b52SHakan Sandell        global $ID, $INPUT;
3810449332Schris
39*f8dcd5b0SAndreas Gohr        // always initialize the configuration
40*f8dcd5b0SAndreas Gohr        $this->configuration = new Configuration();
41*f8dcd5b0SAndreas Gohr
42077c27b2SAndreas Gohr        if(!$INPUT->bool('save') || !checkSecurityToken()) {
43253d4b48SGerrit Uitslag            return;
44253d4b48SGerrit Uitslag        }
4510449332Schris
4610449332Schris        // don't go any further if the configuration is locked
47077c27b2SAndreas Gohr        if($this->configuration->isLocked()) return;
48077c27b2SAndreas Gohr
49077c27b2SAndreas Gohr        // update settings and redirect of successful
50077c27b2SAndreas Gohr        $ok = $this->configuration->updateSettings($INPUT->arr('config'));
51077c27b2SAndreas Gohr        if($ok) { // no errors
52077c27b2SAndreas Gohr            try {
53077c27b2SAndreas Gohr                if($this->configuration->hasChanged()) {
54077c27b2SAndreas Gohr                    $this->configuration->save();
55077c27b2SAndreas Gohr                } else {
56077c27b2SAndreas Gohr                    $this->configuration->touch();
57253d4b48SGerrit Uitslag                }
58e98b5e44SAndreas Gohr                msg($this->getLang('updated'), 1);
59077c27b2SAndreas Gohr            } catch(Exception $e) {
60077c27b2SAndreas Gohr                msg($this->getLang('error'), -1);
6110449332Schris            }
623295f40aSAndreas Gohr            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'config'), true, '&'));
63a017dff4SAndreas Gohr        } else {
64a017dff4SAndreas Gohr            $this->hasErrors = true;
6510449332Schris        }
6610449332Schris    }
6710449332Schris
6810449332Schris    /**
6910449332Schris     * output appropriate html
7010449332Schris     */
7161e35c35SAndreas Gohr    public function html() {
72685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
7310449332Schris        global $lang;
74400497e1Schris        global $ID;
7510449332Schris
7689d74a2fSchris        $this->setupLocale(true);
7710449332Schris
78be1cc9aeSAndreas Gohr        echo $this->locale_xhtml('intro');
7910449332Schris
80be1cc9aeSAndreas Gohr        echo '<div id="config__manager">';
8110449332Schris
82077c27b2SAndreas Gohr        if($this->configuration->isLocked()) {
83be1cc9aeSAndreas Gohr            echo '<div class="info">' . $this->getLang('locked') . '</div>';
84077c27b2SAndreas Gohr        }
8510449332Schris
865d85efc6SAdrian Lang        // POST to script() instead of wl($ID) so config manager still works if
875d85efc6SAdrian Lang        // rewrite config is broken. Add $ID as hidden field to remember
885d85efc6SAdrian Lang        // current ID in most cases.
89be1cc9aeSAndreas Gohr        echo '<form action="' . script() . '" method="post">';
90be1cc9aeSAndreas Gohr        echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
91634d7150SAndreas Gohr        formSecurityToken();
92a1ef8b4dSAndreas Gohr        $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
9310449332Schris
944fa2dffcSBen Coburn        $in_fieldset = false;
954fa2dffcSBen Coburn        $first_plugin_fieldset = true;
964fa2dffcSBen Coburn        $first_template_fieldset = true;
97077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
98077c27b2SAndreas Gohr            if(is_a($setting, SettingHidden::class)) {
99685bdd2eSBen Coburn                continue;
100077c27b2SAndreas Gohr            } else if(is_a($setting, settingFieldset::class)) {
1014fa2dffcSBen Coburn                // config setting group
1024fa2dffcSBen Coburn                if($in_fieldset) {
103be1cc9aeSAndreas Gohr                    echo '</table>';
104be1cc9aeSAndreas Gohr                    echo '</div>';
105be1cc9aeSAndreas Gohr                    echo '</fieldset>';
1064fa2dffcSBen Coburn                } else {
1074fa2dffcSBen Coburn                    $in_fieldset = true;
1084fa2dffcSBen Coburn                }
109a017dff4SAndreas Gohr                if($first_plugin_fieldset && $setting->getType() == 'plugin') {
110a1ef8b4dSAndreas Gohr                    $this->printH1('plugin_settings', $this->getLang('_header_plugin'));
1114fa2dffcSBen Coburn                    $first_plugin_fieldset = false;
112a017dff4SAndreas Gohr                } else if($first_template_fieldset && $setting->getType() == 'template') {
113a1ef8b4dSAndreas Gohr                    $this->printH1('template_settings', $this->getLang('_header_template'));
1144fa2dffcSBen Coburn                    $first_template_fieldset = false;
1154fa2dffcSBen Coburn                }
116be1cc9aeSAndreas Gohr                echo '<fieldset id="' . $setting->getKey() . '">';
117be1cc9aeSAndreas Gohr                echo '<legend>' . $setting->prompt($this) . '</legend>';
118be1cc9aeSAndreas Gohr                echo '<div class="table">';
119be1cc9aeSAndreas Gohr                echo '<table class="inline">';
1204fa2dffcSBen Coburn            } else {
1214fa2dffcSBen Coburn                // config settings
122a017dff4SAndreas Gohr                list($label, $input) = $setting->html($this, $this->hasErrors);
12310449332Schris
1245c17d2d3SAndreas Gohr                $class = $setting->isDefault()
12564159a61SAndreas Gohr                    ? ' class="default"'
1265c17d2d3SAndreas Gohr                    : ($setting->isProtected() ? ' class="protected"' : '');
1275c17d2d3SAndreas Gohr                $error = $setting->hasError()
12864159a61SAndreas Gohr                    ? ' class="value error"'
12964159a61SAndreas Gohr                    : ' class="value"';
13064159a61SAndreas Gohr                $icon = $setting->caution()
131c6639e6aSAndreas Gohr                    ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' .
13264159a61SAndreas Gohr                    'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />'
13364159a61SAndreas Gohr                    : '';
13410449332Schris
135be1cc9aeSAndreas Gohr                echo '<tr' . $class . '>';
136be1cc9aeSAndreas Gohr                echo '<td class="label">';
137be1cc9aeSAndreas Gohr                echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>';
138be1cc9aeSAndreas Gohr                echo $icon . $label;
139be1cc9aeSAndreas Gohr                echo '</td>';
140be1cc9aeSAndreas Gohr                echo '<td' . $error . '>' . $input . '</td>';
141be1cc9aeSAndreas Gohr                echo '</tr>';
14210449332Schris            }
1434fa2dffcSBen Coburn        }
14410449332Schris
145be1cc9aeSAndreas Gohr        echo '</table>';
146be1cc9aeSAndreas Gohr        echo '</div>';
1474fa2dffcSBen Coburn        if($in_fieldset) {
148be1cc9aeSAndreas Gohr            echo '</fieldset>';
1494fa2dffcSBen Coburn        }
15010449332Schris
151685bdd2eSBen Coburn        // show undefined settings list
152077c27b2SAndreas Gohr        $undefined_settings = $this->configuration->getUndefined();
153685bdd2eSBen Coburn        if($allow_debug && !empty($undefined_settings)) {
154253d4b48SGerrit Uitslag            /**
155253d4b48SGerrit Uitslag             * Callback for sorting settings
156253d4b48SGerrit Uitslag             *
157077c27b2SAndreas Gohr             * @param Setting $a
158077c27b2SAndreas Gohr             * @param Setting $b
159253d4b48SGerrit Uitslag             * @return int if $a is lower/equal/higher than $b
160253d4b48SGerrit Uitslag             */
161a1ef8b4dSAndreas Gohr            function settingNaturalComparison($a, $b) {
162077c27b2SAndreas Gohr                return strnatcmp($a->getKey(), $b->getKey());
163253d4b48SGerrit Uitslag            }
164253d4b48SGerrit Uitslag
165a1ef8b4dSAndreas Gohr            usort($undefined_settings, 'settingNaturalComparison');
166a1ef8b4dSAndreas Gohr            $this->printH1('undefined_settings', $this->getLang('_header_undefined'));
167be1cc9aeSAndreas Gohr            echo '<fieldset>';
168be1cc9aeSAndreas Gohr            echo '<div class="table">';
169be1cc9aeSAndreas Gohr            echo '<table class="inline">';
170685bdd2eSBen Coburn            foreach($undefined_settings as $setting) {
171e7296041SAndreas Gohr                list($label, $input) = $setting->html($this);
172be1cc9aeSAndreas Gohr                echo '<tr>';
173e7296041SAndreas Gohr                echo '<td class="label">' . $label . '</td>';
174e7296041SAndreas Gohr                echo '<td>' . $input . '</td>';
175be1cc9aeSAndreas Gohr                echo '</tr>';
176685bdd2eSBen Coburn            }
177be1cc9aeSAndreas Gohr            echo '</table>';
178be1cc9aeSAndreas Gohr            echo '</div>';
179be1cc9aeSAndreas Gohr            echo '</fieldset>';
180685bdd2eSBen Coburn        }
181685bdd2eSBen Coburn
182685bdd2eSBen Coburn        // finish up form
183be1cc9aeSAndreas Gohr        echo '<p>';
184be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="do"     value="admin" />';
185be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="page"   value="config" />';
18610449332Schris
187077c27b2SAndreas Gohr        if(!$this->configuration->isLocked()) {
188be1cc9aeSAndreas Gohr            echo '<input type="hidden" name="save"   value="1" />';
189be1cc9aeSAndreas Gohr            echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
190be1cc9aeSAndreas Gohr            echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
19110449332Schris        }
19210449332Schris
193be1cc9aeSAndreas Gohr        echo '</p>';
19410449332Schris
195be1cc9aeSAndreas Gohr        echo '</form>';
196be1cc9aeSAndreas Gohr        echo '</div>';
19710449332Schris    }
19810449332Schris
19910449332Schris    /**
200253d4b48SGerrit Uitslag     * @param bool $prompts
201253d4b48SGerrit Uitslag     */
20261e35c35SAndreas Gohr    public function setupLocale($prompts = false) {
20389d74a2fSchris        parent::setupLocale();
204a017dff4SAndreas Gohr        if(!$prompts || $this->promptsLocalized) return;
205d6fc72e1SAndreas Gohr        $this->lang = array_merge($this->lang, $this->configuration->getLangs());
206a017dff4SAndreas Gohr        $this->promptsLocalized = true;
20789d74a2fSchris    }
20889d74a2fSchris
2094fa2dffcSBen Coburn    /**
2104fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2114fa2dffcSBen Coburn     *
2124fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
213253d4b48SGerrit Uitslag     *
214253d4b48SGerrit Uitslag     * @return array
2154fa2dffcSBen Coburn     */
21661e35c35SAndreas Gohr    public function getTOC() {
217b8595a66SAndreas Gohr        $this->setupLocale(true);
218b8595a66SAndreas Gohr
219685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
220d6fc72e1SAndreas Gohr        $toc = array();
221d6fc72e1SAndreas Gohr        $check = false;
222685bdd2eSBen Coburn
223a017dff4SAndreas Gohr        // gather settings data into three sub arrays
224d6fc72e1SAndreas Gohr        $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
225077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
226d6fc72e1SAndreas Gohr            if(is_a($setting, SettingFieldset::class)) {
227d6fc72e1SAndreas Gohr                $labels[$setting->getType()][] = $setting;
2284fa2dffcSBen Coburn            }
2294fa2dffcSBen Coburn        }
2304fa2dffcSBen Coburn
231d6fc72e1SAndreas Gohr        // top header
232cbfba956SMichael Hamann        $title = $this->getLang('_configuration_manager');
233d6fc72e1SAndreas Gohr        $toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
234d6fc72e1SAndreas Gohr
235d6fc72e1SAndreas Gohr        // main entries
236d6fc72e1SAndreas Gohr        foreach(['dokuwiki', 'plugin', 'template'] as $section) {
237d6fc72e1SAndreas Gohr            if(empty($labels[$section])) continue; // no entries, skip
238d6fc72e1SAndreas Gohr
239d6fc72e1SAndreas Gohr            // create main header
240d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem(
241d6fc72e1SAndreas Gohr                $section . '_settings',
242d6fc72e1SAndreas Gohr                $this->getLang('_header_' . $section),
243d6fc72e1SAndreas Gohr                1
244d6fc72e1SAndreas Gohr            );
245d6fc72e1SAndreas Gohr
246d6fc72e1SAndreas Gohr            // create sub headers
247d6fc72e1SAndreas Gohr            foreach($labels[$section] as $setting) {
248d6fc72e1SAndreas Gohr                /** @var SettingFieldset $setting */
249e1b31a95SBen Coburn                $name = $setting->prompt($this);
250d6fc72e1SAndreas Gohr                $toc[] = html_mktocitem($setting->getKey(), $name, 2);
251e1b31a95SBen Coburn            }
252e1b31a95SBen Coburn        }
253e1b31a95SBen Coburn
254d6fc72e1SAndreas Gohr        // undefined settings if allowed
255d6fc72e1SAndreas Gohr        if(count($this->configuration->getUndefined()) && $allow_debug) {
256d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
257d6fc72e1SAndreas Gohr        }
258d6fc72e1SAndreas Gohr
259d6fc72e1SAndreas Gohr        return $toc;
2604fa2dffcSBen Coburn    }
2614fa2dffcSBen Coburn
262253d4b48SGerrit Uitslag    /**
263253d4b48SGerrit Uitslag     * @param string $id
264253d4b48SGerrit Uitslag     * @param string $text
265253d4b48SGerrit Uitslag     */
266a1ef8b4dSAndreas Gohr    protected function printH1($id, $text) {
267be1cc9aeSAndreas Gohr        echo '<h1 id="' . $id . '">' . $text . '</h1>';
2684fa2dffcSBen Coburn    }
2694fa2dffcSBen Coburn
27061e35c35SAndreas Gohr    /**
27161e35c35SAndreas Gohr     * Adds a translation to this plugin's language array
27261e35c35SAndreas Gohr     *
273a017dff4SAndreas Gohr     * Used by some settings to set up dynamic translations
274a017dff4SAndreas Gohr     *
27561e35c35SAndreas Gohr     * @param string $key
27661e35c35SAndreas Gohr     * @param string $value
27761e35c35SAndreas Gohr     */
27861e35c35SAndreas Gohr    public function addLang($key, $value) {
27961e35c35SAndreas Gohr        if(!$this->localised) $this->setupLocale();
28061e35c35SAndreas Gohr        $this->lang[$key] = $value;
28161e35c35SAndreas Gohr    }
28210449332Schris}
283