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