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 // TODO: refactoring ongoing down from here.
43 var $mElements = array();
46 function __construct($formName) {
47 $this->name = $formName;
50 function setRequest(&$request) {
51 $this->mRequest = &$request;
54 function &getElement($name) {
55 return $this->mElements[$name];
58 function &getElements() {
59 return $this->mElements;
65 // enctype - enctype="multipart/form-data"
69 function getName() { return $this->name; }
72 if (!isset($this->mRequest)) return false;
74 foreach ($this->mElements as $el) {
75 if (strtolower(get_class($el))=="submit") {
76 $name = $el->getName();
77 $value = $this->mRequest->getAttribute($name);
86 function OutputError($error,$scope="")
88 $this->error=(strcmp($scope,"") ? $scope.": ".$error : $error);
89 if(strcmp($function=$this->debugFunction,"")
90 && strcmp($this->error,""))
91 $function($this->error);
96 // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
99 // checked - for type radio and checkbox
100 // size - width pixels or chars
102 // src - for type image
103 // tabindex - support A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
104 // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
107 // onselect - INPUT and TEXTAREA
109 function addInput($arguments) {
110 if(strcmp(gettype($arguments),"array"))
111 $this->OutputError("arguments must be array","AddInput");
113 if(!isset($arguments["type"]) || !strcmp($arguments["type"],""))
114 return($this->OutputError("Type not defined","AddInput"));
116 if(!isset($arguments["name"]) || !strcmp($arguments["name"],""))
117 return($this->OutputError("Name of element not defined","AddInput"));
119 if (isset($this->mElements[$arguments["name"]]))
120 return($this->OutputError("it was specified '".$arguments["name"]."' name of an already defined input","AddInput"));
122 switch($arguments["type"]) {
126 import('form.TextField');
127 $el = new TextField($arguments["name"]);
128 $el->setMaxLength(@$arguments["maxlength"]);
129 if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
133 import('form.DateField');
134 $el = new DateField($arguments["name"]);
135 $el->setMaxLength("10");
139 import('form.FloatField');
140 $el = new FloatField($arguments["name"]);
141 if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
145 import('form.TextArea');
146 $el = new TextArea($arguments["name"]);
147 $el->setColumns(@$arguments["cols"]);
148 $el->setRows(@$arguments["rows"]);
149 if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
153 import('form.Checkbox');
154 $el = new Checkbox($arguments["name"]);
155 if (@$arguments["checked"]) $el->setChecked(true);
156 $el->setData(@$arguments["data"]);
159 case "checkboxgroup":
160 import('form.CheckboxGroup');
161 $el = new CheckboxGroup($arguments["name"]);
162 if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
163 if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
164 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
165 $el->setData(@$arguments["data"]);
169 import('form.Combobox');
170 $el = new Combobox($arguments["name"]);
171 $el->setData(@$arguments["data"]);
172 $el->setDataDefault(@$arguments["empty"]);
173 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
177 import('form.Hidden');
178 $el = new Hidden($arguments["name"]);
182 import('form.Submit');
183 $el = new Submit($arguments["name"]);
187 import('form.Calendar');
188 $el = new Calendar($arguments["name"]);
189 $el->setHighlight(@$arguments["highlight"]);
193 import('form.Table');
194 $el = new Table($arguments["name"]);
195 $el->setData(@$arguments["data"]);
196 $el->setWidth(@$arguments["width"]);
200 import('form.UploadFile');
201 $el = new UploadFile($arguments["name"]);
202 if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
206 return($this->OutputError("Type not found for input element","AddInput"));
209 $el->setFormName($this->name);
210 if (isset($arguments["id"])) $el->setId($arguments["id"]);
211 if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
212 if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
213 if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
215 if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
216 if (isset($arguments["size"])) $el->setSize($arguments["size"]);
218 if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
219 if (isset($arguments["value"])) $el->setValue($arguments["value"]);
221 if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
222 if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
224 $this->mElements[$arguments["name"]] = &$el;
228 function addInputElement(&$el) {
229 if ($el && is_object($el)) {
231 return($this->OutputError("no name in element","addInputElement"));
233 if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
235 $el->setFormName($this->name);
236 $this->mElements[$el->getName()] = &$el;
241 function toStringOpenTag() {
242 $html = "<form name=\"$this->name\"";
244 $html .= ' method="post"';
246 // Add enctype for file upload forms.
247 foreach ($this->mElements as $elname=>$el) {
248 if (strtolower(get_class($this->mElements[$elname])) == 'uploadfile') {
249 $html .= ' enctype="multipart/form-data"';
258 function toStringCloseTag() {
260 foreach ($this->mElements as $elname=>$el) {
261 if (strtolower(get_class($this->mElements[$elname]))=="hidden") {
262 $html .= $this->mElements[$elname]->toStringControl()."\n";
271 $vars['open'] = $this->toStringOpenTag();
272 $vars['close'] = $this->toStringCloseTag();
274 foreach ($this->mElements as $elname=>$el) {
275 if (is_object($this->mElements[$elname]))
276 $vars[$elname] = $this->mElements[$elname]->toArray();
282 function getValueByElement($elname) {
283 return $this->mElements[$elname]->getValue();
286 function setValueByElement($elname, $value) {
287 if (isset($this->mElements[$elname])) {
288 $this->mElements[$elname]->setValue($value);