<?
/* Delta Playground, (c) Maciej Łebkowski 2005 */
/* autoforms, automatyczne generowanie formularzy */
/* http://delta.lebkowski.info/autoform */
/* &delta:info; autoform 2.0 SRC:1 CSS:no PHP:yes SQL:mod JS:no AJAX:no */
class autoform{
var $tabela; //wtf?
var $pola; //lista pól, które mają być w formularzu
var $dane; //dane w/w pól.
var $opis; //opis (label) dla pól.
var $html; //tabela template`ów.
var $custom; // custom[zmienna]['element'] = specyficzne ustawienia (templejty)
// dla pól. 'element', 'label', 'input';
// WAŻNE: są to tylko nazwy kluczy z tabeli $html;
var $kryteria; //dane kryteriów poprawności pól 'nazwa'=>'pattern';
function autoform(){
$this->html = array(
'target' => $_SERVER['REQUEST_URI'],
'submit' => '<input type="submit" value="Wy¶lij" class="submit">',
'text' => '<input type="text" name="&zm;" value="&val;" id="&zm;" class="text">',
'file' => '<input type="file" name="&zm;" value="&val;" id="&zm;" class="file">',
'password' => '<input type="password" name="&zm;" value="&val;" id="&zm;" class="password">',
'checkbox' => '<input type="checkbox" value="1" name="&zm;" id="&zm;"&ch; class="check">',
'textarea' => '<textarea name="&zm;" id="&zm;" cols="50" rows="7">&val;</textarea>',
'hidden' => '<input type="hidden" name="&zm;" value="&val;">',
'none' => '',
'select' => 'funkcja',
'enum' => 'funkcja',
'element' => '&label;',
'label' => '<label class="&class;" for="&zm;">&opis; &input;</label>',
'label_ch' => '<label class="&class;" for="&zm;">&input; &opis;</label>',
'label_h' => '&input;',
'form' => '<form id="&nazwa;" action="⌖" method="post">'
.'<div>⊤ &element; &custom; &submit; </div></form>',
'form_plik' => '<form id="&nazwa;" action="⌖" method="post"'
.' enctype="multipart/form-data">'
.'<div>&element; &custom; &submit; </div></form>',
'error' => array(
'empty' => 'Pole nie może pozostać puste',
'numeric' => 'Wpisz liczbę',
'num_or_none' => 'Wpisz liczbę lub pozostaw puste pole'
)
);
$this->kryteria = array(
'empty' => '/.+/',
'numeric' => '/[0-9]+/',
'num_or_none' => '/[0-9]*/',
);
$this->nazwa = 'defaultform';
}
function SetTemplate($tmpname, $kod, $sekcja = ''){
if( $sekcja ) $this->html[$sekcja][$tmpname] = $kod;
else $this->html[$tmpname] = $kod;
}
function CustomTemplate( $n, $tmpname, $kod ){
$this->custom[$n][$tmpname] = $kod;
}
function connect_db($host, $user, $pass, $dbname){
mysql_connect( $host, $user, $pass );
mysql_select_db( $dbname );
$this->dbname = $dbname;
}
function Nazwij( $s ){
if( $s ) $this->nazwa = $s; else unset($this->nazwa);
}
function Submit($submit){
$this->html['submit'] = $submit;
}
function LadujPola( $pola ){
if(!is_array($pola)) $pola = explode( ' ', $pola );
foreach( $pola as $pole ){
$this->pola[]['name'] = $pole;
$this->custom[$pole]['input'] = 'text'; //$this->Rozpoznaj($pole); /*mod*/
}
}
function LadujDane( $dane, $validate = true ){
if( is_array($dane) )
foreach ($dane as $k=>$v) $this->dane[$k] = $v;
if( !$this->expated ) $this->expat( );
return $validate ? $this->validate() : true;
}
function CzyscDane(){
unset($this->dane);
}
function expat(){
foreach ($this->custom as $k => $n ){
$inp = $n['input'];
if(method_exists($this, 'Set'.$inp ) ){
$af = 'Set'.$inp;
$this->custom[$k]['input'] = $this->$af($k);
}
}
$this->expated = true;
}
function Handle( $zmienna, $funkcja, $prefix){
$this->handlers['f_'.$prefix.'_'.$zmienna] = create_function ( '', $funkcja );
/*mod*/
}
function Opis( $zmienna, $opis='' ){
if(!is_array( $zmienna ) )
$_o[$zmienna] = $opis;
else $_o = $zmienna;
foreach( $_o as $zm => $op ) $this->opis[$zm] = $op;
}
function Input( $zmienna, $input, $custom = ''){
foreach( explode( ' ', $zmienna) as $z ){
$this->custom[$z]['input'] = $input;
if( !$this->custom[$z]['label'] )
switch (strtolower( $input )){
case 'checkbox': $this->custom[$z]['label'] = 'label_ch';
break;
case 'hidden': $this->custom[$z]['label'] = 'label_h';
break;
default: break;
}
}
if( $custom ) $this->html[$input] = $custom;
}
function IsSelect( $name, $vals, $desc = array()){
$this->custom[$name]['input'] = 'select';
$this->custom[$name]['vals'] = $vals;
$this->custom[$name]['desc'] = $desc;
}
function SetSelect( $name ){
$this->custom[$name]['input'] = $name;
$vals = $this->custom[$name]['vals'];
$desc = $this->custom[$name]['desc'];
$opts = '';
foreach( $vals as $v ){
list( $ii, $opis ) = each( $desc );
$sel = ($this->dane[$name] == $v) ? ' selected' : '';
$opts .= '<option value="'.$v.'"'.$sel.'>'.($opis ? $opis : $v);
}
$this->html[$name] = '<select id="&zm;" name="&zm;">'.$opts.'</select>';
return $name;
}
function IsEnum( $name, $descs){
$this->custom[$name]['input'] = 'enum';
$this->custom[$name]['descs'] = $descs;
}
function SetEnum( $name ){
$descs = $this->custom[$name]['descs'];
$this->custom[$name]['input'] = $name;
$opts = '';
$cnter = 0;
foreach( $descs as $d ){
$cnter++;
$cn = $name.'_'.$cnter;
$sel = (!empty($this->dane[$cn])) ? ' checked' : '';
$opts .= '<label for="'.$cn.'"><input type="checkbox" value="'.$d.'"'
.' name="'.$cn.'" id="'.$cn.'"'.$sel.'>'.$d.'</label>';
if( $v = $this->dane[$cn] ) $this->dane[$name] .= ','.$v;
}
$this->dane[$name] = trim ($this->dane[$name], ',' );
$this->html[$name] = '<fieldset id="&zm;"><input name="&zm;" value="&val;" type="hidden">'
.$opts.'</fieldset>';
return $name;
}
function Options( $zm, $wart, $opis = array() ){
foreach( $wart as $w ) {
list($i,$o) = each( $opis );
$this->custom[$zm]['select'] = array( 'n'=>$w, 'o' => $o ? $o : $w );
}
}
function Kryterium( $pola, $nazwa, $custom = '', $opis = '' ){
if( !is_array( $pola ) ) $pola = explode( ' ', $pola );
foreach ($pola as $p){
$this->custom[$p]['kryt'] = $nazwa;
}
if($custom) $this->kryteria[$nazwa] = $custom;
if($opis) $this->html['error'][$nazwa] = $opis;
}
function Rozpoznaj($p){
if( $iname = $this->custom[$p['name']]['input'] )
return $iname;
if( $p['type'] == 'int' && $p['len'] == 1 )
return 'checkbox';
if( strpos (' blob text', $p['type'] ) )
return 'textarea';
return 'text';
}
function validate(){
unset( $this->notice );
foreach( $this->pola as $pole ){
$k = $pole['name'];
$v = $this->dane[$k];
/* if( strpos( $this->kryteria['empty'], $k ) and empty( $v ) )
$this->notice[$k] = 'Pole nie może pozostać puste!';
if( strpos( $this->kryteria['int'], $k ) and !is_int($v) )
$this->notice[$k] = 'Wipisz liczbę!';
*/
$kname = $this->custom[$k]['kryt'];
if( $reg = $this->kryteria[$kname] )
if( !preg_match( $reg, $v ) )
$this->notice[$k] = $this->html['error'][$kname] ? $this->html['error'][$kname]
: 'Pole ma nieprawidłowy format';
/* if( $func = $this->func['validate_'.$k] )
$this->notice[$k] = $func($v);
*/ }
if( $this->notice ) return false; else {
$set = 'set ';
$func = $this->html['func'];
foreach( $this->pola as $pole ){
$v = $this->dane[$pole['name']];
$fname = 'set_'.$pole['name'];
if( is_array( $func ) and array_key_exists( $fname, $func ) ){
$f = create_function( '',
str_replace( '&val;', $v, $func[$fname] ) );
$set .= $f();
} else
$set.='`'.$pole['name'].'`='."'".$v."', ";
}
}
return trim( $set, ', ' );
}
function pokaz( $add = ''){
$this->html['custom'] .= $add; /*mod*/
$form = $this->html['form'];
$form = str_replace( '⌖', $this->html['target'], $form );
$form = str_replace( '&nazwa;', $this->nazwa, $form );
$form = str_replace( '&submit;', $this->html['submit'], $form );
$form_body = '';
if( ! $this->expated )$this->expat( );
foreach( $this->pola as $pole ){
$n = $pole['name'];
$ele = $this->custom[$n]['element']
? $this->custom[$n]['element'] : $this->html['element'];
$lab = $this->custom[$n]['label']
? $this->custom[$n]['label'] : 'label';
$inp = $this->custom[$n]['input']
? $this->custom[$n]['input'] : $this->html['input'];
$h = str_replace( '&input;',$this->html[$inp],
str_replace( '&label;', $this->html[$lab], $ele ) );
$h = str_replace('&zm;', $n, $h );
$h = str_replace('&val;', htmlspecialchars($this->dane[$n]), $h );
$h = str_replace('&ch;', $this->dane[$n] ? ' checked' : '', $h );
$notice = $this->notice[$n];
/*mod*/ $opis = $this->opis[$n];
$opis = $opis ? $opis : ucfirst( str_replace( '_', ' ', $pole['name'] ) );
if ($notice) {
$h = str_replace( '&class;', 'notice', $h );
$opis.= ' <br>'.$notice ;
} else $h = str_replace( '&class;', 'normal', $h );
$h = str_replace('&opis;', $opis, $h );
$form_body .= $h." \n";
}
$this->html['custom'] .= ' <input type="hidden" name="autoform" value="'.$this->nazwa.'">';
$form = str_replace( '&custom;', $this->html['custom'], $form );
$form = str_replace( '⊤', $this->html['top'], $form );
$form = str_replace( '&element;', $form_body, $form );
return $form;
}
}
?>