More refactoring in Form.class.php.
[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["render"])) $el->setRenderable($arguments["render"]);
146                         if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
147                         
148                         if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
149                         if (isset($arguments["size"])) $el->setSize($arguments["size"]);
150                         
151                         if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
152                         if (isset($arguments["value"])) $el->setValue($arguments["value"]);
153                         
154                         if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
155                         if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
156                         
157                         $this->elements[$arguments["name"]] = &$el;
158                 }
159         }
160         
161         function addInputElement(&$el) {
162                 if ($el && is_object($el)) {
163                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
164                 
165                         $el->setFormName($this->name);
166                         $this->elements[$el->getName()] = &$el;
167                 }
168         }
169         
170         
171         function toStringOpenTag() {
172         $html = "<form name=\"$this->name\"";
173         
174         $html .= ' method="post"';
175         
176         // Add enctype for file upload forms.
177         foreach ($this->elements as $elname=>$el) {
178             if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
179                 $html .= ' enctype="multipart/form-data"';
180                 break;
181             }
182         }
183
184         $html .= ">";
185         return $html;
186     }
187     
188     function toStringCloseTag() {
189         $html = "\n";
190         foreach ($this->elements as $elname=>$el) {
191             if (strtolower(get_class($this->elements[$elname]))=="hidden") {
192                 $html .= $this->elements[$elname]->toStringControl()."\n";
193             }
194         }
195         $html .= "</form>";
196         return $html;
197     }
198         
199         function toArray() {
200         $vars = array();
201         $vars['open'] = $this->toStringOpenTag();
202         $vars['close'] = $this->toStringCloseTag();
203         
204         foreach ($this->elements as $elname=>$el) {
205             if (is_object($this->elements[$elname])) 
206                 $vars[$elname] = $this->elements[$elname]->toArray();
207         }
208 //print_r($vars);
209         return $vars;
210     }
211     
212     function getValueByElement($elname) {
213         return $this->elements[$elname]->getValue();
214     }
215     
216     function setValueByElement($elname, $value) {
217         if (isset($this->elements[$elname])) {
218                 $this->elements[$elname]->setValue($value);
219         }
220     }
221 }