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 */ 98553d24dSAndreas Gohruse dokuwiki\Extension\AdminPlugin; 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 */ 19*8c7c53b0SAndreas Gohrclass admin_plugin_config extends AdminPlugin 20*8c7c53b0SAndreas Gohr{ 2110449332Schris 22bf9be0e3SAndreas Gohr protected const IMGDIR = DOKU_BASE . 'lib/plugins/config/images/'; 23c6639e6aSAndreas Gohr 24077c27b2SAndreas Gohr /** @var Configuration */ 25077c27b2SAndreas Gohr protected $configuration; 26077c27b2SAndreas Gohr 27a017dff4SAndreas Gohr /** @var bool were there any errors in the submitted data? */ 28a017dff4SAndreas Gohr protected $hasErrors = false; 29a017dff4SAndreas Gohr 30a017dff4SAndreas Gohr /** @var bool have the settings translations been loaded? */ 31a017dff4SAndreas Gohr protected $promptsLocalized = false; 32a017dff4SAndreas Gohr 3310449332Schris 3410449332Schris /** 3510449332Schris * handle user request 3610449332Schris */ 3761e35c35SAndreas Gohr public function handle() { 38392c9b52SHakan Sandell global $ID, $INPUT; 3910449332Schris 40f8dcd5b0SAndreas Gohr // always initialize the configuration 41f8dcd5b0SAndreas Gohr $this->configuration = new Configuration(); 42f8dcd5b0SAndreas Gohr 43077c27b2SAndreas Gohr if(!$INPUT->bool('save') || !checkSecurityToken()) { 44253d4b48SGerrit Uitslag return; 45253d4b48SGerrit Uitslag } 4610449332Schris 4710449332Schris // don't go any further if the configuration is locked 48077c27b2SAndreas Gohr if($this->configuration->isLocked()) return; 49077c27b2SAndreas Gohr 50077c27b2SAndreas Gohr // update settings and redirect of successful 51077c27b2SAndreas Gohr $ok = $this->configuration->updateSettings($INPUT->arr('config')); 52077c27b2SAndreas Gohr if($ok) { // no errors 53077c27b2SAndreas Gohr try { 54077c27b2SAndreas Gohr if($this->configuration->hasChanged()) { 55077c27b2SAndreas Gohr $this->configuration->save(); 56077c27b2SAndreas Gohr } else { 57077c27b2SAndreas Gohr $this->configuration->touch(); 58253d4b48SGerrit Uitslag } 59e98b5e44SAndreas Gohr msg($this->getLang('updated'), 1); 60077c27b2SAndreas Gohr } catch(Exception $e) { 61077c27b2SAndreas Gohr msg($this->getLang('error'), -1); 6210449332Schris } 63467c1427SAndreas Gohr send_redirect(wl($ID, ['do' => 'admin', 'page' => 'config'], true, '&')); 64a017dff4SAndreas Gohr } else { 65a017dff4SAndreas Gohr $this->hasErrors = true; 66a6b97c7dSGerrit Uitslag msg($this->getLang('error'), -1); 6710449332Schris } 6810449332Schris } 6910449332Schris 7010449332Schris /** 7110449332Schris * output appropriate html 7210449332Schris */ 7361e35c35SAndreas Gohr public function html() { 74685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 7510449332Schris global $lang; 76400497e1Schris global $ID; 7710449332Schris 7889d74a2fSchris $this->setupLocale(true); 7910449332Schris 80be1cc9aeSAndreas Gohr echo $this->locale_xhtml('intro'); 8110449332Schris 82be1cc9aeSAndreas Gohr echo '<div id="config__manager">'; 8310449332Schris 84077c27b2SAndreas Gohr if($this->configuration->isLocked()) { 85be1cc9aeSAndreas Gohr echo '<div class="info">' . $this->getLang('locked') . '</div>'; 86077c27b2SAndreas Gohr } 8710449332Schris 885d85efc6SAdrian Lang // POST to script() instead of wl($ID) so config manager still works if 895d85efc6SAdrian Lang // rewrite config is broken. Add $ID as hidden field to remember 905d85efc6SAdrian Lang // current ID in most cases. 91923e149aSMichael Große echo '<form id="dw__configform" action="' . script() . '" method="post">'; 92be1cc9aeSAndreas Gohr echo '<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>'; 93634d7150SAndreas Gohr formSecurityToken(); 94a1ef8b4dSAndreas Gohr $this->printH1('dokuwiki_settings', $this->getLang('_header_dokuwiki')); 9510449332Schris 964fa2dffcSBen Coburn $in_fieldset = false; 974fa2dffcSBen Coburn $first_plugin_fieldset = true; 984fa2dffcSBen Coburn $first_template_fieldset = true; 99077c27b2SAndreas Gohr foreach($this->configuration->getSettings() as $setting) { 100467c1427SAndreas Gohr if ($setting instanceof SettingHidden) { 101685bdd2eSBen Coburn continue; 102467c1427SAndreas Gohr } elseif ($setting instanceof SettingFieldset) { 1034fa2dffcSBen Coburn // config setting group 1044fa2dffcSBen Coburn if($in_fieldset) { 105be1cc9aeSAndreas Gohr echo '</table>'; 106be1cc9aeSAndreas Gohr echo '</div>'; 107be1cc9aeSAndreas Gohr echo '</fieldset>'; 1084fa2dffcSBen Coburn } else { 1094fa2dffcSBen Coburn $in_fieldset = true; 1104fa2dffcSBen Coburn } 111a017dff4SAndreas Gohr if ($first_plugin_fieldset && $setting->getType() == 'plugin') { 112a1ef8b4dSAndreas Gohr $this->printH1('plugin_settings', $this->getLang('_header_plugin')); 1134fa2dffcSBen Coburn $first_plugin_fieldset = false; 114a017dff4SAndreas Gohr } elseif ($first_template_fieldset && $setting->getType() == 'template') { 115a1ef8b4dSAndreas Gohr $this->printH1('template_settings', $this->getLang('_header_template')); 1164fa2dffcSBen Coburn $first_template_fieldset = false; 1174fa2dffcSBen Coburn } 118be1cc9aeSAndreas Gohr echo '<fieldset id="' . $setting->getKey() . '">'; 119be1cc9aeSAndreas Gohr echo '<legend>' . $setting->prompt($this) . '</legend>'; 120be1cc9aeSAndreas Gohr echo '<div class="table">'; 121be1cc9aeSAndreas Gohr echo '<table class="inline">'; 1224fa2dffcSBen Coburn } else { 1234fa2dffcSBen Coburn // config settings 124467c1427SAndreas Gohr [$label, $input] = $setting->html($this, $this->hasErrors); 12510449332Schris 1265c17d2d3SAndreas Gohr $class = $setting->isDefault() 12764159a61SAndreas Gohr ? ' class="default"' 1285c17d2d3SAndreas Gohr : ($setting->isProtected() ? ' class="protected"' : ''); 1295c17d2d3SAndreas Gohr $error = $setting->hasError() 13064159a61SAndreas Gohr ? ' class="value error"' 13164159a61SAndreas Gohr : ' class="value"'; 13264159a61SAndreas Gohr $icon = $setting->caution() 133c6639e6aSAndreas Gohr ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' . 13464159a61SAndreas Gohr 'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />' 13564159a61SAndreas Gohr : ''; 13610449332Schris 137be1cc9aeSAndreas Gohr echo '<tr' . $class . '>'; 138be1cc9aeSAndreas Gohr echo '<td class="label">'; 139be1cc9aeSAndreas Gohr echo '<span class="outkey">' . $setting->getPrettyKey() . '</span>'; 140be1cc9aeSAndreas Gohr echo $icon . $label; 141be1cc9aeSAndreas Gohr echo '</td>'; 142be1cc9aeSAndreas Gohr echo '<td' . $error . '>' . $input . '</td>'; 143be1cc9aeSAndreas Gohr echo '</tr>'; 14410449332Schris } 1454fa2dffcSBen Coburn } 14610449332Schris 147be1cc9aeSAndreas Gohr echo '</table>'; 148be1cc9aeSAndreas Gohr echo '</div>'; 1494fa2dffcSBen Coburn if($in_fieldset) { 150be1cc9aeSAndreas Gohr echo '</fieldset>'; 1514fa2dffcSBen Coburn } 15210449332Schris 153685bdd2eSBen Coburn // show undefined settings list 154077c27b2SAndreas Gohr $undefined_settings = $this->configuration->getUndefined(); 155685bdd2eSBen Coburn if($allow_debug && !empty($undefined_settings)) { 156253d4b48SGerrit Uitslag /** 157253d4b48SGerrit Uitslag * Callback for sorting settings 158253d4b48SGerrit Uitslag * 159077c27b2SAndreas Gohr * @param Setting $a 160077c27b2SAndreas Gohr * @param Setting $b 161253d4b48SGerrit Uitslag * @return int if $a is lower/equal/higher than $b 162253d4b48SGerrit Uitslag */ 163a1ef8b4dSAndreas Gohr function settingNaturalComparison($a, $b) { 164077c27b2SAndreas Gohr return strnatcmp($a->getKey(), $b->getKey()); 165253d4b48SGerrit Uitslag } 166253d4b48SGerrit Uitslag 167a1ef8b4dSAndreas Gohr usort($undefined_settings, 'settingNaturalComparison'); 168a1ef8b4dSAndreas Gohr $this->printH1('undefined_settings', $this->getLang('_header_undefined')); 169be1cc9aeSAndreas Gohr echo '<fieldset>'; 170be1cc9aeSAndreas Gohr echo '<div class="table">'; 171be1cc9aeSAndreas Gohr echo '<table class="inline">'; 172685bdd2eSBen Coburn foreach($undefined_settings as $setting) { 173467c1427SAndreas Gohr [$label, $input] = $setting->html($this); 174be1cc9aeSAndreas Gohr echo '<tr>'; 175e7296041SAndreas Gohr echo '<td class="label">' . $label . '</td>'; 176e7296041SAndreas Gohr echo '<td>' . $input . '</td>'; 177be1cc9aeSAndreas Gohr echo '</tr>'; 178685bdd2eSBen Coburn } 179be1cc9aeSAndreas Gohr echo '</table>'; 180be1cc9aeSAndreas Gohr echo '</div>'; 181be1cc9aeSAndreas Gohr echo '</fieldset>'; 182685bdd2eSBen Coburn } 183685bdd2eSBen Coburn 184685bdd2eSBen Coburn // finish up form 185be1cc9aeSAndreas Gohr echo '<p>'; 186be1cc9aeSAndreas Gohr echo '<input type="hidden" name="do" value="admin" />'; 187be1cc9aeSAndreas Gohr echo '<input type="hidden" name="page" value="config" />'; 18810449332Schris 189077c27b2SAndreas Gohr if(!$this->configuration->isLocked()) { 190be1cc9aeSAndreas Gohr echo '<input type="hidden" name="save" value="1" />'; 191be1cc9aeSAndreas Gohr echo '<button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>'; 192be1cc9aeSAndreas Gohr echo '<button type="reset">' . $lang['btn_reset'] . '</button>'; 19310449332Schris } 19410449332Schris 195be1cc9aeSAndreas Gohr echo '</p>'; 19610449332Schris 197be1cc9aeSAndreas Gohr echo '</form>'; 198be1cc9aeSAndreas Gohr echo '</div>'; 19910449332Schris } 20010449332Schris 20110449332Schris /** 202253d4b48SGerrit Uitslag * @param bool $prompts 203253d4b48SGerrit Uitslag */ 20461e35c35SAndreas Gohr public function setupLocale($prompts = false) { 20589d74a2fSchris parent::setupLocale(); 206a017dff4SAndreas Gohr if(!$prompts || $this->promptsLocalized) return; 207d6fc72e1SAndreas Gohr $this->lang = array_merge($this->lang, $this->configuration->getLangs()); 208a017dff4SAndreas Gohr $this->promptsLocalized = true; 20989d74a2fSchris } 21089d74a2fSchris 2114fa2dffcSBen Coburn /** 2124fa2dffcSBen Coburn * Generates a two-level table of contents for the config plugin. 2134fa2dffcSBen Coburn * 2144fa2dffcSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 215253d4b48SGerrit Uitslag * 216253d4b48SGerrit Uitslag * @return array 2174fa2dffcSBen Coburn */ 21861e35c35SAndreas Gohr public function getTOC() { 219b8595a66SAndreas Gohr $this->setupLocale(true); 220b8595a66SAndreas Gohr 221685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 222467c1427SAndreas Gohr $toc = []; 223d6fc72e1SAndreas Gohr $check = false; 224685bdd2eSBen Coburn 225a017dff4SAndreas Gohr // gather settings data into three sub arrays 226d6fc72e1SAndreas Gohr $labels = ['dokuwiki' => [], 'plugin' => [], 'template' => []]; 227077c27b2SAndreas Gohr foreach($this->configuration->getSettings() as $setting) { 228467c1427SAndreas Gohr if($setting instanceof SettingFieldset) { 229d6fc72e1SAndreas Gohr $labels[$setting->getType()][] = $setting; 2304fa2dffcSBen Coburn } 2314fa2dffcSBen Coburn } 2324fa2dffcSBen Coburn 233d6fc72e1SAndreas Gohr // top header 234cbfba956SMichael Hamann $title = $this->getLang('_configuration_manager'); 235d6fc72e1SAndreas Gohr $toc[] = html_mktocitem(sectionID($title, $check), $title, 1); 236d6fc72e1SAndreas Gohr 237d6fc72e1SAndreas Gohr // main entries 238d6fc72e1SAndreas Gohr foreach(['dokuwiki', 'plugin', 'template'] as $section) { 239d6fc72e1SAndreas Gohr if(empty($labels[$section])) continue; // no entries, skip 240d6fc72e1SAndreas Gohr 241d6fc72e1SAndreas Gohr // create main header 242d6fc72e1SAndreas Gohr $toc[] = html_mktocitem( 243d6fc72e1SAndreas Gohr $section . '_settings', 244d6fc72e1SAndreas Gohr $this->getLang('_header_' . $section), 245d6fc72e1SAndreas Gohr 1 246d6fc72e1SAndreas Gohr ); 247d6fc72e1SAndreas Gohr 248d6fc72e1SAndreas Gohr // create sub headers 249d6fc72e1SAndreas Gohr foreach($labels[$section] as $setting) { 250d6fc72e1SAndreas Gohr /** @var SettingFieldset $setting */ 251e1b31a95SBen Coburn $name = $setting->prompt($this); 252d6fc72e1SAndreas Gohr $toc[] = html_mktocitem($setting->getKey(), $name, 2); 253e1b31a95SBen Coburn } 254e1b31a95SBen Coburn } 255e1b31a95SBen Coburn 256d6fc72e1SAndreas Gohr // undefined settings if allowed 257d6fc72e1SAndreas Gohr if(count($this->configuration->getUndefined()) && $allow_debug) { 258d6fc72e1SAndreas Gohr $toc[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 259d6fc72e1SAndreas Gohr } 260d6fc72e1SAndreas Gohr 261d6fc72e1SAndreas Gohr return $toc; 2624fa2dffcSBen Coburn } 2634fa2dffcSBen Coburn 264253d4b48SGerrit Uitslag /** 265253d4b48SGerrit Uitslag * @param string $id 266253d4b48SGerrit Uitslag * @param string $text 267253d4b48SGerrit Uitslag */ 268a1ef8b4dSAndreas Gohr protected function printH1($id, $text) { 269be1cc9aeSAndreas Gohr echo '<h1 id="' . $id . '">' . $text . '</h1>'; 2704fa2dffcSBen Coburn } 2714fa2dffcSBen Coburn 27261e35c35SAndreas Gohr /** 27361e35c35SAndreas Gohr * Adds a translation to this plugin's language array 27461e35c35SAndreas Gohr * 275a017dff4SAndreas Gohr * Used by some settings to set up dynamic translations 276a017dff4SAndreas Gohr * 27761e35c35SAndreas Gohr * @param string $key 27861e35c35SAndreas Gohr * @param string $value 27961e35c35SAndreas Gohr */ 28061e35c35SAndreas Gohr public function addLang($key, $value) { 28161e35c35SAndreas Gohr if(!$this->localised) $this->setupLocale(); 28261e35c35SAndreas Gohr $this->lang[$key] = $value; 28361e35c35SAndreas Gohr } 28410449332Schris} 285