/**
* Generates a html form LABEL element
* Note: disabled and readonly fields will also get an accelerator key
* <code>
* Usage:
* $label = new Label($text,$for,$accesskey,$class);
* print $label->getHtml();
* Or
* Label::display($text,$for,$accesskey,$class);
* </code>
* @package form
*/
class Label extends Html {
/**
* @var String $text The text to show
*/
var $text = '';
/**
* @var String $for The html element ID, for the asociated label
*/
var $for = '';
/**
* @var String $accesskey The accesskey class
*/
var $accesskey = '';
/**
* @var String $title The title
*/
var $title = '';
/**
* Constructor
* @param String $text The text to show
* @param String $for The ID for the control to relate to or false
* @param String $accesskey The accesskey or false
* @param String $class The class name
*/
function Label($text,$for='',$accesskey='',$class='') {
$this->Html();
$this->text = ucfirst($text);
$this->accesskey = ucfirst($accesskey!=''?$accesskey:($accesskey!==false?Accesskey::next($this->text):''));
if ($this->accesskey!='' && $accesskey!==false) {
$this->title = 'Accelerator key, use (Alt + '.$this->accesskey.')';
}
$this->class = $class!=''?$class:'';
$this->for = $for!=''?$for:'';
if ($this->for=='' && $for!==false) {
if (defined('SHOW_ELEMENT_ID') && SHOW_ELEMENT_ID) {
$this->for = Element::id($this->getClassName());
}
}
}
/**
* Returns the html for the label control
* @return String the complete html
*/
function getHtml () {
$html = $this->html;
if ($this->text!='') {
$html .= '<label';
$html .= $this->getAttribute('for');
$html .= $this->getAttribute('accesskey');
$html .= $this->getAttribute('class');
$html .= $this->getAttribute('title');
$html .= ">\r\n\t";
// In order to avoid line breaks and Translate html special chars
$text = str_replace('_', ' ', $this->text);
$text = Htmlspecialchars::encode(ucfirst($text));
$alt = '';
if ($this->accesskey!='' && $this->text!='') {
/**
* @todo strtoupper(...) Should be removed and strpos should be replaced by stripos
* however this is only PHP 5.x compatible
*/
$pos = strpos(strtoupper($text),strtoupper($this->accesskey));
if ($pos!==false) {
$before = substr($text, 0, $pos);
$before = str_replace(' ',' ', $before);
$after = substr($text, $pos + 1);
$after = str_replace(' ',' ', $after);
$access = substr($text, $pos, 1);
$span = new Span($access, CSS_COLOR_DARK);
$text = $before.$span->getHtml().$after;
}
$alt = ' (Alt + '.$this->accesskey.')';
}
$html .= "<b>$text</b> $alt";
$html .= ' '.$this->getElements();
$html .= "</label><br />\r\n";
} else {
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) {
$html .= "<!-- ".$this->getClassName()."->getHtml() Text for the label is empty -->\r\n";
}
}
return $html;
}
/**
* Display html
* <code>
* Usage:
* Label::display($text,$for,$accesskey,$class);
* </code>
* @static
* @param String $text The text to show
* @param String $for The ID for the control to relate to
* @param String $accesskey The accesskey
* @param String $class The class name
*/
function display($text,$for='',$accesskey='',$class='') {
$html = new Label($text,$for,$accesskey,$class);
$html->addHtml();
}
}
?>