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