summaryrefslogtreecommitdiff
path: root/utils/forms.php
blob: 0268315c613664f50da2a8944e170cae632e352c (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php

include_once($UTIL_DIR."/convert.php");

class LineEdit {
	public $label, $name, $value;
		
	function LineEdit($label, $name, $value = "")
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
	}

	function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) ."</div>\n";
		$str .= $indent . "  <div class=\"widget\"><input name=\"vars[".$this->name."]\" value=\"".xmlenc($this->value)."\"/></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class LineEditPwd {
	public $label, $name, $value;
		
	function LineEditPwd($label, $name, $value = "")
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
	}

	function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) ."</div>\n";
		$str .= $indent . "  <div class=\"widget\"><input name=\"vars[".$this->name."]\" value=\"".xmlenc($this->value)."\" type=\"password\"/></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class FileUpload {
	public $label, $name, $accept;

	public function FileUpload($label, $name, $accept = "*")
	{
		$this->label = $label;
		$this->name = $name;
		$this->accept = $accept;
	}

	public function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) . "</div>\n";
		$str .= $indent . "  <div class=\"widget\"><input type=\"file\" name=\"" 
			. $this->name. "\" accept=\"". $this->accept ."\"/></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class Button {
	public $label;

	public function Button($label)
	{
		$this->label = $label;
	}

	public function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\"></div>\n";
		$str .= $indent . "  <div class=\"widget\"><button type=\"submit\">".
			$this->label."</button></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class CheckBox {
	public $label, $name, $value;
		
	public function CheckBox($label, $name, $value = "")
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
	}

	public function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) ."</div>\n";
		$str .= $indent . "  <div class=\"widget\"><input type=\"checkbox\" name=\"vars[".$this->name."]\" value=\"".xmlenc($this->value)."\"/></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class ComboBox {
	public $label, $name, $value, $values;
		
	public function ComboBox($label, $name, $value, $values)
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
		$this->values = $values;
	}

	public function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">".xmlenc($this->label)."</div>\n";
		$str .= $indent . "  <div class=\"widget\">\n";
		$str .= $indent . "    <select name=\"vars[".$this->name."]\">\n";
		foreach($this->values as $k => $v) {
			if($v != $this->value) $str .= $indent . "      <option value=\"".xmlenc($v)."\">".xmlenc($k)."</option>\n";
			else $str .= $indent .  "      <option value=\"".xmlenc($v)."\" selected>".xmlenc($k)."</option>\n";
		}
		$str .= $indent . "    </select>\n";
		$str .= $indent . "  </div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class Hidden {
	public $values;
		
	public function Hidden($values)
	{
		$this->values = $values;
	}

	public function render($indent = "")
	{
		$str = "";
		foreach($this->values as $key => $value) {
			$str .= $indent . "<input type=\"hidden\" name=\"vars[".$key."]\" value=\"".xmlenc($value)."\"/>\n";
		}
		return $str;
	}
}

class TextEdit {
	public $label, $name, $value;
		
	function TextEdit($label, $name, $value = "", $lines = "20")
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
		$this->lines = $lines;
	}

	function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) ."</div>\n";
    $str .= $indent . "  <div class=\"widget\"><textarea rows=\"".$this->lines."\" class=\"textedit\" name=\"vars[".$this->name."]\">".xmlenc($this->value)."</textarea></div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class DateTimeEdit {
	public $label, $name, $timestamp;
		
	function DateTimeEdit($label, $name, $timestamp = 0)
	{
		$this->label = $label;
		$this->name = $name;
		$this->timestamp = $timestamp;
	}

	function render($indent = "")
	{
		if($this->timestamp == 0) $t = time();
		else $t = $this->timestamp;

		$second = date('s',$t);
		$minute = date('i',$t);
		$hour = date('G',$t); 
		$day = date('d',$t);
		$month = date('m',$t);
		$year = date('Y',$t); 

		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "	<div class=\"label\">".xmlenc($this->label)."</div>\n";
		$str .= $indent . "  <div class=\"widget\">\n";
		$str .= $indent . "     <input style=\"width: 50px;\" name=\"vars[".$this->name."_year]\" value=\"".$year."\"/>";
		$str .= "/<input style=\"width: 30px;\" name=\"vars[".$this->name."_month]\" value=\"".$month."\"/>";
		$str .= "/<input style=\"width: 30px;\" name=\"vars[".$this->name."_day]\" value=\"".$day."\"/>";
		$str .= "	- ";
		$str .= "<input style=\"width: 30px;\" name=\"vars[".$this->name."_hour]\" value=\"".$hour."\"/>";
		$str .= ":<input style=\"width: 30px;\" name=\"vars[".$this->name."_minute]\" value=\"".$minute."\"/>";
		$str .= ":<input style=\"width: 30px;\" name=\"vars[".$this->name."_second]\" value=\"".$second."\"/>\n";
		$str .= $indent . "  </div>\n";
		$str .= $indent . "</div>\n";

		return $str;
	}

	public function toTimestamp($t, $name)
	{
		$timestring = sprintf("%d", $t[$name."_year"])  ."/".
			sprintf("%d", $t[$name."_month"]) ."/".
			sprintf("%d", $t[$name."_day"])   ." ".
			sprintf("%d", $t[$name."_hour"])  .":".
			sprintf("%02d", $t[$name."_minute"]).":".
			sprintf("%02d", $t[$name."_second"]);
		
		return strtotime($timestring);
	}
}

