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