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