««« Se kilde koden
triangle.gif Basic Base  Component Db Dto Form Form-elements Jquery Layout Menu Menu-fisheye Mvc Tab Table Template Util
 
arrow-headline.gif Index
 
 Tilbage

Navn : Link.php


Sample code, tutorial

Sådan benyttes komponenten Link klassen

Først skal du inkludere den fil der beskriver komponenten, som en klasse fil

  • <?
    require_once(HTML_PACKAGE_PATH.'/Link.php');
    ?>

Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):

  • <?
    Link
    ::display($param1, $param2, $param3, ...);
    ?>

eller du kan lave en instance af komponenten og benytte metoderne direkte:

  • <?
    $object
    = new Link($param1, $param2, $param3, ...);
    print
    $object->getHtml();
    ?>

Parent html

Sådan vises komponenten Link klassen

Finn-Rasmusssen.com

PHP source code

Den fulde PHP kildekode for Link klassen

<?
/**
* @package base
* @see HTML_BASE_UTIL_PATH.'/Link.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.'/Element.php');
require_once(
HTML_BASE_UTIL_PATH.'/Tabindex.php');
require_once(
HTML_BASE_UTIL_PATH.'/Accesskey.php');
if (
defined('HTML_UTIL_COMPONENT_PATH')) {
    require_once(
HTML_UTIL_COMPONENT_PATH.'/Params.php');
}

/**
* Returns a complete Link as HTML
*    <a class="$class" href="$href" title="$title" name="$name" target="_BLANK">$text</a>
* The link may be terminated by a <br /> or surronded by <li> tags
* as specified by the $aux parameter.
* Note: If you specify "\r\n" in the title tag, then a break is automatically added
* <code>
* Usage:
*   $link = new Link($text,$href,$class,$title,$aux,$target,$name);
*   print $link->getHtml();
* Or
*   $link = new Link('','','','','','',$name);
*   print $link->getHtml(); // Target name
* Or
*   Link::display($text,$href,$class,$title,$aux,$target,$name);
* Or
*   Link::display('','','','','','',$name); // Target name
* </code>
*
* @package base
*/

