Űrlap készítő osztály - beta
<?php
class form_packer
{
#######################################################
# STYLES #
#######################################################
var $t_width; # table width
var $t_left_width; # left col. width
var $t_right_width; # right col. width
var $t_border; # table border
var $td_height; # td height
var $title_style; # title style
var $error_style; # error message style
var $textbox_style; # text, password style
var $textarea_style ; # textarea style
var $select_style; # select style
var $select_value_style; # select value style
var $checkbox_value_style; # checkbox value style
var $radio_value_style; # radio value style
var $if_style_exist; # if form style exist
#######################################################
# FORM PROPERTIES #
#######################################################
var $id; # Form identify - name
var $form_name="form_1"; # Form real name
var $action; # if nothing, it self
var $submit_image; # if set, automaticly change submit button, use relative path ( "../images/submit.jpg" )
var $target = "_self"; # target
var $enctype; # if the form contains file obj -> automaticly set
var $hide = TRUE; # Shuld the form be hidden if there are no errors in the send data?
#######################################################
# ELSE #
#######################################################
var $_error=""; # error trigger
var $_data=""; # storing the form data
var $title; # title...
#######################################################
# function for single input #
#######################################################
function single_ins( $name, $title, $obligate=FALSE, $type="text", $size="", $value="", $error_msg="")
{
$obligate_char = " : ";
if( isset( $_POST[$this->id] ) )
{
$this->_check_data( $name, $type, $obligate, $error_msg );
$value = $_POST[$name];
}
if( $obligate == TRUE )
{
$obligate_char = " :* ";
}
$return = '<tr><td height="'.$this->td_height.'" width="'.$this->t_left_width.'" valign="top"><div style="'.$this->title_style.'">'. $title . $obligate_char .'</div>'.$this->_error_msg .'</td><td height="'.$this->td_height.'" valign="top" width="'.$this->t_right_width.'">';
$value = htmlspecialchars( $value );
switch( $type )
{
case 'password':
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="password" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input style="'.$this->textbox_style.'" name="'. $name .'" '. $attribs .' '. $value .'>';
break;
case 'file':
$this->enctype = 'multipart/form-data';
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="file" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input style="'.$this->textbox_style.'" name="'. $name .'" '. $attribs .' '. $value .'>';
break;
case 'textarea':
$size = explode( 'x', $size );
if( !empty( $size[0] ) && !empty( $size[1] ) )
{
$size = 'cols="'. $size[0] .'" rows="'.$size[1] .'"';
}
$return .= '<textarea style="'.$this->textarea_style.'" name="'. $name .'" '. $size .'">'. $value .'</textarea>';
break;
default:
$size = preg_replace( "/\D/i", "", $size );
if( !empty( $size ) )
{
$size = 'size="'. $size .'"';
}
$attribs = 'type="text" '. $size;
if( !empty( $value ) )
{
$value = 'value="'. $value .'"';
}
$return .= '<input style="'.$this->textbox_style.'" name="'. $name .'" '. $attribs .' '. $value .'>';
}
$return .= '</td></tr>';
$this->_data .= $return;
return true;
}
#######################################################
# function for grouped inputs #
#######################################################
function group_ins( $name, $title, $value, $obligate="", $type="", $description="", $error_msg="" )
{
if( isset( $_POST[$this->id] ) )
{
$this->_check_data( $name, $type, $obligate, $error_msg );
}
if( $obligate == TRUE )
{
$obligate_char = ":* ";
}
$return = '<tr><td height="'.$this->td_height.'" width="'.$this->t_left_width.'" valign="top"><div style="'.$this->title_style.'">'. $title . $obligate_char .'</div>'.$this->_error_msg .'</td><td height="'.$this->td_height.'" width="'.$this->t_right_width.'" valign="top">';
if( !is_array( $value ) OR ( !empty( $description ) && !is_array( $description ) ) )
{
return false;
}
switch( $type )
{
case 'radio':
if( empty( $description ) )
{
$description = $value;
}
foreach( $value as $key => $item )
{
if( isset( $_POST[$this->id] ) && $_POST[$name] == $item )
{
$checked = "checked";
}
else
{
$checked = "";
}
$return .= '<label><div style="'.$this->radio_value_style.'"><input type="radio" name="'. $name .'" value="'. $item .'" '. $checked .'>'. $description[$key] .'</div></label>';
}
break;
case 'check':
if( empty( $description ) )
{
$description = $value;
}
foreach( $value as $key => $item )
{
if( @in_array( $item, $_POST[$name] ) )
{
$checked = "checked";
}
else
{
$checked = "";
}
$return .= '<label><div style="'.$this->checkbox_value_style.'"><input type="checkbox" name="'. $name .'[]" value="'. $item .'" '. $checked .'>'. $description[$key] .'</div></label>';
}
break;
default:
$return .= '<select style="'.$this->select_style.'" name="'. $name .'">';
if( empty( $description ) )
{
$description = $value;
}
$return .= '<option value="">-- Please Select --</option>';
foreach( $value as $key => $item )
{
if( isset( $_POST[$this->id] ) && $_POST[$name] == $item )
{
$selected = "selected";
}
else
{
$selected = "";
}
$return .= '<option value="'. $item .'" '. $selected .'><div style="'.$this->select_value_style.'">'. $description[$key] .'</div></option>';
}
$return .= '</select>';
}
$return .= '</td></tr>';
$this->_data .= $return;
return true;
}
#######################################################
# checking posted data #
#######################################################
function _check_data( $name, $type, $obligate, $error_msg )
{
if( $type == TRUE && $obligate != FALSE )
{
if( $type == 'check' )
{
$string = "";
$i = "";
if( isset( $_POST[$name] ) )
{
$i = count( $_POST[$name] );
}
while( $i > 0 )
{
$i--;
$string .= 'i';
}
}
else
{
$string = $_POST[$name];
}
$count = strlen( $string );
if( $obligate == 1 )
{
if( $count >= $obligate )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = '<div style="'.$this->error_style.'">'.$error_msg.'</div>';
$this->_error = TRUE;
return false;
}
}
else
{
$obligate = explode( "-", $obligate );
if( !empty( $obligate[1] ) )
{
if( ( $count <= max( $obligate[0], $obligate[1] ) ) && ( $count >= min( $obligate[0], $obligate[1] ) ) )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = '<div style="'.$this->error_style.'">'.$error_msg.'</div>';
$this->_error = TRUE;
return false;
}
}
else
{
if( empty( $obligate[1] ) )
{
if( $count >= $obligate[0] )
{
$this->_error_msg = "";
return true;
}
else
{
$this->_error_msg = '<div style="'.$this->error_style.'">'.$error_msg.'</div>';
$this->_error = TRUE;
return false;
}
}
}
}
}
else
{
$this->_error_msg = "";
return true;
}
}
#######################################################
# Final packing #
#######################################################
function packing()
{
$if_style_exist = "";
if( !empty( $this->if_style_exist ) )
{
$if_style_exist = 'class="'. $this->if_style_exist .'"';
}
if( empty( $this->action ) )
{
$this->action = $_SERVER['REQUEST_URI'];
}
if( !empty( $this->title ) )
{
$result = '<h2>'. $this->title .'</h2>';
}
if( !empty( $this->enctype ) )
{
$enctype = 'enctype="'. $this->enctype .'"';
}
$result .= '<table width="'.$this->t_width.'">'. $this->_data .'<tr><td height="'.$this->td_height.'" width="'.$this->t_left_width.'" valign="top"> </td><td height="'.$this->td_height.'" width="'.$this->t_right_width.'" valign="top">* This information is required<td height="'.$this->td_height.'" valign="top"></td></tr></table>';
$return = '<form action="'. $this->action .'" method="post" target="'. $this->target .'" name="'.$this->form_name.'" id="'.$this->form_name.'" '. $if_style_exist .'>';
$return .= $result;
if( !empty($this->submit_image))
{
$return .= '<input type="hidden" value="1" name="'.$this->id.'"><a href="#" onclick="document.'.$this->form_name.'.submit(); return false;"><img border="0" src="'.$this->submit_image.'"></a>';
}
else
{
$return .= '<input name="'. $this->id .'" type="submit"><input type="reset">';
}
$return .= '</form>';
if( $this->_error == FALSE && $this->hide == TRUE && isset( $_POST[$this->id] ) )
{
return true;
}
else
{
return $return;
}
}
}
?>
Használata:
require( 'ws_form_packer.php' );
$test = new form_packer;
$test->hide = TRUE;
$test->td_height="20";
$test->t_width="600";
$test->t_left_width="50%";
$test->t_right_width="50%";
$test->title_style="font-family:arial; font-size:12px; color:#000000; padding:2px;";
$test->error_style="font-family:arial; font-size:12px; color:#900000; padding:2px; font-weight:bold;";
$test->textbox_style="font-family:arial; font-size:12px; color:#000000; width:300px; height:20px";
$test->textarea_style="font-family:arial; font-size:12px; color:#000000; width:300px; height:200px";
$test->select_style="font-family:arial; font-size:12px; color:#000000;width:200px;";
$test->checkbox_value_style="font-family:arial; font-size:12px; color:#000000;width:200px;";
$test->radio_value_style="font-family:arial; font-size:12px; color:#000000;width:200px;";
$test->select_value_style="font-family:arial; font-size:12px; color:#000000;width:200px;";
$test->submit_image="save.gif";
$test->title = "blubb... blubb...";
$test->id = "aquarium";
$test->single_ins( 'blub1', 'blubber', '8-10', 'text', '50', $_POST[blubl], "error message" );
$test->single_ins( 'blubpper', 'blubber', '8-10', 'password', '30', $_POST[blubl], "error message for 2. thing" );
$test->single_ins( 'blub2', 'blubber', FALSE, 'file' );
$test->single_ins( 'blub3', 'blubber', TRUE, 'textarea', '30x10', $_POST[blubl], "error message for textarea" );
$test->group_ins( 'blub4', 'blubber', array('a','b','c'), TRUE, 'select', "", "error message for select group" );
$test->group_ins( 'blub5', 'blubber', array('a','b','c'), TRUE, 'radio', "", "error message for radio group" );
$test->group_ins( 'blub6', 'blubber', array('a','b','c'), '2', 'check', "", "error message for check group" );
$test->single_ins( 'blub7', 'blubber', '8-10', 'text', '30', $_POST[blubl], "error message" );
$test->single_ins( 'blub8', 'blubber', FALSE, 'file', $_POST[blubl], "error message" );
$test->single_ins( 'blub9', 'blubber', FALSE, 'textarea', '30x10', $_POST[blubl], "error message for textarea" );
$test->group_ins( 'blub10', 'blubber', array('a','b','c'), TRUE, 'select' , "", "error message for select group");
$test->group_ins( 'blub11', 'blubber', array('a','b','c'), TRUE, 'radio', "", "error message for radio group" );
$test->group_ins( 'blub12', 'blubber', array('a','b','c'), '2', 'check', "", "error message for check group" );
echo $test->packing();


