Crear función en GAMBAS3 para crear el código para el archivo controlador en php8

 


Ya hemos visto como crear el archivo modelo en base a la nueva tabla

Para crear el contenido del archivo controlador la lógica es igual


Creamos el modulo creadorControlador dentro de la carpeta módulos


Y metemos el siguiente código




 


' Gambas module file

Public Function creaControlador() As String
  
  Dim strControlador As String

  Dim conexion As New Connection
  Dim strDatos As String

  With conexion
    .Type = "mysql"
    .Port = "3306"
    .Host = Settings["servidor"]
    .User = Settings["usuario"]
    .Password = Settings["password"]
    .Name = Settings["baseDeDatos"]
    .Open()
    End With
    
    Dim $result As Result

  

  $result = conexion.Exec("describe " & FMain.txtTabla.Text)

Dim contador As Integer
Dim strSegundoCampo As String
Dim strDatosEditas As String

contador = 0

While $result.Length > contador
  
  strDatos &= " $datos[\"nuevo" & UCase(Mid$($result["Field"], 1, 1)) & Mid$($result["Field"], 2, 100) & "\"] =  $_POST[ \"nuevo" & UCase(Mid$($result["Field"], 1, 1)) & Mid$($result["Field"], 2, 100) & "\"];" & gb.CrLf 
  
  If contador == 1 
    ' SEGUNDO CAMPO
    strSegundoCampo = $result["Field"]
  End If
  
   strDatos &= " $datos[\"" & UCase(Mid$($result["Field"], 1, 1)) & Mid$($result["Field"], 2, 100) & "\"] =  $_POST[ \"editar" & UCase(Mid$($result["Field"], 1, 1)) & Mid$($result["Field"], 2, 100) & "\"];" & gb.CrLf 
  

  $result.MoveNext
  contador = contador + 1


