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