Sådan benyttes komponenten Html klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Html.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Html::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Html($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Html klassen
Den fulde PHP kildekode for Html klassen
<?php/** * @package base * @see HTML_BASE_COMMON_PATH.'/Html.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_COMMON_PATH.'/Object.php');require_once(PORTAL_PATH.CURRENT_MYPHP_VERSION.'/Filename.php');if (defined('HTML_FILE_UTIL_PATH')) { require_once(HTML_FILE_UTIL_PATH.'/File.php');}if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * The base class for all the HTML classes * * You must extend this class and write your own getHtml() method * <code> * Usage: * class Demo extends Html { * function Demo() { parent::__construct(); } // Constructor * function getHtml() { return 'Hey Demo'; } // Abstract, you must implement this * } * and * $demo = new Demo(); * $demo->set('attribute','value'); * print $demo->get('attribute'); * </code> * * @package base */class Html extends Object { /** * @var array $elements An array of elements */ protected $elements = array(); /** * @var int $sizeof The number of elements in the $elements array */ protected $sizeof = 0; /** * Constructor */ function __construct() { parent::__construct(); } /** * Set the named object to the class attribute (key), with same name * @param String $key The key to associate with the object * @param Object $object The object to set */ function setObject($key, $object) { if ($object === NULL || $object === '') { return; } if (!is_object($object)) { $msg = $this->getClassName()."->setObject($key, $object) not a valid object<br />\r\n"; $msg .= "Format: class->key=object<br />\r\n "; $msg .= 'Found : '.$this->getClassName().'->'.$key.'='.gettype($object); if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); } $this->getMsg(LOG_LEVEL_FATAL, $msg); } if (!array_key_exists($key,get_object_vars($this))) { $msg = $this->getClassName()."->setObject($key, $object) not a valid attribute<br />\r\n"; $msg .= "Format: class->key=object<br />\r\n "; $msg .= 'Found : '.$this->getClassName().'->'.$key.'='.(isset($value) ? ''.$value : gettype($object)); if (defined('HTML_LOG_UTIL_PATH')) { Log::fatal($msg, __FILE__, __LINE__); print $this->getMsg(LOG_LEVEL_FATAL, $msg); } else { Message::add($msg, __FILE__, __LINE__); } } $this->$key = $object; // All ok, set object attribute } /** * Set the named attribute (key) of a class to specified value * Display an error message and exit, If not a known attribute * i.e. You MUST define the attribute at class scope as ... protected $myattr=''; * @param String $key The key to set * @param String $value The value of the key */ function set($key, $value) { if ($value !== '') { if (array_key_exists($key,get_object_vars($this))) { $this->$key = $value; } else { $msg = $this->getClassName()."->set() not a valid key=$key<br />\r\n"; $msg .= "File: ".__FILE__." line: ".__LINE__."<br />\r\n "; $msg .= "Format: class->key=value<br />\r\n "; $msg .= 'Found : '.$this->getClassName().'->'.$key.'='.(isset($value)?''.$value:gettype($value)); if (defined('HTML_LOG_UTIL_PATH')) { Log::error($msg, __FILE__, __LINE__); print $this->getMsg(LOG_LEVEL_ERROR, $msg); } else { Message::add($msg, __FILE__, __LINE__); } } } } /** * Get the complete html for a key * Display an error message and exit, If not a known attribute * i.e. You MUST define the 'key' at class scope as ... protected $mykey=''; * Speciel case for POST of multiple lines like: price[] = 10 * @param String $key The value to return * @param String $default The default value to use if no match * @return String the html for the attribute (key) */ function get($key, $default='') { $html = ''; $vars = get_object_vars($this); if (array_key_exists( $key, get_class_vars(get_class($this) ) ) || array_key_exists($key, $vars) ) { $theValue = $this->$key; if (is_array($theValue)) { // Sanity check if (count($theValue) !== 1) { $html = $theValue; // Exchange $html with the array value// print($this->getClassName()."->get() Should never come here key=$key count=".count($theValue)." type=".gettype($html)."-".gettype($theValue)."<br />"); } else { foreach($theValue as $key=>$value) { $html .= $value; } } } else { if ($theValue !== '') { $html .= $theValue; } } } else { // Ignore errros } if ($html === '' && $default !== '') { $html .= $default; // No value found, use default if defined } return $html; } /** * Get the complete html for an attribute, i.e. <a class="myclass" ... * Usage: print '<a '.$html->getAttribute('href').'>test</a>'; * Speciel case for POST of multiple lines like: price[] = 10 * @param String $attribute The attribute to return * @param String $default The value to return, if attribute is specified, but empty * @return String the html for the attribute */ function getAttribute($attribute, $default='') { $html = ''; if (isset($this->$attribute)) { $theValue = $this->$attribute; if (is_array($theValue)) { // Sanity check if (count($theValue) !== 1) { $html .= ' '.$attribute.'="'.$theValue.'"';// print($this->getClassName()."->getAttributeattribute() Should never come here attribute=$attribute count=".count($theValue)."<br />"); } else { foreach($theValue as $key=>$value) { $html .= ' '.$attribute.'="'.$value.'"'; } } } else { if ($theValue !== '') { $html .= ' '.$attribute.'="'.$theValue.'"'; } } } if ($html === '' && $default !== '') { $html .= ' '.$attribute.'="'.$default.'"'; // No value found, use default if defined } return $html; } /** * Get the complete html for a html tag. A default value may be supplied * Returns '', if both the $value and teh $default are empty * <code> * i.e. <p>My paragraph</p> * Usage: * print '$html->getTag('My paragraph', 'p', 'default value', "\t\t"); * </code> * @param String $value The inner html for the tag, * @param String $tag The tag to get * @param String $default The default value to use, if * @param String $tab The tab to insert before the tag start * @param String $nl The new line to insert after the tag start * @return String the html for the attribute */ function getTag($value, $tag, $default='', $tab='', $nl='') { $html = ''; if ($value != '' || $default != '') { if ($tag != '') { $html .= "$tab<$tag>$nl"; } $html .= $value != ''?$value:$default; if ($tag != '') { $html .= "</$tag>\r\n"; } } return $html; } /** * Add an element, * the number of elements are stored internally in an array. * @param Object $element The element to add to array */ function add($element) {// if (!is_object($element)) {// $msg = $this->getClassName()."->add($element) not a valid object<br />\r\n";// $msg .= "Format: class->add(element)<br />\r\n ";// $msg .= 'Found : '.$this->getClassName().'->add($element), where $element type='.gettype($element);// if (defined('HTML_LOG_UTIL_PATH')) {// Log::fatal($msg, __FILE__, __LINE__);// }// $this->getMsg(defined('LOG_LEVEL_FATAL')?LOG_LEVEL_FATAL:'fatal', $msg);;// } $this->sizeof = array_push($this->elements, $element); } /** * Get the number of elements added * @return int The number of elements in array */ function getSizeof() { return $this->sizeof; } /** * Get the element at position $i in array * @param int $i, the element $i in array * @return Object The element in question or '' if not found */ function getElement($i) { if (array_key_exists($i, $this->elements)) { return $this->elements[$i]; } else { return ''; } } /** * Get all the elements as html * @return String The html for all the element in the array */ function getElements() { $html = ''; $sizeOf = $this->getSizeof(); for ($i = 0; $i < $sizeOf; $i++) { $element = $this->getElement($i); // Get the next element from array if (is_object($element)) { $html .= $element->getHtml(); // and get the html for the element if (// $element instanceof Link ||// $element instanceof Links ||// $element instanceof Img ||// $element instanceof Image ||// $element instanceof Images ||// $element instanceof Raw || $element instanceof Td || $element instanceof Li || $element instanceof Span ) { // Ignore, Problem if Td(), Li(), Span() or Raw()) } else { $html .= "\r\n"; } } else { $html .= "$element"; // Must be a plain string } } return $html; } /** * Get the array of request parameters which will minimize or maximize this component * <code> * $array = $this->getToogle(REQUEST_LAYOUT_SHOW, LAYOUT_SHOW, LAYOUT_SHOW_TOP); * </code> * @param String $request The request key to use * @param String $key The key name to use * @param String $value The value to use * @return array The array of key=>value pair */ function getToogle($request, $key, $value) { $next = ''; $toogle = Request::get($request, $key, __FILE__, __LINE__, true); //if ($key !== $toogle) { if ($toogle & $value) { $toogle = $toogle & ~$value; // Minimize } else { $toogle = $toogle | $value; // Assume maximize } $next = array($request=>$toogle); //} return $next; } /** * Get the link to maximize this component * <code> * $array = $this->getMaximize(REQUEST_TAB_SHOW, TAB_SHOW, TAB_SHOW_ITEM); * </code> * @param String $request The request parameter to use for the item * @param String $key The final show flags to use for the item * @param String $value The id for the selected item to use * @return String The html for the maximize link to use if enabled */ function getMaximize() { $html = ''; $param = $this->getMinimize(); // Toogle functionality, return '' or an array if (is_array($param)) { if (defined('DEBUG_LEVEL_SHOW_MM') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_MM) { $urlencode = true; $file = __FILE__; $line = __LINE__; $validate = true; $params = Params::get($param, $urlencode, $file, $line, $validate); $class = CSS_CANVAS.' '.CSS_PRINTER; // $this->getCssClass() $text = ''; if (defined('DEBUG_LEVEL_SHOW_EDIT') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_EDIT) { $text = $this->getClassName(); // TODO add a new LINK_LAYOUT_XX style } $title = LINK_TITLE_CLICK_MAXIMIZE.' '.$this->getClassName(); $link = new Link($text, $params, $class, $title);// $link->add(new Images(IMAGE_TRIANGLE,'','', $title, $class)); $html .= $link->getHtml()." "; } else { $html .= "<!-- ".$this->getClassName()."->getMaximize() Disabled -->\r\n"; } } else { if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "<!-- ".$this->getClassName()."->getMaximize() not defined -->\r\n"; } } return $html; } /** * Toogle the request parameters which will minimize or maximize this component * You may override this function in order to create the minimize functionality * @abstract * @return array The array of key=>value pair */ function getMinimize() { return ''; // return $this->getToogle(REQUEST_TAB_SHOW, TAB_SHOW, TAB_SHOW_ITEM); } /** * Return a new Image object, which may be clickable, if the function getMinimize() returns an array * @param String $value The value name of the image. I.e. IMAGE_TRIANGLE * @param String $class The CSS class name to use for the image * @param String $aux The link is used inside an UL tag if LINK_LAYOUT_LI * @return Object The object of the triangle image, optionally surronded by a link */ function newTriangle($value, $class='', $aux='') { $object = new Raw(); $param = $this->getMinimize(); // '' OR an array if (is_array($param)) { $object = new Images($value, '', '', '', $class); if (defined('DEBUG_LEVEL_SHOW_MM') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_MM) { $urlencode = true; $file = __FILE__; $line = __LINE__; $validate = true; $params = Params::get($param, $urlencode, $file, $line, $validate); $object = new Link('', $params, $class, LINK_TITLE_CLICK_MINIMIZE.' '.$this->getClassName(), $aux); if ( $this instanceof MenuFisheye ) { $object->add(new Span(LINK_TEXT_CLICK_MINIMIZE,'','',"display: none;")); // Fisheye menu } $object->add(new Images($value,'','','', $class)); } } else { // TODO what } return $object; } /** * Get the start of the Html for this component * @abstract TODO * @return String the html for the start of this component */ public function getStartHtml() { $html = ""; $html .= $this->getElements(); return $html; } /** * Get the end of the Html for this component * @abstract TODO * @return String the html for the end of this component */ public function getEndHtml() { $html = ""; return ""; } /** * Get the Html for this component * Assume that in some developer mode, some icons will show up * If user clicks a specific help icon, some fancy help/docphp will show up * Returns the html for the element * @abstract TODO * @return String The complete html */ public function getHtml() { $html = $this->html; $html .= $this->getStartHtml(); $html .= $this->getEndHtml(); return $html; } /** * Show source * <code> * Usage: * Classname:showsource($path, $aux); * i.e. Classname:showsource(); * </code> * @static * @param String $path The name of the path to the source file. I.e. HTML_BASE_COMMON_PATH * @param String $aux The type of link to use: BR LI */ static function showsource($path='HTML_BASE_COMMON_PATH', $aux='') { ShowSource::link($path, $this->getClassName(), $aux); }}?>
Den fulde HTML kildekode for Html klassen
<? <!-- DEBUG: Html --> ?>
Her er 'klasse metoderne' for Html klassen:
Her er 'objekt variable' for Html klassen: