/**
* The required files
*/
require_once(HTML_BASE_COMMON_PATH.'/Html.php');
/**
* Generates a html form
* <code>
* Usage:
* $form = new Form('goHere.php','get');
* $form->add(new SubmitButton($action,$method,$name,$attr,$title,$onsubmit,$enctype));
* print $form->getHtml();
* Or:
* Form::start($action,$method,$name,$attr,$title,$onsubmit,$enctype);
* elements::display()
* Form::end();
* </code>
* @package form
*/
class Form extends Html {
var $action = ''; // Where to go
var $method = ''; // How to send data POST/GET
var $name = ''; // The name of the form (NOT xhtml 1.0 strict compliant)
var $id = ''; // The id of the form (NOT netscape 4.x compliant)
var $attr = ''; // Additional attributes for the form
var $title = ''; // The title (tooltip) of the form
var $onsubmit = ''; // Onsubmit event
var $enctype = ''; // The encoding type: multipart/form-data
/**
* Constructor
* @param String $action The action where to go, default 'self'
* @param String $method The method get/post
* @param String $name The name if any (NOT xhtml 1.0 strict compliant)
* @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
* @param String $title The title (tooltip)
* @param String $onsubmit The onsubmit event
* @param String $enctype The encoding type
*/
function Form($action='',$method='',$name='',$attr='',$title='',$onsubmit='',$enctype='') {
$this->Html();
$this->method = $method!=''?$method:FORM_METHOD_GET;
$this->action = $this->getAction($action);
$this->name = $name!=''?$name:'';
$this->attr = $attr!=''?' '.$attr:'';
$this->title = $title!=''?$title:'';
$this->onsubmit = $onsubmit!=''?$onsubmit:'';
$this->enctype = $enctype;
if ($this->enctype=='multipart/form-data' && $this->method!='post') {
die($this->getClassName()."() must use POST when using enctype=='multipart/form-data'");
}
if (defined('SHOW_ELEMENT_ID') && SHOW_ELEMENT_ID) {
$this->id = Element::id($this->getClassName());
if ($this->name=='') {
$this->name = $this->id; // NOT xhtml 1.0 strict compliant
}
}
}
/**
* Get and modify the action value, special case for form post
* Strip off any request parameters
* Add a terminating slash if not already there
* Add index.php if not already there
* Samples:
* '' index.php prices prices/ /prices /prices/ prices/index.php
* http://hvepse.dk http://hvepse.dk/ http://hvepse.dk/index.php
* http://hvepse.dk/price http://hvepse.dk/price/ http://hvepse.dk/price/index.php
* @param String $action The action from the constructor
* @return String The modified action for post
*/
function getAction($action) {
$theAction = $action!=''?$action:$_SERVER['PHP_SELF'];
if ($this->method == FORM_METHOD_POST) {
$url = explode('?',$theAction);
$theAction = $url[0];
if ($theAction == '') {
$theAction .= INDEX_FILE_NAME;
}
if (strpos($theAction, INDEX_FILE_NAME) === false) {
if (strpos($theAction, '/') === false) {
$theAction .= '/'; // Add ending slash
}
if (strrpos($theAction, '/') !== strlen($theAction) -1) {
$theAction .= '/'; // Add ending slash
}
$theAction .= INDEX_FILE_NAME;
}
}
return $theAction;
}
/**
* Returns the html for the start of a form
* @return String The html
*/
function getStart() {
$html = '';
$html .= '<form';
$html .= $this->getAttribute('action');
$html .= $this->getAttribute('method');
$html .= $this->getAttribute('name'); // NOT xhtml 1.0 strict compliant
$html .= $this->getAttribute('id'); // NOT netscape 4.x compliant
$html .= $this->getAttribute('title');
$html .= $this->getAttribute('onsubmit');
$html .= $this->getAttribute('enctype');
$html .= $this->attr; // more attributes, if required
$html .= '>'."\r\n";
$html .= $this->getElements(); // as html
return $html;
}
/**
* Returns the html for the end of a form
* @return String The html
*/
function getEnd() {
return "</form>\r\n";
}
/**
* Get the complete html for a Form
* @return String the html
*/
function getHtml() {
$html = $this->html;
$html .= $this->getStart();
$html .= $this->getEnd();
return $html;
}
/**
* Display start html
* <code>
* Usage:
* Form::start($action,$method,$name,$attr,$title,$onsubmit,$enctype);
* </code>
* @static
* @param String $action The action where to go, default 'self'
* @param String $method The method get/post
* @param String $name The name if any (NOT xhtml 1.0 strict compliant)
* @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
* @param String $title The title (tooltip)
* @param String $onsubmit The onsubmit event
* @param String $enctype The encoding type
*/
function start($action='',$method='',$name='',$attr='',$title='',$onsubmit='',$enctype='') {
switch ($action) {
case 'fieldset':
if (defined('HTML_BASE_UTIL_PATH')) {
Fieldset::start($action,$method);
}
break;
default:
$html = new Form($action,$method,$name,$attr,$title,$onsubmit,$enctype);
$html->addHtml($html->getStart());
break;
}
}
/**
* Display end html
* <code>
* Usage:
* Form::end($element);
* </code>
* @static
*/
function end($element='') {
switch ($element) {
case 'fieldset':
if (defined('HTML_BASE_UTIL_PATH')) {
Fieldset::end();
}
break;
default:
$html = new Html();
$html->addHtml(Form::getEnd());
break;
}
}
/**
* Display html
* <code>
* Usage:
* Form::display($action,$method,$name,$attr,$title,$onsubmit,$enctype);
* </code>
* @static
* @param String $action The action where to go, default 'self'
* @param String $method The method get/post
* @param String $name The name if any (NOT xhtml 1.0 strict compliant)
* @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"'
* @param String $title The title (tooltip)
* @param String $onsubmit The onsubmit event
* @param String $enctype The encoding type
*/
function display($action='',$method='',$name='',$attr='',$title='',$onsubmit='',$enctype='') {
$html = new Form($action,$method,$name,$attr,$title,$onsubmit,$enctype);
$html->addHtml($html->getHtml());
}