Sådan benyttes komponenten ElementFactory klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/ElementFactory.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? ElementFactory::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new ElementFactory($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten ElementFactory klassen
Den fulde PHP kildekode for ElementFactory klassen
<?php/** * @package form * @see HTML_FORM_COMPONENT_PATH.'/ElementFactory.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * 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 __construct() { } /** * 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 * @param String $onblur The onblur event code, if any * @return Object The html as an Object */ public static function newElement($type, $key, $name, $value, $len='', $required='', $debug='', $checked='', $class='', $onblur='') { $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, $onblur)); } 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 (OR $onclick if a Checkbox) * @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 * @param String $onblur The onblur event code, if any * @return Object The html as an Object */ public static function newInput($type, $name, $value, $len='', $debug='', $checked='', $class='', $onblur='') { $object = new Raw(); $title = $debug; $element = ucfirst($type); // The element class name switch ($type) { case 'hidden': $title = $debug; $object = new $element($name, $value, $title); break; case 'file': if ($element=='') { $element .= 'upload'; } // Intentionally fall through case 'text': case 'password': case 'readonly': case 'disabled': $size = ""; $maxlength = $len; $disabled = ""; $readonly = ""; $onclick = ""; $tabindex = ""; $accesskey = ""; $onfocus = ""; $object = new $element($name, $value, $class, $size, $maxlength, $disabled, $readonly, $onclick, $title, $tabindex, $accesskey, $onfocus, $onblur); break; case 'checkbox': case 'radio': $disabled = ""; $onclick = $len; $tabindex = ""; $accesskey = ""; $object = new $element($name, $value, $class, $checked, $disabled, $onclick, $title, $tabindex, $accesskey); break; case 'textarea': // Change to new type of element $text = $value; $rows = ""; $cols = ""; $tabindex = ""; $accesskey = ""; $wrap = ""; $object = new $element($name, $text, $rows, $cols, $class, $title, $tabindex, $accesskey, $wrap); 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 */ public static 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(); }}?>
Den fulde HTML kildekode for ElementFactory klassen
<? <!-- DEBUG: Checkbox --> <input type="checkbox" name="Test" id="Checkbox1" class="baseBody" value="Test" tabindex="1" /> <!-- DEBUG: Label --> <label for="Checkbox1" accesskey="T" title="Accelerator key, use (Alt + T)"> <b><span class="baseColorDark">T</span>est</b> (Alt + T) </label><br /> ?>
Her er 'klasse metoderne' for ElementFactory klassen:
Her er 'objekt variable' for ElementFactory klassen: