xref: /dokuwiki/lib/plugins/config/admin.php (revision e7296041855e1739088397d1445fcfffbc47e90b)
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
32253d4b48SGerrit Uitslag    /**
33077c27b2SAndreas Gohr     * admin_plugin_config constructor.
34253d4b48SGerrit Uitslag     */
35077c27b2SAndreas Gohr    public function __construct() {
36077c27b2SAndreas Gohr        $this->configuration = new Configuration();
37077c27b2SAndreas Gohr    }
3810449332Schris
3910449332Schris    /**
4010449332Schris     * handle user request
4110449332Schris     */
4261e35c35SAndreas Gohr    public function handle() {
43392c9b52SHakan Sandell        global $ID, $INPUT;
4410449332Schris
45077c27b2SAndreas Gohr        if(!$INPUT->bool('save') || !checkSecurityToken()) {
46253d4b48SGerrit Uitslag            return;
47253d4b48SGerrit Uitslag        }
4810449332Schris
4910449332Schris        // don't go any further if the configuration is locked
50077c27b2SAndreas Gohr        if($this->configuration->isLocked()) return;
51077c27b2SAndreas Gohr
52077c27b2SAndreas Gohr        // update settings and redirect of successful
53077c27b2SAndreas Gohr        $ok = $this->configuration->updateSettings($INPUT->arr('config'));
54077c27b2SAndreas Gohr        if($ok) { // no errors
55077c27b2SAndreas Gohr            try {
56077c27b2SAndreas Gohr                if($this->configuration->hasChanged()) {
57077c27b2SAndreas Gohr                    $this->configuration->save();
58077c27b2SAndreas Gohr                } else {
59077c27b2SAndreas Gohr                    $this->configuration->touch();
60253d4b48SGerrit Uitslag                }
61e98b5e44SAndreas Gohr                msg($this->getLang('updated'), 1);
62077c27b2SAndreas Gohr            } catch(Exception $e) {
63077c27b2SAndreas Gohr                msg($this->getLang('error'), -1);
6410449332Schris            }
653295f40aSAndreas Gohr            send_redirect(wl($ID, array('do' => 'admin', 'page' => 'config'), true, '&'));
66a017dff4SAndreas Gohr        } else {
67a017dff4SAndreas Gohr            $this->hasErrors = true;
6810449332Schris        }
6910449332Schris    }
7010449332Schris
7110449332Schris    /**
7210449332Schris     * output appropriate html
7310449332Schris     */
7461e35c35SAndreas Gohr    public function html() {
75685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
7610449332Schris        global $lang;
77400497e1Schris        global $ID;
7810449332Schris
7989d74a2fSchris        $this->setupLocale(true);
8010449332Schris
81be1cc9aeSAndreas Gohr        echo $this->locale_xhtml('intro');
8210449332Schris
83be1cc9aeSAndreas Gohr        echo '<div id="config__manager">';
8410449332Schris
85077c27b2SAndreas Gohr        if($this->configuration->isLocked()) {
86be1cc9aeSAndreas Gohr            echo '<div class="info">' . $this->getLang('locked') . '</div>';
87077c27b2SAndreas Gohr        }
8810449332Schris
895d85efc6SAdrian Lang        // POST to script() instead of wl($ID) so config manager still works if
905d85efc6SAdrian Lang        // rewrite config is broken. Add $ID as hidden field to remember
915d85efc6SAdrian Lang        // current ID in most cases.
92be1cc9aeSAndreas Gohr        echo '<form action="' . script() . '" method="post">';
93be1cc9aeSAndreas Gohr        echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>';
94634d7150SAndreas Gohr        formSecurityToken();
95a1ef8b4dSAndreas Gohr        $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki'));
9610449332Schris
974fa2dffcSBen Coburn        $in_fieldset = false;
984fa2dffcSBen Coburn        $first_plugin_fieldset = true;
994fa2dffcSBen Coburn        $first_template_fieldset = true;
100077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
101077c27b2SAndreas Gohr            if(is_a($setting, SettingHidden::class)) {
102685bdd2eSBen Coburn                continue;
103077c27b2SAndreas Gohr            } else if(is_a($setting, settingFieldset::class)) {
1044fa2dffcSBen Coburn                // config setting group
1054fa2dffcSBen Coburn                if($in_fieldset) {
106be1cc9aeSAndreas Gohr                    echo '</table>';
107be1cc9aeSAndreas Gohr                    echo '</div>';
108be1cc9aeSAndreas Gohr                    echo '</fieldset>';
1094fa2dffcSBen Coburn                } else {
1104fa2dffcSBen Coburn                    $in_fieldset = true;
1114fa2dffcSBen Coburn                }
112a017dff4SAndreas Gohr                if($first_plugin_fieldset && $setting->getType() == 'plugin') {
113a1ef8b4dSAndreas Gohr                    $this->printH1('plugin_settings', $this->getLang('_header_plugin'));
1144fa2dffcSBen Coburn                    $first_plugin_fieldset = false;
115a017dff4SAndreas Gohr                } else if($first_template_fieldset && $setting->getType() == 'template') {
116a1ef8b4dSAndreas Gohr                    $this->printH1('template_settings', $this->getLang('_header_template'));
1174fa2dffcSBen Coburn                    $first_template_fieldset = false;
1184fa2dffcSBen Coburn                }
119be1cc9aeSAndreas Gohr                echo '<fieldset id="' . $setting->getKey() . '">';
120be1cc9aeSAndreas Gohr                echo '<legend>' . $setting->prompt($this) . '</legend>';
121be1cc9aeSAndreas Gohr                echo '<div class="table">';
122be1cc9aeSAndreas Gohr                echo '<table class="inline">';
1234fa2dffcSBen Coburn            } else {
1244fa2dffcSBen Coburn                // config settings
125a017dff4SAndreas Gohr                list($label, $input) = $setting->html($this, $this->hasErrors);
12610449332Schris
1275c17d2d3SAndreas Gohr                $class = $setting->isDefault()
12864159a61SAndreas Gohr                    ? ' class="default"'
1295c17d2d3SAndreas Gohr                    : ($setting->isProtected() ? ' class="protected"' : '');
1305c17d2d3SAndreas Gohr                $error = $setting->hasError()
13164159a61SAndreas Gohr                    ? ' class="value error"'
13264159a61SAndreas Gohr                    : ' class="value"';
13364159a61SAndreas Gohr                $icon = $setting->caution()
134c6639e6aSAndreas Gohr                    ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' .
13564159a61SAndreas Gohr                    'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />'
13664159a61SAndreas Gohr                    : '';
13710449332Schris
138be1cc9aeSAndreas Gohr                echo '<tr' . $class . '>';
139be1cc9aeSAndreas Gohr                echo '<td class="label">';
140be1cc9aeSAndreas Gohr                echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>';
141be1cc9aeSAndreas Gohr                echo $icon . $label;
142be1cc9aeSAndreas Gohr                echo '</td>';
143be1cc9aeSAndreas Gohr                echo '<td' . $error . '>' . $input . '</td>';
144be1cc9aeSAndreas Gohr                echo '</tr>';
14510449332Schris            }
1464fa2dffcSBen Coburn        }
14710449332Schris
148be1cc9aeSAndreas Gohr        echo '</table>';
149be1cc9aeSAndreas Gohr        echo '</div>';
1504fa2dffcSBen Coburn        if($in_fieldset) {
151be1cc9aeSAndreas Gohr            echo '</fieldset>';
1524fa2dffcSBen Coburn        }
15310449332Schris
154685bdd2eSBen Coburn        // show undefined settings list
155077c27b2SAndreas Gohr        $undefined_settings = $this->configuration->getUndefined();
156685bdd2eSBen Coburn        if($allow_debug && !empty($undefined_settings)) {
157253d4b48SGerrit Uitslag            /**
158253d4b48SGerrit Uitslag             * Callback for sorting settings
159253d4b48SGerrit Uitslag             *
160077c27b2SAndreas Gohr             * @param Setting $a
161077c27b2SAndreas Gohr             * @param Setting $b
162253d4b48SGerrit Uitslag             * @return int if $a is lower/equal/higher than $b
163253d4b48SGerrit Uitslag             */
164a1ef8b4dSAndreas Gohr            function settingNaturalComparison($a, $b) {
165077c27b2SAndreas Gohr                return strnatcmp($a->getKey(), $b->getKey());
166253d4b48SGerrit Uitslag            }
167253d4b48SGerrit Uitslag
168a1ef8b4dSAndreas Gohr            usort($undefined_settings, 'settingNaturalComparison');
169a1ef8b4dSAndreas Gohr            $this->printH1('undefined_settings', $this->getLang('_header_undefined'));
170be1cc9aeSAndreas Gohr            echo '<fieldset>';
171be1cc9aeSAndreas Gohr            echo '<div class="table">';
172be1cc9aeSAndreas Gohr            echo '<table class="inline">';
173685bdd2eSBen Coburn            foreach($undefined_settings as $setting) {
174*e7296041SAndreas Gohr                list($label, $input) = $setting->html($this);
175be1cc9aeSAndreas Gohr                echo '<tr>';
176*e7296041SAndreas Gohr                echo '<td class="label">' . $label . '</td>';
177*e7296041SAndreas Gohr                echo '<td>' . $input . '</td>';
178be1cc9aeSAndreas Gohr                echo '</tr>';
179685bdd2eSBen Coburn            }
180be1cc9aeSAndreas Gohr            echo '</table>';
181be1cc9aeSAndreas Gohr            echo '</div>';
182be1cc9aeSAndreas Gohr            echo '</fieldset>';
183685bdd2eSBen Coburn        }
184685bdd2eSBen Coburn
185685bdd2eSBen Coburn        // finish up form
186be1cc9aeSAndreas Gohr        echo '<p>';
187be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="do"     value="admin" />';
188be1cc9aeSAndreas Gohr        echo '<input type="hidden" name="page"   value="config" />';
18910449332Schris
190077c27b2SAndreas Gohr        if(!$this->configuration->isLocked()) {
191be1cc9aeSAndreas Gohr            echo '<input type="hidden" name="save"   value="1" />';
192be1cc9aeSAndreas Gohr            echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>';
193be1cc9aeSAndreas Gohr            echo '<button type="reset">' . $lang['btn_reset'] . '</button>';
19410449332Schris        }
19510449332Schris
196be1cc9aeSAndreas Gohr        echo '</p>';
19710449332Schris
198be1cc9aeSAndreas Gohr        echo '</form>';
199be1cc9aeSAndreas Gohr        echo '</div>';
20010449332Schris    }
20110449332Schris
20210449332Schris    /**
203253d4b48SGerrit Uitslag     * @param bool $prompts
204253d4b48SGerrit Uitslag     */
20561e35c35SAndreas Gohr    public function setupLocale($prompts = false) {
20689d74a2fSchris        parent::setupLocale();
207a017dff4SAndreas Gohr        if(!$prompts || $this->promptsLocalized) return;
208d6fc72e1SAndreas Gohr        $this->lang = array_merge($this->lang, $this->configuration->getLangs());
209a017dff4SAndreas Gohr        $this->promptsLocalized = true;
21089d74a2fSchris    }
21189d74a2fSchris
2124fa2dffcSBen Coburn    /**
2134fa2dffcSBen Coburn     * Generates a two-level table of contents for the config plugin.
2144fa2dffcSBen Coburn     *
2154fa2dffcSBen Coburn     * @author Ben Coburn <btcoburn@silicodon.net>
216253d4b48SGerrit Uitslag     *
217253d4b48SGerrit Uitslag     * @return array
2184fa2dffcSBen Coburn     */
21961e35c35SAndreas Gohr    public function getTOC() {
220b8595a66SAndreas Gohr        $this->setupLocale(true);
221b8595a66SAndreas Gohr
222685bdd2eSBen Coburn        $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here.
223d6fc72e1SAndreas Gohr        $toc = array();
224d6fc72e1SAndreas Gohr        $check = false;
225685bdd2eSBen Coburn
226a017dff4SAndreas Gohr        // gather settings data into three sub arrays
227d6fc72e1SAndreas Gohr        $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []];
228077c27b2SAndreas Gohr        foreach($this->configuration->getSettings() as $setting) {
229d6fc72e1SAndreas Gohr            if(is_a($setting, SettingFieldset::class)) {
230d6fc72e1SAndreas Gohr                $labels[$setting->getType()][] = $setting;
2314fa2dffcSBen Coburn            }
2324fa2dffcSBen Coburn        }
2334fa2dffcSBen Coburn
234d6fc72e1SAndreas Gohr        // top header
235cbfba956SMichael Hamann        $title = $this->getLang('_configuration_manager');
236d6fc72e1SAndreas Gohr        $toc[] = html_mktocitem(sectionID($title, $check), $title, 1);
237d6fc72e1SAndreas Gohr
238d6fc72e1SAndreas Gohr        // main entries
239d6fc72e1SAndreas Gohr        foreach(['dokuwiki', 'plugin', 'template'] as $section) {
240d6fc72e1SAndreas Gohr            if(empty($labels[$section])) continue; // no entries, skip
241d6fc72e1SAndreas Gohr
242d6fc72e1SAndreas Gohr            // create main header
243d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem(
244d6fc72e1SAndreas Gohr                $section . '_settings',
245d6fc72e1SAndreas Gohr                $this->getLang('_header_' . $section),
246d6fc72e1SAndreas Gohr                1
247d6fc72e1SAndreas Gohr            );
248d6fc72e1SAndreas Gohr
249d6fc72e1SAndreas Gohr            // create sub headers
250d6fc72e1SAndreas Gohr            foreach($labels[$section] as $setting) {
251d6fc72e1SAndreas Gohr                /** @var SettingFieldset $setting */
252e1b31a95SBen Coburn                $name = $setting->prompt($this);
253d6fc72e1SAndreas Gohr                $toc[] = html_mktocitem($setting->getKey(), $name, 2);
254e1b31a95SBen Coburn            }
255e1b31a95SBen Coburn        }
256e1b31a95SBen Coburn
257d6fc72e1SAndreas Gohr        // undefined settings if allowed
258d6fc72e1SAndreas Gohr        if(count($this->configuration->getUndefined()) && $allow_debug) {
259d6fc72e1SAndreas Gohr            $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1);
260d6fc72e1SAndreas Gohr        }
261d6fc72e1SAndreas Gohr
262d6fc72e1SAndreas Gohr        return $toc;
2634fa2dffcSBen Coburn    }
2644fa2dffcSBen Coburn
265253d4b48SGerrit Uitslag    /**
266253d4b48SGerrit Uitslag     * @param string $id
267253d4b48SGerrit Uitslag     * @param string $text
268253d4b48SGerrit Uitslag     */
269a1ef8b4dSAndreas Gohr    protected function printH1($id, $text) {
270be1cc9aeSAndreas Gohr        echo '<h1 id="' . $id . '">' . $text . '</h1>';
2714fa2dffcSBen Coburn    }
2724fa2dffcSBen Coburn
27361e35c35SAndreas Gohr    /**
27461e35c35SAndreas Gohr     * Adds a translation to this plugin's language array
27561e35c35SAndreas Gohr     *
276a017dff4SAndreas Gohr     * Used by some settings to set up dynamic translations
277a017dff4SAndreas Gohr     *
27861e35c35SAndreas Gohr     * @param string $key
27961e35c35SAndreas Gohr     * @param string $value
28061e35c35SAndreas Gohr     */
28161e35c35SAndreas Gohr    public function addLang($key, $value) {
28261e35c35SAndreas Gohr        if(!$this->localised) $this->setupLocale();
28361e35c35SAndreas Gohr        $this->lang[$key] = $value;
28461e35c35SAndreas Gohr    }
28510449332Schris}
286