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