summaryrefslogtreecommitdiff
blob: daa95cf8d704c0bec0d4484ffa075ef5e70b2ab7 (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
<?php
abstract class form_element {
	protected $htmlname, $label;
	public $status=true;
	function __construct($htmlname, $label) {
		$this->htmlname=htmlentities($htmlname);
		$this->label=htmlentities($label);
	}
	public function output($rw=true, $val=false) {
		echo "$this->label: ";
	}
	public function process() {
		return isset($_REQUEST[$this->htmlname])?$_REQUEST[$this->htmlname]:false;
	}
	public function verify($val) {
		return $val !== false;
	}
}
class text_input extends form_element {
	public function output($rw=true, $val=false) {
		parent::output($rw, $val);
		echo $rw?"<input name=\"$this->htmlname\"".($val===false?'':'value="'.htmlentities($val).'"').' />':($val===false?'':htmlentities($val));
		echo "<br/>\n";
	}
}
class select extends form_element {
	private $options;
	function __construct($htmlname, $label, $options) {
		parent::__construct($htmlname, $label);
		$this->options=$options;
	}
	public function output($rw=true, $val=false) {
		parent::output($rw, $val);
		if ($rw) {
			echo '<select name="'.$this->htmlname.'">'."\n";
			$i=0;
		}
		foreach ($this->options as $value => $label) {
			if ($rw)
				echo "\t".'<option value="'.$i++.'"'.($value == $val?' selected="selected"':'').'>'.htmlentities($label).'</option>'."\n";
			elseif ($value == $val)
				echo htmlentities($label);
		}
		if ($rw)
			echo '</select>';
		echo "<br/>\n";
	}
	public function process() {
		$vals=array_keys($this->options);
		if (isset($_REQUEST[$this->htmlname]) && is_numeric($_REQUEST[$this->htmlname]) && isset($vals[$_REQUEST[$this->htmlname]])) {
			return $vals[$_REQUEST[$this->htmlname]];
		} else return false;
	}
	public function verify($val) {
		return isset($this->options[$val]);
	}
}
class radio_array extends select {
	public function output($rw=true, $val=false) {
		if (!$rw) return parent::output($rw, $val);
		echo "$this->label:<br/>\n";
		$i=0;
		foreach ($this->options as $value => $label) {
			echo "\t<input type=\"radio\" id=\"$this->htmlname-$i\" name=\"$this->htmlname\" value=\"".$i."\"".($value == $val?' checked="checked"':'')."\" /><label for=\"$this->htmlname-$i\">".htmlentities($label)."</label><br/>\n";
			$i++;
		}
	}
}
class checkbox_array extends form_element {
	protected $array;
	function __construct($htmlname, $label, $array, $delim=' ') {
		parent::__construct($htmlname, $label);
		$this->array=$array;
		$this->delim=$delim;
	}
	public function output($rw=true, $val=false) {
		$this->set_val($val);
		if (strlen($this->label))
			echo "<b>$this->label:</b><br/>\n";
		$i=0;
		foreach ($this->array as $value => $label) {
			$label=htmlentities($label);
			if ($rw)
				echo "\t<input type=\"checkbox\" id=\"$this->htmlname-$i\" name=\"$this->htmlname[$i]\"".($this->val_has($value)?' checked="checked"':'')." /><label for=\"$this->htmlname-$i\">$label</label><br/>\n";
			elseif ($this->val_has($value))
				echo "$label<br/>\n";
			$i++;
		}
	}
	public function process() {
		$val=array();
		if (isset($_REQUEST[$this->htmlname])) {
			$vals=array_keys($this->array);
			foreach ($_REQUEST[$this->htmlname] as $i => $null) {
				$val[]=$vals[$i];
			}
		}
		return implode($this->delim, $val);
	}
	public function verify($val) {
		if ($val === false) return false;
		if (strlen($val) == 0) return true;
		$val=explode($this->delim, $val);
		foreach ($val as $i => $value) {
			if (isset($this->array[$value])) {
				unset($val[$i]);
			}
		}
		return count($val) == 0;
	}
	private $vals;
	protected function set_val($val) {
		$this->vals=explode($this->delim, $val);
	}
	protected function val_has($needle) {
		return in_array($needle, $this->vals);
	}
}
class layered_checkbox_array extends checkbox_array {
	private $depth=0, $path_delims=array('', '/', '-');
	function __construct($htmlname, $label, &$array, $delim=' ', $metadata) {
		parent::__construct($htmlname, $label, $array, $delim);
		$this->metadata=$metadata;
		for ($i=current($array); is_array($i); $i=current($i)) $this->depth++;
		global $S;
		if (!in_array('lca', $S['scripts'])) {
			$S['scripts'][]='lca';
		}
	}
	public function output($rw=true, $val=false) {
		$this->set_val($val);
		if ($this->label) {
			echo '<h4>'.htmlentities($this->label).'</h4>';
		}
		if ($rw)
			$this->r_output($this->array);
		else
			$this->r_ro_output($this->array);
	}
	public function process() {
		return implode($this->delim, $this->r_process($this->array));
	}
	public function verify($val) {
		if ($val === false) return false;
		if (strlen($val) == 0) return true;
		$val=explode($this->delim, $val);
		$r=$this->r_verify($val, $this->array);
		debug('lca', 'verify leftovers: '.implode(' ',$r));
		return count($r) == 0;
	}
	private function r_output(&$array, $depth=0, $path=null, $name=null) {
		static $uid=0, $ucid=0;
		$S['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($S['conf']['id'])) {
					$S['conf']['id']=self::b36($uid++);
				}
				echo 'Search: <input id="'.$S['conf']['id'].'-q" onkeyup="lca_search(this.value, document.getElementById(\''.$S['conf']['id'].'\'), 0, '.$this->depth.')" /> <a href="javascript:q=document.getElementById(\''.$S['conf']['id'].'-q\'); q.value=\'\'; q.onkeyup()">Clear</a> <a href="javascript:lca_show_checked(document.getElementById(\''.$S['conf']['id'].'\'), 0, '.$this->depth.'); undefined">Show checked</a><br/>'."\n";
			}
			echo '<div class="lca'.(isset($S['conf']['autosize'])?' autosize" style="font-size: '.pow(1.15, $autosize)*100.0.'%':'').'" id="'.$S['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 (lca_show_checked(document.getElementById('{$S['conf']['id']}'), 0, $this->depth) == 0) lca_search(document.getElementById('{$S['conf']['id']}-q').value, document.getElementById('{$S['conf']['id']}'), 0, $this->depth);\n-->\n</script>\n";
		} else {
			$meta=$this->metadata[$depth];
			if (isset($meta['tag'])) {
				echo '<'.$meta['tag'].' class="lcae'.(isset($meta['search'])?' lcas':'').(isset($meta['collapsed'])?' lca'.($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:lcat(\''.self::b36($uid).'\')">&plusmn;</a>';
				}
			}
			if (isset($meta['checkbox'])) {
				$enc=self::b36($ucid++);
				echo '<input type="checkbox" id="-'.$enc.'" name="'.$this->htmlname.'['.$enc.']"'.($this->val_has($this->format_label($array, $meta['checkbox'], $path, $name))?' checked="checked"':'').' /><label for="-'.$enc.'">'.$this->format_label($array, $meta['label'], $path, $name).'</label>'."\n";
			} elseif (isset($meta['label'])) {
				echo '<span class="lcal">'.$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'])) {
				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 r_ro_output(&$array, $depth=0, $path=null, $name=null) {
		if ($depth == 0) {
			foreach ($array as $name => &$val) {
				$this->r_ro_output($val, $depth+1, $name, $name);
			}
		} else {
			$meta=$this->metadata[$depth];
			if (isset($meta['checkbox'])) {
				$val=$this->format_label($array, $meta['checkbox'], $path, $name);
				if ($this->val_has($val)) {
					echo $this->format_label($array, $meta['label'], $path, $name)."<br/>\n";
				}
			}
			if ($depth < $this->depth) {
				foreach ($array as $name => &$val)
					$this->r_ro_output($val, $depth+1, $path.$meta['delim'].$name, $name);
			}
		}
	}
	private function format_label(&$array, $label='%p', $path, $name) {
		$arg=$array;
		$out=str_replace(array('%p', '%n'), array($path, $name), $label);
		if (strpos($label, '$')) {
			while (is_array(current($arg))) {
				$arg=current($arg);
			}
			$out=eval("extract(\$arg, EXTR_PREFIX_INVALID, 'var_');\n".(strpos($label, 'return')===0?$out:"return <<<_XQ1\n$out\n_XQ1").";\n");
		}
		return strpos($label, 'return')===0?$out:htmlentities($out);
	}
	private static function b36($n) {
		return base_convert($n, 10, 36);
	}
}
?>