summaryrefslogtreecommitdiff
blob: b23b8a53d742c59d2a348162bec5b2f4a2042d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
class wizard_step {
	var $module, $step, $title, $next, $data=array();
	function __construct($mod, $step) {
		$this->module=$mod;
		$this->step=$step;
		$file=FRONTEND."/modules/$mod/step$step.php";
		if (!is_readable($file)) {
			throw_exception("$mod step $step doesn't exist!");
		}
		require($file);
		$this->title="Step $step".($title?" - $title":'');
		$this->next=isset($next)?$next:$step+1;
	}
	public function output() {
		echo "<h3>$this->title</h3>\n";
		echo '<form action="'.url('config/'.wizard::$configuration->id).'" method="post">';
		foreach ($this->data as $obj) {
			if (!$obj->status) {
				echo print_warning('Please complete this field.');
			}
			$obj->output();
		}
		echo '<br/><input type="submit" name="wizard_submit['.$this->step.']" value="Continue" />'."\n";
	}
	public function process() {
		global $request;
		if (!isset($request['wizard_submit'][$this->step])) {
			return $this->step;
		}
		$result=$this->next;
		foreach ($this->data as $obj) {
			if (!$obj->status=$obj->process()) {
				$result=$this->step;
			}
		}
		return $result;
	}
	private function text($text) {
		$this->data[]=new wizard_text($text);
	}
	private function text_input($optname, $htmlname, $label) {
		$this->data[]=new wizard_text_input($optname, $htmlname, $label);
	}
	private function select($optname, $htmlname, $label, $options) {
		$this->data[]=new wizard_select($optname, $htmlname, $label, $options);
	}
	private function radio($optname, $htmlname, $label, $options) {
		$this->data[]=new wizard_radio($optname, $htmlname, $label, $options);
	}
	private function checkbox_array($optname, $htmlname, $label, $array, $delim=' ') {
		$this->data[]=new wizard_checkbox_array($optname, $htmlname, $label, $array, $delim=' ');
	}
	private function layered_checkbox_array($optname, $htmlname, $label, &$array, $delim=' ', $metadata) {
		$this->data[]=new wizard_layered_checkbox_array($optname, $htmlname, $label, $array, $delim, $metadata);
	}
	private function query($q) {
		return $GLOBALS['S']['pdo']->query($q);
	}
	private function get_opt($opt) {
		return wizard::get_opt($opt);
	}
}
abstract class wizard {
	public $status=true;
	public static $configuration;
	public static function set_configuration(&$c) {
		self::$configuration=&$c;
	}
	abstract public function output();
	abstract public function process();
	public static function get_opt($name) {
		$opts=self::$configuration->get_opts();
		return isset($opts[$name])?$opts[$name]:null;
	}
	protected static function set_opt($name, $val) {
		debug('wizard', "$name=$val");
		if (substr($name, 0, 1) == ':') {
			self::$configuration->$name=$val;
			self::$configuration->write();
		} else {
			self::$configuration->set_opt($name, $val);
		}
	}
	protected static function opt_is($name, $val) {
		$opts=self::$configuration->get_opts();
		if (isset($opts[$name]) && $opts[$name] === $val) {
			return true;
		} else {
			return false;
		}
	}
}
class wizard_text extends wizard {
	protected $text;
	function __construct($text) {
		$this->text=$text;
	}
	public function output() {
		echo $this->text;
	}
	public function process() {
		return true;
	}
}
abstract class wizard_input extends wizard {
	protected $optname, $htmlname, $label;
	function __construct($optname, $htmlname, $label) {
		$this->optname=$optname;
		$this->htmlname=htmlentities($htmlname);
		$this->label=htmlentities($label);
	}
	public function output() {
		echo "$this->label ";
	}
	public function process() {
		global $request;
		if (isset($request[$this->optname])) {
			self::set_opt($this->optname, $request[$this->optname]);
			return true;
		} else {
			return false;
		}
	}
}
class wizard_text_input extends wizard_input {
	public function output() {
		parent::output();
	}
}
class wizard_select extends wizard_input {
	private $options;
	function __construct($optname, $htmlname, $label, $options) {
		parent::__construct($optname, $htmlname, $label);
		$this->options=$options;
	}
	public function output() {
		parent::output();
		echo '<select name="'.$this->htmlname.'">'."\n";
		foreach ($this->options as $val => $label) {
			echo "\t".'<option value="'.htmlentities($val).'"'.(self::opt_is($this->optname, $val)?' selected="selected"':'').'>'.htmlentities($label).'</option>'."\n";
		}
		echo '</select>'."\n";
	}
}
class wizard_radio extends wizard_select {
	public function output() {
		echo "$this->label:<br/>\n";
		$i=0;
		foreach ($this->options as $val => $label) {
			echo "\t<input type=\"radio\" id=\"$this->htmlname-$i\" name=\"$this->htmlname\" value=\"".htmlentities($val)."\"".(self::opt_is($this->optname, $val)?' checked="checked"':'')."\" /><label for=\"$this->htmlname-$i\">".htmlentities($label)."</label>\n";
		}
	}
}
class wizard_checkbox_array extends wizard_input {
	protected $array;
	function __construct($optname, $htmlname, $label, $array, $delim=' ') {
		parent::__construct($optname, $htmlname, $label);
		$this->array=$array;
		$this->delim=$delim;
	}
	public function output() {
		echo "$this->label:<br/>\n";
		$i=0;
		foreach ($this->array as $val => $label) {
			echo "\t<input type=\"checkbox\" id=\"$this->htmlname-$i\" name=\"$this->htmlname[$i]\"".(self::opt_has($this->optname, $val, $this->delim)?' checked="checked"':'')." /><label for=\"$this->htmlname-$i\">$label</label><br/>\n";
			$i++;
		}
	}
	public function process() {
		global $request;
		$val=array();
		// FIXME we're assuming that array_keys order is determinate and the same as the foreach in output()
		$vals=array_keys($this->array);
		foreach ($request[$this->htmlname] as $i) {
			$val[]=$vals[$i];
		}
		self::set_opt($this->optname, implode($this->delim, $vals));
	}
	private static $opt_cache;
	protected static function opt_has($name, $val, $delim=' ') {
		if (!isset(self::$opt_cache[$name][$delim])) {
			self::$opt_cache[$name][$delim]=explode($delim, self::get_opt($name));
		}
		return in_array($val, self::$opt_cache[$name][$delim]);
	}
}
class wizard_layered_checkbox_array extends wizard_checkbox_array {
	private $depth=0, $path_delims=array('', '/', '-');
	function __construct($optname, $htmlname, $label, &$array, $delim=' ', $metadata) {
		parent::__construct($optname, $htmlname, $label, &$array, $delim);
		$this->metadata=$metadata;
		for ($i=current(&$array); is_array($i); $i=current($i)) $this->depth++;
		global $S;
		if (!in_array('wlca', $S['scripts'])) {
			$S['scripts'][]='wlca';
		}
	}
	public function output() {
		if ($this->label) {
			echo '<h4>'.htmlentities($this->label).'</h4>';
		}
		$this->r_output($this->array);
	}
	public function process() {
		self::set_opt($this->optname, implode($this->delim, $this->r_process($this->array)));
		return true;
	}
	private function r_output(&$array, $depth=0, $path=null, $name=null) {
		static $uid=0, $ucid=0;
		$conf=&$this->metadata[0];
		if ($depth == 0) {
			$search=$autosize=0;
			for ($i=1; $i<count($this->metadata); $i++) {
				$m=&$this->metadata[$i];
				if (isset($m['tag'])) {
					$autosize++;
				}
				if (isset($m['search'])) {
					$search++;
				}
			}
			if ($search) {
				if (!isset($conf['id'])) {
					$conf['id']=self::b36($uid++);
				}
				echo 'Search: <input id="'.$conf['id'].'-q" onkeyup="wlca_search(this.value, document.getElementById(\''.$conf['id'].'\'), 0, '.$this->depth.')" /> <a href="javascript:q=document.getElementById(\''.$conf['id'].'-q\'); q.value=\'\'; q.onkeyup()">Clear</a><br/>'."\n";
			}
			echo '<div class="wlca'.(isset($conf['autosize'])?' autosize" style="font-size: '.pow(1.15, $autosize)*100.0.'%':'').'" id="'.$conf['id'].'">'."\n";
			foreach ($array as $name => &$val) {
				$this->r_output($val, $depth+1, $name, $name);
				$uid++;
			}
			echo '</div>';
			echo "<script type=\"text/javascript\">\n<!--\nwlca_search(document.getElementById('{$conf['id']}-q').value, document.getElementById('{$conf['id']}'), 0, $this->depth);\n-->\n</script>\n";
		} elseif ($depth <= $this->depth) {
			$meta=$this->metadata[$depth];
			if (isset($meta['tag'])) {
				echo '<'.$meta['tag'].' class="wlcae'.(isset($meta['search'])?' wlcas':'').(isset($meta['collapsed'])?' wlca'.($meta['collapsed']?'c':'C'):'').(isset($meta['class'])?' '.$meta['class']:'').'" id="'.self::b36($uid).'"'.($depth > 1 && isset($this->metadata[$depth-1]['collapsed']) && $this->metadata[$depth-1]['collapsed'] && false?' style="display: none"':'').'>';
				if (isset($meta['collapsed']) && $depth < $this->depth) {
					echo '<a href="javascript:wlcat(\''.self::b36($uid).'\')">&plusmn;</a>';
				}
			}
			if (isset($meta['checkbox'])) {
				$hex=self::b36($ucid++);
				echo '<input type="checkbox" id="-'.$hex.'" name="'.$this->htmlname.'['.$hex.']"'.(self::opt_has($this->optname, $this->format_label($array, $meta['checkbox'], $path, $name), $this->delim)?' checked="checked"':'').' /><label for="-'.$hex.'">'.$this->format_label($array, $meta['label'], $path, $name).'</label>'."\n";
			} elseif (isset($meta['label'])) {
				echo '<span class="wlcal">'.$this->format_label($array, $meta['label'], $path, $name)."</span>\n";
			}
			if ($depth < $this->depth) {
				foreach ($array as $name => &$val) {
					$uid++;
					$this->r_output($val, $depth+1, (isset($path)?($path.$meta['delim']):'').$name, $name);
				}
			}
			if (isset($meta['tag'])) {
				echo '</'.$meta['tag'].">\n";
			}
		}
	}
	private function r_process(&$array, $depth=0, $path=null, $name=null) {
		static $ucid=0, $r;
		if ($depth == 0) {
			$r=array();
			foreach ($array as $name => &$val) {
				$this->r_process($val, $depth+1, $name, $name);
			}
			return $r;
		} elseif ($depth <= $this->depth) {
			$meta=$this->metadata[$depth];
			if (isset($meta['checkbox'])) {
				global $request;
				if (isset($request[$this->htmlname][self::b36($ucid)])) {
					$r[]=$this->format_label($array, $meta['checkbox'], $path, $name);
				}
				$ucid++;
			}
			if ($depth + 1 <= $this->depth) {
				foreach ($array as $name => &$val) {
					$this->r_process($val, $depth+1, (isset($path)?($path.$meta['delim']):'').$name, $name);
				}
			}
		}
	}
	private function format_label(&$array, $label='%p', $path, $name) {
		$arg=$array;
		$label=str_replace(array('%p', '%n'), array($path, $name), $label);
		if (strpos($label, '$')) {
			while (is_array(current($arg))) {
				$arg=current($arg);
			}
			$label=eval("extract(\$arg, EXTR_PREFIX_INVALID, 'var_');\nreturn <<<_XQ1\n$label\n_XQ1;\n");
		}
		return htmlentities($label);
	}
	private static function b36($n) {
		return base_convert($n, 10, 36);
	}
}
?>