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 // +----------------------------------------------------------------------+
31 // | Class generates elements of specification HTML 4.01
32 // | http://www.w3.org/TR/1999/REC-html401-19991224
34 // +----------------------------------------------------------------------+
38 var $name = ''; // Form name.
39 var $elements = array(); // An array of input controls in form.
41 function __construct($name) {
45 function getElement($name) {
46 return $this->elements[$name];
49 function getElements() {
50 return $this->elements;
53 function getName() { return $this->name; }
55 // addInput - adds an input object to the form.
56 function addInput($params) {
57 switch($params['type']) {
59 import('form.TextField');
60 $el = new TextField($params['name']);
61 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
65 import('form.PasswordField');
66 $el = new PasswordField($params['name']);
67 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
71 import('form.DateField');
72 $el = new DateField($params['name']);
73 $el->setMaxLength('10');
77 import('form.FloatField');
78 $el = new FloatField($params['name']);
79 if (isset($params['format'])) $el->setFormat($params['format']);
83 import('form.TextArea');
84 $el = new TextArea($params['name']);
85 if (isset($params['cols'])) $el->setColumns($params['cols']);
86 if (isset($params['rows'])) $el->setRows($params['rows']);
87 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
91 import('form.Checkbox');
92 $el = new Checkbox($params['name']);
93 // TODO: refactoring ongoing down from here.
94 if (@$params["checked"]) $el->setChecked(true);
95 $el->setData(@$params["data"]);
99 import('form.CheckboxGroup');
100 $el = new CheckboxGroup($params["name"]);
101 if (isset($params["layout"])) $el->setLayout($params["layout"]);
102 if (isset($params["groupin"])) $el->setGroupIn($params["groupin"]);
103 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
104 $el->setData(@$params["data"]);
108 import('form.Combobox');
109 $el = new Combobox($params["name"]);
110 $el->setData(@$params["data"]);
111 $el->setDataDefault(@$params["empty"]);
112 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
116 import('form.Hidden');
117 $el = new Hidden($params["name"]);
121 import('form.Submit');
122 $el = new Submit($params["name"]);
126 import('form.Calendar');
127 $el = new Calendar($params["name"]);
128 $el->setHighlight(@$params["highlight"]);
132 import('form.Table');
133 $el = new Table($params["name"]);
134 $el->setData(@$params["data"]);
135 $el->setWidth(@$params["width"]);
139 import('form.UploadFile');
140 $el = new UploadFile($params["name"]);
141 if (isset($params["maxsize"])) $el->setMaxSize($params["maxsize"]);
145 $el->setFormName($this->name);
146 if (isset($params["id"])) $el->setId($params["id"]);
147 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
148 if (isset($params["enable"])) $el->setEnabled($params["enable"]);
150 if (isset($params["style"])) $el->setStyle($params["style"]);
151 if (isset($params["size"])) $el->setSize($params["size"]);
153 if (isset($params["label"])) $el->setLabel($params["label"]);
154 if (isset($params["value"])) $el->setValue($params["value"]);
156 if (isset($params["onchange"])) $el->setOnChange($params["onchange"]);
157 if (isset($params["onclick"])) $el->setOnClick($params["onclick"]);
159 $this->elements[$params["name"]] = &$el;
163 function addInputElement(&$el) {
164 if ($el && is_object($el)) {
165 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
167 $el->setFormName($this->name);
168 $this->elements[$el->name] = &$el;
173 function toStringOpenTag() {
174 $html = "<form name=\"$this->name\"";
176 $html .= ' method="post"';
178 // Add enctype for file upload forms.
179 foreach ($this->elements as $elname=>$el) {
180 if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
181 $html .= ' enctype="multipart/form-data"';
190 function toStringCloseTag() {
192 foreach ($this->elements as $elname=>$el) {
193 if (strtolower(get_class($this->elements[$elname]))=="hidden") {
194 $html .= $this->elements[$elname]->getHtml()."\n";
203 $vars['open'] = $this->toStringOpenTag();
204 $vars['close'] = $this->toStringCloseTag();
206 foreach ($this->elements as $elname=>$el) {
207 if (is_object($this->elements[$elname]))
208 $vars[$elname] = $this->elements[$elname]->toArray();
214 function getValueByElement($elname) {
215 return $this->elements[$elname]->getValue();
218 function setValueByElement($elname, $value) {
219 if (isset($this->elements[$elname])) {
220 $this->elements[$elname]->setValue($value);