summaryrefslogtreecommitdiff
blob: 381a267bec2036bf874a2f90375eb375415243e1 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
class wizard_step {
	var $configuration, $module, $step, $title, $next, $data=array();
	function __construct(&$c, $step, $noload=false) {
		$this->configuration=&$c;
		$this->module=new module($c->module);
		$this->step=$step;
		if (!$noload) {
			$file=$this->module->dir."/step$step.php";
			if (!is_readable($file)) {
				throw_exception("$mod step $step doesn't exist!");
			}
			require($file);
		}
		$this->title=$this->module->steps[$step-1];
		$this->next=isset($next)?$next:($this->step == $this->module->numsteps?null:$step+1);
	}
	public function output() {
		global $conf;
		echo "<div class=\"wizard\" id=\"step$this->step\">";
		echo '<form action="'.url('config/'.$this->configuration->id).'" method="post"><a style="float: right" href="'.url('config/'.$this->configuration->id.'/status').'">Status</a><h3>Step '.$this->step.': '.$this->title."</h3>\n";
		$scale=$conf['progressbar_width']/$this->module->numsteps;
		echo '<img src="'.url('images/full.gif').'" style="border-left: 1px solid black; border-top: 1px solid black; border-bottom: 1px solid black; width: '.$this->step*$scale.'px; height: 15px" /><img src="'.url('images/empty.gif').'" style="border-right: 1px solid black; border-top: 1px solid black; border-bottom: 1px solid black; width: '.(count($this->module->steps)-$this->step)*$scale.'px; height: 15px" /><br/>'."\n";
		$this->echo_buttons();
		foreach ($this->data as $obj) {
			if (!$obj->status) {
				echo print_warning('Please complete this field.');
			}
			$obj->output();
		}
		echo '<br/>';
		$this->echo_buttons();
		echo '</div>'."\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;
	}
	public function verify() {
		foreach ($this->data as $obj) {
			if (!($obj->status=$obj->verify())) {
				return $obj->status;
			}
		}
		return true;
	}
	private function text($text) {
		$this->data[]=new wizard_text($this->configuration, $text);
	}
	private function text_input($optname, $htmlname, $label) {
		$this->data[]=new wizard_text_input($this->configuration, $optname, $htmlname, $label);
	}
	private function select($optname, $htmlname, $label, $options) {
		$this->data[]=new wizard_select($this->configuration, $optname, $htmlname, $label, $options);
	}
	private function radio($optname, $htmlname, $label, $options) {
		$this->data[]=new wizard_radio($this->configuration, $optname, $htmlname, $label, $options);
	}
	private function checkbox_array($optname, $htmlname, $label, $array, $delim=' ') {
		$this->data[]=new wizard_checkbox_array($this->configuration, $optname, $htmlname, $label, $array, $delim=' ');
	}
	private function layered_checkbox_array($optname, $htmlname, $label, &$array, $delim=' ', $metadata) {
		$this->data[]=new wizard_layered_checkbox_array($this->configuration, $optname, $htmlname, $label, $array, $delim, $metadata);
	}
	private function query($q) {
		return $GLOBALS['S']['pdo']->query($q);
	}
	private function get_opt($opt) {
		return $this->configuration->get_opt($opt);
	}
	private function echo_buttons() {
		echo '<input type="button" onclick="window.location=\''.url('config/'.$this->configuration->id.'/'.($this->step-1)).'\'" value="Back" /> <input style="float: right" type="submit" name="wizard_submit['.$this->step.']" value="Next" /><br/>';
	}
}
abstract class wizard {
	public $status=true, $configuration;
	function __construct(&$c) {
		$this->configuration=&$c;
	}
	abstract public function output();
	abstract public function process();
	abstract public function verify();
	abstract public function clear();
	protected function get_opt($name) {
		return $this->configuration->get_opt($name);
	}
	protected function set_opt($name, $val) {
		debug('wizard', "$name=$val");
		if (substr($name, 0, 1) == ':') {
			$this->configuration->$name=$val;
			$this->configuration->write();
		} else {
			$this->configuration->set_opt($name, $val);
		}
	}
	protected function opt_is($name, $val) {
		return $this->configuration->opt_is($name, $val);
	}
	protected function delete_opt($name) {
		return $this->configuration->delete_opt($name);
	}
}
class wizard_text extends wizard {
	protected $text;
	function __construct(&$c, $text) {
		parent::__construct($c);
		$this->text=$text;
	}
	public function output() {
		echo $this->text;
	}
	public function process() {
		return true;
	}
	public function verify() {
		return true;
	}
	public function clear() {}
}
abstract class wizard_input extends wizard {
	protected $optname, $htmlname, $label;
	function __construct(&$c, $optname, $htmlname, $label) {
		parent::__construct($c);
		$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;
		}
	}
	public function verify() {
		return $this->get_opt($this->optname)!==null;
	}
	public function clear() {
		return $this->delete_opt($this->optname);
	}
}
class wizard_text_input extends wizard_input {
	public function output() {
		parent::output();
		echo "<input name=\"$this->htmlname\" /><br/>\n";
	}
}
class wizard_select extends wizard_input {
	private $options;
	function __construct(&$c, $optname, $htmlname, $label, $options) {
		parent::__construct($c, $optname, $htmlname, $label);
		$this->options=$options;
	}
	public function output() {
		parent::output();
		echo '<select name="'.$this->htmlname.'">'."\n";
		$i=0;
		foreach ($this->options as $val => $label) {
			echo "\t".'<option value="'.$i++.'"'.($this->opt_is($this->optname, $val)?' selected="selected"':'').'>'.htmlentities($label).'</option>'."\n";
		}
		echo '</select>'."\n";
	}
	public function process() {
		global $request;
		$vals=array_keys($this->options);
		if (isset($request[$this->htmlname]) && is_numeric($request[$this->htmlname]) && isset($vals[$request[$this->htmlname]])) {
			$this->set_opt($this->optname, $vals[$request[$this->htmlname]]);
			return true;
		} else return false;
	}
	public function verify() {
		if (($val=$this->get_opt($this->optname)) === null) return null;
		return isset($this->options[$val]);
	}
}
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=\"".$i."\"".($this->opt_is($this->optname, $val)?' checked="checked"':'')."\" /><label for=\"$this->htmlname-$i\">".htmlentities($label)."</label>\n";
			$i++;
		}
	}
}
class wizard_checkbox_array extends wizard_input {
	protected $array;
	function __construct(&$c, $optname, $htmlname, $label, $array, $delim=' ') {
		parent::__construct($c, $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]\"".($this->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();
		$vals=array_keys($this->array);
		foreach ($request[$this->htmlname] as $i) {
			$val[]=$vals[$i];
		}
		$this->set_opt($this->optname, implode($this->delim, $vals));
	}
	public function verify() {
		if (($vals=$this->get_opt($this->optname)) === null) return null;
		if (strlen($vals) == 0) return true;
		$vals=explode($this->delim, $vals);
		foreach ($vals as $i => $val) {
			if (isset($this->array[$val])) {
				unset($vals[$i]);
			}
		}
		return count($vals) == 0;
	}
	private static $opt_cache;
	protected function opt_has($name, $val, $delim=' ') {
		if (!isset(self::$opt_cache[$name][$delim])) {
			self::$opt_cache[$name][$delim]=explode($delim, $this->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(&$c, $optname, $htmlname, $label, &$array, $delim=' ', $metadata) {
		parent::__construct($c, $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() {
		$this->set_opt($this->optname, implode($this->delim, $this->r_process($this->array)));
		return true;
	}
	public function verify() {
		if (($vals=$this->get_opt($this->optname)) === null) return null;
		if (strlen($vals) == 0) return true;
		$vals=explode($this->delim, $vals);
		$r=$this->r_verify($vals, $this->array);
		debug('wlca', 'got results: '.implode(' ',$r));
		return count($r) == 0;
	}
	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> <a href="javascript:wlca_show_checked(document.getElementById(\''.$conf['id'].'\'), 0, '.$this->depth.'); undefined">Show checked</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 '<h3 style="display: none">No results</h3></div>';
			echo "<script type=\"text/javascript\">\n<!--\nif (wlca_show_checked(document.getElementById('{$conf['id']}'), 0, $this->depth) == 0) wlca_search(document.getElementById('{$conf['id']}-q').value, document.getElementById('{$conf['id']}'), 0, $this->depth);\n-->\n</script>\n";
		} else {
			$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'])) {
				$enc=self::b36($ucid++);
				echo '<input type="checkbox" id="-'.$enc.'" name="'.$this->htmlname.'['.$enc.']"'.($this->opt_has($this->optname, $this->format_label($array, $meta['checkbox'], $path, $name), $this->delim)?' checked="checked"':'').' /><label for="-'.$enc.'">'.$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, $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;
		} else { 
			$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 < $this->depth) {
				foreach ($array as $name => &$val)
					$this->r_process($val, $depth+1, $path.$meta['delim'].$name, $name);
			}
		}
	}
	private function &r_verify(&$vals, &$array, $depth=0, $path=null, $name=null) {
		if ($depth == 0) {
			foreach($array as $name => &$val) {
				$this->r_verify($vals, $val, $depth+1, $name, $name);
			}
			return $vals;
		} else {
			$meta=$this->metadata[$depth];
			if (isset($meta['checkbox'])) {
				$label=$this->format_label($array, $meta['checkbox'], $path, $name);
				if (($i=array_search($label, $vals)) !== false) {
					unset($vals[$i]);
				}
			}
			if ($depth < $this->depth) {
				foreach($array as $name => &$val)
					$this->r_verify($vals, $val, $depth+1, $path.$meta['delim'].$name, $name);
			}
			return $vals;
		}
	}
	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);
	}
}
?>