<?php
class Formulario {
  function Cargar($fichero){
    if (file_exists($fichero)) {
      $xml_indice = simplexml_load_file($fichero);
      foreach ($xml_indice->form as $FORM) {     
        $id   = (string) $FORM["id"];
        $this->Tabla[$id]["nombre"] = (string)$FORM["nombre"];
        $this->Tabla[$id]["tipo"] = (string)$FORM["tipo"];
        $this->Tabla[$id]["href"] = (string) str_replace("^", "&", $FORM["href"]);
        $this->Tabla[$id]["get"] = (string) str_replace("^", "&", $FORM["get"]);
        $this->Tabla[$id]["style"] = (string)$FORM["style"];
        $this->Tabla[$id]["tabla"] = (string)$FORM["tabla"];
        $this->Tabla[$id]["clase"] = (string)$FORM["clase"];
        $this->Tabla[$id]["ancho"] = (string)$FORM["ancho"];
        $this->Tabla[$id]["nivel"] = (string)$FORM["nivel"];
        $this->Tabla[$id]["fondo"] = (string)$FORM["fondo"];
        $this->Tabla[$id]["enctype"] = (string)$FORM["enctype"];
        foreach ($FORM->campo as $CAMPO) { 
          $orden = (string) $CAMPO["orden"];              
          $this->Campos[$id][$orden]["label"] = (string)$CAMPO["label"];
          $this->Campos[$id][$orden]["tipo"] = (string)$CAMPO["tipo"];
          $this->Campos[$id][$orden]["obligatorio"] = (string)$CAMPO["obligatorio"];
          $this->Campos[$id][$orden]["protegido"] = (string)$CAMPO["protegido"];
          $this->Campos[$id][$orden]["min"] = (string)$CAMPO["min"];
          $this->Campos[$id][$orden]["max"] = (string)$CAMPO["max"];
          $this->Campos[$id][$orden]["defecto"] = (string)$CAMPO["defecto"];
          $this->Campos[$id][$orden]["len"] = (string)$CAMPO["len"];
          $this->Campos[$id][$orden]["maxlen"] = (string)$CAMPO["maxlen"];
          $this->Campos[$id][$orden]["filas"] = (string)$CAMPO["filas"];
          $this->Campos[$id][$orden]["columnas"] = (string)$CAMPO["columnas"];
          $this->Campos[$id][$orden]["accion"] = (string) str_replace("^", "&", $CAMPO["accion"]);
          $this->Campos[$id][$orden]["fuente"] = (string)$CAMPO["fuente"];
          $this->Campos[$id][$orden]["texto"] = (string)$CAMPO["texto"];
          $this->Campos[$id][$orden]["grupo"] = (string)$CAMPO["grupo"];
          $this->Campos[$id][$orden]["step"] = (string)$CAMPO["step"];
          $this->Campos[$id][$orden]["ctl"] = (string)$CAMPO["ctl"];  // Aplicar o no la HTMLSPECIALCHARS()
          $this->Campos[$id][$orden]["accept"] = (string)$CAMPO["accept"];  // tipos de ficheros a aceptar
        }
      }
    }   
    else {die ("(OBJ_Formularios) Error de carga del fichero de formularios");}
    $this->valores = array();
  }
  function Crear_Formulario($formulario, $FORM_Tabla, $FORM_Campos, $FORM_Valores, $TABLAS_Select){
   $this->Salida = "";
   foreach ($FORM_Campos[$formulario] as $campo => $v) {
     if ($FORM_Campos[$formulario][$campo]['defecto'] != ""){$wk_defecto = $FORM_Campos[$formulario][$campo]['defecto'];} else {$wk_defecto = "";}
     $valor = "k_".$campo;
     if (!isset($$valor)){$$valor = $wk_defecto;}
   }
   $this->Salida .= "<form action='".$FORM_Tabla[$formulario]['href'].$FORM_Tabla[$formulario]['get']."' name='form_".$formulario."' method='post' ";
   if (isset($FORM_Tabla[$formulario]['enctype'])){$this->Salida .= "enctype='".$FORM_Tabla[$formulario]['enctype']."'";}
   $this->Salida .= ">\r\n";
   foreach ($FORM_Campos[$formulario] as $campo => $v) { // Genera los campos Hidden
     if (strtoupper($FORM_Campos[$formulario][$campo]['tipo'] == "H")){
       if (isset($FORM_Valores["k_".$campo])){$valor = $FORM_Valores["k_".$campo];} else {$valor = "";}
       $this->Salida .= "<input type='hidden' name='i_".$campo."' value='".$valor."' />\r\n";
      }
   }    
   if (strtoupper($FORM_Tabla[$formulario]['tabla']) == "SI"){
     if ($FORM_Tabla[$formulario]['fondo'] != ""){$w_fondo = "style='background:".$FORM_Tabla[$formulario]['fondo'].";'";} else {$w_fondo = "";}
     if ($FORM_Tabla[$formulario]['clase'] != ""){$this->Salida .= "<table class='".$FORM_Tabla[$formulario]['clase']."' ".$w_fondo.">\r\n";}
     else {
       if ($FORM_Tabla[$formulario]['ancho'] != ""){$w_ancho = "width='".$FORM_Tabla[$formulario]['ancho']."'";} else {$w_ancho = "";}
       if ($FORM_Tabla[$formulario]['nivel'] != ""){$w_border = "border='".$FORM_Tabla[$formulario]['nivel']."'";} else {$w_border = "";}
       $this->Salida .= "<table ".$w_fondo." ".$w_ancho." ".$w_border.">\r\n";
     }
     $w_nivel = 1;
     $max_nivel = $FORM_Tabla[$formulario]['nivel'];
     $sw_tabla = "S";     
   }
   else {
     $w_nivel = 0;
     $max_nivel = 0;
     $sw_tabla = "N";     
   }
   $sw_grupo = "N";
   $sw_cerrar = "N";
   foreach ($FORM_Campos[$formulario] as $campo => $v) {
     if (strtoupper($FORM_Campos[$formulario][$campo]['obligatorio']) == "SI"){$wk_obligatorio = "required";} else {$wk_obligatorio = "";}
     if (strtoupper($FORM_Campos[$formulario][$campo]['protegido']) == "SI"){$wk_protegido = "readonly";} else {$wk_protegido = "";}
     if ($FORM_Campos[$formulario][$campo]['min'] != ""){$wk_min = "min='".$FORM_Campos[$formulario][$campo]['min']."'";} else {$wk_min = "";}
     if ($FORM_Campos[$formulario][$campo]['max'] != ""){$wk_max = "max='".$FORM_Campos[$formulario][$campo]['max']."'";} else {$wk_max = "";}
     if ($FORM_Campos[$formulario][$campo]['len'] != ""){$wk_len = "size='".$FORM_Campos[$formulario][$campo]['len']."'";} else {$wk_len = "";}
     if ($FORM_Campos[$formulario][$campo]['maxlen'] != ""){$wk_maxlen = "maxlength='".$FORM_Campos[$formulario][$campo]['maxlen']."'";} else {$wk_maxlen = "";}
     if ($FORM_Campos[$formulario][$campo]['filas'] != ""){$wk_filas = "rows='".$FORM_Campos[$formulario][$campo]['filas']."'";} else {$wk_filas = "";}
     if ($FORM_Campos[$formulario][$campo]['columnas'] != ""){$wk_columnas = "cols='".$FORM_Campos[$formulario][$campo]['columnas']."'";} else {$wk_columnas = "";}
     if ($FORM_Campos[$formulario][$campo]['step'] != ""){$wk_step = "step='".$FORM_Campos[$formulario][$campo]['step']."'";} else {$wk_step = "";}
     if ($FORM_Campos[$formulario][$campo]['accept'] != ""){$wk_accept = "accept='".$FORM_Campos[$formulario][$campo]['accept']."'";} else {$wk_accept = "";}
     if ($FORM_Campos[$formulario][$campo]['accion'] != ""){
       if (strtoupper($FORM_Campos[$formulario][$campo]['tipo'] == "L")){$wk_accion = "OnChange='".$FORM_Campos[$formulario][$campo]['accion']."'";}
       elseif ($FORM_Campos[$formulario][$campo]['tipo'] == "F"){$wk_accion = "OnBlur='".$FORM_Campos[$formulario][$campo]['accion']."'";}
//       elseif ($FORM_Campos[$formulario][$campo]['tipo'] != "B" AND $FORM_Campos[$formulario][$campo]['tipo'] != "S" AND $FORM_Valores["k_".$campo] == ""){$wk_accion = "OnClick='".$FORM_Campos[$formulario][$campo]['accion']."'";}
//       else {$wk_accion = $FORM_Valores["k_".$campo];}
       else {$wk_accion = "OnClick='".$FORM_Campos[$formulario][$campo]['accion']."'";}
     }
     else {
       switch ($FORM_Campos[$formulario][$campo]['tipo']){  
         case "Z":
         case "S":
         case "B":
           $wk_accion = "";
           break;
         default:
           if ($FORM_Valores["k_".$campo] != ""){$wk_accion = $FORM_Valores["k_".$campo];}
           else {$wk_accion = "";}
           break;
       }
     }
     if ($FORM_Campos[$formulario][$campo]['defecto'] != ""){$valor = $FORM_Campos[$formulario][$campo]['defecto'];} else {$valor = "";}
     if (isset($FORM_Valores["k_".$campo])){$valor = $FORM_Valores["k_".$campo];}
     switch (strtoupper($FORM_Campos[$formulario][$campo]['tipo'])){
       case "T": // Tipo Texto
         $wk_salida = "<input type='text' name='i_".$campo."' value='".$valor."' ".$wk_len." ".$wk_maxlen." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "N": // Tipo Numerico
         $wk_salida = "<input type='number' name='i_".$campo."' value='".$valor."' ".$wk_min." ".$wk_max." ".$wk_step." ".$wk_len." ".$wk_maxlen." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "K": // Tipo Checkbox
         if ($valor == "S"){$chk = "checked";} else {$chk = "";}
         $wk_salida = "<input type='checkbox' value='S' name='i_".$campo."' ".$chk." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "Y": // Tipo Radio
         $gr = $FORM_Campos[$formulario][$campo]['grupo'];
         if ($FORM_Valores["k_".$gr] == $FORM_Campos[$formulario][$campo]['defecto']){$chk = "checked";} else {$chk = "";}
         $wk_salida = "<input type='radio' value='".$FORM_Campos[$formulario][$campo]['defecto']."' name='i_".$FORM_Campos[$formulario][$campo]['grupo']."' ".$chk."/> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "E": // Tipo Email
         $wk_salida = "<input type='email' name='i_".$campo."' value='".$valor."' ".$wk_len." ".$wk_maxlen." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "F": // Tipo Fichero
         $wk_salida = "<input type='file' name='i_".$campo."' ".$wk_accept." ".$wk_obligatorio." ".$wk_accion." />";
         break;
       case "D": // Tipo Fecha
         $wk_salida = "<input type='date' name='i_".$campo."' value='".$valor."' ".$wk_min." ".$wk_max." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "I": // Tipo Hora
         $wk_salida = "<input type='time' name='i_".$campo."' value='".$valor."' ".$wk_min." ".$wk_max." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "C": // Tipo Color
         $wk_salida = "<input type='color' name='i_".$campo."' value='".$valor."' ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "H": // Tipo Oculto
         $wk_salida = "";
         break;
       case "A": // Tipo TextArea
         $wk_salida = "<textarea name='i_".$campo."' ".$wk_filas." ".$wk_columnas." ".$wk_obligatorio." />".$valor."</textarea>\r\n";
         break;
       case "S": // Tipo Submit
         $wk_salida = "<input type='submit' value='".$FORM_Campos[$formulario][$campo]['label']."' />\r\n";
         break;
       case "R": // Tipo Reset
         $wk_salida = "<input type='reset' value='".$FORM_Campos[$formulario][$campo]['label']."' />\r\n";
         break;
       case "B": // Tipo Buttom
         $wk_salida = "<input type='button' value='".$FORM_Campos[$formulario][$campo]['label']."' ".$wk_accion." />\r\n";
         break;
       case "L": // Tipo Select
         $wk_salida = "<select name='i_".$campo."' ".$wk_len." ".$wk_accion.">\r\n";
         $wk_salida .= "<option value=''></option>\r\n";
         $w_tabla = $FORM_Campos[$formulario][$campo]['fuente'];
         for ($i=0; $i<count($TABLAS_Select[$w_tabla]); $i++){
           if ($TABLAS_Select[$w_tabla][$i][0] == $valor){$sel = "selected";} else {$sel = "";}
           $wk_salida .= "<option ".$sel." value='".$TABLAS_Select[$w_tabla][$i][0]."'>".$TABLAS_Select[$w_tabla][$i][1]."</option>\r\n";
         }
         $wk_salida .= "</select> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "P": // Tipo Password
         $wk_salida = "<input type='password' name='i_".$campo."' value='".$valor."' ".$wk_len." ".$wk_maxlen." ".$wk_obligatorio." ".$wk_protegido." /> ".$FORM_Campos[$formulario][$campo]['texto']."\r\n";
         break;
       case "Z": // Tipo etiqueta de texto
         $wk_salida = $FORM_Campos[$formulario][$campo]['texto'];
         $wk_salida = str_replace("/", "<strong>", $wk_salida);
         $wk_salida = str_replace("|", "</strong>", $wk_salida);
         break;
       default:
         die ("(OBJ_Formularios) Error de creacion de campos. Tipo erroneo");
         exit();
     }
     if ($sw_tabla == "S"){
       switch (strtoupper($FORM_Campos[$formulario][$campo]['tipo'])){
         case "B":
         case "S":
         case "R":
           if ($sw_grupo == "N"){
             $this->Salida .= "<tr><td colspan=2 align='center'>".$wk_salida;
             $sw_cerrar = "S";
           }
           else {
             $this->Salida .= $wk_salida." ";
           }
           $sw_grupo = "S";
           break;
         case "H":
           break;
         case "Z":
           if ($sw_cerrar == "S"){
             $this->Salida .= "</td></tr>";
             $sw_cerrar = "N";
           }
           $this->Salida .= "<tr><td colspan=2>".$wk_salida."</td></tr>\r\n";
           $sw_grupo = "N";             
           break;
         default:
           if ($sw_cerrar == "S"){
             $this->Salida .= "</td></tr>";
             $sw_cerrar = "N";
           }
           $this->Salida .= "<tr><td>".$FORM_Campos[$formulario][$campo]['label']."</td>\r\n";
           $this->Salida .= "<td>".$wk_salida."</td></tr>\r\n";
           $sw_grupo = "N";
           break;
       }
     }
   }
   if ($sw_cerrar == "S"){
     $this->Salida .= "</td></tr>";
     $sw_cerrar = "N";
   }
   if (strtoupper($FORM_Tabla[$formulario]['tabla']) == "SI"){$this->Salida .= "</table>\r\n";}  
   $this->Salida .= "</form>\r\n";
  }
  function Entrada_Formulario($formulario, $FORM_Tabla, $FORM_Campos){
   $this->Salida = "";
   foreach ($FORM_Campos[$formulario] as $campo => $v) {
     switch ($FORM_Campos[$formulario][$campo]['tipo']){
       case "T":
       case "N":
       case "K":
       case "Y":
       case "E":
       case "C":
       case "A":
       case "H":
       case "L":
       case "P":
       case "I":
         if ($FORM_Campos[$formulario][$campo]['ctl'] == "no"){
           $this->valores["k_".$campo] = $_POST["i_".$campo];
         }
         else {
           $this->valores["k_".$campo] = htmlspecialchars($_POST["i_".$campo]);
         }
         break;
       case "D":
         $this->valores["k_".$campo] = htmlspecialchars($_POST["i_".$campo]);
         if ($this->valores["k_".$campo] == ""){$this->valores["k_".$campo] = "0001/01/01";}
         break;
       case "F":
       case "Z":
         break;
     }
   }
  }
}