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