X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2Fform%2FForm.class.php;h=5a4baa9befad6bb9ac2f85fff92b8092c41a5998;hb=a7e4eb9d02d8cc9eb46fa0a2bb3afd24ddeb7507;hp=250f5313ccfda1a6ff2201fedba90b2f06cb7d0d;hpb=aad5111d2e9f3329dc1002e897dd910a60946d6c;p=timetracker.git diff --git a/WEB-INF/lib/form/Form.class.php b/WEB-INF/lib/form/Form.class.php index 250f5313..5a4baa9b 100644 --- a/WEB-INF/lib/form/Form.class.php +++ b/WEB-INF/lib/form/Form.class.php @@ -26,219 +26,137 @@ // | https://www.anuko.com/time_tracker/credits.htm // +----------------------------------------------------------------------+ -// +----------------------------------------------------------------------+ -// | -// | Class generates elements of specification HTML 4.01 -// | http://www.w3.org/TR/1999/REC-html401-19991224 -// | -// +----------------------------------------------------------------------+ - +// Form class is a container for HTML forms we use in the application. +// It contains an array of $elements - which are individual input controls +// belonging to a form. class Form { + var $name = ''; // Form name. + var $elements = array(); // An array of input controls in form. - var $name = ''; - // TODO: refactoring ongoing down from here. + function __construct($name) { + $this->name = $name; + } - var $mId = ""; - var $error; - var $debugFunction; - var $mElements = array(); - var $mRequest; - - function __construct($formid) { - $this->name = $formid; - } - - function setRequest(&$request) { - $this->mRequest = &$request; - } + function getElement($name) { + return $this->elements[$name]; + } - function &getElement($name) { - return $this->mElements[$name]; - } - - function &getElements() { - return $this->mElements; - } - - //// FORM element - // action - // method - GET, POST - // enctype - enctype="multipart/form-data" - // name - // onsubmit - // onreset - function setName($value) { $this->name = $value; } - function getName() { return $this->name; } - - function setId($value) { $this->mId = $value; } - function getId() { return $this->mId; } - - function isSubmit() { - if (!isset($this->mRequest)) return false; - $result = false; - foreach ($this->mElements as $el) { - if (strtolower(get_class($el))=="submit") { - $name = $el->getName(); - $value = $this->mRequest->getAttribute($name); - if($value) { - $result = true; - } - } - } - return $result; - } - - function OutputError($error,$scope="") - { - $this->error=(strcmp($scope,"") ? $scope.": ".$error : $error); - if(strcmp($function=$this->debugFunction,"") - && strcmp($this->error,"")) - $function($this->error); - return($this->error); - } - - //// INPUT element - // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON - // name - // value - // checked - for type radio and checkbox - // size - width pixels or chars - // maxlength - // src - for type image - // tabindex - support A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA - // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA - // onfocus - // onblur - // onselect - INPUT and TEXTAREA - // onchange - function addInput($arguments) { - if(strcmp(gettype($arguments),"array")) - $this->OutputError("arguments must be array","AddInput"); - - if(!isset($arguments["type"]) || !strcmp($arguments["type"],"")) - return($this->OutputError("Type not defined","AddInput")); - - if(!isset($arguments["name"]) || !strcmp($arguments["name"],"")) - return($this->OutputError("Name of element not defined","AddInput")); - - if (isset($this->mElements[$arguments["name"]])) - return($this->OutputError("it was specified '".$arguments["name"]."' name of an already defined input","AddInput")); - - switch($arguments["type"]) { - - case "textfield": - case "text": - import('form.TextField'); - $el = new TextField($arguments["name"]); - $el->setMaxLength(@$arguments["maxlength"]); - if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]); - break; - - case "datefield": - import('form.DateField'); - $el = new DateField($arguments["name"]); - $el->setMaxLength("10"); - break; - - case "floatfield": - import('form.FloatField'); - $el = new FloatField($arguments["name"]); - if (isset($arguments["format"])) $el->setFormat($arguments["format"]); - break; - - case "textarea": - import('form.TextArea'); - $el = new TextArea($arguments["name"]); - $el->setColumns(@$arguments["cols"]); - $el->setRows(@$arguments["rows"]); - if (isset($arguments["maxlength"])) $el->setMaxLength($arguments["maxlength"]); - break; - - case "checkbox": - import('form.Checkbox'); - $el = new Checkbox($arguments["name"]); - if (@$arguments["checked"]) $el->setChecked(true); - $el->setData(@$arguments["data"]); - break; - + function getElements() { + return $this->elements; + } + + function getName() { return $this->name; } + + // addInput - adds an input object to the form. + function addInput($params) { + switch($params['type']) { + case 'text': + import('form.TextField'); + $el = new TextField($params['name']); + if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']); + break; + + case 'password': + import('form.PasswordField'); + $el = new PasswordField($params['name']); + if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']); + break; + + case 'datefield': + import('form.DateField'); + $el = new DateField($params['name']); + $el->setMaxLength('10'); + break; + + case 'floatfield': + import('form.FloatField'); + $el = new FloatField($params['name']); + if (isset($params['format'])) $el->setFormat($params['format']); + break; + + case 'textarea': + import('form.TextArea'); + $el = new TextArea($params['name']); + if (isset($params['maxlength'])) $el->setMaxLength($params['maxlength']); + break; + + case 'checkbox': + import('form.Checkbox'); + $el = new Checkbox($params['name']); + break; + + case 'hidden': + import('form.Hidden'); + $el = new Hidden($params['name']); + break; + + case 'submit': + import('form.Submit'); + $el = new Submit($params['name']); + break; + +// TODO: refactoring ongoing down from here. case "checkboxgroup": import('form.CheckboxGroup'); - $el = new CheckboxGroup($arguments["name"]); - if (isset($arguments["layout"])) $el->setLayout($arguments["layout"]); - if (isset($arguments["groupin"])) $el->setGroupIn($arguments["groupin"]); - if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]); - $el->setData(@$arguments["data"]); + $el = new CheckboxGroup($params["name"]); + if (isset($params["layout"])) $el->setLayout($params["layout"]); + if (isset($params["groupin"])) $el->setGroupIn($params["groupin"]); + if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]); + $el->setData(@$params["data"]); break; case "combobox": import('form.Combobox'); - $el = new Combobox($arguments["name"]); - $el->setData(@$arguments["data"]); - $el->setDataDefault(@$arguments["empty"]); - if (isset($arguments["datakeys"])) $el->setDataKeys($arguments["datakeys"]); + $el = new Combobox($params["name"]); + $el->setData(@$params["data"]); + $el->setDataDefault(@$params["empty"]); + if (isset($params["datakeys"])) $el->setDataKeys($params["datakeys"]); break; - - case "hidden": - import('form.Hidden'); - $el = new Hidden($arguments["name"]); - break; - - case "submit": - import('form.Submit'); - $el = new Submit($arguments["name"]); - break; - + case "calendar": import('form.Calendar'); - $el = new Calendar($arguments["name"]); - $el->setHighlight(@$arguments["highlight"]); + $el = new Calendar($params["name"]); + $el->setHighlight(@$params["highlight"]); break; case "table": import('form.Table'); - $el = new Table($arguments["name"]); - $el->setData(@$arguments["data"]); - $el->setWidth(@$arguments["width"]); + $el = new Table($params["name"]); + $el->setData(@$params["data"]); + $el->setWidth(@$params["width"]); break; case "upload": import('form.UploadFile'); - $el = new UploadFile($arguments["name"]); - if (isset($arguments["maxsize"])) $el->setMaxSize($arguments["maxsize"]); + $el = new UploadFile($params["name"]); + if (isset($params["maxsize"])) $el->setMaxSize($params["maxsize"]); break; - - default: - return($this->OutputError("Type not found for input element","AddInput")); } if ($el!=null) { $el->setFormName($this->name); - if (isset($arguments["id"])) $el->setId($arguments["id"]); - if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]); - if (isset($arguments["render"])) $el->setRenderable($arguments["render"]); - if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]); + if (isset($params["id"])) $el->setId($params["id"]); + $el->localize(); + if (isset($params["enable"])) $el->setEnabled($params["enable"]); - if (isset($arguments["style"])) $el->setStyle($arguments["style"]); - if (isset($arguments["size"])) $el->setSize($arguments["size"]); + if (isset($params["style"])) $el->setStyle($params["style"]); + if (isset($params["size"])) $el->setSize($params["size"]); - if (isset($arguments["label"])) $el->setLabel($arguments["label"]); - if (isset($arguments["value"])) $el->setValue($arguments["value"]); + if (isset($params["label"])) $el->setLabel($params["label"]); + if (isset($params["value"])) $el->setValue($params["value"]); - if (isset($arguments["onchange"])) $el->setOnChange($arguments["onchange"]); - if (isset($arguments["onclick"])) $el->setOnClick($arguments["onclick"]); + if (isset($params["onchange"])) $el->setOnChange($params["onchange"]); + if (isset($params["onclick"])) $el->setOnClick($params["onclick"]); - $this->mElements[$arguments["name"]] = &$el; + $this->elements[$params["name"]] = &$el; } } function addInputElement(&$el) { if ($el && is_object($el)) { - if (!$el->getName()) - return($this->OutputError("no name in element","addInputElement")); - - if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]); + $el->localize(); $el->setFormName($this->name); - $this->mElements[$el->getName()] = &$el; + $this->elements[$el->name] = &$el; } } @@ -246,14 +164,11 @@ class Form { function toStringOpenTag() { $html = "
"; @@ -279,21 +194,21 @@ class Form { $vars['open'] = $this->toStringOpenTag(); $vars['close'] = $this->toStringCloseTag(); - foreach ($this->mElements as $elname=>$el) { - if (is_object($this->mElements[$elname])) - $vars[$elname] = $this->mElements[$elname]->toArray(); + foreach ($this->elements as $elname=>$el) { + if (is_object($this->elements[$elname])) + $vars[$elname] = $this->elements[$elname]->toArray(); } //print_r($vars); return $vars; } function getValueByElement($elname) { - return $this->mElements[$elname]->getValue(); + return $this->elements[$elname]->getValue(); } function setValueByElement($elname, $value) { - if (isset($this->mElements[$elname])) { - $this->mElements[$elname]->setValue($value); + if (isset($this->elements[$elname])) { + $this->elements[$elname]->setValue($value); } } }