class ImageComboBox {
	public $label, $name, $value, $values;
		
	public function ImageComboBox($label, $name, $value, $values)
	{
		$this->label = $label;
		$this->name = $name;
		$this->value = $value;
		$this->values = $values;
	}

	public function render($indent = "")
	{
		$selheight = 64;
		$height = $selheight * 0.8;

		if(get_class($this->values) == "Icons") $numicons = sizeof($this->values->icons);
		else $numicons = sizeof($this->values);

		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">".xmlenc($this->label)."</div>\n";
		$str .= $indent . "  <div class=\"widget\">\n";
		$str .= $indent . "    <script type=\"text/javascript\">\n";
		$str .= $indent . "    function setSelection(value)\n";
		$str .= $indent . "    {\n";
		$str .= $indent . "      document.getElementById('".$this->name."').value = value\n";
		$str .= $indent . "      var container = document.getElementById('icons');\n";
		$str .= $indent . "      var icons = document.getElementsByName('icon');\n";
		$str .= $indent . "      for(i = 0; i < icons.length; i++) {\n";
		$str .= $indent . "        if(icons[i].alt == value) icons[i].height = ".$selheight.";\n";
		$str .= $indent . "        else icons[i].height = ".$height.";\n";
		//		$str .= $indent . "        if(icons[i].src == value) icons[i].style.border = '10px blue';\n";
		//		$str .= $indent . "        else { icons[i].style.border = '0px' }\n";
		$str .= $indent . "      }\n";
		$str .= $indent . "    }\n";
		$str .= $indent . "    </script>\n";
		$str .= $indent . "    <div style=\"width:300px;overflow:auto;height:".($selheight+24)."px;\">\n";
		$str .= $indent . "      <div id=\"icons\" style=\"text-align: left; height: ".$selheight."px; width:".(($height+8)*($numicons-1)+$selheight)."px; display: block;\">\n";
		$str .= $indent . "        <input id=\"".$this->name."\" name=\"vars[".$this->name."]\" type=\"hidden\" value=\"\"/>\n";
		if(get_class($this->values) == "Icons") {
			foreach($this->values->icons as $icon) {
				$str .= $indent . "        <img name=\"icon\" alt=\"".$icon->file."\" src=\"".$icon->prefix.$icon->file."\" onclick=\"setSelection('".$icon->file."')\"/>\n";
			}
		} else {
			foreach($this->values as $k => $v) {
				$str .= $indent . "        <img name=\"icon\" alt=\"".$v."\" src=\"".$v."\" onclick=\"setSelection('".$v."')\"/>\n";
			}
		}
		$str .= $indent . "      </div>\n"; // box
		$str .= $indent . "    </div>\n"; // overflow
		$str .= $indent . "    <script type=\"text/javascript\">setSelection('".$this->value."');</script>\n";
		$str .= $indent . "  </div>\n"; // widget
		$str .= $indent . "</div>\n"; // input
		return $str;
	}
}

class ListEditor {
	public $label, $name, $namewidget, $valuewidget, $values;

	public function ListEditor($label, $name, $namewidget, $valuewidget, $values = array())
	{
		$this->label = $label;
		$this->name = $name;
		$this->namewidget = $namewidget;
		$this->valuewidget = $valuewidget;
		$this->values = $values;
	}

