| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?php
- /*
- Desarrollador: Ing. Jean Jairo Benitez Meza
- Ultima Modificación: 11/04/2023
- Módulo: Formularios Dinámicos
- */
- namespace App\Http\Controllers;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\ResponseController;
- use App\Http\Controllers\EncryptionController;
- use Illuminate\Http\Request;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Validator;
- use App\Http\Controllers\FunctionsController;
- use App\Http\Controllers\ResourcesController;
- class TableController extends Controller
- {
- private $responseController;
- private $encController;
- private $functionsController;
- private $resourcesController;
-
- public function __construct(){
- $this->responseController = new ResponseController();
- $this->encController = new EncryptionController();
- $this->functionsController = new FunctionsController();
- $this->resourcesController = new ResourcesController();
- }
-
- public function getTables($user, $line){
- $arrResponseCheckUser = $this->resourcesController->checkUserEnc($user, $line);
- if ($arrResponseCheckUser['error']) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, $arrResponseCheckUser['msg'], [], 401);
- }
- try {
- $arrTables = DB::table('S002V01TTABL')
- ->where('TABL_NULI', '=', $line)
- ->get([
- 'TABL_IDTA AS CODIGO_TABLA',
- 'TABL_NOMB AS NOMBRE_TABLA',
- 'TABL_FERE AS FECHA_REGISTRA',
- 'TABL_FEMO AS FECHA_MODIFICA',
- 'TABL_USRE AS USUARIO_REGISTRA',
- 'TABL_USMO AS USUARIO_MODIFICA',
- 'TABL_ESTA AS ESTADO'
- ]);
- $arrTables = json_decode(json_encode($arrTables), true);
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_TABLE_GET000: Ocurrió un error al consultar los datos.", $th->getMessage(), 500);
- }
- $responseCheckLatestUpdate = $this->resourcesController->checkLatestUpdate($arrTables, $line);
- if ($responseCheckLatestUpdate['error']) {
- return $this->responseController->makeResponse(true, $responseCheckLatestUpdate['msg'], [], 500);
- }
- $arrTables = $responseCheckLatestUpdate['response'];
- foreach ($arrTables as $key => $table) {
- $arrColumns = DB::getSchemaBuilder()->getColumnListing($table['NOMBRE_TABLA']);
- $table['COLUMNS'] = $arrColumns;
- $arrTables[$key] = $table;
- }
- return $this->responseController->makeResponse(false, "ÉXITO", $arrTables);
- }
- public function getColumnsByTables(Request $request){
- $validator = Validator::make($request->all(), [
- 'tables' => 'required|array',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_TABLE_GET000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- try {
- $form = $request->all();
- $resp = array();
- foreach ($form['tables'] as $table) {
- $data = array();
- $data['table'] = $table;
- $data['columns'] = [];
- $column_response = $this->getColumns(strtolower( $table));
- if ( $column_response['error']) return $this->responseController->makeResponse(false, $column_response['msg']);
- $columns = $column_response['response'];
- foreach ($columns as $column) {
- $nameColumn = explode('_', $column->COLUMN_NAME)[1];
- if ($nameColumn !== 'INEX'){
- $data['columns'][] = $column->COLUMN_NAME;
- }
- }
- $resp[] = $data;
- }
- } catch (\Throwable $th) {
- return $this->responseController->makeResponse(true, "ERR_TABLE_GET000: Ocurrió un error al consultar los datos.", $th->getMessage(), 500);
- }
- return $this->responseController->makeResponse(false, "ÉXITO", $resp);
- }
- public function getColumnsExtra(Request $request) {
- $validator = Validator::make($request->all(), [
- 'numberForm' => 'required|integer',
- 'tables' => 'required|array',
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- $form = $request->all();
- $data = array();
- $data['table'] = 'NA';
- $data['type'] = 'JSON';
- $getColumnsExtra = DB::table('S002V01TFODI')->where('FODI_NUFO', '=', $form['numberForm'])->first('FODI_DAFO');
- $arrFields = json_decode($getColumnsExtra->FODI_DAFO);
- foreach ($arrFields->fields as $keyFields => $fields) {
- foreach ($fields->form as $keyForm => $form) {
- if ($form->data_table->table == 'NA') {
- $data['columns'][] = $form->data_table->column;
- }
- }
- }
- return $this->responseController->makeResponse(false, "ÉXITO", $data);
- }
- public function createTable(Request $request){
- $validator = Validator::make($request->all(), [
- 'table_name' => 'required|string',
- 'user' => 'required|string'
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_TABLE_REG000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $request_form = $request->all();
- $table_name = $this->encController->decrypt($request_form['table_name']);
- try {
- $user = $this->encController->decrypt($request_form['user']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_REG001: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- try {
- $validateInsert = DB::table('S002V01TTABL')->insert([
- "TABL_NULI" => 1,
- "TABL_NOMB" => $table_name,
- "TABL_USRE" => $user,
- "TABL_FERE" => $currentDate,
- "TABL_FEAR" => DB::raw('CURRENT_TIMESTAMP')
- ]);
- if (!$validateInsert) {
- return $this->responseController->makeResponse(true, "ERR_TABLE_REG002: No se pudo realizar la inserción en la base.", [], 500);
- }
- }catch (\PDOException $e){
- if($e->getCode() == 23000){
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_REG003: La tabla ya ha sido registrada anteriormente.", $e->getMessage(), 500);
- }else{
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_REG004: Ocurrió un error al consultar los datos.", $e->getMessage(), 500);
- }
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_REG005: Ocurrió un error al consultar los datos.", [], 500);
- }
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Registro correcto");
- }
- public function updateTable(Request $request){
- $validator = Validator::make($request->all(), [
- 'table_code' => 'required|string',
- 'table_name' => 'required|string',
- 'user' => 'required|string'
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_TABLE_UPD000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $request_form = $request->all();
- $table_name = $this->encController->decrypt($request_form['table_name']);
- try {
- $user = $this->encController->decrypt($request_form['user']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD001: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
- try {
- $data_table = DB::table('S002V01TTABL')->where('TABL_IDTA', '=', $request_form['table_code'])->first();
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD002: Ocurrió un error al obtener los datos de la base.", $th->getMessage(), 500);
- }
- if (empty($data_table)) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD003: El nombre de la tabla no existe.", [], 500);
- }
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- try{
- $validateUpdate = DB::table('S002V01TTABL')->where('TABL_IDTA', '=', $request_form['table_code'])->update([
- "TABL_NOMB" => $table_name,
- "TABL_USMO" => $user,
- "TABL_FEMO" => $currentDate,
- "TABL_FEAR" => DB::raw('CURRENT_TIMESTAMP')
- ]);
- if (!$validateUpdate) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD004: No se pudo realizar la inserción en la base.", [], 500);
- }
- }catch (\PDOException $e){
- if($e->getCode() == 23000){
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD005: La tabla ya ha sido registrada anteriormente.", $e->getMessage(), 500);
- }else{
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD006: Ocurrió un error al consultar los datos.", $e->getMessage(), 500);
- }
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD007: Ocurrió un error al consultar los datos.", $th->getMessage(), 500);
- }
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Modificación correcta");
- }
- public function updateStateTables(Request $request){
- $validator = Validator::make($request->all(), [
- 'table_name' => 'required|string',
- 'state' => 'required|string',
- 'user' => 'required|string'
- ]);
- if ($validator->fails()) {
- return $this->responseController->makeResponse(
- true,
- "ERR_TABLE_UPD000: Se encontraron uno o más errores.",
- $this->responseController->makeErrors($validator->errors()->messages()),
- 401
- );
- }
- DB::beginTransaction();
- $form = $request->all();
- try{
- $valid_tb = DB::table('S002V01TTABL')
- ->where('TABL_NOMB', $form['table_name'])
- ->first();
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD001: Ocurrió un error al hacer la consulta en la base.", $th->getMessage(), 500);
- }
- if (empty($valid_tb)) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD002: El nombre de la tabla ".$form['table_name']." no existe.", [], 500);
- }
- try {
- $user = $this->encController->decrypt($form['user']);
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD003: No se pudo obtener el usuario.", $th->getMessage(), 500);
- }
- $now = $this->functionsController->now();
- $currentDate = $now->toDateTimeString();
- $upd = [
- "TABL_ESTA" => "",
- "TABL_USMO" => $user,
- "TABL_FEMO" => $currentDate,
- "TABL_FEAR" => DB::raw('CURRENT_TIMESTAMP')
- ];
- switch ($form['state']){
- case "delete":
- $upd["TABL_ESTA"] = "Eliminado";
- break;
- case "active":
- $upd["TABL_ESTA"] = "Activo";
- break;
- default:
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD004: No ha elegido una opción correcta para cambiar el estado", [], 500);
- }
- try{
- $validateUpdate = DB::table('S002V01TTABL')->where('TABL_NOMB', $form['table_name'])->update($upd);
- if (!$validateUpdate) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD005: No se pudo realizar la modificación en la base.", [], 500);
- }
- }catch (\PDOException $e){
- if($e->getCode() == 23000){
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD005: La tabla ya ha sido registrada anteriormente.", [], 500);
- }else{
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD006: Ocurrió un error al consultar los datos.", [], 500);
- }
- } catch (\Throwable $th) {
- DB::rollBack();
- return $this->responseController->makeResponse(true, "ERR_TABLE_UPD007: Ocurrió un error al consultar los datos.", [], 500);
- }
- DB::commit();
- return $this->responseController->makeResponse(false, "ÉXITO: Modificación correcta");
- }
- /* --------------------------------------- FUNCIONES ---------------------------------------------- */
- private function getColumns($table_name){
- $tables_name = $this->getTablesName();
- if ( $tables_name['error']) {
- return $tables_name;
- }
- $tables = $tables_name['response'];
- $valid_table = false;
- foreach ($tables as $table) {
- if ($table->TABLE_NAME == $table_name){
- $valid_table = true;
- break;
- }
- }
- if (!$valid_table) {
- return ["error" => true, "msg" => "ERR_DYNTABLE_REG000: No se encontró la tabla en la base de datos."];
- }
- $databaseName = DB::connection()->getDatabaseName();
- $qry = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$databaseName."' AND TABLE_NAME = '".$table_name."'";
- try{
- $data_columns = DB::select($qry);
- }catch (\Exception $e){
- return ["error" => true, "msg" => "ERR_DYNTABLE_REG000: No se pudo realizar la consulta a la base."];
- }
- return ["error" => false, "msg" => "ÉXITO", "response" => $data_columns];
- }
- private function getTablesName(){
- $databaseName = DB::connection()->getDatabaseName();
- try{
- $resp = DB::select('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="'.$databaseName.'"');
- }catch (\Exception $e){
- return ["error" => true, "msg" => "ERR_DYNTABLE_REG000: Ocurrió un error al obtener el nombre de las tablas."];
- }
- if ( count((array)$resp) == 0) {
- return ["error" => true, "msg" => "ERR_DYNTABLE_REG000: No se pudo obtener el nombre de las tablas."];
- }
- return ["error" => false, "msg" => "ÉXITO", "response" => $resp];
- }
- }
|