blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Form  /  Elementfactory   Login nu   Login
blank.gif
 ««« Se kilde koden
blank.gif
triangle.gif Basic Base Component Db Dto Form  Form-elements Jquery Layout Menu Menu-fisheye Mvc Tab Table Template Util
blank.gif
blank.gif
 
arrow-headline.gif Index
 
  Tilbage

Navn : ElementFactory.php


Sample code, tutorial

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();
    ?>

Parent html

Sådan vises komponenten ElementFactory klassen



PHP source code

Den fulde PHP kildekode for ElementFactory klassen

<?
/**
* @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.10
* @since 22-feb-2007
*/

/**
* 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

<?
<input type="checkbox" name="Test" id="Checkbox53" class="baseBody" value="Test" tabindex="2" />

<
label for="Checkbox53" accesskey="E" title="Accelerator key, use (Alt + E)">
    <
b>T<span class="baseColorDark">e</span>st</b>&nbsp; (Alt + E) </label><br />


?>

Class methods

Her er 'klasse metoderne' for ElementFactory klassen:

  • elementfactory
  • newelement
  • newinput
  • newlabel
  • gethtml

Object vars

Her er 'objekt variable' for ElementFactory klassen:


 
triangle.gif

danmark

Germany

England

France

Italy

Norge

Sverige

USA


 
blank.gif