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 // +----------------------------------------------------------------------+
37 var $name = ''; // Form name.
38 var $elements = array(); // An array of input controls in form.
40 function __construct($name) {
44 function getElement($name) {
45 return $this->elements[$name];
48 function getElements() {
49 return $this->elements;
52 function getName() { return $this->name; }
54 // addInput - adds an input object to the form.
55 function addInput($params) {
56 switch($params['type']) {
58 import('form.TextField');
59 $el = new TextField($params['name']);
60 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
64 import('form.PasswordField');
65 $el = new PasswordField($params['name']);
66 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
70 import('form.DateField');
71 $el = new DateField($params['name']);
72 $el->setMaxLength('10');
76 import('form.FloatField');
77 $el = new FloatField($params['name']);
78 if (isset($params['format'])) $el->setFormat($params['format']);
82 import('form.TextArea');
83 $el = new TextArea($params['name']);
84 if (isset($params['cols'])) $el->setColumns($params['cols']);
85 if (isset($params['rows'])) $el->setRows($params['rows']);
86 if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']);
90 import('form.Checkbox');
91 $el = new Checkbox($params['name']);
92 // TODO: refactoring ongoing down from here.
93 if (@$params["checked"]) $el->setChecked(true);
94 $el->setData(@$params["data"]);
98 import('form.CheckboxGroup');
99 $el = new CheckboxGroup($params["name"]);
100 if (isset($params["layout"])) $el->setLayout($params["layout"]);
101 if (isset($params["groupin"])) $el->setGroupIn($params["groupin"]);
102 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
103 $el->setData(@$params["data"]);
107 import('form.Combobox');
108 $el = new Combobox($params["name"]);
109 $el->setData(@$params["data"]);
110 $el->setDataDefault(@$params["empty"]);
111 if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]);
115 import('form.Hidden');
116 $el = new Hidden($params["name"]);
120 import('form.Submit');
121 $el = new Submit($params["name"]);
125 import('form.Calendar');
126 $el = new Calendar($params["name"]);
127 $el->setHighlight(@$params["highlight"]);
131 import('form.Table');
132 $el = new Table($params["name"]);
133 $el->setData(@$params["data"]);
134 $el->setWidth(@$params["width"]);
138 import('form.UploadFile');
139 $el = new UploadFile($params["name"]);
140 if (isset($params["maxsize"])) $el->setMaxSize($params["maxsize"]);
144 $el->setFormName($this->name);
145 if (isset($params["id"])) $el->setId($params["id"]);
146 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
147 if (isset($params["enable"])) $el->setEnabled($params["enable"]);
149 if (isset($params["style"])) $el->setStyle($params["style"]);
150 if (isset($params["size"])) $el->setSize($params["size"]);
152 if (isset($params["label"])) $el->setLabel($params["label"]);
153 if (isset($params["value"])) $el->setValue($params["value"]);
155 if (isset($params["onchange"])) $el->setOnChange($params["onchange"]);
156 if (isset($params["onclick"])) $el->setOnClick($params["onclick"]);
158 $this->elements[$params["name"]] = &$el;
162 function addInputElement(&$el) {
163 if ($el && is_object($el)) {
164 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
166 $el->setFormName($this->name);
167 $this->elements[$el->name] = &$el;
172 function toStringOpenTag() {
173 $html = "<form name=\"$this->name\"";
175 $html .= ' method="post"';
177 // Add enctype for file upload forms.
178 foreach ($this->elements as $elname=>$el) {
179 if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
180 $html .= ' enctype="multipart/form-data"';
189 function toStringCloseTag() {
191 foreach ($this->elements as $elname=>$el) {
192 if (strtolower(get_class($this->elements[$elname]))=="hidden") {
193 $html .= $this->elements[$elname]->getHtml()."\n";
202 $vars['open'] = $this->toStringOpenTag();
203 $vars['close'] = $this->toStringCloseTag();
205 foreach ($this->elements as $elname=>$el) {
206 if (is_object($this->elements[$elname]))
207 $vars[$elname] = $this->elements[$elname]->toArray();
213 function getValueByElement($elname) {
214 return $this->elements[$elname]->getValue();
217 function setValueByElement($elname, $value) {
218 if (isset($this->elements[$elname])) {
219 $this->elements[$elname]->setValue($value);