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 39f8dcd5b0SAndreas Gohr // always initialize the configuration 40f8dcd5b0SAndreas Gohr $this->configuration = new Configuration(); 41f8dcd5b0SAndreas 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 } 62*467c1427SAndreas Gohr send_redirect(wl($ID, ['do' => 'admin', 'page' => 'config'], true, '&')); 63a017dff4SAndreas Gohr } else { 64a017dff4SAndreas Gohr $this->hasErrors = true; 65a6b97c7dSGerrit Uitslag msg($this->getLang('error'), -1); 6610449332Schris } 6710449332Schris } 6810449332Schris 6910449332Schris /** 7010449332Schris * output appropriate html 7110449332Schris */ 7261e35c35SAndreas Gohr public function html() { 73685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 7410449332Schris global $lang; 75400497e1Schris global $ID; 7610449332Schris 7789d74a2fSchris $this->setupLocale(true); 7810449332Schris 79be1cc9aeSAndreas Gohr echo $this->locale_xhtml('intro'); 8010449332Schris 81be1cc9aeSAndreas Gohr echo '<div id="config__manager">'; 8210449332Schris 83077c27b2SAndreas Gohr if($this->configuration->isLocked()) { 84be1cc9aeSAndreas Gohr echo '<div class="info">' . $this->getLang('locked') . '</div>'; 85077c27b2SAndreas Gohr } 8610449332Schris 875d85efc6SAdrian Lang // POST to script() instead of wl($ID) so config manager still works if 885d85efc6SAdrian Lang // rewrite config is broken. Add $ID as hidden field to remember 895d85efc6SAdrian Lang // current ID in most cases. 90923e149aSMichael Große echo '<form id="dw__configform" action="' . script() . '" method="post">'; 91be1cc9aeSAndreas Gohr echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>'; 92634d7150SAndreas Gohr formSecurityToken(); 93a1ef8b4dSAndreas Gohr $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 9410449332Schris 954fa2dffcSBen Coburn $in_fieldset = false; 964fa2dffcSBen Coburn $first_plugin_fieldset = true; 974fa2dffcSBen Coburn $first_template_fieldset = true; 98077c27b2SAndreas Gohr foreach($this->configuration->getSettings() as $setting) { 99*467c1427SAndreas Gohr if ($setting instanceof SettingHidden) { 100685bdd2eSBen Coburn continue; 101*467c1427SAndreas Gohr } elseif ($setting instanceof SettingFieldset) { 1024fa2dffcSBen Coburn // config setting group 1034fa2dffcSBen Coburn if($in_fieldset) { 104be1cc9aeSAndreas Gohr echo '</table>'; 105be1cc9aeSAndreas Gohr echo '</div>'; 106be1cc9aeSAndreas Gohr echo '</fieldset>'; 1074fa2dffcSBen Coburn } else { 1084fa2dffcSBen Coburn $in_fieldset = true; 1094fa2dffcSBen Coburn } 110a017dff4SAndreas Gohr if ($first_plugin_fieldset && $setting->getType() == 'plugin') { 111a1ef8b4dSAndreas Gohr $this->printH1('plugin_settings', $this->getLang('_header_plugin')); 1124fa2dffcSBen Coburn $first_plugin_fieldset = false; 113a017dff4SAndreas Gohr } elseif ($first_template_fieldset && $setting->getType() == 'template') { 114a1ef8b4dSAndreas Gohr $this->printH1('template_settings', $this->getLang('_header_template')); 1154fa2dffcSBen Coburn $first_template_fieldset = false; 1164fa2dffcSBen Coburn } 117be1cc9aeSAndreas Gohr echo '<fieldset id="' . $setting->getKey() . '">'; 118be1cc9aeSAndreas Gohr echo '<legend>' . $setting->prompt($this) . '</legend>'; 119be1cc9aeSAndreas Gohr echo '<div class="table">'; 120be1cc9aeSAndreas Gohr echo '<table class="inline">'; 1214fa2dffcSBen Coburn } else { 1224fa2dffcSBen Coburn // config settings 123*467c1427SAndreas Gohr [$label, $input] = $setting->html($this, $this->hasErrors); 12410449332Schris 1255c17d2d3SAndreas Gohr $class = $setting->isDefault() 12664159a61SAndreas Gohr ? ' class="default"' 1275c17d2d3SAndreas Gohr : ($setting->isProtected() ? ' class="protected"' : ''); 1285c17d2d3SAndreas Gohr $error = $setting->hasError() 12964159a61SAndreas Gohr ? ' class="value error"' 13064159a61SAndreas Gohr : ' class="value"'; 13164159a61SAndreas Gohr $icon = $setting->caution() 132c6639e6aSAndreas Gohr ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' . 13364159a61SAndreas Gohr 'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />' 13464159a61SAndreas Gohr : ''; 13510449332Schris 136be1cc9aeSAndreas Gohr echo '<tr' . $class . '>'; 137be1cc9aeSAndreas Gohr echo '<td class="label">'; 138be1cc9aeSAndreas Gohr echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>'; 139be1cc9aeSAndreas Gohr echo $icon . $label; 140be1cc9aeSAndreas Gohr echo '</td>'; 141be1cc9aeSAndreas Gohr echo '<td' . $error . '>' . $input . '</td>'; 142be1cc9aeSAndreas Gohr echo '</tr>'; 14310449332Schris } 1444fa2dffcSBen Coburn } 14510449332Schris 146be1cc9aeSAndreas Gohr echo '</table>'; 147be1cc9aeSAndreas Gohr echo '</div>'; 1484fa2dffcSBen Coburn if($in_fieldset) { 149be1cc9aeSAndreas Gohr echo '</fieldset>'; 1504fa2dffcSBen Coburn } 15110449332Schris 152685bdd2eSBen Coburn // show undefined settings list 153077c27b2SAndreas Gohr $undefined_settings = $this->configuration->getUndefined(); 154685bdd2eSBen Coburn if($allow_debug && !empty($undefined_settings)) { 155253d4b48SGerrit Uitslag /** 156253d4b48SGerrit Uitslag * Callback for sorting settings 157253d4b48SGerrit Uitslag * 158077c27b2SAndreas Gohr * @param Setting $a 159077c27b2SAndreas Gohr * @param Setting $b 160253d4b48SGerrit Uitslag * @return int if $a is lower/equal/higher than $b 161253d4b48SGerrit Uitslag */ 162a1ef8b4dSAndreas Gohr function settingNaturalComparison($a, $b) { 163077c27b2SAndreas Gohr return strnatcmp($a->getKey(), $b->getKey()); 164253d4b48SGerrit Uitslag } 165253d4b48SGerrit Uitslag 166a1ef8b4dSAndreas Gohr usort($undefined_settings, 'settingNaturalComparison'); 167a1ef8b4dSAndreas Gohr $this->printH1('undefined_settings', $this->getLang('_header_undefined')); 168be1cc9aeSAndreas Gohr echo '<fieldset>'; 169be1cc9aeSAndreas Gohr echo '<div class="table">'; 170be1cc9aeSAndreas Gohr echo '<table class="inline">'; 171685bdd2eSBen Coburn foreach($undefined_settings as $setting) { 172*467c1427SAndreas Gohr [$label, $input] = $setting->html($this); 173be1cc9aeSAndreas Gohr echo '<tr>'; 174e7296041SAndreas Gohr echo '<td class="label">' . $label . '</td>'; 175e7296041SAndreas Gohr echo '<td>' . $input . '</td>'; 176be1cc9aeSAndreas Gohr echo '</tr>'; 177685bdd2eSBen Coburn } 178be1cc9aeSAndreas Gohr echo '</table>'; 179be1cc9aeSAndreas Gohr echo '</div>'; 180be1cc9aeSAndreas Gohr echo '</fieldset>'; 181685bdd2eSBen Coburn } 182685bdd2eSBen Coburn 183685bdd2eSBen Coburn // finish up form 184be1cc9aeSAndreas Gohr echo '<p>'; 185be1cc9aeSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 186be1cc9aeSAndreas Gohr echo '<input type="hidden" name="page" value="config" />'; 18710449332Schris 188077c27b2SAndreas Gohr if(!$this->configuration->isLocked()) { 189be1cc9aeSAndreas Gohr echo '<input type="hidden" name="save" value="1" />'; 190be1cc9aeSAndreas Gohr echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>'; 191be1cc9aeSAndreas Gohr echo '<button type="reset">' . $lang['btn_reset'] . '</button>'; 19210449332Schris } 19310449332Schris 194be1cc9aeSAndreas Gohr echo '</p>'; 19510449332Schris 196be1cc9aeSAndreas Gohr echo '</form>'; 197be1cc9aeSAndreas Gohr echo '</div>'; 19810449332Schris } 19910449332Schris 20010449332Schris /** 201253d4b48SGerrit Uitslag * @param bool $prompts 202253d4b48SGerrit Uitslag */ 20361e35c35SAndreas Gohr public function setupLocale($prompts = false) { 20489d74a2fSchris parent::setupLocale(); 205a017dff4SAndreas Gohr if(!$prompts || $this->promptsLocalized) return; 206d6fc72e1SAndreas Gohr $this->lang = array_merge($this->lang, $this->configuration->getLangs()); 207a017dff4SAndreas Gohr $this->promptsLocalized = true; 20889d74a2fSchris } 20989d74a2fSchris 2104fa2dffcSBen Coburn /** 2114fa2dffcSBen Coburn * Generates a two-level table of contents for the config plugin. 2124fa2dffcSBen Coburn * 2134fa2dffcSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 214253d4b48SGerrit Uitslag * 215253d4b48SGerrit Uitslag * @return array 2164fa2dffcSBen Coburn */ 21761e35c35SAndreas Gohr public function getTOC() { 218b8595a66SAndreas Gohr $this->setupLocale(true); 219b8595a66SAndreas Gohr 220685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 221*467c1427SAndreas Gohr $toc = []; 222d6fc72e1SAndreas Gohr $check = false; 223685bdd2eSBen Coburn 224a017dff4SAndreas Gohr // gather settings data into three sub arrays 225d6fc72e1SAndreas Gohr $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []]; 226077c27b2SAndreas Gohr foreach($this->configuration->getSettings() as $setting) { 227*467c1427SAndreas Gohr if($setting instanceof SettingFieldset) { 228d6fc72e1SAndreas Gohr $labels[$setting->getType()][] = $setting; 2294fa2dffcSBen Coburn } 2304fa2dffcSBen Coburn } 2314fa2dffcSBen Coburn 232d6fc72e1SAndreas Gohr // top header 233cbfba956SMichael Hamann $title = $this->getLang('_configuration_manager'); 234d6fc72e1SAndreas Gohr $toc[] = html_mktocitem(sectionID($title, $check), $title, 1); 235d6fc72e1SAndreas Gohr 236d6fc72e1SAndreas Gohr // main entries 237d6fc72e1SAndreas Gohr foreach(['dokuwiki', 'plugin', 'template'] as $section) { 238d6fc72e1SAndreas Gohr if(empty($labels[$section])) continue; // no entries, skip 239d6fc72e1SAndreas Gohr 240d6fc72e1SAndreas Gohr // create main header 241d6fc72e1SAndreas Gohr $toc[] = html_mktocitem( 242d6fc72e1SAndreas Gohr $section . '_settings', 243d6fc72e1SAndreas Gohr $this->getLang('_header_' . $section), 244d6fc72e1SAndreas Gohr 1 245d6fc72e1SAndreas Gohr ); 246d6fc72e1SAndreas Gohr 247d6fc72e1SAndreas Gohr // create sub headers 248d6fc72e1SAndreas Gohr foreach($labels[$section] as $setting) { 249d6fc72e1SAndreas Gohr /** @var SettingFieldset $setting */ 250e1b31a95SBen Coburn $name = $setting->prompt($this); 251d6fc72e1SAndreas Gohr $toc[] = html_mktocitem($setting->getKey(), $name, 2); 252e1b31a95SBen Coburn } 253e1b31a95SBen Coburn } 254e1b31a95SBen Coburn 255d6fc72e1SAndreas Gohr // undefined settings if allowed 256d6fc72e1SAndreas Gohr if(count($this->configuration->getUndefined()) && $allow_debug) { 257d6fc72e1SAndreas Gohr $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 258d6fc72e1SAndreas Gohr } 259d6fc72e1SAndreas Gohr 260d6fc72e1SAndreas Gohr return $toc; 2614fa2dffcSBen Coburn } 2624fa2dffcSBen Coburn 263253d4b48SGerrit Uitslag /** 264253d4b48SGerrit Uitslag * @param string $id 265253d4b48SGerrit Uitslag * @param string $text 266253d4b48SGerrit Uitslag */ 267a1ef8b4dSAndreas Gohr protected function printH1($id, $text) { 268be1cc9aeSAndreas Gohr echo '<h1 id="' . $id . '">' . $text . '</h1>'; 2694fa2dffcSBen Coburn } 2704fa2dffcSBen Coburn 27161e35c35SAndreas Gohr /** 27261e35c35SAndreas Gohr * Adds a translation to this plugin's language array 27361e35c35SAndreas Gohr * 274a017dff4SAndreas Gohr * Used by some settings to set up dynamic translations 275a017dff4SAndreas Gohr * 27661e35c35SAndreas Gohr * @param string $key 27761e35c35SAndreas Gohr * @param string $value 27861e35c35SAndreas Gohr */ 27961e35c35SAndreas Gohr public function addLang($key, $value) { 28061e35c35SAndreas Gohr if(!$this->localised) $this->setupLocale(); 28161e35c35SAndreas Gohr $this->lang[$key] = $value; 28261e35c35SAndreas Gohr } 28310449332Schris} 284