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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
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.
33 var $name = ''; // Form name.
34 var $elements = array(); // An array of input controls in form.
36 function __construct($name) {
40 function getElement($name) {
41 return $this->elements[$name];
44 function getElements() {
45 return $this->elements;
48 function getName() { return $this->name; }
50 // addInput - adds an input object to the form.
51 function addInput($params) {
52 switch($params['type']) {
54 import('form.TextField');
55 $el = new TextField($params['name']);
56 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
60 import('form.PasswordField');
61 $el = new PasswordField($params['name']);
62 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
66 import('form.DateField');
67 $el = new DateField($params['name']);
68 $el->setMaxLength('10');
72 import('form.FloatField');
73 $el = new FloatField($params['name']);
74 if (isset($params['format'])) $el->setFormat($params['format']);
78 import('form.TextArea');
79 $el = new TextArea($params['name']);
80 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
84 import('form.Checkbox');
85 $el = new Checkbox($params['name']);
89 import('form.Hidden');
90 $el = new Hidden($params['name']);
94 import('form.Submit');
95 $el = new Submit($params['name']);
98 // TODO: refactoring ongoing down from here.
100 import('form.CheckboxGroup');
101 $el = new CheckboxGroup($params["name"]);
102 if (isset($params["layout"])) $el->setLayout($params["layout"]);
103 if (isset($params["groupin"])) $el->setGroupIn($params["groupin"]);
104 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
105 $el->setData(@$params["data"]);
109 import('form.Combobox');
110 $el = new Combobox($params["name"]);
111 $el->setData(@$params["data"]);
112 $el->setDataDefault(@$params["empty"]);
113 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
117 import('form.Calendar');
118 $el = new Calendar($params["name"]);
119 $el->setHighlight(@$params["highlight"]);
123 import('form.Table');
124 $el = new Table($params["name"]);
125 $el->setData(@$params["data"]);
126 $el->setWidth(@$params["width"]);
130 import('form.UploadFile');
131 $el = new UploadFile($params["name"]);
132 if (isset($params["maxsize"])) $el->setMaxSize($params["maxsize"]);
136 $el->setFormName($this->name);
137 if (isset($params["id"])) $el->setId($params["id"]);
138 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
139 if (isset($params["enable"])) $el->setEnabled($params["enable"]);
141 if (isset($params["style"])) $el->setStyle($params["style"]);
142 if (isset($params["size"])) $el->setSize($params["size"]);
144 if (isset($params["label"])) $el->setLabel($params["label"]);
145 if (isset($params["value"])) $el->setValue($params["value"]);
147 if (isset($params["onchange"])) $el->setOnChange($params["onchange"]);
148 if (isset($params["onclick"])) $el->setOnClick($params["onclick"]);
150 $this->elements[$params["name"]] = &$el;
154 function addInputElement(&$el) {
155 if ($el && is_object($el)) {
156 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
158 $el->setFormName($this->name);
159 $this->elements[$el->name] = &$el;
164 function toStringOpenTag() {
165 $html = "<form name=\"$this->name\"";
167 $html .= ' method="post"';
169 // Add enctype for file upload forms.
170 foreach ($this->elements as $elname=>$el) {
171 if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
172 $html .= ' enctype="multipart/form-data"';
181 function toStringCloseTag() {
183 foreach ($this->elements as $elname=>$el) {
184 if (strtolower(get_class($this->elements[$elname]))=="hidden") {
185 $html .= $this->elements[$elname]->getHtml()."\n";
194 $vars['open'] = $this->toStringOpenTag();
195 $vars['close'] = $this->toStringCloseTag();
197 foreach ($this->elements as $elname=>$el) {
198 if (is_object($this->elements[$elname]))
199 $vars[$elname] = $this->elements[$elname]->toArray();
205 function getValueByElement($elname) {
206 return $this->elements[$elname]->getValue();
209 function setValueByElement($elname, $value) {
210 if (isset($this->elements[$elname])) {
211 $this->elements[$elname]->setValue($value);