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; 11077c27b2SAndreas Gohruse dokuwiki\plugin\config\core\Setting; 12077c27b2SAndreas Gohruse dokuwiki\plugin\config\core\SettingFieldset; 13077c27b2SAndreas Gohruse dokuwiki\plugin\config\core\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 } 61077c27b2SAndreas 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 8110449332Schris print $this->locale_xhtml('intro'); 8210449332Schris 8324a33b42SAndreas Gohr ptln('<div id="config__manager">'); 8410449332Schris 85077c27b2SAndreas Gohr if($this->configuration->isLocked()) { 86e4a98f5cSAnika Henke ptln('<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. 92f338aa80SAndreas Gohr ptln('<form action="' . script() . '" method="post">'); 93f24af591SAnika Henke ptln('<div class="no"><input type="hidden" name="id" value="' . $ID . '" /></div>'); 94634d7150SAndreas Gohr formSecurityToken(); 954fa2dffcSBen Coburn $this->_print_h1('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) { 1064fa2dffcSBen Coburn ptln(' </table>'); 107e260f93bSAnika Henke ptln(' </div>'); 1084fa2dffcSBen Coburn ptln(' </fieldset>'); 1094fa2dffcSBen Coburn } else { 1104fa2dffcSBen Coburn $in_fieldset = true; 1114fa2dffcSBen Coburn } 112077c27b2SAndreas Gohr // fixme this should probably be a function in setting: 113a017dff4SAndreas Gohr if($first_plugin_fieldset && $setting->getType() == 'plugin') { 1144fa2dffcSBen Coburn $this->_print_h1('plugin_settings', $this->getLang('_header_plugin')); 1154fa2dffcSBen Coburn $first_plugin_fieldset = false; 116a017dff4SAndreas Gohr } else if($first_template_fieldset && $setting->getType() == 'template') { 1174fa2dffcSBen Coburn $this->_print_h1('template_settings', $this->getLang('_header_template')); 1184fa2dffcSBen Coburn $first_template_fieldset = false; 1194fa2dffcSBen Coburn } 120077c27b2SAndreas Gohr ptln(' <fieldset id="' . $setting->getKey() . '">'); 1214fa2dffcSBen Coburn ptln(' <legend>' . $setting->prompt($this) . '</legend>'); 122c7b28ffdSAnika Henke ptln(' <div class="table">'); 1234fa2dffcSBen Coburn ptln(' <table class="inline">'); 1244fa2dffcSBen Coburn } else { 1254fa2dffcSBen Coburn // config settings 126a017dff4SAndreas Gohr list($label, $input) = $setting->html($this, $this->hasErrors); 12710449332Schris 128*5c17d2d3SAndreas Gohr $class = $setting->isDefault() 12964159a61SAndreas Gohr ? ' class="default"' 130*5c17d2d3SAndreas Gohr : ($setting->isProtected() ? ' class="protected"' : ''); 131*5c17d2d3SAndreas Gohr $error = $setting->hasError() 13264159a61SAndreas Gohr ? ' class="value error"' 13364159a61SAndreas Gohr : ' class="value"'; 13464159a61SAndreas Gohr $icon = $setting->caution() 135c6639e6aSAndreas Gohr ? '<img src="' . self::IMGDIR . $setting->caution() . '.png" ' . 13664159a61SAndreas Gohr 'alt="' . $setting->caution() . '" title="' . $this->getLang($setting->caution()) . '" />' 13764159a61SAndreas Gohr : ''; 13810449332Schris 13910449332Schris ptln(' <tr' . $class . '>'); 140dde31035SAndreas Gohr ptln(' <td class="label">'); 141*5c17d2d3SAndreas Gohr ptln(' <span class="outkey">' . $setting->getPrettyKey() . '</span>'); 142bccdba06SAndreas Gohr ptln(' ' . $icon . $label); 143dde31035SAndreas Gohr ptln(' </td>'); 14410449332Schris ptln(' <td' . $error . '>' . $input . '</td>'); 14510449332Schris ptln(' </tr>'); 14610449332Schris } 1474fa2dffcSBen Coburn } 14810449332Schris 14910449332Schris ptln(' </table>'); 150c7b28ffdSAnika Henke ptln(' </div>'); 1514fa2dffcSBen Coburn if($in_fieldset) { 1524fa2dffcSBen Coburn ptln(' </fieldset>'); 1534fa2dffcSBen Coburn } 15410449332Schris 155685bdd2eSBen Coburn // show undefined settings list 156077c27b2SAndreas Gohr $undefined_settings = $this->configuration->getUndefined(); 157685bdd2eSBen Coburn if($allow_debug && !empty($undefined_settings)) { 158253d4b48SGerrit Uitslag /** 159253d4b48SGerrit Uitslag * Callback for sorting settings 160253d4b48SGerrit Uitslag * 161077c27b2SAndreas Gohr * @param Setting $a 162077c27b2SAndreas Gohr * @param Setting $b 163253d4b48SGerrit Uitslag * @return int if $a is lower/equal/higher than $b 164253d4b48SGerrit Uitslag */ 165253d4b48SGerrit Uitslag function _setting_natural_comparison($a, $b) { 166077c27b2SAndreas Gohr return strnatcmp($a->getKey(), $b->getKey()); 167253d4b48SGerrit Uitslag } 168253d4b48SGerrit Uitslag 169685bdd2eSBen Coburn usort($undefined_settings, '_setting_natural_comparison'); 170685bdd2eSBen Coburn $this->_print_h1('undefined_settings', $this->getLang('_header_undefined')); 171685bdd2eSBen Coburn ptln('<fieldset>'); 172c7b28ffdSAnika Henke ptln('<div class="table">'); 173685bdd2eSBen Coburn ptln('<table class="inline">'); 174685bdd2eSBen Coburn $undefined_setting_match = array(); 175685bdd2eSBen Coburn foreach($undefined_settings as $setting) { 17664159a61SAndreas Gohr if( 17764159a61SAndreas Gohr preg_match( 178c6639e6aSAndreas Gohr '/^(?:plugin|tpl)' . Configuration::KEYMARKER . '.*?' . Configuration::KEYMARKER . '(.*)$/', 179077c27b2SAndreas Gohr $setting->getKey(), 18064159a61SAndreas Gohr $undefined_setting_match 18164159a61SAndreas Gohr ) 18264159a61SAndreas Gohr ) { 183685bdd2eSBen Coburn $undefined_setting_key = $undefined_setting_match[1]; 184685bdd2eSBen Coburn } else { 185077c27b2SAndreas Gohr $undefined_setting_key = $setting->getKey(); 186685bdd2eSBen Coburn } 187685bdd2eSBen Coburn ptln(' <tr>'); 188077c27b2SAndreas Gohr ptln( 189077c27b2SAndreas Gohr ' <td class="label"><span title="$meta[\'' . $undefined_setting_key . '\']">$' . 190*5c17d2d3SAndreas Gohr 'conf' . '[\'' . $setting->getArrayKey() . '\']</span></td>' 191077c27b2SAndreas Gohr ); 192685bdd2eSBen Coburn ptln(' <td>' . $this->getLang('_msg_' . get_class($setting)) . '</td>'); 193685bdd2eSBen Coburn ptln(' </tr>'); 194685bdd2eSBen Coburn } 195685bdd2eSBen Coburn ptln('</table>'); 196c7b28ffdSAnika Henke ptln('</div>'); 197685bdd2eSBen Coburn ptln('</fieldset>'); 198685bdd2eSBen Coburn } 199685bdd2eSBen Coburn 200685bdd2eSBen Coburn // finish up form 20110449332Schris ptln('<p>'); 20210449332Schris ptln(' <input type="hidden" name="do" value="admin" />'); 20310449332Schris ptln(' <input type="hidden" name="page" value="config" />'); 20410449332Schris 205077c27b2SAndreas Gohr if(!$this->configuration->isLocked()) { 20610449332Schris ptln(' <input type="hidden" name="save" value="1" />'); 207ae614416SAnika Henke ptln(' <button type="submit" name="submit" accesskey="s">' . $lang['btn_save'] . '</button>'); 208ae614416SAnika Henke ptln(' <button type="reset">' . $lang['btn_reset'] . '</button>'); 20910449332Schris } 21010449332Schris 21110449332Schris ptln('</p>'); 21210449332Schris 21310449332Schris ptln('</form>'); 21410449332Schris ptln('</div>'); 21510449332Schris } 21610449332Schris 21710449332Schris /** 218253d4b48SGerrit Uitslag * @param bool $prompts 219253d4b48SGerrit Uitslag */ 22061e35c35SAndreas Gohr public function setupLocale($prompts = false) { 22189d74a2fSchris parent::setupLocale(); 222a017dff4SAndreas Gohr if(!$prompts || $this->promptsLocalized) return; 2235675a07cSAndreas Gohr $this->configuration->getLangs(); 224a017dff4SAndreas Gohr $this->promptsLocalized = true; 22589d74a2fSchris } 22689d74a2fSchris 2274fa2dffcSBen Coburn /** 2284fa2dffcSBen Coburn * Generates a two-level table of contents for the config plugin. 2294fa2dffcSBen Coburn * 2304fa2dffcSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 231253d4b48SGerrit Uitslag * 232253d4b48SGerrit Uitslag * @return array 2334fa2dffcSBen Coburn */ 23461e35c35SAndreas Gohr public function getTOC() { 235b8595a66SAndreas Gohr $this->setupLocale(true); 236b8595a66SAndreas Gohr 237685bdd2eSBen Coburn $allow_debug = $GLOBALS['conf']['allowdebug']; // avoid global $conf; here. 238685bdd2eSBen Coburn 239a017dff4SAndreas Gohr // gather settings data into three sub arrays 2404fa2dffcSBen Coburn $toc = array('conf' => array(), 'plugin' => array(), 'template' => null); 241077c27b2SAndreas Gohr foreach($this->configuration->getSettings() as $setting) { 2424fa2dffcSBen Coburn if(is_a($setting, 'setting_fieldset')) { 243a017dff4SAndreas Gohr $toc[$setting->getType()][] = $setting; 2444fa2dffcSBen Coburn } 2454fa2dffcSBen Coburn } 2464fa2dffcSBen Coburn 247e1b31a95SBen Coburn // build toc 248b8595a66SAndreas Gohr $t = array(); 249b8595a66SAndreas Gohr 250cbfba956SMichael Hamann $check = false; 251cbfba956SMichael Hamann $title = $this->getLang('_configuration_manager'); 252cbfba956SMichael Hamann $t[] = html_mktocitem(sectionID($title, $check), $title, 1); 253b8595a66SAndreas Gohr $t[] = html_mktocitem('dokuwiki_settings', $this->getLang('_header_dokuwiki'), 1); 254253d4b48SGerrit Uitslag /** @var setting $setting */ 255e1b31a95SBen Coburn foreach($toc['conf'] as $setting) { 256e1b31a95SBen Coburn $name = $setting->prompt($this); 257077c27b2SAndreas Gohr $t[] = html_mktocitem($setting->getKey(), $name, 2); 258e1b31a95SBen Coburn } 259e1b31a95SBen Coburn if(!empty($toc['plugin'])) { 260b8595a66SAndreas Gohr $t[] = html_mktocitem('plugin_settings', $this->getLang('_header_plugin'), 1); 261e1b31a95SBen Coburn } 262e1b31a95SBen Coburn foreach($toc['plugin'] as $setting) { 263e1b31a95SBen Coburn $name = $setting->prompt($this); 264077c27b2SAndreas Gohr $t[] = html_mktocitem($setting->getKey(), $name, 2); 265e1b31a95SBen Coburn } 266e1b31a95SBen Coburn if(isset($toc['template'])) { 267b8595a66SAndreas Gohr $t[] = html_mktocitem('template_settings', $this->getLang('_header_template'), 1); 268e1b31a95SBen Coburn $setting = $toc['template']; 269e1b31a95SBen Coburn $name = $setting->prompt($this); 270077c27b2SAndreas Gohr $t[] = html_mktocitem($setting->getKey(), $name, 2); 271e1b31a95SBen Coburn } 272077c27b2SAndreas Gohr if(count($this->configuration->getUndefined()) && $allow_debug) { 273b8595a66SAndreas Gohr $t[] = html_mktocitem('undefined_settings', $this->getLang('_header_undefined'), 1); 274e1b31a95SBen Coburn } 275e1b31a95SBen Coburn 276b8595a66SAndreas Gohr return $t; 2774fa2dffcSBen Coburn } 2784fa2dffcSBen Coburn 279253d4b48SGerrit Uitslag /** 280253d4b48SGerrit Uitslag * @param string $id 281253d4b48SGerrit Uitslag * @param string $text 282253d4b48SGerrit Uitslag */ 28361e35c35SAndreas Gohr protected function _print_h1($id, $text) { 28463e967bdSAnika Henke ptln('<h1 id="' . $id . '">' . $text . '</h1>'); 2854fa2dffcSBen Coburn } 2864fa2dffcSBen Coburn 28761e35c35SAndreas Gohr /** 28861e35c35SAndreas Gohr * Adds a translation to this plugin's language array 28961e35c35SAndreas Gohr * 290a017dff4SAndreas Gohr * Used by some settings to set up dynamic translations 291a017dff4SAndreas Gohr * 29261e35c35SAndreas Gohr * @param string $key 29361e35c35SAndreas Gohr * @param string $value 29461e35c35SAndreas Gohr */ 29561e35c35SAndreas Gohr public function addLang($key, $value) { 29661e35c35SAndreas Gohr if(!$this->localised) $this->setupLocale(); 29761e35c35SAndreas Gohr $this->lang[$key] = $value; 29861e35c35SAndreas Gohr } 29910449332Schris} 300