	public function render($indent = "")
	{
		$str  = $indent . "<script type=\"text/javascript\">\n";
		$str .= $indent . "//<!--\n";
		$str .= $indent . "function deleteItem()\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  var items = document.getElementById('items');\n";
		$str .= $indent . "  var selIndex = items.selectedIndex;\n";
		$str .= $indent . "  if(selIndex < 0) return;\n";
		$str .= $indent . "  var item = items.options.item(selIndex);\n";
		$str .= $indent . "  items.removeChild(item);\n";
		$str .= $indent . "}\n";
		$str .= $indent . "\n";
		$str .= $indent . "function addItem()\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  var items = document.getElementById('items');\n";
		$str .= $indent . "  var item_names = document.getElementsByName('vars[".$this->namewidget->name."]');\n";
		$str .= $indent . "  var item_values = document.getElementsByName('vars[".$this->valuewidget->name."]');\n";
		$str .= $indent . "  var item_name = item_names[0].value;\n";
		$str .= $indent . "  var item_value = item_values[0].value;\n";
		$str .= $indent . "  var item = document.createElement('option');\n";
		$str .= $indent . "  item.value = item_value + ':' + item_name;\n";
		$str .= $indent . "  item.innerHTML = item_value + ':' + item_name;\n";
		$str .= $indent . "  items.appendChild(item);\n";
		$str .= $indent . "}\n";
		$str .= $indent . "\n";
		$str .= $indent . "function move(mod)\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  var items = document.getElementById('items');\n";
		$str .= $indent . "  var idx = items.selectedIndex;\n";
		$str .= $indent . "\n";
		$str .= $indent . "  if(idx < 0 || idx + mod < 0 || idx + mod > items.length - 1) return;\n";
		$str .= $indent . "\n";
		$str .= $indent . "  var tmp_value = items.options[idx].value;\n";
		$str .= $indent . "  var tmp_innerHTML = items.options[idx].innerHTML;\n";
		$str .= $indent . "\n";
		$str .= $indent . "  items.options[idx].value = items.options[idx + mod].value;\n";
		$str .= $indent . "  items.options[idx].innerHTML = items.options[idx + mod].innerHTML;\n";
		$str .= $indent . "  items.options[idx + mod].value = tmp_value;\n";
		$str .= $indent . "  items.options[idx + mod].innerHTML = tmp_innerHTML;\n";
		$str .= $indent . "\n";
		$str .= $indent . "  items.selectedIndex += mod;\n";
		$str .= $indent . "}\n";
		$str .= $indent . "\n";
		$str .= $indent . "function moveUp()\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  move(-1)\n";
		$str .= $indent . "}\n";
		$str .= $indent . "\n";
		$str .= $indent . "function moveDown()\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  move(1)\n";
		$str .= $indent . "}\n";
		$str .= $indent . "\n";
		$str .= $indent . "function selectAll()\n";
		$str .= $indent . "{\n";
		$str .= $indent . "  var items = document.getElementById('items');\n";
		$str .= $indent . "  var len = items.length;\n";
		$str .= $indent . "  var data = '';\n";
		$str .= $indent . "  for(i = 0; i < len; i++) {\n";
		$str .= $indent . "    items.options[i].selected = true;\n";
		$str .= $indent . "  }\n";
		$str .= $indent . "}\n";
		$str .= $indent . "//-->\n";
		$str .= $indent . "</script>\n";
		$str .= $indent . "<select multiple size=\"8\" id=\"items\" name=\"".$this->name."[]\">\n";
		if(sizeof($this->values)) {
			foreach($this->values as $key => $val) {
				$str .= $indent . "  <option value=\"".$key.":".$val."\">".$key.":".$val."</option>\n";
			}
		}
		$str .= $indent . "</select><br/>\n";
		$str .= $indent . "<div class=\"button\" button onclick=\"moveUp()\">/\</div><br/>\n";
		$str .= $indent . "<div class=\"button\" onclick=\"moveDown()\">\/</div><br/>\n";
		$str .= $this->namewidget->render($indent);
		$str .= $this->valuewidget->render($indent);
		$str .= "<div class=\"button\" onclick=\"addItem()\">+</div><br/>\n";
		$str .= $indent . "<div class=\"button\" onclick=\"deleteItem()\">-</div><br/>\n";
		return $str;
	}

	public function splitValues($values)
	{
		$out = array();
		foreach($values as $value) {
			$vals = explode(":", $value);
			$out[$vals[0]]=$vals[1];
		}
		return $out;
	}
}

class MultiList {
	public $label, $name, $values;
		
	function MultiList($label, $name, $values = "")
	{
		$this->label = $label;
		$this->name = $name;
		$this->values = $values;
	}

	function render($indent = "")
	{
		$str  = $indent . "<div class=\"input\">\n";
		$str .= $indent . "  <div class=\"label\">". xmlenc($this->label) ."</div>\n";
		$str .= $indent . "  <div class=\"widget\">\n";
		$str .= $indent . "    <select multiple size=\"8\" id=\"items\" name=\"".$this->name."[]\">\n";
		if(sizeof($this->values)) {
			foreach($this->values as $value => $active) {
				if($active == true) $sel = " selected=\"true\"";
				else $sel = "";
				$str .= $indent . "      <option value=\"".xmlenc($value)."\"".$sel.">".xmlenc($value)."</option>\n";
			}
		}
		$str .= $indent . "    </select>\n";
		$str .= $indent . "  </div>\n";
		$str .= $indent . "</div>\n";
		return $str;
	}
}

class Form {
	public $widgets = array();
	public $action;
	public $hasFileUpload = false;
	public $hasListEditor = false;

	public function addWidget($widget)
	{
		if(get_class($widget) == "FileUpload") $this->hasFileUpload = true;
		if(get_class($widget) == "ListEditor") $this->hasListEditor = true;
		array_push($this->widgets, $widget);	
	}

	public function render($indent = "")
	{
		global $m, $s;
		$str  = $indent . "<form method=\"post\"\n";
		if($this->hasFileUpload) $str .= $indent . "      enctype=\"multipart/form-data\"\n";
		if($this->hasListEditor) $str .= $indent . "      onsubmit=\"selectAll();\"\n";
		$str .= $indent . "      action=\"?page=admin&amp;m=".$m."&amp;s=".$s."&amp;a=".$this->action."\">\n";
		foreach($this->widgets as $widget) {
			$str .= $widget->render($indent . "  ");
		}
		$str .= $indent . "</form>\n";
		echo $str;
	}

	public function Form($action)
	{
		$this->action = $action;
	}
}

?>