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