-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJMForm.php
More file actions
152 lines (144 loc) · 4.74 KB
/
SJMForm.php
File metadata and controls
152 lines (144 loc) · 4.74 KB
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
<?php
class SJMForm {
public $parser = null;
private $tree = array();
public function __construct($fml = null) {
// support legacy usage
if ($fml !== null) {
$this->parse($fml);
}
}
public function parse($fml) {
if ($this->parser === null) {
require_once('SJMForm.parser.php');
$this->parser = new SJMFormParser();
// require_once('SJMForm.regex.php');
// $this->parser = new SJMFormRegex();
}
$ast = $this->parser->parse($fml);
$this->tree = $ast;
}
// compile form
public function __toString() {
$out = '';
foreach ($this->tree as $node) {
$method = 'produce_'.$node->type;
if(!method_exists($this, $method)) continue;
$out .= $this->$method($node);
}
return $out;
}
// preprocess data received from the form
public function preprocess($data) {
$out = array();
foreach ($this->tree as $node) {
$name = $node->name;
if ($node->type=='checkbox') {
// checkboxes don't send anything for unchecked boxes
foreach ($node->childNodes as $child) {
$key = "{$name}_{$child->name}";
$out[$key] = isset($data[$key]);
}
} else {
$value = isset($data[$name]) ? $data[$name] : '';
$out[$name] = $value;
}
}
return $out;
}
// set form values
public function set($data) {
foreach ($this->tree as $node) {
$type = $node->type;
$name = $node->name;
// special handling for checkboxes
if ($type=='checkbox') {
foreach ($node->childNodes as $child) {
$key = $name.'_'.$child->name;
if (!isset($data[$key])) continue;
$child->args[1] = $data[$key];
}
continue;
}
// do we have a value for the current node?
if (!isset($data[$name])) continue;
$value = $data[$name];
// handle radio buttons and select
if ($type=='radio' || $type=='select') {
foreach ($node->childNodes as $child) {
$child->args[1] = $value == $child->name;
}
continue;
}
// hidden fields have no display name argument
if ($type=='hidden') {
$node->args[0] = $value;
continue;
}
// all other types have their value as second argument
$node->args[1] = $value;
}
}
// html generators
private function produce_hidden($e) {
if (!isset($e->args[0])) $e->args[0] = '';
return "<input type=\"hidden\" name=\"$e->name\" value=\"".htmlentities($e->args[0])."\" id=\"$e->name\">\n";
}
private function produce_text($e) {
if (!isset($e->args[1])) $e->args[1] = '';
$out = "<dt><label for=\"$e->name\">".htmlentities($e->args[0])."</label></dt>\n";
$out .= "<dd><input type=\"text\" name=\"$e->name\" value=\"".htmlentities($e->args[1])."\" size=\"30\" id=\"$e->name\"></dd>\n";
return $out;
}
private function produce_password($e) {
if (!isset($e->args[1])) $e->args[1] = '';
$out = "<dt><label for=\"$e->name\">".htmlentities($e->args[0])."</label></dt>\n";
$out .= "<dd><input type=\"password\" name=\"$e->name\" value=\"".htmlentities($e->args[1])."\" size=\"30\" id=\"$e->name\"></dd>\n";
return $out;
}
private function produce_textarea($e) {
if (!isset($e->args[1])) $e->args[1] = '';
$out = "<dt><label for=\"$e->name\">".htmlentities($e->args[0])."</label></dt>\n";
$out .= "<dd><textarea name=\"$e->name\" rows=\"6\" cols=\"40\" id=\"$e->name\">".htmlentities($e->args[1])."</textarea></dd>\n";
return $out;
}
private function produce_radio($e) {
$out = "<dt><label>".htmlentities($e->args[0])."</label></dt>\n";
foreach ($e->childNodes as $c) {
if (!isset($c->args[1])) $c->args[1] = false;
$checked = ($c->args[1]) ? ' checked' : '';
$id = $e->name.'_'.$c->name;
$out .= "<dd>\n";
$out .= "\t<input type=\"radio\" name=\"$e->name\" value=\"$c->name\" id=\"$id\"$checked>\n";
$out .= "\t<label for=\"$id\">".htmlentities($c->args[0])."</label>\n";
$out .= "</dd>\n";
}
return $out;
}
private function produce_checkbox($e) {
$out = "<dt><label>".htmlentities($e->args[0])."</label></dt>";
foreach ($e->childNodes as $c) {
if (!isset($c->args[1])) $c->args[1] = false;
$checked = ($c->args[1]) ? ' checked' : '';
$id = $e->name.'_'.$c->name;
$out .= "<dd>\n";
$out .= "\t<input type=\"checkbox\" name=\"$id\" value=\"1\" id=\"$id\"$checked>\n";
$out .= "\t<label for=\"$id\">".htmlentities($c->args[0])."</label>\n";
$out .= "</dd>\n";
}
return $out;
}
private function produce_select($e) {
$out = "<dt><label for=\"$e->name\">".htmlentities($e->args[0])."</label></dt>\n";
$out .= "<dd>\n";
$out .= "\t<select size=\"1\" name=\"$e->name\" id=\"$e->name\">\n";
foreach ($e->childNodes as $c) {
if (!isset($c->args[1])) $c->args[1] = false;
$selected = ($c->args[1]) ? ' selected=\"selected\"' : '';
$out .= "\t<option value=\"$c->name\"$selected>".htmlentities($c->args[0])."</option>\n";
}
$out .= "</select>\n";
$out .= "</dd>\n";
return $out;
}
}