2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // +----------------------------------------------------------------------+
31 // | Class generates elements of specification HTML 4.01
32 // | http://www.w3.org/TR/1999/REC-html401-19991224
34 // +----------------------------------------------------------------------+
39 var $mMethod = "post";
44 var $mElements = array();
48 function Form($formid) {
49 $this->formName = $formid;
52 function setRequest(&$request) {
53 $this->mRequest = &$request;
56 /* function setFormBean(&$bean) {
57 $this->mFormBean = &$bean;
60 function &getElement($name) {
61 return $this->mElements[$name];
64 function &getElements() {
65 return $this->mElements;
71 // enctype - enctype="multipart/form-data"
75 function setName($value) { $this->formName = $value; }
76 function getName() { return $this->formName; }
78 function setId($value) { $this->mId = $value; }
79 function getId() { return $this->mId; }
81 function setAction($value) { $this->mAction = $value; }
82 function getAction() { return $this->mAction; }
84 function setMethod($value) { $this->mMethod = $value; }
85 function getMethod() { return $this->mMethod; }
87 function setEnctype($value) { $this->mEnctype = $value; }
88 function getEnctype() { return $this->mEnctype; }
91 if (!isset($this->mRequest)) return false;
93 foreach ($this->mElements as $el) {
94 if (strtolower(get_class($el))=="submit") {
95 $name = $el->getName();
96 $value = $this->mRequest->getAttribute($name);
105 function OutputError($error,$scope="")
107 $this->error=(strcmp($scope,"") ? $scope.": ".$error : $error);
108 if(strcmp($function=$this->debugFunction,"")
109 && strcmp($this->error,""))
110 $function($this->error);
111 return($this->error);
115 // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
118 // checked - for type radio and checkbox
119 // size - width pixels or chars
121 // src - for type image
122 // tabindex - support A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
123 // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
126 // onselect - INPUT and TEXTAREA
128 function addInput($arguments) {
129 if(strcmp(gettype($arguments),"array"))
130 $this->OutputError("arguments must be array","AddInput");
132 if(!isset($arguments["type"]) || !strcmp($arguments["type"],""))
133 return($this->OutputError("Type not defined","AddInput"));
135 if(!isset($arguments["name"]) || !strcmp($arguments["name"],""))
136 return($this->OutputError("Name of element not defined","AddInput"));
138 if (isset($this->mElements[$arguments["name"]]))
139 return($this->OutputError("it was specified '".$arguments["name"]."' name of an already defined input","AddInput"));
141 switch($arguments["type"]) {
145 import('form.TextField');
146 $el = new TextField($arguments["name"]);
147 $el->setMaxLength(@$arguments["maxlength"]);
148 if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
152 import('form.DateField');
153 $el = new DateField($arguments["name"]);
154 $el->setMaxLength("10");
158 import('form.FloatField');
159 $el = new FloatField($arguments["name"]);
160 if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
164 import('form.TextArea');
165 $el = new TextArea($arguments["name"]);
166 $el->setColumns(@$arguments["cols"]);
167 $el->setRows(@$arguments["rows"]);
168 if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
172 import('form.Checkbox');
173 $el = new Checkbox($arguments["name"]);
174 if (@$arguments["checked"]) $el->setChecked(true);
175 $el->setData(@$arguments["data"]);
178 case "checkboxgroup":
179 import('form.CheckboxGroup');
180 $el = new CheckboxGroup($arguments["name"]);
181 if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
182 if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
183 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
184 $el->setData(@$arguments["data"]);
188 import('form.Combobox');
189 $el = new Combobox($arguments["name"]);
190 $el->setData(@$arguments["data"]);
191 $el->setDataDefault(@$arguments["empty"]);
192 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
196 import('form.Hidden');
197 $el = new Hidden($arguments["name"]);
201 import('form.Submit');
202 $el = new Submit($arguments["name"]);
206 import('form.Calendar');
207 $el = new Calendar($arguments["name"]);
208 $el->setHighlight(@$arguments["highlight"]);
212 import('form.Table');
213 $el = new Table($arguments["name"]);
214 $el->setData(@$arguments["data"]);
215 $el->setWidth(@$arguments["width"]);
219 import('form.UploadFile');
220 $el = new UploadFile($arguments["name"]);
221 if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
225 return($this->OutputError("Type not found for input element","AddInput"));
228 $el->setFormName($this->formName);
229 if (isset($arguments["id"])) $el->setId($arguments["id"]);
230 if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
231 if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
232 if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
234 if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
235 if (isset($arguments["size"])) $el->setSize($arguments["size"]);
237 if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
238 if (isset($arguments["value"])) $el->setValue($arguments["value"]);
240 if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
241 if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
243 $this->mElements[$arguments["name"]] = &$el;
247 function addInputElement(&$el) {
248 if ($el && is_object($el)) {
250 return($this->OutputError("no name in element","addInputElement"));
252 if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
254 $el->setFormName($this->formName);
255 $this->mElements[$el->getName()] = &$el;
260 function toStringOpenTag() {
261 $html = "<form name=\"$this->formName\"";
264 $html .= " id=\"$this->mId\"";
266 if ($this->mAction!="")
267 $html .= " action=\"$this->mAction\"";
269 if ($this->mMethod!="")
270 $html .= " method=\"$this->mMethod\"";
273 foreach ($this->mElements as $elname=>$el) {
274 if (strtolower(get_class($this->mElements[$elname]))=="uploadfile") {
275 $this->mEnctype = "multipart/form-data";
279 if ($this->mEnctype!="")
280 $html .= " enctype=\"$this->mEnctype\"";
286 function toStringCloseTag() {
288 foreach ($this->mElements as $elname=>$el) {
289 if (strtolower(get_class($this->mElements[$elname]))=="hidden") {
290 $html .= $this->mElements[$elname]->toStringControl()."\n";
299 $vars['open'] = $this->toStringOpenTag();
300 $vars['close'] = $this->toStringCloseTag();
302 foreach ($this->mElements as $elname=>$el) {
303 if (is_object($this->mElements[$elname]))
304 $vars[$elname] = $this->mElements[$elname]->toArray();
310 function getValueByElement($elname) {
311 return $this->mElements[$elname]->getValue();
314 function setValueByElement($elname, $value) {
315 if (isset($this->mElements[$elname])) {
316 $this->mElements[$elname]->setValue($value);