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