Sådan benyttes komponenten Sortby klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Sortby.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Sortby::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Sortby($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Sortby klassen
Den fulde PHP kildekode for Sortby klassen
<?php/** * @package base * @see HTML_BASE_UTIL_PATH.'/Sortby.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.'/Html.php');require_once(HTML_BASE_UTIL_PATH.'/Link.php');require_once(HTML_BASE_UTIL_PATH.'/Image.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Params.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Request.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Encrypt.php');if (defined('HTML_LANGUAGE_UTIL_PATH')) { require_once(HTML_LANGUAGE_UTIL_PATH.'/Translate.php');}/** * Returns a complete Sort By Link as HTML * The link may be terminated by a <br /> or surronded by <li> tags * as specified by the $aux parameter * * <code> * <a class="$class" href="$href" title="$title">$text</a> * Usage: * $link = new Sortby($text, $href, $class, $title, $aux, $target, $name, $tabindex, $onclick, $accesskey); * print $link->getHtml(); * Or * Sortby::display($text, $href, $class, $title, $aux, $target, $name, $tabindex, $onclick, $accesskey); * </code> * @package base */class Sortby extends Link { /** * Constructor * @param String $text The text for the link * @param String $href The url for the link * @param String $class The class of the link * @param String $title The tool tip of the link * @param String $aux Add 'br' or 'li' html tags, if required * @param String $target The target for the link I.e. _BLANK * @param String $name The name for the link * @param String $tabindex The name of the tab index * @param String $onclick The javascript onclick function name * @param String $accesskey The access key */ function __construct($text='', $href='', $class='', $title='', $aux='', $target='', $name='', $tabindex='', $onclick='', $accesskey='') { $orderby = Request::get(REQUEST_ORDER_BY_NAME, '', __FILE__, __LINE__, true); $sortby = Request::get(REQUEST_SORT_BY_NAME, SORT_BY_DEFAULT, __FILE__, __LINE__, true); $theText = $this->getText($text); $theHref = $this->getHref($href, $text, $sortby, $orderby); $theClass = $this->getClass($class); $theTitle = $this->getTitle($title, $sortby, $orderby); parent::__construct($theText, $theHref, $theClass, $theTitle, $aux, $target, $name, $tabindex, $onclick, $accesskey); $this->addImage($text, $sortby, $orderby, $theClass); } /** * Get the text for the Sort By link, where the sort order is defined * @param String $text The text to show * @return String The html */ function getText($text) { $theText = str_replace(SELECT_TABLE_ID,'', $text); $translatedText = $text; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $translatedText = Translate::sql($text, TRANSLATE_QUERY);// print "$text - $translatedText<br />\r\n"; } $theText = ucfirst(str_replace('_',' ', $translatedText)); if ($theText == '') { $theText = REQUEST_SORT_BY_NAME; } return $theText; } /** * Get the href for the Sort By link, where the sort order is defined * @param String $href The href to use, The table name * @param String $text The text to show * @param String $sortby The Sort By request parameter * @param String $orderby The Order By request parameter * @return String The html */ function getHref($href, $text, $sortby, $orderby) { $theHref = Server::getPhpSelf(); $params = ''; $extra = array(); $extra[@REQUEST_TABLE] = Encrypt::it( $href ); // Which in fact is the table name switch ($sortby) { case SORT_BY_DESC: $extra[REQUEST_SORT_BY_NAME] = SORT_BY_ASC; break; case SORT_BY_ASC: $extra[REQUEST_SORT_BY_NAME] = SORT_BY_DESC; break; default: $extra[REQUEST_SORT_BY_NAME] = SORT_BY_DEFAULT; break; }// Cosmic Radiason: for some reason, the FireFox could not accept that the orderBy line was before the sort order// When the request orderBy = text and sorBy = asc// When http://localhost/www/myphp-1.11/myphp-1.11-dto/html/test/index.php?mvcORDER_BY=text&mvcSORT_BY=asc// The æøå was translated to a < ? > if ($text != '') { $extra[REQUEST_ORDER_BY_NAME] = $text; if (defined('HTML_LANGUAGE_UTIL_PATH')) { $translatedText = Translate::sql($text, TRANSLATE_TEXT); $translatedQuery = Translate::sql($text, TRANSLATE_QUERY); $extra[REQUEST_ORDER_BY_NAME] = $translatedText; } } $urlencode = true; $params = Params::get($extra, $urlencode, __FILE__, __LINE__); return $theHref.$params; } /** * Get the CSS class name for the Sort By link, where the sort order is defined * @param String $class The class to use * @return String The html */ function getClass($class) { return $class != '' ? $class : CSS_COLOR_HEADER; } /** * Get the title for the Sort By link, where the sort order is defined * @param String $title The title to show * @param String $sortby The Sort By request parameter * @param String $orderby The Order By request parameter * @return String The html */ function getTitle($title, $sortby, $orderby) { return $title != '' ? $title : ucfirst(strtolower(REQUEST_ORDER_BY_NAME)).' '.$orderby.' '.$sortby; } /** * Add the image to use for the Sort By link, where the sort order is defined * @param String $text The text to show * @param String $sortby The Sort By request parameter * @param String $orderby The Order By request parameter * @param String $class The CSS class name to use * @return String The html */ function addImage($text, $sortby, $orderby, $class) { $image = ''; if ($text == $orderby) { $width = ""; $height = ""; $alt = ""; $image = new Images($sortby, $width, $height, $alt, $class); } if ($image !== '') { $this->add($image); } } /** * Display a Sort By link * <code> * Usage: * Sortby::display($text, $href, $class, $title, $aux, $target, $name, $tabindex, $onclick, $accesskey); * </code> * @static * @param String $text The text for the link * @param String $href The url for the link * @param String $class The css class of the link * @param String $title The tool tip of the link * @param String $aux Add 'br' or 'li' html tags, if required * @param String $target The target for the link. I.e. _BLANK * @param String $name The name for the link * @param String $tabindex The name of the tab index * @param String $onclick The javascript onclick function name * @param String $accesskey The access key */ public static function display($text='', $href='', $class='', $title='', $aux='', $target='', $name='', $tabindex='', $onclick='', $accesskey='') { $html = new Sortby($text, $href, $class, $title, $aux, $target, $name, $tabindex, $onclick, $accesskey); $html->addHtml(); }}?>
Den fulde HTML kildekode for Sortby klassen
<? <!-- DEBUG: Sortby --> <!-- mvcSORT_BY --><a class="baseColorHeader" href="/source-code/base/Sortby/index.php?mvcSORT_BY=asc" title="Mvcorder_by desc"><!-- DEBUG: Images --> <img src="http://finnrasmussen.dk/images/desc.gif" width="5" height="10" alt="desc.gif" class="baseColorHeader" /> mvcSORT_BY</a> ?>
Her er 'klasse metoderne' for Sortby klassen:
Her er 'objekt variable' for Sortby klassen: