1<?php
2
3if (!class_exists('settingswrapper',false)){
4require_once(DOKU_INC.'lib/plugins/config/settings/config.class.php');
5
6class settingswrapper{
7	protected $_setting = null;				// the settings_* class form config plugin.
8	protected $_key = null;					// the key of the setting
9	protected $_level = null;					// the settingslevel object
10	public $_value = null;					// the value defined in this level
11	public $_protect = false;				// the protection added by this level
12	protected $_old_val = null;				// we store the old value here if the value is updated.
13	protected $_updated = false;				// flag to indicate if value was updated.
14	protected $_changed = false;				// we mark a field for change (e.g. save failed, we want to show field as changed.)
15
16	function __construct($key,settingslevel $level, array $meta, $set){
17		$this->_key = $key;
18		$this->_level = $level;
19		$this->_protect = @$set['protect'];
20		$this->_value = @$set['value'];
21		$this->_initSetting($meta);
22	}
23
24	function setProtect($val){
25		if (!$val)
26			unset($this->_protect);
27		else
28			$this->_protect = (bool)$val;
29		return true;
30	}
31
32	function tryUpdate($val){
33		$this->_old_val = $this->_setting->_local;
34		$changed = $this->_setting->update($val);
35		if ($this->_setting->_error){
36			return false;
37		}
38		$this->_updated = true;
39		if ($this->_setting->_local !== null && $this->_setting->_local === $this->_setting->_default){
40			$this->_setting->_local = null;
41		}
42		$this->_value = $this->_setting->_local;
43		return true;
44	}
45
46	protected function _initSetting($meta){
47		$class_meta = array_shift($meta);
48		if($class_meta && class_exists('setting_'.$class_meta)){
49			$class = 'setting_'.$class_meta;
50		}elseif($class_meta == ''){
51			$class = 'setting';			// the '' option is the textarea...
52		}else{
53			die('TODO: extend this function!');
54		}
55		$this->_setting = new $class($this->_key,$meta);
56
57		$this->_setting->initialize(
58			$this->_level->getDefault($this->_key),
59			$this->_level->getLocal($this->_key),
60		// protection applies from parent not the current level. If you protect a level's value, you can still change it on this level, but it's protected for children.
61			$this->_level->getParentProtected($this->_key)
62		);
63
64	}
65	function currentValue(){
66		if ($this->_changed) return $this->_old_val;
67		return $this->_setting->_local === null ? $this->_setting->_default : $this->_setting->_local;
68	}
69	function markChanged(){
70		$this->_changed = true;
71	}
72
73	protected function _showHierarchyButton(){
74		return "<button class='settingstree_button_show_hierarchy' onclick=\"jQuery(this).trigger('show_in_hierarchy',['{$this->_key}','{$this->_level->path}']);\">".settingshierarchy::$helper->getLang('show_hierarchy')."</button>";
75	}
76	protected function _showProtectedCell(){
77		return "<td class='protect_area' data-currentval='".($this->_protect ? '1' : '0')."'>
78				<label for='settingstree_{$this->_key}_protect'>".settingshierarchy::$helper->getLang('protected')."</label>
79				<input class='protect_input' type='checkbox' name='protect[{$this->_key}]' id='settingstree_{$this->_key}_protect' value='1' "
80				.($this->_protect ? 'checked="checked"' : '')." ".($this->_level->getParentProtected($this->_key) !== null ? "disabled='disabled'":"")."/>
81			</td>";
82	}
83	protected function _showInputUpdateResults(){
84		if ($this->_setting->_error){
85			return "<div class='error'>".settingshierarchy::$helper->getLang('invalid_value').($this->_setting->_input !== null ? ": <code>{$this->format($this->_setting->_input)}</code>": "!")." </div>";
86		}elseif ($this->_updated && !$this->changed){
87			return "<div class='info'>".settingshierarchy::$helper->getLang('updated_value_from').": <code>{$this->format($this->_old_val,$default)}</code>". ($default ? " <small>(".settingshierarchy::$helper->getLang('default_value').")</small> ":"")." </div>";
88		}
89
90	}
91
92	function showHtml(){
93		$lang = $this->_level->getHierarchy();
94
95		list($label,$input) = $this->_setting->html($lang);	// html only uses the $plugin as parameter to have a getLang method, we emulate that on hierarchy.
96		$cssclass = $this->_setting->is_default() ? ' class="default"' : ($this->_setting->is_protected() ? ' class="protected"' : '');
97		$errorclass = ($this->_setting->error() ? ' value error' : ' value'). ($this->_changed ? " changed" : "");
98		$has_icon = $this->_setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$this->_setting->caution().'.png" alt="'.$this->_setting->caution().'" title="'.$lang->getLang($this->_setting->caution()).'" />' : '';
99
100
101		$ret = "<tr {$cssclass}><td class='label' colspan='2'>";
102		$ret .=	"<span class='outkey'>{$this->_setting->_out_key(true,true)}</span>{$has_icon}{$label}";
103		// DECIDE: push to plugin debug?
104		/*		$ret .= "<code><small>debug: _local = ".var_export($this->_setting->_local ,1).",
105		_default = ".var_export($this->_setting->_default ,1).",
106		_protected = ".var_export($this->_setting->_protected ,1).",
107		_updated: '{$this->_updated}',
108		_old_val: ".var_export($this->_old_val ,1)."</small></code><br/>";*/
109		$ret .= $this->_showHierarchyButton();
110		$ret .= "</td></tr>";
111		$ret .= "<tr {$cssclass}>".$this->_showProtectedCell();
112		$ret .= "<td class='input_area  {$errorclass}' data-currentval='{$this->currentValue()}'>";
113		$ret .= $this->_showInputUpdateResults();
114		$ret .= "{$input}</td></tr>";
115		return $ret;
116	}
117
118	function format($value,&$default = null){
119		$default = false;
120		if ($value === null){
121			$default = true;
122			$value = $this->_level->getDefault($this->_key);
123		}
124		return $this->_level->getHierarchy()->format($this->_key,$value);
125	}
126
127
128}
129
130class settingswrapper_export extends settingswrapper{
131	protected function _showHierarchyButton(){
132		return "";
133	}
134	protected function _showProtectedCell(){
135		return "";
136	}
137	function showHtml(){
138		$lang = $this->_level->getHierarchy();
139
140		list($label,$input) = $this->_setting->html($lang);	// html only uses the $plugin as parameter to have a getLang method, we emulate that on hierarchy.
141		$input = strtr($input,array('id="config_'=>'id="export_'));	// we need to replace the id/for for the inputs/labels, as they can be present on pages where config options are available and then they can violate the id-uniqueness constraint of html.
142		$label = strtr($label,array('for="config_'=>'for="export_'));
143		$cssclass = $this->_setting->is_default() ? ' class="default"' : ($this->_setting->is_protected() ? ' class="protected"' : '');
144		$errorclass = ($this->_setting->error() ? ' value error' : ' value'). ($this->_changed ? " changed" : "");
145		$has_icon = $this->_setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$this->_setting->caution().'.png" alt="'.$this->_setting->caution().'" title="'.$lang->getLang($this->_setting->caution()).'" />' : '';
146
147
148		$ret = "<tr {$cssclass}><td class='label'>";
149		$ret .=	"<span class='outkey'>{$this->_setting->_out_key(true,true)}</span>{$has_icon}{$label}";
150		$ret .= "</td>";
151		$ret .= "<td class='input_area  {$errorclass}' data-currentval='{$this->currentValue()}'>";
152		$ret .= $this->_showInputUpdateResults();
153		$ret .= "{$input}</td></tr>";
154		return $ret;
155	}
156}
157} // class_exists