-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathForm.class.php
More file actions
210 lines (175 loc) · 6.73 KB
/
Form.class.php
File metadata and controls
210 lines (175 loc) · 6.73 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
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
<?php
/* Copyright (c) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
// Form class is a container for HTML forms we use in the application.
// It contains an array of $elements - which are individual input controls
// belonging to a form.
class Form {
var $name = ''; // Form name.
var $elements = array(); // An array of input controls in form.
function __construct($name) {
$this->name = $name;
}
function getElement($name) {
return $this->elements[$name];
}
function getElements() {
return $this->elements;
}
function getName() { return $this->name; }
// addInput - adds an input object to the form.
function addInput($params) {
switch($params['type']) {
case 'text':
import('form.TextField');
$el = new TextField($params['name']);
if (isset($params['class'])) $el->setCssClass($params['class']);
if (isset($params['minlength'])) $el->setMinLength($params['minlength']);
if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
if (isset($params['placeholder'])) $el->setPLaceholder($params['placeholder']);
break;
case 'password':
import('form.PasswordField');
$el = new PasswordField($params['name']);
if (isset($params['class'])) $el->setCssClass($params['class']);
if (isset($params['minlength'])) $el->setMinLength($params['minlength']);
if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
break;
case 'datefield':
import('form.DateField');
$el = new DateField($params['name']);
$el->setMaxLength('10');
break;
case 'floatfield':
import('form.FloatField');
$el = new FloatField($params['name']);
if (isset($params['format'])) $el->setFormat($params['format']);
break;
case 'textarea':
import('form.TextArea');
$el = new TextArea($params['name']);
if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
break;
case 'checkbox':
import('form.Checkbox');
$el = new Checkbox($params['name']);
break;
case 'hidden':
import('form.Hidden');
$el = new Hidden($params['name']);
break;
case 'submit':
import('form.Submit');
$el = new Submit($params['name']);
break;
case 'upload':
import('form.UploadFile');
$el = new UploadFile($params['name']);
if (isset($params['maxsize'])) $el->setMaxSize($params['maxsize']);
break;
// TODO: refactoring ongoing down from here.
case "checkboxgroup":
import('form.CheckboxGroup');
$el = new CheckboxGroup($params["name"]);
if (isset($params["layout"])) $el->setLayout($params["layout"]);
if (isset($params["groupin"])) $el->setGroupIn($params["groupin"]);
if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
$el->setData(@$params["data"]);
break;
case "combobox":
import('form.Combobox');
$el = new Combobox($params["name"]);
$el->setData(@$params["data"]);
$el->setDataDefault(@$params["empty"]);
if (isset($params['class'])) $el->setCssClass($params['class']);
if (isset($params["multiple"])) {
$el->setMultiple($params["multiple"]);
$el->name .= '[]'; // Add brackets to the end of name to get back an array on POST.
}
if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
break;
case "multipleselectcombobox":
import('form.MultipleSelectCombobox');
$multipleSelectComboboxName = $params["name"].'[]';
$el = new MultipleSelectCombobox($multipleSelectComboboxName);
$el->setData(@$params["data"]);
$el->setDataDefault(@$params["empty"]);
if (isset($params['class'])) $el->setCssClass($params['class']);
if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
break;
case "calendar":
import('form.Calendar');
$el = new Calendar($params["name"]);
$el->setHighlight(@$params["highlight"]);
break;
case "table":
import('form.Table');
$el = new Table($params["name"]);
$el->setData(@$params["data"]);
$el->setWidth(@$params["width"]);
break;
}
if ($el!=null) {
$el->setFormName($this->name);
if (isset($params["id"])) $el->setId($params["id"]);
$el->localize();
if (isset($params["enable"])) $el->setEnabled($params["enable"]);
if (isset($params["style"])) $el->setStyle($params["style"]);
if (isset($params["size"])) $el->setSize($params["size"]);
if (isset($params["label"])) $el->setLabel($params["label"]);
if (isset($params["value"])) $el->setValue($params["value"]);
if (isset($params["onchange"])) $el->setOnChange($params["onchange"]);
if (isset($params["onclick"])) $el->setOnClick($params["onclick"]);
$this->elements[$params["name"]] = &$el;
}
}
function addInputElement(&$el) {
if ($el && is_object($el)) {
$el->localize();
$el->setFormName($this->name);
$this->elements[$el->name] = &$el;
}
}
function toStringOpenTag() {
$html = "<form name=\"$this->name\"";
$html .= ' method="post"';
// Add enctype for file upload forms.
foreach ($this->elements as $elname=>$el) {
if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
$html .= ' enctype="multipart/form-data"';
break;
}
}
$html .= ">";
return $html;
}
function toStringCloseTag() {
$html = "\n";
foreach ($this->elements as $elname=>$el) {
if (strtolower(get_class($this->elements[$elname]))=="hidden") {
$html .= $this->elements[$elname]->getHtml()."\n";
}
}
$html .= "</form>";
return $html;
}
function toArray() {
$vars = array();
$vars['open'] = $this->toStringOpenTag();
$vars['close'] = $this->toStringCloseTag();
foreach ($this->elements as $elname=>$el) {
if (is_object($this->elements[$elname]))
$vars[$elname] = $this->elements[$elname]->toArray();
}
//print_r($vars);
return $vars;
}
function getValueByElement($elname) {
return $this->elements[$elname]->getValue();
}
function setValueByElement($elname, $value) {
if (isset($this->elements[$elname])) {
$this->elements[$elname]->setValue($value);
}
}
}