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