/**
* The required files
*/
require_once(HTML_BASE_UTIL_PATH.'/Raw.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Label.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Text.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Readonly.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Textarea.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Password.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Hidden.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Disabled.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Checkbox.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Radio.php');
require_once(HTML_FORM_COMPONENT_PATH.'/Fileupload.php');
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');
}
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
/**
* The Element Factory is used to return a complete populated
* element object, consisting of a label object and a input object.
* <code>
* Usage:
* $factory = new ElementFactory();
* $element = $factory->newElement($type,$key,$name,$value,$len,$required,$debug, $class);
* print $element->getHtml();
* Or
* $label = $factory->newLabel($type,$key,$required;
* print $label->getHtml();
* Or
* $input = $factory->newInput($type,$name,$value,$len,$debug, $class);
* print $input->getHtml();
* Or
* $element = ElementFactory::newElement($type,$key,$name,$value,$len,$required,$debug, $class);
* print $element->getHtml();
* Or
* $label = ElementFactory::newLabel($type,$key,$required);
* print $label->getHtml(); // Use the object
* Or
* $input = ElementFactory::newInput($type,$name,$value,$len,$debug, $class);
* print $input->getHtml();
* </code>
* @package form
*/
class ElementFactory {
/**
* Constructor
*/
function ElementFactory() {
}
/**
* Return the element for a Label and Input as an object
* checkbox and radio buttons are also supported
* @param String $type The type to use
* @param String $key The Key to use
* @param String $name The name to use
* @param String $value The value to use
* @param String $len The maxsize to use OR $onclick if Checkbox
* @param String $required The required text to use, if any
* @param String $debug The debug text to use, if any
* @param String $checked The checked attribute if ticked off for radio/checkbox
* @param String $class The CSS class name to use, if any
* @return Object The html as an Object
*/
function newElement($type,$key,$name,$value,$len='',$required='',$debug='',$checked='', $class='') {
$object = new Raw();
if ($type=='radio' || $type=='checkbox') {
$object->add(ElementFactory::newInput($type,$name,$value,$len,$debug,$checked,$class));
$object->add(ElementFactory::newLabel($type,$key,$required));
} else {
$object->add(ElementFactory::newLabel($type,$key,$required));
$object->add(ElementFactory::newInput($type,$name,$value,$len,$debug,'',$class));
}
return $object;
}
/**
* Return the Input element as an object
* @param String $type The type to use
* @param String $name The name to use
* @param String $value The value to use
* @param String $len The maxsize to use
* @param String $debug The debug text to use, if any
* @param String $checked The checked attribute if ticked off for radio/checkbox
* @param String $class The CSS class name to use, if any
* @return Object The html as an Object
*/
function newInput($type,$name,$value,$len='',$debug='',$checked='', $class='') {
$object = new Raw();
$element = ucfirst($type); // The element class name
switch ($type) {
case 'hidden':
$object = new $element($name,$value,'','',$len,'','','',$debug,'');
break;
case 'file':
if ($element=='') {
$element .= 'upload';
}
// Intentionally fall through
case 'text':
case 'password':
case 'readonly':
case 'disabled':
$object = new $element($name,$value,$class,'',$len,'','','',$debug,'');
break;
case 'checkbox':
case 'radio':
$object = new $element($name,$value,$class,$checked,'',$len,$debug);
break;
case 'textarea':
// Change to new type of element
$object = new $element($name,$value,'','',$class,$debug);
break;
default:
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n"."ElementFactory::newInput(type,..), unknown type, found type=".$type);
break;
}
return $object;
}
/**
* Return the Label element as an object
* @param String $type The type to use
* @param String $key The Key to use
* @param String $required The required text to use, if any
* @return Object The html as an Object
*/
function newLabel($type,$key,$required='') {
$object = new Raw();
/**
* Ignore the accesskey and 'for=' for Label when type is readonly, disabled or hidden
*/
$for = '';
$accesskey = '';
switch ($type) {
case 'readonly':
case 'disabled':
case 'hidden':
$for = false;
$accesskey = false;
break;
default:
break;
}
if ($type!='hidden') {
$text = $key;
if (defined('HTML_LANGUAGE_UTIL_PATH')) {
$text = Translate::sql($key);
}
$object = new Label($text.$required,$for,$accesskey);
}
return $object;
}
/**
* Get the html code
* @return String The html code
*/
function getHtml() {
$element = ElementFactory::newElement('checkbox', 'Test', 'Test', 'Test');
return $element->getHtml();
}
}
?>
HTML source code
Den fulde HTML kildekode for ElementFactory klassen