Wend
    
  strControlador &= "" & "<?php" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "class Controlador" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & " {" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " /*=============================================" & gb.CrLf
  strControlador &= "" & " REGISTRO" & gb.CrLf
  strControlador &= "" & " =============================================*/" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " static public function ctrIngresar(){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  if(isset($_POST[\"nuevo" & UCase(Mid$(strSegundoCampo, 1, 1)) & Mid$(strSegundoCampo, 2, 100) & "\"])){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    $tabla = \"" & FMain.txtTabla.Text & "\";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
 
 'LLENA ARREGLO
  strControlador &= "" & strDatos & gb.CrLf
 
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    $respuesta = Modelo" & utilerias.strPrimeraMayuscula(FMain.txtTabla.Text) & "::mdlIngresar($tabla, $datos);" & gb.CrLf
  strControlador &= "" & "    " & gb.CrLf
  strControlador &= "" & "    " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    if($respuesta == \"ok\"){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     echo '<script>" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     swal({" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      type: \"success\"," & gb.CrLf
  strControlador &= "" & "      title: \"Guardado correctamente!\"," & gb.CrLf
  strControlador &= "" & "      showConfirmButton: true," & gb.CrLf
  strControlador &= "" & "      confirmButtonText: \"Cerrar\"" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     }).then(function(result){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      if(result.value){" & gb.CrLf
  strControlador &= "" & "      " & gb.CrLf
  strControlador &= "" & "       window.location = \"" & FMain.txtTabla.text & " \";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     });" & gb.CrLf
  strControlador &= "" & "    " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     </script>';" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    }else{" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "                                    echo '<script>" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     swal({" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      type: \"error\"," & gb.CrLf
  strControlador &= "" & "      title: \"'.$respuesta.'\"," & gb.CrLf
  strControlador &= "" & "      showConfirmButton: true," & gb.CrLf
  strControlador &= "" & "      confirmButtonText: \"Cerrar\"" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     }).then(function(result){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      if(result.value){" & gb.CrLf
  strControlador &= "" & "      " & gb.CrLf
  strControlador &= "" & "       window.location = \"" & FMain.txtTabla.text & "\";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     });" & gb.CrLf
  strControlador &= "" & "    " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     </script>';" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "                                } " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   " & gb.CrLf
  strControlador &= "" & "   }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " /*=============================================" & gb.CrLf
  strControlador &= "" & " MOSTRAR " & gb.CrLf
  strControlador &= "" & " =============================================*/" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " static public function ctrMostrar($item, $valor){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  $tabla = \"" & FMain.txtTabla.text & " \"; " & gb.CrLf 
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  $respuesta = Modelo" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "::mdlMostrar($tabla, $item, $valor); " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  return $respuesta;" & gb.CrLf
  strControlador &= "" & " }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
 
 
 
  strControlador &= "" & " /*=============================================" & gb.CrLf
  strControlador &= "" & " EDITAR " & gb.CrLf
  strControlador &= "" & " =============================================*/" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " static public function ctrEditar(){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  if(isset($_POST[\"editar" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "\"])){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    $tabla = \"" & FMain.txtTabla.Text & "\";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  $datos = $_POST;" & gb.CrLf 
  strControlador &= "" & "          " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    $respuesta = Modelo" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "::mdlEditar($tabla, $datos); " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    if($respuesta == \"ok\"){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     echo'<script>" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     swal({" & gb.CrLf
  strControlador &= "" & "        type: \"success\"," & gb.CrLf
  strControlador &= "" & "        title: \"Editado correctamente\"," & gb.CrLf
  strControlador &= "" & "        showConfirmButton: true," & gb.CrLf
  strControlador &= "" & "        confirmButtonText: \"Cerrar\"" & gb.CrLf
  strControlador &= "" & "        }).then(function(result) {" & gb.CrLf
  strControlador &= "" & "         if (result.value) {" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "         window.location = \"" & FMain.txtTabla.text & " \";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "         }" & gb.CrLf
  strControlador &= "" & "        })" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     </script>';" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    }" & gb.CrLf
    strControlador &= "" & "   else{" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    echo'<script>" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "     swal({" & gb.CrLf
  strControlador &= "" & "        type: \"error\"," & gb.CrLf
  strControlador &= "" & "        title: \"'.$respuesta.'\"," & gb.CrLf
  strControlador &= "" & "        showConfirmButton: true," & gb.CrLf
  strControlador &= "" & "        confirmButtonText: \"Cerrar\"" & gb.CrLf
  strControlador &= "" & "        }).then(function(result) {" & gb.CrLf
  strControlador &= "" & "       if (result.value) {" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "       window.location = \"" & FMain.txtTabla.text & "\";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "       }" & gb.CrLf
  strControlador &= "" & "      })" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "      </script>';" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "}" & gb.CrLf

  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " /*=============================================" & gb.CrLf
  strControlador &= "" & " BORRAR " & gb.CrLf
  strControlador &= "" & " =============================================*/" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " static public function ctrBorrar(){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  if(isset($_GET[\"borrar" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "\"])){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   $tabla =\"" & FMain.txtTabla.Text & " \";" & gb.CrLf
  strControlador &= "" & "   $datos = $_GET[\"id" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "\"];" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   $respuesta = Modelo" & UCase(Mid$(FMain.txtTabla.text, 1, 1)) & Mid$(FMain.txtTabla.text, 2, 100) & "::mdlBorrar($tabla, $datos); " & gb.CrLf 
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   if($respuesta == \"ok\"){" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    echo'<script>" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    swal({" & gb.CrLf
  strControlador &= "" & "       type: \"success\"," & gb.CrLf
  strControlador &= "" & "       title: \"Borrado correctamente\"," & gb.CrLf
  strControlador &= "" & "       showConfirmButton: true," & gb.CrLf
  strControlador &= "" & "       confirmButtonText: \"Cerrar\"," & gb.CrLf
  strControlador &= "" & "       closeOnConfirm: false" & gb.CrLf
  strControlador &= "" & "       }).then(function(result) {" & gb.CrLf
  strControlador &= "" & "        if (result.value) {" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "        window.location = \"" & FMain.txtTabla.text & " \";" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "        }" & gb.CrLf
  strControlador &= "" & "       })" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "    </script>';" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "   }  " & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "  }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & " }" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "" & gb.CrLf
  strControlador &= "" & "}" & gb.CrLf
  
  
  strControlador &= "" & " // BUSCA " & utilerias.strPrimeraMayuscula(FMain.txtTabla.Text) & "" & gb.CrLf

  strControlador &= "" & " if(isset($_POST[\"buscar" & utilerias.strPrimeraMayuscula(FMain.txtTabla.Text) & "\"])) {" & gb.CrLf
  strControlador &= "" & "   " & gb.CrLf
  strControlador &= "" & "  include '../modelos/" & FMain.txtTabla.Text & ".modelo.php';" & gb.CrLf
  strControlador &= "" & "  " & gb.CrLf
  strControlador &= "" & "  $valor = $_POST[\"id" & utilerias.strPrimeraMayuscula(FMain.txtTabla.Text) & "\"];" & gb.CrLf
  strControlador &= "" & "  " & gb.CrLf
  strControlador &= "" & " $respuesta = Modelo" & utilerias.strPrimeraMayuscula(FMain.txtTabla.Text) & "::mdlMostrar(\"" & FMain.txtTabla.Text & "\",\"id\",$valor);" & gb.CrLf
  strControlador &= "" & "        " & gb.CrLf
  strControlador &= "" & " echo json_encode($respuesta);" & gb.CrLf
  strControlador &= "" & " " & gb.CrLf
  strControlador &= "" & "}" & gb.CrLf
  
  Return strControlador

  
End



Comentarios

Entradas populares