/**
* Design the system constants as checkbox, radio box or input fields
* <code>
* Usage:
* $components = $DEFINE_LAYOUT_SHOW; // The array of possible defines
* $type = DESIGN_TYPE_CHECKBOX; // Which look and feel to use
* $component = Request::get(REQUEST_LAYOUT_SHOW,LAYOUT_SHOW);
* $show = REQUEST_LAYOUT_SHOW; // The name of the request parameter
* $html = new Design($components,$type,$component,$show);
* print $html->getHtml();
* Or
* Dump::display($components,$type,$component,$show);
* </code>
* @package layout
*/
class Design extends Html {
var $components = '';
var $type = '';
var $component = '';
var $show = '';
/**
* Constructor
* @param String $components The system components to display
* @param String $type The display type (field, checkbox, radio)
* @param String $component The requested parameters
* @param String $show The display type to show
*/
function Design($components='',$type='',$component='',$show='') {
$this->Html();
$this->components = $components!=''?$components:DEFINE_SETUP_PATH;
$this->type = $type!=''?$type:DESIGN_TYPE_TEXT;
$this->component = $component;
$this->show = $show!=''?$show:'';
}
/**
* Get the javascript onclick event code for a Checkbox.
* If not a checkbox, just return an empty string
* @param String $type The element type to use
* @param String $value The value for the element
* @return String The onClick javascript, if a Checkbox else ''
*/
function getOnClick($type, $value) {
$onclick = ''; // Pure Javascript
switch ($type) {
case DESIGN_TYPE_CHECKBOX: // Special case
$onclick .= "if(this.checked){";
$onclick .= "this.form.".$this->show.".value";
$onclick .= is_bool($value)?'=false':'|='.$value;
$onclick .= ';';
$onclick .= '}else{';
$onclick .= "this.form.".$this->show.".value";
$onclick .= is_bool($value)?'=true':'&='.(~$value);
$onclick .= ';';
$onclick .= '}';
break;
default: // Ignore
break;
}
return $onclick;
}
/**
* Get the checked value for a Checkbox or Radio.
* If not a checkbox or radio, just return an empty string
* @param String $type The element type to use
* @param String $component The element component to use
* @param String $value The value for the element
* @return String The $checked value else ''
*/
function getChecked($type, $component, $value) {
$checked = ''; // Pure Javascript
switch ($type) {
case DESIGN_TYPE_CHECKBOX: // Special case
if ($value & $component) {
$checked = 'checked';
} else {
// Do nothing
}
break;
case DESIGN_TYPE_RADIO:
if ($value & $component) {
$checked = 'checked';
}
break;
default: // Ignore
break;
}
return $checked;
}
/**
* Return the element for a Label and Input as an object
* checkbox and radio buttons are also supported
* @param String $type The type to use
* @param String $key The Key to use
* @param String $name The name to use
* @param String $value The value to use
* @param String $len The maxsize to use OR $onclick if Checkbox
* @param String $required The required text to use, if any
* @param String $debug The debug text to use, if any
* @param String $checked The checked attribute if ticked off for radio/checkbox
* @return Object The html as an Object
*/
function newElement($type,$key,$name,$value,$onclick='',$required='',$debug='',$checked='') {
$required = ''; // Not used
return ElementFactory::newElement($type,$key,$name,$value,$onclick,$required,$debug,$checked);
}
/**
* Get the html, and return it as a Form page
* @return String The html
*/
function getFormHtml() {
$html = '';
if (is_array($this->components)) {
$hr = new Tag('','hr');
$html .= $hr->getHtml();
$form = new Form();
$legend = new Legend('Unknown Array Name');
$hexvalue = 0; // None
foreach ($this->components as $key=>$value) {
if ($key=='ARRAY_NAME') {
$legend = new Legend($value);
}
else {
$onclick = $this->getOnClick($this->type, $value);
$checked = $this->getChecked($this->type, $this->component, $value);
switch ($this->type) {
case DESIGN_TYPE_CHECKBOX: // Special case
if ($value & $this->component) {
$hexvalue |= $value;
}
else {
// Do nothing
}
$form->add($this->newElement('checkbox',$key,'',$value,$onclick,'','',$checked));
break;
case DESIGN_TYPE_RADIO: // Special case
$form->add($this->newElement('radio',$key,$this->show,$value,$onclick,'','',$checked));
break;
case DESIGN_TYPE_DEC2BIN:
$format = new Format($value,FORMAT_STYLE_BIN);
$debug = new Format($value,FORMAT_STYLE_HEX);
$form->add($this->newElement('text',$key,$key,$format->getHtml(),$onclick,'',$debug->getHtml(),$checked));
break;
case DESIGN_TYPE_DEC2HEX:
$format = new Format($value,FORMAT_STYLE_HEX);
$debug = new Format($value,FORMAT_STYLE_BIN);
$form->add($this->newElement('text',$key,$key,$format->getHtml(),$onclick,'',$debug->getHtml(),$checked));
break;
case DESIGN_TYPE_TEXT:
$debug = new Format($value,FORMAT_STYLE_HEX);
$form->add($this->newElement('text',$key,$key,$value,$onclick,'',$debug->getHtml(),$checked));
break;
default:
$msg = $this->getClassName().'.php, Unknown type='.$this->type.'<br />';
Log::error(__FILE__,__LINE__,$msg);
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".$msg);
break;
}
}
}
// Special case
if ($this->type==DESIGN_TYPE_CHECKBOX) {
$form->add(new Hidden($this->show,$hexvalue));
foreach ($_GET as $key=>$value) {
if ($key!=$this->show) {
$form->add(new Hidden($key,$value));
}
}
// SubmitButton($name='',$value='',$class='',$disabled='',$onclick='',$title='',$tabindex='',$accesskey='')
$form->add(new SubmitButton('',BUTTON_SET_ALL,'','',"this.form.".$this->show.".value=".'0xffff;'));
$form->add(new SubmitButton('',BUTTON_CLEAR_ALL,'','',"this.form.".$this->show.".value=".'1;'));
}
$form->add(new Hiddens());
$form->add(new SubmitButton('',BUTTON_CHANGE));
$fieldset = new Fieldset($legend);
$fieldset->add($form);
$html .= $fieldset->getHtml();
}
else {
$html .= 'Undefined '.$this->components;
}
return $html;
}
/**
* Builds the html for a dump parameter form
* @return String The result as html
*/
function getHtml() {
$html = '';
$html .= $this->getFormHtml(); // Popup the form
return $html;
}
/**
* Display html
* <code>
* Usage:
* Design::display($components,$type,$component);
* </code>
* @static
* @param String $components The system components to display
* @param String $type The display type (field, checkbox, radio(
* @param String $component The system components to display
* @param String $show The display type to show
*/
function display($components='',$type='',$component='',$show='') {
$html = new Design($components,$type,$component,$show);
$html->addHtml();
}
}
?>