A bit more refactoring in forms to make them leaner.
[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   // TODO: refactoring ongoing down from here.
46
47     function &getElement($name) {
48         return $this->elements[$name];
49     }
50     
51     function &getElements() {
52         return $this->elements;
53     }
54     
55         //// FORM element
56         // action
57         // method - GET, POST
58         // enctype - enctype="multipart/form-data"
59         // name
60         // onsubmit
61         // onreset
62     function getName() { return $this->name; }
63
64         //// INPUT element
65         // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
66         // name
67         // value
68         // checked - for type radio and checkbox
69         // size - width pixels or chars
70         // maxlength
71         // src - for type image
72         // tabindex - support  A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
73         // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
74         // onfocus
75         // onblur
76         // onselect -  INPUT and TEXTAREA
77         // onchange
78         function addInput($arguments) {
79                 switch($arguments["type"]) {
80                     
81                         case "textfield":
82                         case "text":
83                             import('form.TextField');
84                             $el = new TextField($arguments["name"]);
85                             $el->setMaxLength(@$arguments["maxlength"]);
86                             if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
87                             break;
88                             
89                         case "datefield":
90                             import('form.DateField');
91                             $el = new DateField($arguments["name"]);
92                                 $el->setMaxLength("10");
93                             break;
94                             
95                         case "floatfield":
96                             import('form.FloatField');
97                             $el = new FloatField($arguments["name"]);
98                             if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
99                             break;
100                             
101                         case "textarea":
102                             import('form.TextArea');
103                             $el = new TextArea($arguments["name"]);
104                             $el->setColumns(@$arguments["cols"]);
105                             $el->setRows(@$arguments["rows"]);
106                             if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
107                             break;
108                             
109                         case "checkbox":
110                             import('form.Checkbox');
111                             $el = new Checkbox($arguments["name"]);
112                             if (@$arguments["checked"]) $el->setChecked(true);
113                             $el->setData(@$arguments["data"]);
114                             break;
115                             
116                         case "checkboxgroup":
117                             import('form.CheckboxGroup');
118                             $el = new CheckboxGroup($arguments["name"]);
119                             if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
120                             if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
121                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
122                             $el->setData(@$arguments["data"]);
123                             break;
124                             
125                         case "combobox":
126                             import('form.Combobox');
127                             $el = new Combobox($arguments["name"]);
128                             $el->setData(@$arguments["data"]);
129                             $el->setDataDefault(@$arguments["empty"]);
130                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
131                             break;
132                             
133                         case "hidden":
134                             import('form.Hidden');
135                             $el = new Hidden($arguments["name"]);
136                             break;
137                          
138                         case "submit":
139                             import('form.Submit');
140                             $el = new Submit($arguments["name"]);
141                             break;
142                             
143                         case "calendar":
144                             import('form.Calendar');
145                             $el = new Calendar($arguments["name"]);
146                             $el->setHighlight(@$arguments["highlight"]);
147                             break;  
148                             
149                         case "table":
150                             import('form.Table');
151                             $el = new Table($arguments["name"]);
152                             $el->setData(@$arguments["data"]);
153                             $el->setWidth(@$arguments["width"]);
154                             break;
155                             
156                         case "upload":
157                             import('form.UploadFile');
158                             $el = new UploadFile($arguments["name"]);
159                             if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
160                             break;
161                 }
162                 if ($el!=null) {
163                         $el->setFormName($this->name);
164                         if (isset($arguments["id"])) $el->setId($arguments["id"]);
165                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
166                         if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
167                         if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
168                         
169                         if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
170                         if (isset($arguments["size"])) $el->setSize($arguments["size"]);
171                         
172                         if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
173                         if (isset($arguments["value"])) $el->setValue($arguments["value"]);
174                         
175                         if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
176                         if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
177                         
178                         $this->elements[$arguments["name"]] = &$el;
179                 }
180         }
181         
182         function addInputElement(&$el) {
183                 if ($el && is_object($el)) {
184                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
185                 
186                         $el->setFormName($this->name);
187                         $this->elements[$el->getName()] = &$el;
188                 }
189         }
190         
191         
192         function toStringOpenTag() {
193         $html = "<form name=\"$this->name\"";
194         
195         $html .= ' method="post"';
196         
197         // Add enctype for file upload forms.
198         foreach ($this->elements as $elname=>$el) {
199             if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
200                 $html .= ' enctype="multipart/form-data"';
201                 break;
202             }
203         }
204
205         $html .= ">";
206         return $html;
207     }
208     
209     function toStringCloseTag() {
210         $html = "\n";
211         foreach ($this->elements as $elname=>$el) {
212             if (strtolower(get_class($this->elements[$elname]))=="hidden") {
213                 $html .= $this->elements[$elname]->toStringControl()."\n";
214             }
215         }
216         $html .= "</form>";
217         return $html;
218     }
219         
220         function toArray() {
221         $vars = array();
222         $vars['open'] = $this->toStringOpenTag();
223         $vars['close'] = $this->toStringCloseTag();
224         
225         foreach ($this->elements as $elname=>$el) {
226             if (is_object($this->elements[$elname])) 
227                 $vars[$elname] = $this->elements[$elname]->toArray();
228         }
229 //print_r($vars);
230         return $vars;
231     }
232     
233     function getValueByElement($elname) {
234         return $this->elements[$elname]->getValue();
235     }
236     
237     function setValueByElement($elname, $value) {
238         if (isset($this->elements[$elname])) {
239                 $this->elements[$elname]->setValue($value);
240         }
241     }
242 }