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