/**
* 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.'/Htmlspecialchars.php');
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
/**
* Generates an INPUT element for a form
* <code>
* <input type="$type" name="$name" value="$value" class="$class" size="$size"
* maxlength="$maxlength" disabled="disabled" readonly="readonly"
* onclick="$onclick" title="$title" tabindex="$tabindex" accesskey="$accesskey" />
* Usage:
* $input = new Input($type,$name,$value,$class,$size,$maxlength,
* $disabled,$readonly,$onclick,$title,$tabindex,$accesskey);
* print $input->getHtml();
* Or
* Input::display($type,$name,$value,$class,$size,$maxlength,
* $disabled,$readonly,$onclick,$title,$tabindex,$accesskey);
* </code>
* @package form
*/
class Input extends Element {
/**
* @var String $type The type of element, i.e. text, radio, checkbox, hidden, ...
*/
var $type = '';
/**
* @var String $size The size of the element
*/
var $size = '';
/**
* @var String $maxlength The maxlength of chars for a text field
*/
var $maxlength = '';
/**
* @var String $disabled The disabled (>IE4.x)
*/
var $disabled = '';
/**
* @var String $readonly The readonly (>IE4.x)
*/
var $readonly = '';
/**
* @var String $checked Radio/checkbox ticked or not
*/
var $checked = '';
/**
* @var boolean $debug Debug may be disabled by the container (false)
*/
var $debug = true;
/**
* Constructor
* @param String $type The type (text, radio, checkbox, file, button, hidden, submit, reset)
* @param String $name The name
* @param String $value The value, if any
* @param String $class The class
* @param String $size The size / or checked for radio/checkbox
* @param String $maxlength The maxlength
* @param String $disabled The disabled
* @param String $readonly The readonly
* @param String $onclick On click event for javascript
* @param String $title The tooltip
* @param String $tabindex The tabindex
* @param String $accesskey The accesskey
*/
function Input($type='',$name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='',$title='',$tabindex='',$accesskey='') {
$this->type = strtolower($type!=''?$type:'text');
$aTabindex = '';
if ($this->type!='hidden' && $disabled=='' && $readonly=='') {
$aTabindex = $tabindex!=''?$tabindex:Tabindex::next();
}
$theAcesskey = '';
if ($disabled=='' && $readonly=='') {
$theAcesskey = $accesskey;
}
$this->Element($name,$value,$class,$title,$aTabindex,$onclick,$theAcesskey);
switch ($this->type) {
case 'text': // Intentionally fall through
case 'password':
if ($this->title=='') {
$this->title = $this->value;
}
// Intentionally fall through
case 'hidden':
// Translate html special chars and remove slashes
// 2007-08-04 changed to htmlspecialchars because of the Gui::display()
// 2008-01-28 Removed again and changed Element to
//$this->value = Htmlspecialchars::encode($this->value);
//$this->value = Htmlspecialchars::encode($value);
// $this->value = stripslashes($this->value);
$this->maxlength = $maxlength!=''?$maxlength:TEXT_MAXLENGTH;
$this->disabled = $disabled;
$this->readonly = $readonly;
$this->size = $size;
$this->class = $class!=''?$class:CSS_TEXT_CLASS;
break;
case 'radio': // Intentionally fall through
case 'checkbox':
$this->class = $class!=''?$class:'';
break;
case 'file':
$this->class = $class!=''?$class:CSS_FILE_BUTTON;
// Intentionally fall through
case 'reset':
case 'submit':
case 'button':
$this->disabled = $disabled;
$this->class = $class!=''?$class:CSS_BUTTON;
if ($this->title=='') {
$this->title = $this->value;
if ($this->accesskey!='') {
$this->title .= ' (Alt + '.$this->accesskey.')';
}
}
break;
case 'image':
// Ignore
break;
default:
$msg = $this->getClassName().'(), unknown input type='.$this->type;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
$html = $this->getMsg(LOG_LEVEL_FATAL, $msg);
} else {
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".$msg);
}
break;
}
/**
* The request parameters will NOT overrule the default parameters
*/
switch ($this->type) {
case 'password':
case 'hidden': // Intentionally fall through
case 'text':
// if ($this->value=='' && isset($_GET[$this->name])) {
// $this->value = Htmlspecialchars::encode($_GET[$this->name]);
// }
// if ($this->value=='' && isset($_POST[$this->name])) {
// $this->value = Htmlspecialchars::encode($_POST[$this->name]);
// }
break;
case 'radio':
if (isset($this->value) && $this->value!=='') {
// Ok, a value is supplied, else force the user to add one
} else {
$this->value = INPUT_VALUE_NONE;
}
// Intentionally fall through
case 'checkbox':
$this->checked = $size; // 'size' is used as a placeholder for checked for radio/checkbox
if (isset($_GET[$this->name])) {
$this->checked = ($_GET[$this->name]==$this->value || $_GET[$this->name]=='on')?'checked':'';
}
if (isset($_POST[$this->name])) {
$this->checked = ($_POST[$this->name]==$this->value || $_POST[$this->name]=='on')?'checked':'';
}
break;
default:
// Ignore
break;
}
}
/**
* Returns the complete html for an input control
* @return String the complete html
*/
function getHtml() {
$html = $this->html;
if ($this->type=='hidden' && $this->name=='') {
$html .= '<!-- Input type=hidden name="'.$this->name.'" is empty -->';
} else {
$html .= '<input';
$html .= $this->getAttribute('type');
$html .= $this->getAttribute('name');
$html .= $this->getAttribute('id');
// Special handling of the value attribute
switch ($this->type) {
case 'hidden':
$html .= ' value'.'="'.$this->getValue().'"';
break;
case 'text':
case 'password':
$html .= $this->getAttribute('class');
$html .= $this->getAttribute('size');
$html .= $this->getAttribute('maxlength');
$html .= $this->getAttribute('disabled');
$html .= $this->getAttribute('readonly');
$html .= $this->getAttribute('onclick');
$html .= $this->getAttribute('value');
break;
case 'checkbox':
case 'radio':
$html .= $this->getAttribute('onclick');
$html .= $this->getAttribute('class');
$html .= $this->getAttribute('checked');
$html .= ' value'.'="'.$this->getValue().'"';
break;
case 'reset':
case 'submit':
case 'button':
$html .= $this->getAttribute('disabled');
$html .= $this->getAttribute('class');
$html .= "\r\n\t";
$html .= $this->getAttribute('onclick');
$html .= $this->getAttribute('value');
break;
case 'file':
$html .= $this->getAttribute('class');
break;
case 'image':
$html .= " NOT TESTED ";
break;
default:
$msg = $this->getClassName().'->getHtml(), unknown input type '.$this->type;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
$html .= $this->getMsg(LOG_LEVEL_FATAL, $msg);
} else {
$html .= $msg;
}
break;
}
if (strpos($this->title,"\r\n")!==false) {
/**
* @todo Does only work in IE, not FireFox
*/
$this->title = str_replace("\r\n","
",$this->title); // <br>
}
$html .= $this->getAttribute('title');
$html .= $this->getAttribute('tabindex');
$html .= $this->getAttribute('accesskey');
if ($this->type=='file') {
$html .= $this->getAttribute('accept');
}
$html .= ' />';
if ($this->type=='password') {
$html .= '<br />';
}
if ($this->type=='text') {
$html .= '<br />'; // Ignore zip, && $this->name!=SELECT_ZIP
}
}
$html .= $this->getElements(); // as html
if ($html!='') {
$html .= "\r\n";
if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_HIDDEN && ($this->type=='hidden' || $this->type=='readonly')) {
// Debug may be disabled by the container
if ($this->debug===true) {
$html .= "<!-- DEBUG Hidden: ".ucfirst($this->type).": name=$this->name value=$this->value -->\r\n";
}
}
}
return $html;
}
/**
* Display the html for the input element
* <code>
* Usage:
* Input::display($type,$name,$value,$class,$size,$maxlength,$disabled,
* $readonly,$onclick,$title,$tabindex,$accesskey);
* </code>
* @static
* @param String $type The type (text, radio, checkbox, file, button, hidden, submit, reset)
* @param String $name The name
* @param String $value The value, if any
* @param String $class The class
* @param String $size The size / or checked for radio/checkbox
* @param String $maxlength The maxlength
* @param String $disabled The disabled
* @param String $readonly The readonly
* @param String $onclick On click event for javascript
* @param String $title The tooltip
* @param String $tabindex The tabindex
* @param String $accesskey The accesskey
*/
function display($type='',$name='',$value='',$class='',$size='',$maxlength='',$disabled='',$readonly='',$onclick='',$title='',$tabindex='',$accesskey='') {
$html = new Input($type,$name,$value,$class,$size,$maxlength,$disabled,$readonly,$onclick,$title,$tabindex,$accesskey);
$html->addHtml();
}
}
?>