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 // +----------------------------------------------------------------------+
39 var $elements = array();
41 function __construct($formName) {
42 $this->name = $formName;
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($arguments) {
57 switch($arguments['type']) {
59 import('form.TextField');
60 $el = new TextField($arguments['name']);
61 $el->setMaxLength(@$arguments['maxlength']);
65 import('form.PasswordField');
66 $el = new PasswordField($arguments['name']);
67 $el->setMaxLength(@$arguments['maxlength']);
70 // TODO: refactoring ongoing down from here.
71 // Change $arguments to something better (maybe). $args or $params?
73 import('form.DateField');
74 $el = new DateField($arguments["name"]);
75 $el->setMaxLength("10");
79 import('form.FloatField');
80 $el = new FloatField($arguments["name"]);
81 if (isset($arguments["format"])) $el->setFormat($arguments["format"]);
85 import('form.TextArea');
86 $el = new TextArea($arguments["name"]);
87 $el->setColumns(@$arguments["cols"]);
88 $el->setRows(@$arguments["rows"]);
89 if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]);
93 import('form.Checkbox');
94 $el = new Checkbox($arguments["name"]);
95 if (@$arguments["checked"]) $el->setChecked(true);
96 $el->setData(@$arguments["data"]);
100 import('form.CheckboxGroup');
101 $el = new CheckboxGroup($arguments["name"]);
102 if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]);
103 if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]);
104 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
105 $el->setData(@$arguments["data"]);
109 import('form.Combobox');
110 $el = new Combobox($arguments["name"]);
111 $el->setData(@$arguments["data"]);
112 $el->setDataDefault(@$arguments["empty"]);
113 if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]);
117 import('form.Hidden');
118 $el = new Hidden($arguments["name"]);
122 import('form.Submit');
123 $el = new Submit($arguments["name"]);
127 import('form.Calendar');
128 $el = new Calendar($arguments["name"]);
129 $el->setHighlight(@$arguments["highlight"]);
133 import('form.Table');
134 $el = new Table($arguments["name"]);
135 $el->setData(@$arguments["data"]);
136 $el->setWidth(@$arguments["width"]);
140 import('form.UploadFile');
141 $el = new UploadFile($arguments["name"]);
142 if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]);
146 $el->setFormName($this->name);
147 if (isset($arguments["id"])) $el->setId($arguments["id"]);
148 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
149 if (isset($arguments["enable"])) $el->setEnabled($arguments["enable"]);
151 if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
152 if (isset($arguments["size"])) $el->setSize($arguments["size"]);
154 if (isset($arguments["label"])) $el->setLabel($arguments["label"]);
155 if (isset($arguments["value"])) $el->setValue($arguments["value"]);
157 if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]);
158 if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]);
160 $this->elements[$arguments["name"]] = &$el;
164 function addInputElement(&$el) {
165 if ($el && is_object($el)) {
166 if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
168 $el->setFormName($this->name);
169 $this->elements[$el->name] = &$el;
174 function toStringOpenTag() {
175 $html = "<form name=\"$this->name\"";
177 $html .= ' method="post"';
179 // Add enctype for file upload forms.
180 foreach ($this->elements as $elname=>$el) {
181 if (strtolower(get_class($this->elements[$elname])) == 'uploadfile') {
182 $html .= ' enctype="multipart/form-data"';
191 function toStringCloseTag() {
193 foreach ($this->elements as $elname=>$el) {
194 if (strtolower(get_class($this->elements[$elname]))=="hidden") {
195 $html .= $this->elements[$elname]->getHtml()."\n";
204 $vars['open'] = $this->toStringOpenTag();
205 $vars['close'] = $this->toStringCloseTag();
207 foreach ($this->elements as $elname=>$el) {
208 if (is_object($this->elements[$elname]))
209 $vars[$elname] = $this->elements[$elname]->toArray();
215 function getValueByElement($elname) {
216 return $this->elements[$elname]->getValue();
219 function setValueByElement($elname, $value) {
220 if (isset($this->elements[$elname])) {
221 $this->elements[$elname]->setValue($value);