Refactoring - removed reference return types from a couple of functions to keep thing...
[timetracker.git] / WEB-INF / lib / form / Form.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // +----------------------------------------------------------------------+
30 // |
31 // | Class generates elements of specification HTML 4.01
32 // | http://www.w3.org/TR/1999/REC-html401-19991224
33 // |
34 // +----------------------------------------------------------------------+
35
36 class Form {
37
38   var $name = '';
39   var $elements = array();
40
41   function __construct($formName) {
42     $this->name = $formName;
43   }
44
45   function getElement($name) {
46     return $this->elements[$name];
47   }
48
49   function getElements() {
50     return $this->elements;
51   }
52
53   function getName() { return $this->name; }
54     
55   // TODO: refactoring ongoing down from here.
56
57
58
59         //// INPUT element
60         // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
61         // name
62         // value
63         // checked - for type radio and checkbox
64         // size - width pixels or chars
65         // maxlength
66         // src - for type image
67         // tabindex - support  A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
68         // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
69         // onfocus
70         // onblur
71         // onselect -  INPUT and TEXTAREA
72         // onchange
73         function addInput($arguments) {
74                 switch($arguments["type"]) {
75                     
76                         case "textfield":
77                         case "text":
78                             import('form.TextField');
79                             $el = new TextField($arguments["name"]);
80                             $el->setMaxLength(@$arguments["maxlength"]);
81                             if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
82                             break;
83                             
84                         case "datefield":
85                             import('form.DateField');
86                             $el = new DateField($arguments["name"]);
87                                 $el->setMaxLength("10");
88                             break;
89                             
90                         case "floatfield":
91                             import('form.FloatField');
92                             $el = new FloatField($arguments["name"]);
93                             if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
94                             break;
95                             
96                         case "textarea":
97                             import('form.TextArea');
98                             $el = new TextArea($arguments["name"]);
99                             $el->setColumns(@$arguments["cols"]);
100                             $el->setRows(@$arguments["rows"]);
101                             if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
102                             break;
103                             
104                         case "checkbox":
105                             import('form.Checkbox');
106                             $el = new Checkbox($arguments["name"]);
107                             if (@$arguments["checked"]) $el->setChecked(true);
108                             $el->setData(@$arguments["data"]);
109                             break;
110                             
111                         case "checkboxgroup":
112                             import('form.CheckboxGroup');
113                             $el = new CheckboxGroup($arguments["name"]);
114                             if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
115                             if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
116                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
117                             $el->setData(@$arguments["data"]);
118                             break;
119                             
120                         case "combobox":
121                             import('form.Combobox');
122                             $el = new Combobox($arguments["name"]);
123                             $el->setData(@$arguments["data"]);
124                             $el->setDataDefault(@$arguments["empty"]);
125                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
126                             break;
127                             
128                         case "hidden":
129                             import('form.Hidden');
130                             $el = new Hidden($arguments["name"]);
131                             break;
132                          
133                         case "submit":
134                             import('form.Submit');
135                             $el = new Submit($arguments["name"]);
136                             break;
137                             
138                         case "calendar":
139                             import('form.Calendar');
140                             $el = new Calendar($arguments["name"]);
141                             $el->setHighlight(@$arguments["highlight"]);
142                             break;  
143                             
144                         case "table":
145                             import('form.Table');
146                             $el = new Table($arguments["name"]);
147                             $el->setData(@$arguments["data"]);
148                             $el->setWidth(@$arguments["width"]);
149                             break;
150                             
151                         case "upload":
152                             import('form.UploadFile');
153                             $el = new UploadFile($arguments["name"]);
154                             if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
155                             break;
156                 }
157                 if ($el!=null) {
158                         $el->setFormName($this->name);
159                         if (isset($arguments["id"])) $el->setId($arguments["id"]);
160                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
161                         if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
162                         if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
163                         
164                         if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
165                         if (isset($arguments["size"])) $el->setSize($arguments["size"]);
166                         
167                         if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
168                         if (isset($arguments["value"])) $el->setValue($arguments["value"]);
169                         
170                         if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
171                         if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
172                         
173                         $this->elements[$arguments["name"]] = &$el;
174                 }
175         }
176         
177         function addInputElement(&$el) {
178                 if ($el && is_object($el)) {
179                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
180                 
181                         $el->setFormName($this->name);
182                         $this->elements[$el->getName()] = &$el;
183                 }
184         }
185         
186         
187         function toStringOpenTag() {
188         $html = "<form name=\"$this->name\"";
189         
190         $html .= ' method="post"';
191         
192         // Add enctype for file upload forms.
193         foreach ($this->elements as $elname=>$el) {
194             if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
195                 $html .= ' enctype="multipart/form-data"';
196                 break;
197             }
198         }
199
200         $html .= ">";
201         return $html;
202     }
203     
204     function toStringCloseTag() {
205         $html = "\n";
206         foreach ($this->elements as $elname=>$el) {
207             if (strtolower(get_class($this->elements[$elname]))=="hidden") {
208                 $html .= $this->elements[$elname]->toStringControl()."\n";
209             }
210         }
211         $html .= "</form>";
212         return $html;
213     }
214         
215         function toArray() {
216         $vars = array();
217         $vars['open'] = $this->toStringOpenTag();
218         $vars['close'] = $this->toStringCloseTag();
219         
220         foreach ($this->elements as $elname=>$el) {
221             if (is_object($this->elements[$elname])) 
222                 $vars[$elname] = $this->elements[$elname]->toArray();
223         }
224 //print_r($vars);
225         return $vars;
226     }
227     
228     function getValueByElement($elname) {
229         return $this->elements[$elname]->getValue();
230     }
231     
232     function setValueByElement($elname, $value) {
233         if (isset($this->elements[$elname])) {
234                 $this->elements[$elname]->setValue($value);
235         }
236     }
237 }