A bit more refactoring.
[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   // TODO: refactoring ongoing down from here.
40
41         var $mId           = "";
42     var $error;
43         var $debugFunction;
44         var $mElements     = array();
45         var $mRequest;
46     
47     function __construct($formid) {
48         $this->name = $formid;
49     }
50     
51     function setRequest(&$request) {
52         $this->mRequest = &$request;
53     }
54
55     function &getElement($name) {
56         return $this->mElements[$name];
57     }
58     
59     function &getElements() {
60         return $this->mElements;
61     }
62     
63         //// FORM element
64         // action
65         // method - GET, POST
66         // enctype - enctype="multipart/form-data"
67         // name
68         // onsubmit
69         // onreset
70         function setName($value) { $this->name = $value; }
71     function getName() { return $this->name; }
72     
73     function setId($value) { $this->mId = $value; }
74     function getId() { return $this->mId; }
75     
76     function isSubmit() {
77         if (!isset($this->mRequest)) return false;
78         $result = false;
79             foreach ($this->mElements as $el) {
80                 if (strtolower(get_class($el))=="submit") {
81                     $name = $el->getName();
82                     $value = $this->mRequest->getAttribute($name);
83                     if($value) {
84                        $result = true; 
85                     }
86                 }
87             }
88         return $result;
89     }
90         
91         function OutputError($error,$scope="")
92         {
93                 $this->error=(strcmp($scope,"") ? $scope.": ".$error : $error);
94                 if(strcmp($function=$this->debugFunction,"")
95                 && strcmp($this->error,""))
96                         $function($this->error);
97                 return($this->error);
98         }
99         
100         //// INPUT element
101         // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
102         // name
103         // value
104         // checked - for type radio and checkbox
105         // size - width pixels or chars
106         // maxlength
107         // src - for type image
108         // tabindex - support  A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
109         // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
110         // onfocus
111         // onblur
112         // onselect -  INPUT and TEXTAREA
113         // onchange
114         function addInput($arguments) {
115                 if(strcmp(gettype($arguments),"array"))
116                         $this->OutputError("arguments must be array","AddInput");
117                         
118                 if(!isset($arguments["type"]) || !strcmp($arguments["type"],""))
119                         return($this->OutputError("Type not defined","AddInput"));
120                         
121                 if(!isset($arguments["name"]) || !strcmp($arguments["name"],""))
122                         return($this->OutputError("Name of element not defined","AddInput"));
123                         
124                 if (isset($this->mElements[$arguments["name"]]))
125                     return($this->OutputError("it was specified '".$arguments["name"]."' name of an already defined input","AddInput"));
126                         
127                 switch($arguments["type"]) {
128                     
129                         case "textfield":
130                         case "text":
131                             import('form.TextField');
132                             $el = new TextField($arguments["name"]);
133                             $el->setMaxLength(@$arguments["maxlength"]);
134                             if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
135                             break;
136                             
137                         case "datefield":
138                             import('form.DateField');
139                             $el = new DateField($arguments["name"]);
140                                 $el->setMaxLength("10");
141                             break;
142                             
143                         case "floatfield":
144                             import('form.FloatField');
145                             $el = new FloatField($arguments["name"]);
146                             if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
147                             break;
148                             
149                         case "textarea":
150                             import('form.TextArea');
151                             $el = new TextArea($arguments["name"]);
152                             $el->setColumns(@$arguments["cols"]);
153                             $el->setRows(@$arguments["rows"]);
154                             if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
155                             break;
156                             
157                         case "checkbox":
158                             import('form.Checkbox');
159                             $el = new Checkbox($arguments["name"]);
160                             if (@$arguments["checked"]) $el->setChecked(true);
161                             $el->setData(@$arguments["data"]);
162                             break;
163                             
164                         case "checkboxgroup":
165                             import('form.CheckboxGroup');
166                             $el = new CheckboxGroup($arguments["name"]);
167                             if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
168                             if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
169                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
170                             $el->setData(@$arguments["data"]);
171                             break;
172                             
173                         case "combobox":
174                             import('form.Combobox');
175                             $el = new Combobox($arguments["name"]);
176                             $el->setData(@$arguments["data"]);
177                             $el->setDataDefault(@$arguments["empty"]);
178                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
179                             break;
180                             
181                         case "hidden":
182                             import('form.Hidden');
183                             $el = new Hidden($arguments["name"]);
184                             break;
185                          
186                         case "submit":
187                             import('form.Submit');
188                             $el = new Submit($arguments["name"]);
189                             break;
190                             
191                         case "calendar":
192                             import('form.Calendar');
193                             $el = new Calendar($arguments["name"]);
194                             $el->setHighlight(@$arguments["highlight"]);
195                             break;  
196                             
197                         case "table":
198                             import('form.Table');
199                             $el = new Table($arguments["name"]);
200                             $el->setData(@$arguments["data"]);
201                             $el->setWidth(@$arguments["width"]);
202                             break;
203                             
204                         case "upload":
205                             import('form.UploadFile');
206                             $el = new UploadFile($arguments["name"]);
207                             if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
208                             break;
209                               
210                         default:
211                                 return($this->OutputError("Type not found for input element","AddInput"));
212                 }
213                 if ($el!=null) {
214                         $el->setFormName($this->name);
215                         if (isset($arguments["id"])) $el->setId($arguments["id"]);
216                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
217                         if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
218                         if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
219                         
220                         if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
221                         if (isset($arguments["size"])) $el->setSize($arguments["size"]);
222                         
223                         if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
224                         if (isset($arguments["value"])) $el->setValue($arguments["value"]);
225                         
226                         if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
227                         if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
228                         
229                         $this->mElements[$arguments["name"]] = &$el;
230                 }
231         }
232         
233         function addInputElement(&$el) {
234                 if ($el && is_object($el)) {
235                         if (!$el->getName())
236                             return($this->OutputError("no name in element","addInputElement"));
237                             
238                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
239                 
240                         $el->setFormName($this->name);
241                         $this->mElements[$el->getName()] = &$el;
242                 }
243         }
244         
245         
246         function toStringOpenTag() {
247         $html = "<form name=\"$this->name\"";
248         
249         if ($this->mId!="") 
250             $html .= " id=\"$this->mId\"";
251             
252         $html .= ' method="post"';
253         
254         // Add enctype for file upload forms.
255         foreach ($this->mElements as $elname=>$el) {
256             if (strtolower(get_class($this->mElements[$elname])) == 'uploadfile') {
257                 $html .= ' enctype="multipart/form-data"';
258                 break;
259             }
260         }
261
262         $html .= ">";
263         return $html;
264     }
265     
266     function toStringCloseTag() {
267         $html = "\n";
268         foreach ($this->mElements as $elname=>$el) {
269             if (strtolower(get_class($this->mElements[$elname]))=="hidden") {
270                 $html .= $this->mElements[$elname]->toStringControl()."\n";
271             }
272         }
273         $html .= "</form>";
274         return $html;
275     }
276         
277         function toArray() {
278         $vars = array();
279         $vars['open'] = $this->toStringOpenTag();
280         $vars['close'] = $this->toStringCloseTag();
281         
282         foreach ($this->mElements as $elname=>$el) {
283             if (is_object($this->mElements[$elname])) 
284                 $vars[$elname] = $this->mElements[$elname]->toArray();
285         }
286 //print_r($vars);
287         return $vars;
288     }
289     
290     function getValueByElement($elname) {
291         return $this->mElements[$elname]->getValue();
292     }
293     
294     function setValueByElement($elname, $value) {
295         if (isset($this->mElements[$elname])) {
296                 $this->mElements[$elname]->setValue($value);
297         }
298     }
299 }