class Link extends Element {
    
/**
     * @var String $text The text to use for the link
     */
    
var $text   = '';

    
/**
     * @var String $href The url to jump to
     */
    
var $href   = '';

    
/**
     * @var String $aux The additional html to add
     */
    
var $aux    = '';

    
/**
     * @var String $target The open in new window
     */
    
var $target = '';

    
/**
     * Constructor
     * @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
     */
    
function Link($text='',$href='',$class='',$title='',$aux='',$target='',$name='',$tabindex='',$onclick='',$accesskey='') {
        
// Must be a link attribute <a name="name">text</a>
        
$aName     = ($name!='' && $text!='')?$name:'';
        
$value     = '';
        
$aClass    = $class!=''?$class:CSS_LINK_COLOR;
        
//$tabindex  = Tabindex::next();
        //$onclick   = '';
        //$accesskey = Accesskey::next($text);
        
$aTitle    = $title!=''?$title:$text; // xhtml 1.0 strict
        
$aTitle   .= $accesskey!=''?' (Alt + '.$accesskey.')':'';
        
$this->Element($aName,$value,$aClass,$aTitle,$tabindex,$onclick,$accesskey);
        
$this->text   = $text;
        
$this->aux    = $aux;
        
$this->target = $target;
        
$linkpath = '';
        
$strHttp  = 'http:/'.'/';
        
$strHttps = 'https:/'.'/';
        if (
strpos($href,$strHttp) !==false ||
            
strpos($href,$strHttps)!==false ||
            
//strpos($href,'..')!==false ||
            
strpos($href,'#')!==false ||
            
strpos($href,'mailto:')!==false ||
            
strpos($href,'javascript:')!==false) {
            
// Skip params check
            
$this->href = $href;
        } else {
            
$this->href = $href!=''?LINK_PATH.$href:$_SERVER['PHP_SELF'];
               
$params = $this->getParams();
            
// Strip off all parameters and add them again
            
$theHref = explode('?',$this->href);
            
$this->href = $theHref[0].$params;
        }
    }

    
/**
     * Get the complete html for a link
     * @return String the html
     */
    
function getParams() {
        
// Do not work with the 'j' as in 'javascript:' or 'mailto:'
        // Because the first position IS '0'
        
$params = '';
        
$strHttp = 'http:/'.'/';
        if (
strpos($this->href,$strHttp)!==false ||
            
//strpos($this->href,'?') !==false ||
            
strpos($this->href,'javascript:')!==false ||
            
strpos($this->href,'mailto:')!==false) {
            
// Ignore
        
} else {
            if (
defined('HTML_UTIL_COMPONENT_PATH')) {
                
$paramsArray = Params::getArray($this->href);
                
$params .= Params::get($paramsArray);
            } else {
                
$querystring = explode('?',$this->href);
                if (
count($querystring)==2) {
                    
$params = '?'.$querystring[1];
                }
            }
        }
        return
$params;
    }

    
/**
     * Get the complete html for a link
     * An image object may be added
     * @return String the html
     */
    
function getHtml() {
        
$html  = $this->html;
        if (
$this->href!='' || $this->name!='') {
            if (
$this->aux != '') {
                if (
$this->aux & (LINK_LAYOUT_LI | LINK_LAYOUT_BR)) {
                    
// Ok
                
} else {
                    die(
$this->getClassName()." Illegal aux=".$this->aux);
                }
            } else {
                
// Ok
            
}
            if (
DEBUG_LEVEL & DEBUG_LEVEL_SHOW_LI && $this->text!='') {
                
$html .= "<!-- $this->text -->";
            }
            if (
$this->aux & LINK_LAYOUT_LI) {
                
$html .= " <li";
                
$class = "$this->class";
                if (
$class!='') {
                    
$html .= ' class="'.$class.'"';
                }
                
$html .= '>';
            }
            if (
strpos($this->title,"\r\n")!==false) {
                
/**
                 * @todo Does only work in IE, not FireFox
                 */
                
$this->title = str_replace("\r\n","&#013;",$this->title); // <br>
            
}
            
$html .= '<a';
            if (
$this->name!='') {
                
$html .= $this->getAttribute('name');
            } else {
                
$html .= $this->getAttribute('id');
                
$html .= $this->getAttribute('class');
                
$html .= $this->getAttribute('href');
                
$html .= $this->getAttribute('title');
                
$html .= $this->getAttribute('target');
                
$html .= $this->getAttribute('onblur');
                
$html .= $this->getAttribute('onfocus');
                
$html .= $this->getAttribute('accesskey');
                
$html .= $this->getAttribute('tabindex');
            }
            
$html .= '>';
            
$image = $this->getElements(); // Image as html
            
if ($image!='') {
                
$html .= $image;
                if (
$this->text!='') {
                    if (
$this->aux & LINK_LAYOUT_BR) {
                        
$html .= "<br />"; // <a href="" ...><img ... /><br />Text</a>
                    
} else {
                        
$html .= '&nbsp;'; // <a href="" ...><img ... />&nbsp;Text</a>
                    
}
                }
            }
            
$text  = $this->text;
            
// 2008-03-13 removed
            //$text  = str_replace('-',"&#187;",$this->text); // In order to avoid line breaks
            
$html .= str_replace(' ',"&nbsp;",$text);       // In order to avoid line breaks
            
$html .= "</a>";
            if (
$this->aux & LINK_LAYOUT_LI) {
                
$html .= "</li>\r\n";
            }
            if (
$this->aux & LINK_LAYOUT_BR) {
                
$html .= "<br />\r\n";
            }
        } else {
            
$html .= '<!-- '.$this->getClassName()."->getHtml(), both href and name are empty -->\r\n";
        }
        return
$html;
    }

    
/**
     * Display html
     * <code>
     * Usage:
     *    Link::display($text,$href,$class,$title,$aux,$target,$name);
     * </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
     */
    
function display($text='',$href='',$class='',$title='',$aux='',$target='',$name='') {
        
$html = new Link($text,$href,$class,$title,$aux,$target,$name);
        
$html->addHtml();
    }
}
?>

HTML source code

Den fulde HTML kildekode for Link klassen

<?
<!-- Finn-Rasmusssen.com --><a id="Link60" class="baseLinkColor" href="http://finn-rasmusssen.com" title="Finn-Rasmusssen.com">Finn-Rasmusssen.com</a>
?>

Class methods

Her er 'klasse metoderne' for Link klassen:

  • link
  • getparams
  • gethtml
  • display
  • object
  • getclassname
  • getmsg
  • addhtml
  • tostring
  • getcachefilename
  • save
  • content
  • html
  • setobject
  • set
  • get
  • getattribute
  • gettag
  • add
  • getsizeof
  • getelement
  • getelements
  • gettoogle
  • getmaximize
  • getminimize
  • newtriangle
  • showsource
  • element
  • getvalue
  • setid
  • setonfocus
  • setonblur
  • id

Object vars

Her er 'objekt variable' for Link klassen:

  • text => Finn-Rasmusssen.com
  • href => http://finn-rasmusssen.com
  • aux =>
  • target =>
  • html =>
  • sql =>
  • elements => Array
  • sizeof => 0
  • name =>
  • id => Link60
  • value =>
  • class => baseLinkColor
  • title => Finn-Rasmusssen.com
  • tabindex =>
  • onclick =>
  • accesskey =>
  • onfocus =>
  • onblur =>

  triangle.gif

danmark

Germany

England

France

Italy

Norge

Sverige

USA