868a3b3c92d4737c204d33b58ae1cc7b6f360884
[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   var $elements = array();
40
41   function __construct($formName) {
42     $this->name = $formName;
43   }
44
45   function getElement($name) {
46     return $this->elements[$name];
47   }
48
49   function getElements() {
50     return $this->elements;
51   }
52
53   function getName() { return $this->name; }
54
55   // addInput - adds an input object to the form.
56   function addInput($arguments) {
57     switch($arguments['type']) {
58       case 'text':
59         import('form.TextField');
60         $el = new TextField($arguments['name']);
61         $el->setMaxLength(@$arguments['maxlength']);
62         if (isset($arguments['aspassword'])) $el->setAsPassword($arguments['aspassword']);
63         break;
64
65       case 'password':
66         import('form.PasswordField');
67         $el = new PasswordField($arguments['name']);
68         $el->setMaxLength(@$arguments['maxlength']);
69         break;
70
71 // TODO: refactoring ongoing down from here.
72 // aspassword - change this name to something better? Perhaps.
73 // Change $arguments to something better too (maybe). $args or $params?
74                         case "datefield":
75                             import('form.DateField');
76                             $el = new DateField($arguments["name"]);
77                                 $el->setMaxLength("10");
78                             break;
79                             
80                         case "floatfield":
81                             import('form.FloatField');
82                             $el = new FloatField($arguments["name"]);
83                             if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
84                             break;
85                             
86                         case "textarea":
87                             import('form.TextArea');
88                             $el = new TextArea($arguments["name"]);
89                             $el->setColumns(@$arguments["cols"]);
90                             $el->setRows(@$arguments["rows"]);
91                             if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
92                             break;
93                             
94                         case "checkbox":
95                             import('form.Checkbox');
96                             $el = new Checkbox($arguments["name"]);
97                             if (@$arguments["checked"]) $el->setChecked(true);
98                             $el->setData(@$arguments["data"]);
99                             break;
100                             
101                         case "checkboxgroup":
102                             import('form.CheckboxGroup');
103                             $el = new CheckboxGroup($arguments["name"]);
104                             if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
105                             if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
106                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
107                             $el->setData(@$arguments["data"]);
108                             break;
109                             
110                         case "combobox":
111                             import('form.Combobox');
112                             $el = new Combobox($arguments["name"]);
113                             $el->setData(@$arguments["data"]);
114                             $el->setDataDefault(@$arguments["empty"]);
115                             if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
116                             break;
117                             
118                         case "hidden":
119                             import('form.Hidden');
120                             $el = new Hidden($arguments["name"]);
121                             break;
122                          
123                         case "submit":
124                             import('form.Submit');
125                             $el = new Submit($arguments["name"]);
126                             break;
127                             
128                         case "calendar":
129                             import('form.Calendar');
130                             $el = new Calendar($arguments["name"]);
131                             $el->setHighlight(@$arguments["highlight"]);
132                             break;  
133                             
134                         case "table":
135                             import('form.Table');
136                             $el = new Table($arguments["name"]);
137                             $el->setData(@$arguments["data"]);
138                             $el->setWidth(@$arguments["width"]);
139                             break;
140                             
141                         case "upload":
142                             import('form.UploadFile');
143                             $el = new UploadFile($arguments["name"]);
144                             if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
145                             break;
146                 }
147                 if ($el!=null) {
148                         $el->setFormName($this->name);
149                         if (isset($arguments["id"])) $el->setId($arguments["id"]);
150                         if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
151                         if (isset($arguments["enable"])) $el->setEnabled($arguments["enable"]);
152                         
153                         if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
154                         if (isset($arguments["size"])) $el->setSize($arguments["size"]);
155                         
156                         if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
157                         if (isset($arguments["value"])) $el->setValue($arguments["value"]);
158                         
159                         if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
160                         if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
161                         
162                         $this->elements[$arguments["name"]] = &$el;
163                 }
164         }
165         
166         function addInputElement(&$el) {
167                 if ($el && is_object($el)) {
168                         if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
169                 
170                         $el->setFormName($this->name);
171                         $this->elements[$el->name] = &$el;
172                 }
173         }
174         
175         
176         function toStringOpenTag() {
177         $html = "<form name=\"$this->name\"";
178         
179         $html .= ' method="post"';
180         
181         // Add enctype for file upload forms.
182         foreach ($this->elements as $elname=>$el) {
183             if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
184                 $html .= ' enctype="multipart/form-data"';
185                 break;
186             }
187         }
188
189         $html .= ">";
190         return $html;
191     }
192     
193     function toStringCloseTag() {
194         $html = "\n";
195         foreach ($this->elements as $elname=>$el) {
196             if (strtolower(get_class($this->elements[$elname]))=="hidden") {
197                 $html .= $this->elements[$elname]->getHtml()."\n";
198             }
199         }
200         $html .= "</form>";
201         return $html;
202     }
203         
204         function toArray() {
205         $vars = array();
206         $vars['open'] = $this->toStringOpenTag();
207         $vars['close'] = $this->toStringCloseTag();
208         
209         foreach ($this->elements as $elname=>$el) {
210             if (is_object($this->elements[$elname])) 
211                 $vars[$elname] = $this->elements[$elname]->toArray();
212         }
213 //print_r($vars);
214         return $vars;
215     }
216     
217     function getValueByElement($elname) {
218         return $this->elements[$elname]->getValue();
219     }
220     
221     function setValueByElement($elname, $value) {
222         if (isset($this->elements[$elname])) {
223                 $this->elements[$elname]->setValue($value);
224         }
225     }
226 }